/*
 * adc7291.h
 */

#ifndef DATA_LOGGER_ADC7291_ADC7291_H_
#define DATA_LOGGER_ADC7291_ADC7291_H_

/*
 * ad7291.h
 */

#ifndef DRIVERS_AHW_DRIVERS_SPI_TO_UART_CONV_MAX14830_INC_MAX14830_H_
#define DRIVERS_AHW_DRIVERS_SPI_TO_UART_CONV_MAX14830_INC_MAX14830_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

/* AD7291 Registers Definition */
#define AD7291_REG_COMMAND              0x00
#define AD7291_REG_VOLTAGE              0x01
#define AD7291_REG_T_SENSE              0x02
#define AD7291_REG_T_AVERAGE            0x03
#define AD7291_REG_CH0_DATA_HIGH        0x04
#define AD7291_REG_CH0_DATA_LOW         0x05
#define AD7291_REG_CH0_HYST             0x06
#define AD7291_REG_CH1_DATA_HIGH        0x07
#define AD7291_REG_CH1_DATA_LOW         0x08
#define AD7291_REG_CH1_HYST             0x09
#define AD7291_REG_CH2_DATA_HIGH        0x0A
#define AD7291_REG_CH2_DATA_LOW         0x0B
#define AD7291_REG_CH2_HYST             0x0C
#define AD7291_REG_CH3_DATA_HIGH        0x0D
#define AD7291_REG_CH3_DATA_LOW         0x0E
#define AD7291_REG_CH3_HYST             0x0F
#define AD7291_REG_CH4_DATA_HIGH        0x10
#define AD7291_REG_CH4_DATA_LOW         0x11
#define AD7291_REG_CH4_HYST             0x12
#define AD7291_REG_CH5_DATA_HIGH        0x13
#define AD7291_REG_CH5_DATA_LOW         0x14
#define AD7291_REG_CH5_HYST             0x15
#define AD7291_REG_CH6_DATA_HIGH        0x16
#define AD7291_REG_CH6_DATA_LOW         0x17
#define AD7291_REG_CH6_HYST             0x18
#define AD7291_REG_CH7_DATA_HIGH        0x19
#define AD7291_REG_CH7_DATA_LOW         0x1A
#define AD7291_REG_CH7_HYST             0x2B
#define AD7291_REG_T_SENSE_HIGH         0x1C
#define AD7291_REG_T_SENSE_LOW          0x1D
#define AD7291_REG_T_SENSE_HYST         0x1E
#define AD7291_REG_VOLTAGE_ALERT_STATUS 0x1F
#define AD7291_REG_T_ALERT_STATUS       0x20

/* AD7291_REG_COMMAND Definition */
#define AD7291_COMMAND_CH(x)            (1 << (15 - x))
#define AD7291_COMMAND_TSENSE           (1 << 7)
#define AD7291_COMMAND_DELAY            (1 << 5)
#define AD7291_COMMAND_EXT_REF          (1 << 4)
#define AD7291_COMMAND_ALERT_POL_LOW    (1 << 3)
#define AD7291_COMMAND_ALERT_POL_HIGH   (0 << 3)
#define AD7291_COMMAND_CLR_ALERT        (1 << 2)
#define AD7291_COMMAND_RESET            (1 << 1)
#define AD7291_COMMAND_AUTOCYCLE        (1 << 0)

typedef enum
{
    AD7291_SCS = -0,                    ///<  AD7291 success
    AD7291_ERR = -1,                    ///< AD7291 error
    AD7291_INVLD_ARG = -2,                  ///<  AD7291 invalid argument
    AD7291_ALRDY_USD =-3,               ///<  AD7291 already used
    AD7291_COM_ERR = -4,                ///<  AD7291 communication error
    AD7291_NULL_PTR = -5,               ///<  AD7291 NULL pointer error
    AD7291_ERR_MAX = -6
}e_ad7291_ret_sts;

/**
 * @brief ad7291 null pointer check function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 */
typedef uint8_t(*ad7291_read_fptr_t)(void* intf_hdl,uint16_t slave_address, uint8_t reg_addr, uint8_t *data, uint16_t len);

/**
 * @brief ad7291 null pointer check function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 */
typedef uint8_t(*ad7291_write_fptr_t)(void* intf_hdl,uint16_t slave_address, uint8_t reg_addr, uint8_t *read_data, uint16_t len);

typedef struct
{
    void  *io_intf_hdle;                        /*!< interface handle*/
    uint8_t slave_address;              /*!< slave_address*/
    ad7291_read_fptr_t read;                /*!<read function pointer*/
    ad7291_write_fptr_t write;              /*!<write function pointer*/

}ad7291_dev_t;


typedef enum
{
    ad7291_SE_CH0=0x80,                     ///< ad7291 channel 0
    ad7291_SE_CH1=0x40,                     ///< ad7291 channel 1
    ad7291_SE_CH2=0x20,                     ///< ad7291 channel 2
    ad7291_SE_CH3=0x10,                     ///< ad7291 channel 3
    ad7291_SE_CH4=0x08,                     ///< ad7291 channel 4
    ad7291_SE_CH5=0x04,                     ///< ad7291 channel 5
    ad7291_SE_CH6=0x02,                     ///< ad7291 channel 6
    ad7291_SE_CH7=0x01,                     ///< ad7291 channel 7
    ad7291_CH_MAX=0b10000,
}e_ad7291_chnl;

/**
 * @brief ad7291 init function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 * @retval sts - returns the AD7291_SCS or AD7291_ERR
 */
e_ad7291_ret_sts ad7291_init(ad7291_dev_t* ad7291_hdl);

/**
 * @brief ad7291 self test function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 * @retval sts - returns the AD7291_SCS or AD7291_ERR
 */
e_ad7291_ret_sts ad7291_self_test(ad7291_dev_t* inst_hdl);

/**
 * @brief ad7291 null pointer check function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 */
e_ad7291_ret_sts AD7291_SetRegisterValue(ad7291_dev_t* ad7291_hdl , unsigned char registerAddress,unsigned short registerValue);

/**
 * @brief ad7291 null pointer check function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 */
e_ad7291_ret_sts ad7291_get_voltage_convertion(ad7291_dev_t* ad7291_hdl,e_ad7291_chnl chnl,float *data);

/**
 * @brief ad7291 null pointer check function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 */
e_ad7291_ret_sts AD7291_GetVoltageConversionResults(ad7291_dev_t* ad7291_hdl,e_ad7291_chnl chnl,float* data);

/**
 * @brief ad7291 null pointer check function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 */
void AD7291_GetRegisterValue(ad7291_dev_t* ad7291_hdl,unsigned char registerAddress,unsigned short* registerValue,unsigned char readsNumber);

/**
 * @brief ad7291 null pointer check function
 * @param[in] ad7291_hdl : structure handle of data logger ad7291
 */
int8_t null_ptr_check(ad7291_dev_t* ad7291_hdl);

/**
 * @brief ad7291 init function
 */
void pwr_brd_adc7291_init(uint8_t ahw_id);

/**
 * @brief ad7291 i2c write function
 */
uint8_t ad7291_i2c_write(void* intf_hdl, uint16_t slv_addr, uint8_t reg_addr, uint8_t *data, uint16_t len);

/**
 * @brief ad7291 i2c read function
 */
uint8_t ad7291_i2c_read(void* intf_hdl, uint16_t slv_addr, uint8_t reg_addr, uint8_t *data, uint16_t len);

#endif


#endif /* DATA_LOGGER_ADC7291_ADC7291_H_ */
