2020-05-12 06:05:35 +02:00
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
/************************************************************************************************************
|
|
|
|
************** Include Headers
|
|
|
|
************************************************************************************************************/
|
|
|
|
|
2020-05-12 06:05:35 +02:00
|
|
|
#include "ee.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
/************************************************************************************************************
|
|
|
|
************** Private Definitions
|
|
|
|
************************************************************************************************************/
|
2020-05-12 06:05:35 +02:00
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#define EE_ERASE_PAGE_ADDRESS 0
|
|
|
|
#define EE_ERASE_PAGE_NUMBER 1
|
|
|
|
#define EE_ERASE_SECTOR_NUMBER 2
|
2020-05-12 06:05:35 +02:00
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32F0
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_ADDRESS
|
|
|
|
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32F1
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_ADDRESS
|
|
|
|
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32F2
|
|
|
|
#define EE_ERASE EE_ERASE_SECTOR_NUMBER
|
|
|
|
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32F3
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_ADDRESS
|
|
|
|
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32F4
|
|
|
|
#define EE_ERASE EE_ERASE_SECTOR_NUMBER
|
|
|
|
#define EE_SIZE 0x20000
|
|
|
|
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32F7
|
|
|
|
#define EE_ERASE EE_ERASE_SECTOR_NUMBER
|
|
|
|
#define EE_SIZE 0x20000
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32H5
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_ADDRESS
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32H7
|
|
|
|
#define EE_ERASE EE_ERASE_SECTOR_NUMBER
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32G0
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32G4
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2021-03-05 08:37:46 +01:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32U0
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2021-03-05 08:37:46 +01:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
#ifdef STM32U5
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2021-03-05 08:37:46 +01:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32L0
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2021-03-05 08:37:46 +01:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
#ifdef STM32L1
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2021-01-18 07:48:18 +01:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
#ifdef STM32L4
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2021-01-18 07:48:18 +01:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32L5
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2023-01-01 10:43:18 +01:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
#ifdef STM32WB
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2023-01-01 10:43:18 +01:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32W0
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2023-04-03 10:56:32 +02:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
#ifdef STM32WBA
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
|
|
|
#undef FLASH_BANK_1
|
2023-04-03 10:56:32 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef STM32WL
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
#ifdef STM32C0
|
|
|
|
#define EE_ERASE EE_ERASE_PAGE_NUMBER
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifndef EE_SIZE
|
|
|
|
#if (EE_ERASE == EE_ERASE_PAGE_NUMBER) || (EE_ERASE == EE_ERASE_PAGE_ADDRESS)
|
|
|
|
#define EE_SIZE FLASH_PAGE_SIZE
|
|
|
|
#elif (EE_ERASE == EE_ERASE_SECTOR_NUMBER)
|
|
|
|
#define EE_SIZE FLASH_SECTOR_SIZE
|
2021-03-05 08:44:58 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
2024-03-25 21:04:49 +01:00
|
|
|
#if defined FLASH_BANK_2
|
2024-03-25 20:36:03 +01:00
|
|
|
#define EE_BANK_SELECT FLASH_BANK_2
|
2024-03-25 21:04:49 +01:00
|
|
|
#elif defined FLASH_BANK_1
|
2024-03-25 20:36:03 +01:00
|
|
|
#define EE_BANK_SELECT FLASH_BANK_1
|
2021-03-05 16:18:22 +01:00
|
|
|
#endif
|
2021-03-05 08:44:58 +01:00
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifndef EE_PAGE_SECTOR
|
|
|
|
#if (EE_BANK_SELECT == FLASH_BANK_2)
|
|
|
|
#define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE / 2) - 1)
|
2023-07-11 08:48:02 +02:00
|
|
|
#else
|
2024-03-25 20:36:03 +01:00
|
|
|
#define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE) - 1)
|
2023-07-11 08:48:02 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifndef EE_ADDRESS
|
|
|
|
#if (EE_BANK_SELECT != FLASH_BANK_2)
|
|
|
|
#define EE_ADDRESS (FLASH_BASE + EE_SIZE * EE_PAGE_SECTOR)
|
2023-09-09 17:32:49 +02:00
|
|
|
#else
|
2024-03-25 20:36:03 +01:00
|
|
|
#define EE_ADDRESS (FLASH_BASE + EE_SIZE * (EE_PAGE_SECTOR * 2 + 1))
|
2023-09-09 17:32:49 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifndef EE_ERASE
|
|
|
|
#error "Not Supported MCU!"
|
2020-05-12 06:05:35 +02:00
|
|
|
#endif
|
|
|
|
|
2024-03-25 20:36:03 +01:00
|
|
|
/************************************************************************************************************
|
|
|
|
************** Private Variables
|
|
|
|
************************************************************************************************************/
|
|
|
|
|
|
|
|
EE_HandleTypeDef eeHandle;
|
|
|
|
|
|
|
|
/************************************************************************************************************
|
|
|
|
************** Private Functions
|
|
|
|
************************************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************************************************
|
|
|
|
************** Public Functions
|
|
|
|
************************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize the EEPROM.
|
|
|
|
* @note Assign a struct/array for storing your data
|
|
|
|
*
|
|
|
|
* @param StoragePointer: Pointer to Storing data
|
|
|
|
* @param Size: Size of Storage, It should be equal or lower than latest sector/page of selected MCU
|
|
|
|
*
|
|
|
|
* @retval bool: true or false
|
|
|
|
*/
|
|
|
|
bool EE_Init(void *StoragePointer, uint32_t Size)
|
2020-05-12 06:05:35 +02:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
bool answer = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (Size > EE_SIZE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
eeHandle.Size = Size;
|
|
|
|
eeHandle.DataPointer = (uint8_t*)StoragePointer;
|
|
|
|
answer = true;
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get Capacity of EEPROM
|
|
|
|
* @note It shall return the latest sector/page size
|
|
|
|
*
|
|
|
|
* @retval uint32_t: Returned size in bytes
|
|
|
|
*/
|
|
|
|
uint32_t EE_Capacity(void)
|
|
|
|
{
|
|
|
|
return EE_SIZE;
|
2020-05-12 06:05:35 +02:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
/***********************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get Capacity of EEPROM
|
|
|
|
* @note It shall return the latest sector/page size
|
|
|
|
*
|
|
|
|
* @param EraseBuffer: Erase data buffer of Storage or not
|
|
|
|
*
|
|
|
|
* @retval bool: true or false
|
|
|
|
*/
|
|
|
|
bool EE_Format(bool EraseBuffer)
|
2021-01-18 07:48:18 +01:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
bool answer = false;
|
2021-01-18 07:48:18 +01:00
|
|
|
uint32_t error;
|
|
|
|
FLASH_EraseInitTypeDef flashErase;
|
2024-03-25 20:36:03 +01:00
|
|
|
do
|
2021-01-18 07:48:18 +01:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
HAL_FLASH_Unlock();
|
|
|
|
#ifdef HAL_ICACHE_MODULE_ENABLED
|
|
|
|
HAL_ICACHE_Disable();
|
|
|
|
#endif
|
|
|
|
#if EE_ERASE == EE_ERASE_PAGE_ADDRESS
|
|
|
|
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
|
|
|
|
flashErase.PageAddress = EE_ADDRESS;
|
|
|
|
flashErase.NbPages = 1;
|
|
|
|
#elif EE_ERASE == EE_ERASE_PAGE_NUMBER
|
|
|
|
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
|
|
|
|
flashErase.Page = EE_PAGE_SECTOR;
|
|
|
|
flashErase.NbPages = 1;
|
|
|
|
#else
|
|
|
|
flashErase.TypeErase = FLASH_TYPEERASE_SECTORS;
|
|
|
|
flashErase.Sector = EE_PAGE_SECTOR;
|
|
|
|
flashErase.NbSectors = 1;
|
2023-07-11 08:48:02 +02:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef EE_BANK_SELECT
|
|
|
|
flashErase.Banks = EE_BANK_SELECT;
|
2023-07-11 08:48:02 +02:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef FLASH_VOLTAGE_RANGE_3
|
|
|
|
flashErase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
|
2021-01-18 07:48:18 +01:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
if (HAL_FLASHEx_Erase(&flashErase, &error) != HAL_OK)
|
|
|
|
{
|
|
|
|
break;
|
2020-05-12 06:05:35 +02:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
if (error != 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
answer = true;
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
2021-01-18 07:48:18 +01:00
|
|
|
HAL_FLASH_Lock();
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef HAL_ICACHE_MODULE_ENABLED
|
2023-07-11 08:48:02 +02:00
|
|
|
HAL_ICACHE_Enable();
|
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
return answer;
|
2020-05-12 06:05:35 +02:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
/***********************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read all data
|
|
|
|
* @note Read from flash memory and fill up the buffer
|
|
|
|
*
|
|
|
|
* @param none
|
|
|
|
*
|
|
|
|
* @retval none
|
|
|
|
*/
|
|
|
|
void EE_Read(void)
|
2020-05-12 06:05:35 +02:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
uint8_t *data = eeHandle.DataPointer;
|
|
|
|
for (uint32_t i = 0; i < eeHandle.Size; i++)
|
2021-01-18 07:48:18 +01:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
*data = (*(__IO uint8_t*) (EE_ADDRESS + i));
|
|
|
|
data++;
|
2021-01-18 07:48:18 +01:00
|
|
|
}
|
2020-05-12 06:05:35 +02:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
/***********************************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write Buffer to flash memory
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param FormatFirst: Format the flash before writing
|
|
|
|
*
|
|
|
|
* @retval bool: true or false
|
|
|
|
*/
|
|
|
|
bool EE_Write(bool FormatFirst)
|
2020-05-12 06:05:35 +02:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
bool answer = true;
|
|
|
|
uint8_t *data = eeHandle.DataPointer;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (FormatFirst)
|
2021-03-05 08:47:26 +01:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
if (EE_Format(false) == false)
|
|
|
|
{
|
|
|
|
answer = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HAL_FLASH_Unlock();
|
|
|
|
#ifdef HAL_ICACHE_MODULE_ENABLED
|
|
|
|
HAL_ICACHE_Disable();
|
2023-07-11 08:48:02 +02:00
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
#if (defined FLASH_TYPEPROGRAM_HALFWORD)
|
|
|
|
for (uint32_t i = 0; i < eeHandle.Size ; i += 2)
|
|
|
|
{
|
|
|
|
uint64_t halfWord;
|
|
|
|
memcpy((uint8_t*)&halfWord, data, 2);
|
|
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, EE_ADDRESS + i, halfWord) != HAL_OK)
|
|
|
|
{
|
|
|
|
answer = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data += 2;
|
2021-03-05 08:47:26 +01:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
#elif (defined FLASH_TYPEPROGRAM_DOUBLEWORD)
|
|
|
|
for (uint32_t i = 0; i < eeHandle.Size; i += 8)
|
2021-03-05 08:47:26 +01:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
uint64_t doubleWord;
|
|
|
|
memcpy((uint8_t*)&doubleWord, data, 8);
|
|
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, EE_ADDRESS + i, doubleWord) != HAL_OK)
|
|
|
|
{
|
|
|
|
answer = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data += 8;
|
2021-03-05 08:47:26 +01:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
#elif (defined FLASH_TYPEPROGRAM_QUADWORD)
|
|
|
|
for (uint32_t i = 0; i < eeHandle.Size; i += 16)
|
2021-01-18 07:48:18 +01:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK)
|
|
|
|
{
|
|
|
|
answer = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data += 16;
|
2023-07-11 08:48:02 +02:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
#elif (defined FLASH_TYPEPROGRAM_FLASHWORD)
|
|
|
|
for (uint32_t i = 0; i < eeHandle.Size; i += 32)
|
2023-07-11 08:48:02 +02:00
|
|
|
{
|
2024-03-25 20:36:03 +01:00
|
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK)
|
|
|
|
{
|
|
|
|
answer = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data += 32;
|
2021-01-18 07:48:18 +01:00
|
|
|
}
|
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
2021-03-05 08:47:26 +01:00
|
|
|
HAL_FLASH_Lock();
|
2024-03-25 20:36:03 +01:00
|
|
|
#ifdef HAL_ICACHE_MODULE_ENABLED
|
2023-07-11 08:48:02 +02:00
|
|
|
HAL_ICACHE_Enable();
|
|
|
|
#endif
|
2024-03-25 20:36:03 +01:00
|
|
|
return answer;
|
2020-05-12 06:05:35 +02:00
|
|
|
}
|
2024-03-25 20:36:03 +01:00
|
|
|
|
|
|
|
/***********************************************************************************************************/
|