STM32L0 internal EEPROM write and read

STM32L0 internal EEPROM write and read

Description

The EEPROM write operation in STM32L0 consists of unlocking, writing, and locking processes, and the reading process does not need to be unlocked. As for the non-volatile space inside the STM32L0 is divided into FLASH and EEPROM, it is mainly reflected in the use of tools such as ST-LINK to erase the entire chip, only the FLASH space is erased, and the EEPROM part will not be erased, just like an external EEPROM chip , MPU code upgrade does not affect the content of EEPROM. The following takes the writing and reading of the internal EEPROM as an example.

Basic write and read functions

Define the address space of the internal EEPROM:

//STM32L031K6T6
#define EEPROM_BASE_ADDR	0x08080000
#define EEPROM_BYTE_SIZE	0x03FF

Basic byte write function

//Byte write
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();
}

Basic byte read function

//Byte read
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++;
	}
}

Write and read function with check

If it is considered that errors may occur during the writing and reading process, in order to ensure the correctness of the operation, a verification method is required for writing and reading.
For the writing process, it is necessary to read back the written data for comparison.
For the reading process, it is necessary to compare the data read back twice.
If the comparison is correct, the operation is complete.
If it is wrong, you can re-write or read the operation, and within the set re-operation times, re-operation recognition, if it is correct, then the report is correct, if it is wrong, then the error is reported.

Set repeat check times

#include <string.h>
#define iEEPROM_CHECK_NUM 2

Write function with operation check

HAL_StatusTypeDef EEPROM_WRITE_W_CHECK(uint16_t BiasAddress, uint8_t *Data, uint16_t len)
{
	uint8_t buff[len];
	uint16_t i;
	for (i=0;i<iEEPROM_CHECK_NUM;i++)
	{
		EEPROM_WRITE(BiasAddress, Data, len);
		EEPROM_READ(BiasAddress, buff, len);
		if (memcmp(Data, buff, len)==0)
		{
			return HAL_OK;
		}
	}

	return HAL_ERROR;
}

Read function with operation check

HAL_StatusTypeDef EEPROM_Read_W_CHECK(uint16_t BiasAddress, uint8_t *Data, uint16_t len)
{
	uint8_t buff0[len];
	uint8_t buff1[len];
	uint16_t i;
	for (i=0;i<iEEPROM_CHECK_NUM;i++)
	{
		EEPROM_READ(BiasAddress, buff0, len);
		EEPROM_READ(BiasAddress, buff1, len);

		if (memcmp(buff0, buff1, len)==0)
		{
			memcpy(Data, buff0, len);
			return HAL_OK;
		}
	}

	return HAL_ERROR;
}

Among them, BiasAddress is 0 corresponding to the 0 address of the internal EEPROM (EEPROM_BASE_ADDR defines its base address), Data is the data byte pointer, and len is the operation byte length.

It should be noted that, based on the process and design, the use of EEPROM is also divided into two types, one of which is similar to FRAM and can be written directly without erasing in advance. The EEPROM inside STM32 is also of this type; the other one This is similar to FLASH, it needs to be erased based on Page before it can be written correctly. For some EEPROMs, it supports erasing based on the smallest Byte unit of Page.

The internal EEPROM erase function provided by HAL only erases a word, that is, rewrites the data to 0x00000000 at a certain address, which has the same effect as directly calling the write function and writing 0x00000000 at the address. The internal EEPROM erase function of HAL is as follows:

/**
  * @brief  Erase a word in data memory.
  * @param  Address specifies the address to be erased.
  * @note   To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
  *         must be called before.
  *         Call the @ref 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_StatusTypeDef 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 */
    pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

      /* Write 00000000h to valid address in the data memory */
      *(__IO uint32_t *) Address = 0x00000000U;

    status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  }
   
  /* Return the erase status */
  return status;
}  

If the above code is to be used for the internal FLASH operation of the STM32, a page erase operation is also required.

-End-

Guess you like

Origin blog.csdn.net/hwytree/article/details/103799910