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 i2cTimeout = 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; } |
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure the I2C clock source. The clock is derived from the HSI */
RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
/* GPIO_EXT_I2C_SCL_GPIO_CLK and GPIO_EXT_I2C_SDA_GPIO_CLK Periph clock enable */
RCC_AHBPeriphClockCmd(GPIO_EXT_I2C_SCL_GPIO_CLK | GPIO_EXT_I2C_SDA_GPIO_CLK, ENABLE);
/* GPIO_EXT_I2C Periph clock enable */
RCC_APB1PeriphClockCmd(GPIO_EXT_I2C_CLK, ENABLE);
/* Connect PXx to I2C_SCL*/
GPIO_PinAFConfig(GPIO_EXT_I2C_SCL_GPIO_PORT, GPIO_EXT_I2C_SCL_SOURCE, GPIO_EXT_I2C_SCL_AF);
/* Connect PXx to I2C_SDA*/
GPIO_PinAFConfig(GPIO_EXT_I2C_SDA_GPIO_PORT, GPIO_EXT_I2C_SDA_SOURCE, GPIO_EXT_I2C_SDA_AF);
/* GPIO configuration */
/* Configure GPIO_EXT_I2C pins: SCL */
GPIO_InitStructure.GPIO_Pin = GPIO_EXT_I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIO_EXT_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
/* Configure GPIO_EXT_I2C pins: SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_EXT_I2C_SDA_PIN;
GPIO_Init(GPIO_EXT_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
/* I2C configuration */
/* GPIO_EXT_I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_Timing = GPIO_EXT_I2C_TIMING;
/* Apply GPIO_EXT_I2C configuration after enabling it */
I2C_Init(GPIO_EXT_I2C, &I2C_InitStructure);
/* GPIO_EXT_I2C Peripheral Enable */
I2C_Cmd(GPIO_EXT_I2C, ENABLE);
/*Init I2C Slave MPC23017, all port is output*/
mpc23017Init();
}
这是我的I2C初始化,端口 和 I2C 2的选择宏定义,已经检查好多遍了,不会有什么问题。
另外你时钟配置的是多少? 超过 400K 需要打开 SYSCFG 的 FastMode 控制位。
评分
查看全部评分
找个其它正常的IIC器,试试,
看看是硬件还是软件的问题。
评分
查看全部评分