本人使用STM8S207MB与触摸芯片TS20通信,使用硬件I2C。现在向TS20学数据已经没问题了,但是卡在读数据。
每当读了第一次数据之后,I2C通信就卡主了,就算复位也无效,一定要断电才行。
我初步分析是读完数据后从设备TS20没有释放SDA,导致无法再进行I2C通信。下面贴上读取数据的代码,希望大家能帮我分析一下,谢谢!
- void I2C_Touch_ReadData(u8* pBuffer,bool HLRAddr,u8 ReadAddr,u8 NumByteToRead)
- {
- /* While the bus is busy */
- while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
-
- /* Generate start & wait event detection */
- I2C_GenerateSTART(ENABLE);
- /* Test on EV5 and clear it */
- while (!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT));
-
- /* Send Hight Or Low address*/
- if(HLRAddr)
- {
- I2C_Send7bitAddress(VDD_ADDRESS, I2C_DIRECTION_TX);
- }
- else
- {
- I2C_Send7bitAddress(GND_ADDRESS, I2C_DIRECTION_TX);
- }
- /* Test on EV6 and clear it */
- while (!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));
- I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
-
- /* Send Address of first byte to be read & wait event detection */
- //I2C_SendData((u8)(ReadAddr >> 8)); /* MSB */
- /* Test on EV8 and clear it */
- //while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
- I2C_SendData((u8)(ReadAddr)); /* LSB */
- /* Test on EV8 and clear it */
- while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
-
- /* Send STRAT condition a second time */
- I2C_GenerateSTART(ENABLE);
- /* Test on EV5 and clear it */
- while (!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT));
-
- /* Send Hight Or Low address*/
- if(HLRAddr)
- {
- I2C_Send7bitAddress(VDD_R_ADDRESS, I2C_DIRECTION_RX);
- }
- else
- {
- I2C_Send7bitAddress(GND_R_ADDRESS, I2C_DIRECTION_RX);
- }
- /* Test on EV6 and clear it */
- while (!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));
- I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
-
- /* While there is data to be read */
- while(NumByteToRead)
- {
- if(NumByteToRead == 1)
- {
- /* Disable Acknowledgement */
- I2C_AcknowledgeConfig(I2C_ACK_NONE);
-
- /* Send STOP Condition */
- I2C_GenerateSTOP(ENABLE);
- }
- /* Test on EV7 and clear it */
- if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
- {
- /* Read a byte from the EEPROM */
- *pBuffer = I2C_ReceiveData();
- /* Point to the next location where the byte read will be saved */
- pBuffer++;
-
- /* Decrement the read bytes counter */
- NumByteToRead--;
- }
- }
- /* Enable Acknowledgement to be ready for another reception */
- //I2C_AcknowledgeConfig(I2C_ACK_CURR);
- I2C_CR2 |= MASK_I2C_CR2_ACK;
- }
|