Read and write operations of eeprom on stm32l0 chip

1. Introduction

1.1 Introduction to on-chip eeprom

The on-chip eeprom feature of L0 is mainly used to store system configuration information. If f103 needs to store configuration information, an additional eeprom chip is needed. The on-chip eeprom mechanism also conforms to the low power consumption attributes of this series.

1.2 Write eeprom operation

The purpose of this operation is to write a word or part of a word into the data EEPROM. The user must write the correct value in the correct address and case. The memory interface automatically performs an erase operation when necessary (if all bits are currently set to 0, there is no need to delete the old content before writing). Similarly, if the data to be written is 0, only the erase operation is performed. When only a write operation or an erase operation is performed, the duration is Tprog (3.2 ms); if both are performed, the duration is 2 x Tprog (6.4 ms). When the FIX flag is set to 1 for each erase and write operation, the memory interface can be forced to execute.
Please be aware of

  • Generally, it is 0xff after the flash is erased, and 0x00 after the on-chip eeprom is erased
  • Generally, flash needs to be erased before programming, and the memory interface of stm32 automatically executes the erase operation, without the need for the user to execute additional erase instructions.

Two, programming

//eeprom地址
#define EEPROM_BASE_ADDR	0x08080000
//向偏移地址写入len个字节
void eeprom_write(uint16_t BiasAddress, uint8_t *Data, uint16_t len)
{
    
    
	uint16_t i;
	HAL_StatusTypeDef status = HAL_OK;

	HAL_FLASHEx_DATAEEPROM_Unlock();
	for(i=0;i<len;i++)
	{
    
    
		status +=HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, EEPROM_BASE_ADDR+BiasAddress+i, *Data);
		Data++;
	}
	HAL_FLASHEx_DATAEEPROM_Lock();
}
//向偏移地址读取len个字节
void eeprom_read(uint16_t BiasAddress,uint8_t *Buffer,uint16_t Len)
{
    
    
	uint8_t *wAddr;
	wAddr=(uint8_t *)(EEPROM_BASE_ADDR+BiasAddress);
	while(Len--)
	{
    
    
		*Buffer++=*wAddr++;
	}
}

Three, test

3.1 Test code

//测试
	printf("开始测试\r\n");
	printf("写入eeprom数据:[0]:%x [1]:%x\r\n",eeprom_write_data[0],eeprom_write_data[1]);
	eeprom_write(0,eeprom_write_data,2);
	HAL_Delay(10);
	eeprom_read(0,eeprom_read_data,2);
	printf("读取的eeprom数据:[0]:%x [1]:%x\r\n",eeprom_read_data[0],eeprom_read_data[1]);
	printf("片上eeprom测试完成\r\n");

3.2 Test results

The content of writing two bytes to 0x08080000 and reading the two from the address is the same, the test is successful.
Insert picture description here

Four, routine

Routine link

Guess you like

Origin blog.csdn.net/weixin_43810563/article/details/114879719