使用硬件I2C,配置了GPIO和I2C模式,测试读写EEPROM,总是返回写数据失败!驱动编写如下,请问大神哪些地方有可能出错呢??怎么就是读写不进去??
使用的F767IGT6开发版
int main(void)
{
SystemClock_Config();
LED_GPIO_Config();
LED_BLUE;
DEBUG_USART_Config();
SysTick_Init();
I2C_EE_Init();
if(I2C_Test() ==1)
{
LED_GREEN;
}
else
{
LED_RED;
}
while (1)
{
}
}
void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*!< EEPROM_I2C Periph clock enable */
EEPROM_I2C_CLK_ENABLE();
/*!< EEPROM_I2C_SCL_GPIO_CLK and EEPROM_I2C_SDA_GPIO_CLK Periph clock enable */
EEPROM_I2C_SCL_GPIO_CLK_ENABLE();
EEPROM_I2C_SDA_GPIO_CLK_ENABLE();
/*!< Configure EEPROM_I2C pins: SCL */
GPIO_InitStructure.Pin = EEPROM_I2C_SCL_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Alternate = EEPROM_I2C_SCL_AF;
HAL_GPIO_Init(EEPROM_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure EEPROM_I2C pins: SDA */
GPIO_InitStructure.Pin = EEPROM_I2C_SDA_PIN;
HAL_GPIO_Init(EEPROM_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
}
void I2C_Mode_Config(void)
{
I2C_Handle.Instance = EEPROM_I2C;
I2C_Handle.Init.Timing = 0x90913232;//100KHz
I2C_Handle.Init.OwnAddress1 = 0;
I2C_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2C_Handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
I2C_Handle.Init.OwnAddress2 = 0;
I2C_Handle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
I2C_Handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
I2C_Handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
HAL_I2C_Init(&I2C_Handle);
HAL_I2CEx_AnalogFilter_Config(&I2C_Handle, I2C_ANALOGFILTER_ENABLE);
}
void I2C_EE_Init(void)
{
I2C_GPIO_Config();
I2C_Mode_Config();
}
uint8_t I2C_Test(void)
{
u16 i;
printf(&quot;写入数据&quot;);
for ( i=0; i<=255; i++ )
{
I2c_Buf_Write = i;
printf(&quot;0x%02X &quot;, I2c_Buf_Write);
if(i%16 == 15)
printf(&quot;
&quot;);
}
if(HAL_I2C_Mem_Write(&I2C_Handle, 0xA0, 0, I2C_MEMADD_SIZE_8BIT,I2c_Buf_Write,256, 0x10) == HAL_OK )
printf(&quot;写入数据成功&quot;);
else
{
printf(&quot;写入数据失败&quot;);
return 0;
}
printf(&quot;读出数据&quot;);
if(HAL_I2C_IsDeviceReady(&I2C_Handle,0xA0, 10, 1000)!=HAL_OK)
return 0;
HAL_I2C_Mem_Read(&I2C_Handle,0xA1, 0, I2C_MEMADD_SIZE_8BIT,I2c_Buf_Read,256, 0x10);
for (i=0; i<256; i++)
{
if(I2c_Buf_Read != I2c_Buf_Write)
{
printf(&quot;0x%02X &quot;, I2c_Buf_Read);
printf(&quot;写入与读出不一致&quot;);
return 0;
}
printf(&quot;0x%02X &quot;, I2c_Buf_Read);
if(i%16 == 15)
printf(&quot;
&quot;);
}
printf(&quot;读写测试成功&quot;);
return 1;
} |