一 问题描述
如前言所述,现象表现为 I2C 无法收发数据。 得无法理解之处,在于之前已经完成了 I2C 的移植工作,并且运行正常。
现在遵照正确的方式,新添一路 I2C 接口, 只是更改了对应的 I2C 接口及引脚,为什么无法收发数据。 简化测试程序如下。
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIO TX/RX clock */
I2Cx_SCL_GPIO_CLK_ENABLE();
I2Cx_SDA_GPIO_CLK_ENABLE();
/* Enable I2Cx clock */
I2Cx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* I2C TX GPIO pin configuration */
GPIO_InitStruct.Pin = I2Cx_SDA_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = I2Cx_SCL_SDA_AF;
HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = I2Cx_SCL_PIN;
HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
I2cHandle.Instance = I2Cx;
I2cHandle.Init.ClockSpeed = 100000;
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE;
I2cHandle.Init.OwnAddress1 = 0;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2cHandle.Init.OwnAddress2 = 0;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
while(1)
{
HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer,
TXBUFFERSIZE, 10000);
}
|