之前使用STM32的F系列时一直没有内部EEPROM,后来听说L0系列才有内部EEPROM,第一次使用L0,就先测试下内部EEPROM的读写。
内部FLASH和EEPROM这种掉电后还能保存内容的内存统称为non-volatile memory(NVM),STM32L053内部有2K的EEPROM.
内部EEPROM的页大小为一个Word为单位,擦除是必须以页为单位,所以库函数里的擦除函数也是每次擦除4个字节:
/** * @brief Erase a word in data memory. * @param Address: specifies the address to be erased. * @note To correctly run this function, the HAL_FLASHEx_DATAEEPROM_Unlock() function * must be called before. * Call the HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access * and Flash program erase control register access(recommended to protect * the DATA_EEPROM against possible unwanted operation). * @retval HAL status */ HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address) { HAL_StatusTypeDef status = HAL_OK; /* Check the parameters */ assert_param(IS_FLASH_DATA_ADDRESS(Address)); /* Wait for last operation to be completed */ status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); if(status == HAL_OK) { /* Clean the error context */ ProcFlash.ErrorCode = HAL_FLASH_ERROR_NONE; /* Write "00000000h" to valid address in the data memory" */ *(__IO uint32_t *) Address = 0x00000000; status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); } return status; }
写EEPROM的顺序和使用flash类似,要先解锁(UNLOCK) 才能写,写的时候可以字节、半字、字为单位都可以。
写的时候可以不擦除,内部会自动擦除。需要注意的是写EEPROM会消耗固定的时间,所以需要在程序对时间要求严格时
正确安排写EEPROM的时机。
写EEPROM的函数如下:
void EEPROM_Write(uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite) { uint16_t t; HAL_FLASHEx_DATAEEPROM_Unlock(); for(t = 0;t < NumToWrite;t++) { HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE,DATA_EEPROM_BASE + WriteAddr + t,*(pBuffer + t)); } HAL_FLASHEx_DATAEEPROM_Unlock(); }
main函数里面先写EEPROM,然后再读出来验证是否和写一样,一样的话点亮LED:
#include "main.h" uint8_t Test[25] = "http://www.eepw.com.cn/"; uint8_t Buff[25]; int main(void) { uint8_t res; HAL_Init(); SystemClock_Config(); LED_Init(); EEPROM_Write(0,Test,25); EEPROM_Read(0,Buff,25); res = strcmp((const char*)Buff,(const char*)Test); if(0 == res) { while(1) { HAL_Delay(500); LED_TOG; } } }
测试代码:EEPROM.rar