/** * Implementing the CHAR dev write API. * * Note, this doesn't implement ulFlags specific options yet! **/ static BT_ERROR i2cWrite(BT_HANDLE hI2C, BT_u16 usDevice, BT_u8 *pucSource, BT_u32 ulLength) { BT_ERROR Error = BT_ERR_NONE; i2cStart(hI2C); Error = i2cSendAddress(hI2C, usDevice, BT_I2C_WRITE_ACCESS); if (Error) goto err_out; Error = i2cSendData(hI2C, pucSource, ulLength); if (Error) goto err_out; Error = i2cStop(hI2C); return Error; err_out: i2cStop(hI2C); return Error; }
static BT_ERROR i2cRead(BT_HANDLE hI2C, BT_u16 usDevice, BT_u8 *pucDest, BT_u32 ulLength) { BT_ERROR Error = BT_ERR_NONE; i2cStart(hI2C); Error = i2cSendAddress(hI2C, usDevice, BT_I2C_READ_ACCESS); if (Error) goto err_out; Error = i2cGetData(hI2C, pucDest, ulLength); if (Error) goto err_out; Error = i2cStop(hI2C); return Error; err_out: i2cStop(hI2C); return Error; }
void i2cWrite(uint8_t device, uint8_t address, uint8_t *data, uint8_t length) { i2cSendAddress(device, WRITE, address); int i; for(i = 0; i<length; i++) { /* Send data through I2C bus */ I2C_BUS->DR = *data; data++; /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C_BUS, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); } /* Send STOP condition */ I2C_GenerateSTOP(I2C_BUS, ENABLE); }
void i2cRead(uint8_t device, uint8_t address, uint8_t *data, uint8_t length) { i2cSendAddress(device, READ, address); int i; for(i = 0; i<length; i++) { /* Test on EV7 and clear it */ while(!I2C_CheckEvent(I2C_BUS, I2C_EVENT_MASTER_BYTE_RECEIVED)); *data = I2C_ReceiveData(I2C_BUS); data++; /* Disable Acknowledgement */ if(!(i < (length - 1))) I2C_AcknowledgeConfig(I2C_BUS, DISABLE); } /* Send STOP Condition */ I2C_GenerateSTOP(I2C_BUS, ENABLE); }