/*******************************************************************************
 * @file	ir_i2c.h
 * @date	2024-07-17
   ******************************************************************************/

#ifndef _IR_I2C_H_
#define _IR_I2C_H_

#include "sdk_common.h"
			
#include <stdbool.h>
#include "nrf.h"
#include "nrf_drv_gpiote.h"

#define IR_I2C_INSTANCE 	1 /**< I2C instance index. */
//#define IR_I2C_INSTANCE 	1 /**< I2C instance index. */
//#define IR_I2C_SDA_PIN    	NRF_GPIO_PIN_MAP(0,10)
//#define IR_I2C_SCL_PIN    	NRF_GPIO_PIN_MAP(0,9)
#define IR_I2C_SDA_PIN    	NRF_GPIO_PIN_MAP(1,15)
#define IR_I2C_SCL_PIN    	NRF_GPIO_PIN_MAP(1,14)
/**
 * COMMAND CONSTANTS
 * Commands are 16-bit writes: bits 15:14 are 0s
 * Bits 13:10 are the command value below
 * Bits 9:0 are data for the command, but not all bits are used with all commands
 */

// The NOP command is included for completeness. It is a valid I2C operation.
#define AD5272_COMMAND_NOP 			0x00	// Do nothing. Why you would want to do this I don't know

// write the 10 or 8 data bits to the RDAC wiper register (it must be unlocked first)
#define AD5272_RDAC_WRITE 			0x01

#define AD5272_RDAC_READ 			0x02	// read the RDAC wiper register

#define AD5272_50TP_WRITE 			0x03	// store RDAC setting to 50-TP

// SW reset: refresh RDAC with last 50-TP stored value
// If not 50-TP value, reset to 50% I think???
// data bits are all dont cares
#define AD5272_RDAC_REFRESH 		0x04	// TODO refactor this to AD5272_SOFT_RESET

// read contents of 50-TP in next frame, at location in data bits 5:0,
// see Table 16 page 22 Rev D datasheet
// location 0x0 is reserved, 0x01 is first programmed wiper location, 0x32 is 50th programmed wiper location
#define AD5272_50TP_WIPER_READ 		0x05

/**
 * Read contents of last-programmed 50-TP location
 * This is the location used in SW Reset command 4 or on POR
 */
#define AD5272_50TP_LAST_USED 		0x06

#define AD5272_CONTROL_WRITE 		0x07	// data bits 2:0 are the control bits

#define AD5272_CONTROL_READ 		0x08	// data bits all dont cares

#define AD5272_SHUTDOWN 			0x09	// data bit 0 set = shutdown, cleared = normal mode


/**
 * Control bits are three bits written with command 7
 */
// enable writing to the 50-TP memory by setting this control bit C0
// default is cleared so 50-TP writing is disabled
// only 50 total writes are possible!
#define AD5272_50TP_WRITE_ENABLE 0x01

// enable writing to volatile RADC wiper by setting this control bit C1
// otherwise it is frozen to the value in the 50-TP memory
// default is cleared, can't write to the wiper
#define AD5272_RDAC_WIPER_WRITE_ENABLE 0x02

// enable high precision calibration by clearing this control bit C2
// set this bit to disable high accuracy mode (dunno why you would want to)
// default is 0 = emabled
#define AD5272_RDAC_CALIB_DISABLE 0x04

// 50TP memory has been successfully programmed if this bit is set
#define AD5272_50TP_WRITE_SUCCESS 0x08

#define AD5272_NORMAL_MODE 0x01
#define AD5272_SHUTDOWN_MODE 0x01
#ifndef ADA2200_SYNCO_PIN
#define ADA2200_SYNCO_PIN		NRF_GPIO_PIN_MAP(0,17)
#endif
void ir_i2c_uninit(void);
void ir_i2c_init(void);

void ir_irq_init(void);
void ir_irq_uninit(void);








uint8_t ir_command_read(uint8_t device_id, uint8_t address, uint8_t *data);
void ir_command_write(uint8_t device_id, uint8_t address, uint8_t data);
void ad5272_i2c_is_busy(void);
int8_t ad5272_write_and_read_rdac (uint16_t data_16_to_write);
int16_t ad5272_read_rdac (void);
int8_t ad5272_write_rdac (uint16_t data_16_to_write);

void read_all_50tp (void);
void ad5272_RDAC_refresh(void);
void ad5272_shutdown_mode(void);
void ad5272_normal_mode(void);

#endif /* !_ADA5272_I2C_H_ */

