diff --git a/cm_pcf8574_driver.c b/cm_pcf8574_driver.c index 6fef72a..f65305c 100644 --- a/cm_pcf8574_driver.c +++ b/cm_pcf8574_driver.c @@ -5,8 +5,21 @@ * Author: Chris */ #include "cm_pcf8574_driver.h" +#include -int cm_pcf8574_RegTransEvt(cm_pcf8574_t inst, void i2c_inst, setGet_I2C_Event_fpt i2c_transfer_evt){ +#define NUM_OF_RETRYS 3 + +static int checkInst(cm_pcf8574_t *inst){ + + if(inst->i2c_transfer_evt == NULL)return CM_PCF8574_NOK; + if(inst->i2c_inst == NULL)return CM_PCF8574_NOK; + + return CM_PCF8574_OK; + + +} + +int cm_pcf8574_RegTransEvt(cm_pcf8574_t *inst, void *i2c_inst, setGet_I2C_Event_fpt i2c_transfer_evt){ if(inst == NULL) return CM_PCF8574_NOK; @@ -19,3 +32,47 @@ int cm_pcf8574_RegTransEvt(cm_pcf8574_t inst, void i2c_inst, setGet_I2C_Event_fp } + + +/*// No need to do this.. because I2C driver does this for us.. +int cm_pcf8574_IsDeviceActive(cm_pcf8574_t *inst){ + + if(checkInst(inst)) return CM_PCF8574_NOK; + + uint8_t tx_buf[1] = {0}; + uint8_t ret = 0; + + for(uint8_t retry_ctr = 0; retry_ctr < NUM_OF_RETRYS; retry_ctr++){ + + ret = inst->i2c_transfer_evt(inst->i2c_inst, (uint8_t*)&tx_buf, 2, CM_PCF8574_I2C_GET); + + if(ret == 0)return 0; + + } + + return ret; +}*/ +int cm_pcf8574_SetPort(cm_pcf8574_t *inst, uint8_t port_val){ + + if(checkInst(inst)) return CM_PCF8574_NOK; + + //inst->Port_1 = (cm_io_port_t)port_val; + + return inst->i2c_transfer_evt(inst->i2c_inst, (uint8_t*)&port_val, sizeof(inst->Port_1), CM_PCF8574_I2C_SET); + + + + + +return 0; +} + +// ret = inst->i2c_transfer_evt(inst->i2c_transfer_inst, (uint8_t*)&tx_buf, 2, LTR390_I2C_SET); +int cm_pcf8574_Init(cm_pcf8574_t *inst){ + + if(checkInst(inst)) return CM_PCF8574_NOK; + + //return cm_pcf8574_IsDeviceActive(inst); + + return 0; +} diff --git a/cm_pcf8574_driver.h b/cm_pcf8574_driver.h index 161e015..e43ba93 100644 --- a/cm_pcf8574_driver.h +++ b/cm_pcf8574_driver.h @@ -8,11 +8,19 @@ #ifndef PCF8574_IO_EXPANDER_CM_PCF8574_DRIVER_H_ #define PCF8574_IO_EXPANDER_CM_PCF8574_DRIVER_H_ +#include "stdint.h" + enum{ CM_PCF8574_OK = 0, CM_PCF8574_NOK }; +enum{ + CM_PCF8574_I2C_GET = 0, + CM_PCF8574_I2C_SET = 1, + +}; + typedef int (*setGet_I2C_Event_fpt)(void *inst, uint8_t *data, uint32_t len, uint8_t set_get); @@ -38,7 +46,8 @@ typedef struct{ }cm_pcf8574_t; -int cm_pcf8574_RegTransEvt(cm_pcf8574_t inst, void i2c_inst, setGet_I2C_Event_fpt i2c_transfer_evt); -int cm_pcf8574_init(cm_pcf8574_t *inst); +int cm_pcf8574_SetPort(cm_pcf8574_t *inst, uint8_t port_val); +int cm_pcf8574_RegTransEvt(cm_pcf8574_t *inst, void *i2c_inst, setGet_I2C_Event_fpt i2c_transfer_evt); +int cm_pcf8574_Init(cm_pcf8574_t *inst); #endif /* PCF8574_IO_EXPANDER_CM_PCF8574_DRIVER_H_ */