hi:
大家好,遇到一个I2C的问题,请大家帮分析一下,不胜感激!
系统使用I2C接2片 MPC23017实现GPIO的扩展,使用
STM32F072RB 的I2C 2,引脚GPIO都配置了AF ,且I2C初始化也是参考了ST的例子。
发送数据时i2c_expand_gpio_writeBuffer函数里,while(I2C_GetFlagStatus(GPIO_EXT_I2C, I2C_ISR_TXIS) == RESET) 直到超时一直得不到TXIS置位,请大家帮分析下。
uint8_t i2c_expand_gpio_writeBuffer(uint8_t i2c_chip_addr,uint8_t* pBuffer, uint16_t WriteAddr, uint16_t* NumByteToWrite)
{
uint32_t DataNum = 0;
uint32_t i2c
timeout = 0;
/* Configure slave address, nbytes, reload and generate start */
I2C_TransferHandling(GPIO_EXT_I2C, i2c_chip_addr, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
i2cTimeout = GPIO_EXT_I2C_LONG_TIMEOUT;
while(I2C_GetFlagStatus(GPIO_EXT_I2C, I2C_ISR_TXIS) == RESET)
{
if((i2cTimeout--) == 0) return I2C_ERROR_TIMEOUT;
}
/* Send address */
I2C_SendData(GPIO_EXT_I2C, (uint8_t)WriteAddr);
/* Wait until TCR flag is set */
i2cTimeout = GPIO_EXT_I2C_LONG_TIMEOUT;
while(I2C_GetFlagStatus(GPIO_EXT_I2C, I2C_ISR_TCR) == RESET)
{
if((i2cTimeout--) == 0) return I2C_ERROR_TIMEOUT;
}
/* Update CR2 : set Slave Address , set write request, generate Start and set end mode */
I2C_TransferHandling(GPIO_EXT_I2C, i2c_chip_addr, (uint8_t)(*NumByteToWrite), I2C_AutoEnd_Mode, I2C_No_StartStop);
while (DataNum != (*NumByteToWrite))
{
/* Wait until TXIS flag is set */
i2cTimeout = GPIO_EXT_I2C_LONG_TIMEOUT;
while(I2C_GetFlagStatus(GPIO_EXT_I2C, I2C_ISR_TXIS) == RESET)
{
if((i2cTimeout--) == 0) return I2C_ERROR_TIMEOUT;
}
/* Write data to TXDR */
I2C_SendData(GPIO_EXT_I2C, (uint8_t)(pBuffer[DataNum]));
/* Update number of transmitted data */
DataNum++;
}
/* Wait until STOPF flag is set */
i2cTimeout = GPIO_EXT_I2C_LONG_TIMEOUT;
while(I2C_GetFlagStatus(GPIO_EXT_I2C, I2C_ISR_STOPF) == RESET)
{
if((i2cTimeout--) == 0) return I2C_ERROR_TIMEOUT;
}
/* Clear STOPF flag */
I2C_ClearFlag(GPIO_EXT_I2C, I2C_ICR_STOPCF);
i2c_expand_gpio_waitStandbyState(i2c_chip_addr);
/* If all operations OK, return sEE_OK (0) */
return OK;
}
一周热门 更多>