0 参考资料
FM24W256(Cypress公司生产)数据手册
1 基于HAL库实现硬件I2C读写
1.1 硬件I2C初始化
FM24W256支持最高1MHz的I2C频率,本例也配置为1MHz,相关初始化代码如下:
/**
* @brief 使用硬件I2C的E2P初始化
*
* @return int 0:成功 -1:失败
*/
int bsp_i2c_e2p_init(void)
{
u8 test_buff;
/* 速度配置为1MHz,FM24W256支持最高1MHz速率 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00401242;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler(__FILE__, __LINE__);
return -1;
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler(__FILE__, __LINE__);
return -1;
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler(__FILE__, __LINE__);
return -1;
}
/*
检查E2P是否能够正常读取
*/
if (i2c_fram_read(&test_buff, 0, 1) != HAL_OK)
{
Error_Handler(__FILE__, __LINE__);
return -1;
}
return 0;
}
/**
* @brief I2C底层初始化
*
* @param hi2c I2C句柄
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if (hi2c->Instance == I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */
/* USER CODE END I2C1_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C12;
PeriphClkInit.I2c12ClockSelection = RCC_I2C12CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler(__FILE__, __LINE__);
return;
}
__HAL_RCC_GPIOD_CLK_ENABLE();
/**I2C1 GPIO Configuration
PD3 ------> I2C1_SDA
PD12 ------> I2C1_SCL
*/
GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_I2C1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
/**
* @brief I2C底层去初始化
*
* @param hi2c I2C句柄
*/
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
{
if (hi2c->Instance == I2C1)
{
/* USER CODE BEGIN I2C1_MspDeInit 0 */
/* USER CODE END I2C1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C1_CLK_DISABLE();
/**I2C1 GPIO Configuration
PD3 ------> I2C1_SDA
PD12 ------> I2C1_SCL
*/
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_3);
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_12);
/* USER CODE BEGIN I2C1_MspDeInit 1 */
/* USER CODE END I2C1_MspDeInit 1 */
}
}
1.2 硬件I2C读取数据
/**
* @brief 从铁电存储器读取n字节
*
* @param pBuffer 读取buffer
* @param ReadAddr 读取地址
* @param length 长度
* @return int 0:成功 -1:失败
*/
int i2c_fram_read(uint8_t *pBuffer, uint16_t ReadAddr, uint16_t length)
{
u8 addr_buffer[2];
HAL_StatusTypeDef status = HAL_OK;
/* 构造读取写入buffer */
addr_buffer[0] = (ReadAddr >> 8) & 0xff;
addr_buffer[1] = (ReadAddr >> 0) & 0xff;
status = HAL_I2C_Master_Transmit(&hi2c1, EEPROM_ADDRESS, addr_buffer, 2, 100);
if (status != HAL_OK)
{
return -1;
}
status = HAL_I2C_Master_Receive(&hi2c1, EEPROM_ADDRESS, pBuffer, length, 100);
if (status != HAL_OK)
{
return -1;
}
return 0;
}
这里使用的是FM24W256的随机读取方式,时序图如下:
1.3 硬件I2C写入数据
/**
* @brief 向铁电存储器写入n字节
* 写入长度不得大于EEPROM_WRITE_MAX_LENGTH
* @param pBuffer 写buffer
* @param WriteAddr 写首地址
* @param length 长度
* @return int 0:成功 -1:失败
*/
int i2c_fram_write(uint8_t *pBuffer, uint16_t WriteAddr, uint16_t length)
{
uint16_t i, j = 2;
uint8_t write_buffer[2 + EEPROM_WRITE_MAX_LENGTH];
HAL_StatusTypeDef status = HAL_OK;
/* 单次写入长度超过限制 */
if (length > EEPROM_WRITE_MAX_LENGTH)
{
return -1;
}
/* 构造写入buffer */
write_buffer[0] = (WriteAddr >> 8) & 0xff;
write_buffer[1] = (WriteAddr >> 0) & 0xff;
for (i = 0; i < length; i++)
{
write_buffer[j++] = pBuffer[i];
}
status = HAL_I2C_Master_Transmit(&hi2c1, EEPROM_ADDRESS, write_buffer, 2 + length, 100);
if (status != HAL_OK)
{
return -1;
}
return 0;
}
这里使用的FM24W256写入时序图如下: