BYTE I2C_CPS1848_Read(uint8_t cps1848_index, uint32_t RegAddr, uint32_t *pdwData) { volatile uint32_t i2cdata = 0; uint16_t NumByteToRead = 4; DWORD dwBusyCount = 0; const DWORD dwTimeOut = 1000; CHECK_POINTER_RET_ERR(pdwData); /* 等待busy位 */ dwBusyCount = 0; CHECK_BUSY(I2C_GetFlagStatus(CPS1848_I2C, I2C_FLAG_BUSY), RESET, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); /* Enable Acknowledgement to be ready for another reception necessary...*/ I2C_AcknowledgeConfig(CPS1848_I2C, ENABLE); /* 发送开始信号 */ I2C_GenerateSTART(CPS1848_I2C, ENABLE); /* 等待握手 */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_MODE_SELECT), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); /* 发送目标地址 : Reset the address bit0 for write*/ I2C_Send7bitAddress(CPS1848_I2C, ((CPS1848_I2C_ADDR+cps1848_index) << 1), I2C_Direction_Transmitter); /* 等待握手 */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); /* 发送寄存器地址--高8bit */ I2C_SendData(CPS1848_I2C, ((RegAddr>>18) & 0xff)); /* 等待握手 */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); /* 发送寄存器地址--中8bit */ I2C_SendData(CPS1848_I2C, ((RegAddr>>10) & 0xff)); /* 等待握手 */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); /* 发送寄存器地址--低8bit */ I2C_SendData(CPS1848_I2C, ((RegAddr>>2) & 0xff)); /* 等待握手 */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); /* 开始读取数据 */ I2C_GenerateSTART(CPS1848_I2C, ENABLE); /* Test on EV5 and clear it */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_MODE_SELECT), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); /* 发送读取的地址: Set the address bit0 for read*/ I2C_Send7bitAddress(CPS1848_I2C, ((CPS1848_I2C_ADDR+cps1848_index) << 1) | 0x01, I2C_Direction_Receiver); /* Test on EV6 and clear it */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); while(NumByteToRead--) { if (0 == NumByteToRead) { /* 最后一个数据了,发送非应答信号(Disable Acknowledgement),结束传输;*/ I2C_AcknowledgeConfig(CPS1848_I2C, DISABLE); /* 发送停止位 */ I2C_GenerateSTOP(CPS1848_I2C, ENABLE); } /* Test on EV7 and clear it */ dwBusyCount = 0; CHECK_BUSY(I2C_CheckEvent(CPS1848_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED), SUCCESS, dwBusyCount, dwTimeOut); CHECK_WAIT_BUSY(dwBusyCount, dwTimeOut); i2cdata = (i2cdata<<8) | (I2C_ReceiveData(CPS1848_I2C)); } *pdwData = i2cdata; /* Enable Acknowledgement to be ready for another reception */ I2C_AcknowledgeConfig(CPS1848_I2C, ENABLE); return ERR_SUCCESS; } |