STM32存储左右互搏 I2C总线读写EEPROM ZD24C1MA
在较低容量存储领域,EEPROM是常用的存储介质,不同容量的EEPROM的地址对应位数不同,在发送字节的格式上有所区别。EEPROM是非快速访问存储,因为EEPROM按页进行组织,在连续操作模式,当跨页时访问地址不是跳到下一页到开始,而是跳到当前页的首地址,因此跨页时要重新指定起始地址。而在控制端发送写操作I2C数据后还需要有等待EEPROM内部操作完成的时间才能进行下一次操作。ZD24C1MA是1M bit / 128K Byte容量的EEPROM,ZD24C1MA的管脚定义为:
这里介绍STM32访问1Mbit EEPROM ZD24C1MA的例程。采用STM32CUBEIDE开发平台,以STM32F401CCU6芯片为例,通过STM32 I2C硬件电路实现读写操作,通过UART串口进行控制。
STM32工程配置
首先建立基本工程并设置时钟:
配置硬件I2C接口:
配置UART1作为通讯串口:
保存并生成初始工程代码:
STM32工程代码
这里的测试逻辑比较简单:
当串口收到指令0x01时向EEPROM 地址0写入预设的256个字节0x00~0xFF(不跨页),然后读出并通过串口打印出
当串口收到指令0x02时向EEPROM 地址600写入预设的256个字节0xFF~0x00(跨页),然后读出并通过串口打印出
ZD24C1MA的设备默认访问地址为0xA0, ZD24C1MA的存储单元地址访问略为特殊,17位地址分为两部分,最高位的1位放置于I2C设备默认访问地址的第1位,I2C设备默认访问地址第0位仍然为读写控制位,由于采用硬件I2C控制,库函数自行通过识别调用的是发送还是接收函数对第0位进行发送前设置,因此,不管是调用库函数的I2C写操作还是读操作,提供的地址相同。17位地址的低16位通过在发送设备地址后的作为跟随的第一,二个字节发送。
建立ZD24C1MA.h库头文件
#ifndef INC_ZD24C1MA_H_
#define INC_ZD24C1MA_H_#include "main.h"void PY_Delay_us_t(uint32_t Delay);
void ZD24C1MA_Read(uint32_t addr, uint8_t * data, uint32_t len);
void ZD24C1MA_Write(uint32_t addr, uint8_t * data, uint32_t len);#endif
建立ZD24C1MA.c库源文件:
#include <string.h>
#include <ZD24C1MA.h>#define Page_Size 256
#define Delay_Param 5
extern I2C_HandleTypeDef hi2c1;
extern uint8_t ZD24C1MA_Default_I2C_Addr ;void ZD24C1MA_Read(uint32_t addr, uint8_t * data, uint32_t len)
{uint8_t ZD24C1MA_I2C_Addr;ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | ((addr>>16)<<1); //highest 1-bit access address placed into I2C addressuint8_t RA[2];RA[0] = (addr & 0xFF00)>>8; //high 8-bit access address placed into I2C first dataRA[1] =addr & 0x00FF; //low 8-bit access address placed into I2C first dataHAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, &RA[0], 2, 2700); //Write address for readHAL_I2C_Master_Receive(&hi2c1, ZD24C1MA_I2C_Addr, data, len, 2700); //Read data}void ZD24C1MA_Write(uint32_t addr, uint8_t * data, uint32_t len)
{uint8_t ZD24C1MA_I2C_Addr;uint32_t addr_page = addr/Page_Size;uint32_t addr_index = addr%Page_Size;uint32_t TLEN;uint8_t TAD[Page_Size+2];uint32_t i=0;if(len<=(Page_Size-addr_index)){TAD[0] = (addr & 0xFF00) >> 8;TAD[1] = addr & 0x00FF ;memcpy(TAD+2, data, len);ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | ((addr>>16)<<1); //highest 1-bit access address placed into I2C addressHAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, len+2, 2700); //Write dataPY_Delay_us_t(Delay_Param*1000);}else{TAD[0] = (addr & 0xFF00) >> 8;TAD[1] = addr & 0x00FF ;memcpy(TAD+2, data, (Page_Size-addr_index));ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | ((addr>>16)<<1); //highest 1-bit access address placed into I2C addressHAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, (Page_Size-addr_index)+2, 2700); //Write dataPY_Delay_us_t(Delay_Param*1000);TLEN = (len-(Page_Size-addr_index));while( TLEN >= Page_Size ){addr_page += 1;TAD[0] = ((addr_page*Page_Size) & 0xFF00 ) >> 8;TAD[1] = (addr_page*Page_Size) & 0x00FF ;memcpy(TAD+2, data + (Page_Size-addr_index) + i*Page_Size, Page_Size);ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | (((addr_page*Page_Size)>>16)<<1); //highest 1-bit access address placed into I2C addressHAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, Page_Size+2, 2700); //Write dataHAL_Delay(Delay_Param);i++;TLEN -= Page_Size;PY_Delay_us_t(Delay_Param*1000);}if(TLEN>0){addr_page += 1;TAD[0] = ((addr_page*Page_Size) & 0xFF00 ) >> 8;TAD[1] = (addr_page*Page_Size) & 0x00FF ;memcpy(TAD+2, data + (Page_Size-addr_index) + i*Page_Size, TLEN);ZD24C1MA_I2C_Addr = ZD24C1MA_Default_I2C_Addr | (((addr_page*Page_Size)>>16)<<1); //highest 1-bit access address placed into I2C addressHAL_I2C_Master_Transmit(&hi2c1, ZD24C1MA_I2C_Addr, TAD, TLEN+2, 2700); //Write dataPY_Delay_us_t(Delay_Param*1000);}}}
完成的main.c文件代码如下:
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2023 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
//Written by Pegasus Yu in 2023
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include <ZD24C1MA.h>
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{__IO uint32_t firstms, secondms;__IO uint32_t counter = 0;firstms = HAL_GetTick()+1;secondms = firstms+1;while(uwTick!=firstms) ;while(uwTick!=secondms) counter++;usDelayBase = ((float)counter)/1000;
}void PY_Delay_us_t(uint32_t Delay)
{__IO uint32_t delayReg;__IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);delayReg = 0;while(delayReg!=usNum) delayReg++;
}void PY_usDelayOptimize(void)
{__IO uint32_t firstms, secondms;__IO float coe = 1.0;firstms = HAL_GetTick();PY_Delay_us_t(1000000) ;secondms = HAL_GetTick();coe = ((float)1000)/(secondms-firstms);usDelayBase = coe*usDelayBase;
}void PY_Delay_us(uint32_t Delay)
{__IO uint32_t delayReg;__IO uint32_t msNum = Delay/1000;__IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);if(msNum>0) HAL_Delay(msNum);delayReg = 0;while(delayReg!=usNum) delayReg++;
}
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;UART_HandleTypeDef huart1;/* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t cmd=0; //for status control
uint8_t * RData; //USB rx data pointer
uint32_t RDataLen; //USB rx data length
uint8_t * TData; //USB tx data pointer
uint32_t TDataLen; //USB tx data lengthuint8_t ZD24C1MA_Default_I2C_Addr = 0xA0; //Pin A2=A1=0uint32_t ZD24C1MA_Access_Addr = 0; //ZD24C1MA access address (17-bit)uint8_t testdata[256];uint8_t URX;
/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_I2C1_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */PY_usDelayTest();PY_usDelayOptimize();HAL_UART_Receive_IT(&huart1, &URX, 1);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){if(cmd==1){cmd=0;for(uint32_t j=0; j<256; j++){testdata[j] = j;}ZD24C1MA_Access_Addr = 0; //Set access address hereZD24C1MA_Write(ZD24C1MA_Access_Addr, testdata, 256); //Write datamemset(testdata, 0, 256);ZD24C1MA_Read(ZD24C1MA_Access_Addr, testdata, 256); //Read dataHAL_UART_Transmit(&huart1, testdata, 256, 2700);}if(cmd==2){cmd=0;for(uint32_t j=0; j<256; j++){testdata[j] = 255-j;}ZD24C1MA_Access_Addr = 600; //Set access address hereZD24C1MA_Write(ZD24C1MA_Access_Addr, testdata, 256); //Write datamemset(testdata, 0, 256);ZD24C1MA_Read(ZD24C1MA_Access_Addr, testdata, 256); //Read dataHAL_UART_Transmit(&huart1, testdata, 256, 2700);}PY_Delay_us_t(100);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Configure the main internal regulator output voltage*/__HAL_RCC_PWR_CLK_ENABLE();__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLM = 25;RCC_OscInitStruct.PLL.PLLN = 336;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;RCC_OscInitStruct.PLL.PLLQ = 7;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}
}/*** @brief I2C1 Initialization Function* @param None* @retval None*/
static void MX_I2C1_Init(void)
{/* USER CODE BEGIN I2C1_Init 0 *//* USER CODE END I2C1_Init 0 *//* USER CODE BEGIN I2C1_Init 1 *//* USER CODE END I2C1_Init 1 */hi2c1.Instance = I2C1;hi2c1.Init.ClockSpeed = 400000;hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;hi2c1.Init.OwnAddress1 = 0;hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;hi2c1.Init.OwnAddress2 = 0;hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;if (HAL_I2C_Init(&hi2c1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN I2C1_Init 2 *//* USER CODE END I2C1_Init 2 */}/*** @brief USART1 Initialization Function* @param None* @retval None*/
static void MX_USART1_UART_Init(void)
{/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate = 115200;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = UART_PARITY_NONE;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;if (HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 *//* USER CODE END USART1_Init 2 */}/*** @brief GPIO Initialization Function* @param None* @retval None*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 *//* GPIO Ports Clock Enable */__HAL_RCC_GPIOH_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if(huart==&huart1){cmd = URX;HAL_UART_Receive_IT(&huart1, &URX, 1);}}
/* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef USE_FULL_ASSERT
/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
STM32范例测试
上述范例的测试效果如下:
指令0x01不跨页写读:
指令0x02跨页写读:
STM32例程下载
STM32F401CCU6 I2C总线读写EEPROM ZD24C1MA例程
–End–