ee.c aktualisiert

This commit is contained in:
RainerWieland 2024-08-03 16:53:57 +02:00
parent d0c4961590
commit aeb9c279be

789
ee.c
View File

@ -1,395 +1,394 @@
/************************************************************************************************************
/************************************************************************************************************ ************** Include Headers
************** Include Headers ************************************************************************************************************/
************************************************************************************************************/
#include "EEE4STM32.h"
#include "ee.h" #include <string.h>
#include <string.h>
/************************************************************************************************************
/************************************************************************************************************ ************** Private Definitions
************** Private Definitions ************************************************************************************************************/
************************************************************************************************************/
#define EE_ERASE_PAGE_ADDRESS 0
#define EE_ERASE_PAGE_ADDRESS 0 #define EE_ERASE_PAGE_NUMBER 1
#define EE_ERASE_PAGE_NUMBER 1 #define EE_ERASE_SECTOR_NUMBER 2
#define EE_ERASE_SECTOR_NUMBER 2
#ifdef STM32F0
#ifdef STM32F0 #define EE_ERASE EE_ERASE_PAGE_ADDRESS
#define EE_ERASE EE_ERASE_PAGE_ADDRESS #define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) #endif
#endif
#ifdef STM32F1
#ifdef STM32F1 #define EE_ERASE EE_ERASE_PAGE_ADDRESS
#define EE_ERASE EE_ERASE_PAGE_ADDRESS #define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) #endif
#endif
#ifdef STM32F2
#ifdef STM32F2 #define EE_ERASE EE_ERASE_SECTOR_NUMBER
#define EE_ERASE EE_ERASE_SECTOR_NUMBER #define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) #endif
#endif
#ifdef STM32F3
#ifdef STM32F3 #define EE_ERASE EE_ERASE_PAGE_ADDRESS
#define EE_ERASE EE_ERASE_PAGE_ADDRESS #define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) #endif
#endif
#ifdef STM32F4
#ifdef STM32F4 #define EE_ERASE EE_ERASE_SECTOR_NUMBER
#define EE_ERASE EE_ERASE_SECTOR_NUMBER #define EE_SIZE 0x20000
#define EE_SIZE 0x20000 #define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024)
#define FLASH_SIZE ((((uint32_t)(*((uint16_t *)FLASHSIZE_BASE)) & (0xFFFFU))) * 1024) #define FLASH_F4_OFFSET 4
#define FLASH_F4_OFFSET 4 #endif
#endif
#ifdef STM32F7
#ifdef STM32F7 #define EE_ERASE EE_ERASE_SECTOR_NUMBER
#define EE_ERASE EE_ERASE_SECTOR_NUMBER #define EE_SIZE 0x20000
#define EE_SIZE 0x20000 #endif
#endif
#ifdef STM32H5
#ifdef STM32H5 #define EE_ERASE EE_ERASE_SECTOR_NUMBER
#define EE_ERASE EE_ERASE_SECTOR_NUMBER #endif
#endif
#ifdef STM32H7
#ifdef STM32H7 #define EE_ERASE EE_ERASE_SECTOR_NUMBER
#define EE_ERASE EE_ERASE_SECTOR_NUMBER #endif
#endif
#ifdef STM32G0
#ifdef STM32G0 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32G4
#ifdef STM32G4 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32U0
#ifdef STM32U0 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32U5
#ifdef STM32U5 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32L0
#ifdef STM32L0 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32L1
#ifdef STM32L1 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32L4
#ifdef STM32L4 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32L5
#ifdef STM32L5 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32WB
#ifdef STM32WB #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32W0
#ifdef STM32W0 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32WBA
#ifdef STM32WBA #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #undef FLASH_BANK_1
#undef FLASH_BANK_1 #endif
#endif
#ifdef STM32WL
#ifdef STM32WL #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifdef STM32C0
#ifdef STM32C0 #define EE_ERASE EE_ERASE_PAGE_NUMBER
#define EE_ERASE EE_ERASE_PAGE_NUMBER #endif
#endif
#ifndef EE_SIZE
#ifndef EE_SIZE #if (EE_ERASE == EE_ERASE_PAGE_NUMBER) || (EE_ERASE == EE_ERASE_PAGE_ADDRESS)
#if (EE_ERASE == EE_ERASE_PAGE_NUMBER) || (EE_ERASE == EE_ERASE_PAGE_ADDRESS) #define EE_SIZE FLASH_PAGE_SIZE
#define EE_SIZE FLASH_PAGE_SIZE #elif (EE_ERASE == EE_ERASE_SECTOR_NUMBER)
#elif (EE_ERASE == EE_ERASE_SECTOR_NUMBER) #define EE_SIZE FLASH_SECTOR_SIZE
#define EE_SIZE FLASH_SECTOR_SIZE #endif
#endif #endif
#endif
#if defined FLASH_BANK_2
#if defined FLASH_BANK_2 #define EE_BANK_SELECT FLASH_BANK_2
#define EE_BANK_SELECT FLASH_BANK_2 #elif defined FLASH_BANK_1
#elif defined FLASH_BANK_1 #define EE_BANK_SELECT FLASH_BANK_1
#define EE_BANK_SELECT FLASH_BANK_1 #endif
#endif
#ifndef EE_PAGE_SECTOR
#ifndef EE_PAGE_SECTOR #if (EE_BANK_SELECT == FLASH_BANK_2)
#if (EE_BANK_SELECT == FLASH_BANK_2) #define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE / 2) - 1)
#define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE / 2) - 1) #else
#else #define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE) - 1)
#define EE_PAGE_SECTOR ((FLASH_SIZE / EE_SIZE) - 1) #endif
#endif #endif
#endif
#ifndef EE_ADDRESS
#ifndef EE_ADDRESS #if (EE_BANK_SELECT != FLASH_BANK_2)
#if (EE_BANK_SELECT != FLASH_BANK_2) #define EE_ADDRESS (FLASH_BASE + EE_SIZE * EE_PAGE_SECTOR)
#define EE_ADDRESS (FLASH_BASE + EE_SIZE * EE_PAGE_SECTOR) #else
#else #define EE_ADDRESS (FLASH_BASE + EE_SIZE * (EE_PAGE_SECTOR * 2 + 1))
#define EE_ADDRESS (FLASH_BASE + EE_SIZE * (EE_PAGE_SECTOR * 2 + 1)) #endif
#endif #endif
#endif
#ifndef FLASH_F4_OFFSET
#ifndef FLASH_F4_OFFSET #define FLASH_F4_OFFSET 0
#define FLASH_F4_OFFSET 0 #endif
#endif
#ifndef EE_ERASE
#ifndef EE_ERASE #error "Not Supported MCU!"
#error "Not Supported MCU!" #endif
#endif
/************************************************************************************************************
/************************************************************************************************************ ************** Private Variables
************** Private Variables ************************************************************************************************************/
************************************************************************************************************/
EE_HandleTypeDef eeHandle;
EE_HandleTypeDef eeHandle;
/************************************************************************************************************
/************************************************************************************************************ ************** Private Functions
************** Private Functions ************************************************************************************************************/
************************************************************************************************************/
/************************************************************************************************************
/************************************************************************************************************ ************** Public Functions
************** Public Functions ************************************************************************************************************/
************************************************************************************************************/
/**
/** * @brief Initializes the EEPROM emulation module.
* @brief Initializes the EEPROM emulation module. * @note This function initializes the EEPROM emulation module to enable read and write operations.
* @note This function initializes the EEPROM emulation module to enable read and write operations. * @param StoragePointer: Pointer to the start address of the EEPROM emulation area.
* @param StoragePointer: Pointer to the start address of the EEPROM emulation area. * @param Size: Size of the EEPROM emulation area in bytes.
* @param Size: Size of the EEPROM emulation area in bytes. * @return Boolean value indicating the success of the initialization:
* @return Boolean value indicating the success of the initialization: * - true: Initialization successful.
* - true: Initialization successful. * - false: Initialization failed.
* - false: Initialization failed. */
*/ bool EE_Init(void *StoragePointer, uint32_t Size)
bool EE_Init(void *StoragePointer, uint32_t Size) {
{ bool answer = false;
bool answer = false; do
do {
{ /* checking size of eeprom area*/
/* checking size of eeprom area*/ if (Size > EE_SIZE)
if (Size > EE_SIZE) {
{ eeHandle.Size = 0;
eeHandle.Size = 0; eeHandle.DataPointer = NULL;
eeHandle.DataPointer = NULL; break;
break; }
} eeHandle.Size = Size;
eeHandle.Size = Size; eeHandle.DataPointer = (uint8_t*)StoragePointer;
eeHandle.DataPointer = (uint8_t*)StoragePointer; answer = true;
answer = true;
} while (0);
} while (0);
return answer;
return answer; }
}
/***********************************************************************************************************/
/***********************************************************************************************************/
/**
/** * @brief Retrieves the capacity of the EEPROM emulation area.
* @brief Retrieves the capacity of the EEPROM emulation area. * @note This function returns the total capacity of the EEPROM emulation area in bytes.
* @note This function returns the total capacity of the EEPROM emulation area in bytes. * @return Capacity of the EEPROM emulation area in bytes.
* @return Capacity of the EEPROM emulation area in bytes. */
*/ uint32_t EE_Capacity(void)
uint32_t EE_Capacity(void) {
{ return EE_SIZE;
return EE_SIZE; }
}
/***********************************************************************************************************/
/***********************************************************************************************************/
/**
/** * @brief Formats the EEPROM emulation area.
* @brief Formats the EEPROM emulation area. * @note This function formats the EEPROM emulation area,
* @note This function formats the EEPROM emulation area, * @return bool Boolean value indicating the success of the operation:
* @return bool Boolean value indicating the success of the operation: * - true: Formatting successful.
* - true: Formatting successful. * - false: Formatting failed.
* - false: Formatting failed. */
*/ bool EE_Format(void)
bool EE_Format(void) {
{ bool answer = false;
bool answer = false; uint32_t error;
uint32_t error; FLASH_EraseInitTypeDef flashErase;
FLASH_EraseInitTypeDef flashErase; do
do {
{ HAL_FLASH_Unlock();
HAL_FLASH_Unlock(); #ifdef HAL_ICACHE_MODULE_ENABLED
#ifdef HAL_ICACHE_MODULE_ENABLED /* disabling ICACHE if enabled*/
/* disabling ICACHE if enabled*/ HAL_ICACHE_Disable();
HAL_ICACHE_Disable(); #endif
#endif #if EE_ERASE == EE_ERASE_PAGE_ADDRESS
#if EE_ERASE == EE_ERASE_PAGE_ADDRESS flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
flashErase.TypeErase = FLASH_TYPEERASE_PAGES; flashErase.PageAddress = EE_ADDRESS;
flashErase.PageAddress = EE_ADDRESS; flashErase.NbPages = 1;
flashErase.NbPages = 1; #elif EE_ERASE == EE_ERASE_PAGE_NUMBER
#elif EE_ERASE == EE_ERASE_PAGE_NUMBER flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
flashErase.TypeErase = FLASH_TYPEERASE_PAGES; flashErase.Page = EE_PAGE_SECTOR;
flashErase.Page = EE_PAGE_SECTOR; flashErase.NbPages = 1;
flashErase.NbPages = 1; #else
#else flashErase.TypeErase = FLASH_TYPEERASE_SECTORS;
flashErase.TypeErase = FLASH_TYPEERASE_SECTORS; flashErase.Sector = EE_PAGE_SECTOR + FLASH_F4_OFFSET;
flashErase.Sector = EE_PAGE_SECTOR + FLASH_F4_OFFSET; flashErase.NbSectors = 1;
flashErase.NbSectors = 1; #endif
#endif #ifdef EE_BANK_SELECT
#ifdef EE_BANK_SELECT flashErase.Banks = EE_BANK_SELECT;
flashErase.Banks = EE_BANK_SELECT; #endif
#endif #ifdef FLASH_VOLTAGE_RANGE_3
#ifdef FLASH_VOLTAGE_RANGE_3 flashErase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
flashErase.VoltageRange = FLASH_VOLTAGE_RANGE_3; #endif
#endif /* erasing page/sector */
/* erasing page/sector */ if (HAL_FLASHEx_Erase(&flashErase, &error) != HAL_OK)
if (HAL_FLASHEx_Erase(&flashErase, &error) != HAL_OK) {
{ break;
break; }
} /* checking result */
/* checking result */ if (error != 0xFFFFFFFF)
if (error != 0xFFFFFFFF) {
{ break;
break; }
} answer = true;
answer = true;
} while (0);
} while (0);
HAL_FLASH_Lock();
HAL_FLASH_Lock(); #ifdef HAL_ICACHE_MODULE_ENABLED
#ifdef HAL_ICACHE_MODULE_ENABLED HAL_ICACHE_Enable();
HAL_ICACHE_Enable(); #endif
#endif return answer;
return answer; }
}
/***********************************************************************************************************/
/***********************************************************************************************************/
/**
/** * @brief Reads data from the EEPROM emulation area.
* @brief Reads data from the EEPROM emulation area. * @note This function reads data from the EEPROM emulation area
* @note This function reads data from the EEPROM emulation area * and loads it into the specified storage pointer.
* and loads it into the specified storage pointer. */
*/ void EE_Read(void)
void EE_Read(void) {
{ uint8_t *data = eeHandle.DataPointer;
uint8_t *data = eeHandle.DataPointer; #ifdef HAL_ICACHE_MODULE_ENABLED
#ifdef HAL_ICACHE_MODULE_ENABLED /* disabling ICACHE if enabled*/
/* disabling ICACHE if enabled*/ HAL_ICACHE_Disable();
HAL_ICACHE_Disable(); #endif
#endif if (data != NULL)
if (data != NULL) {
{ /* reading flash */
/* reading flash */ for (uint32_t i = 0; i < eeHandle.Size; i++)
for (uint32_t i = 0; i < eeHandle.Size; i++) {
{ *data = (*(__IO uint8_t*) (EE_ADDRESS + i));
*data = (*(__IO uint8_t*) (EE_ADDRESS + i)); data++;
data++; }
} }
} #ifdef HAL_ICACHE_MODULE_ENABLED
#ifdef HAL_ICACHE_MODULE_ENABLED /* disabling ICACHE if enabled*/
/* disabling ICACHE if enabled*/ HAL_ICACHE_Enable();
HAL_ICACHE_Enable(); #endif
#endif }
}
/***********************************************************************************************************/
/***********************************************************************************************************/
/**
/** * @brief Writes data to the EEPROM emulation area.
* @brief Writes data to the EEPROM emulation area. * @note This function writes data to the EEPROM emulation area.
* @note This function writes data to the EEPROM emulation area. * @retval true if the write operation is successful, false otherwise.
* @retval true if the write operation is successful, false otherwise. */
*/ bool EE_Write(void)
bool EE_Write(void) {
{ bool answer = true;
bool answer = true; uint8_t *data = eeHandle.DataPointer;
uint8_t *data = eeHandle.DataPointer; do
do {
{ /* checking eeprom is initialize correctly */
/* checking eeprom is initialize correctly */ if (data == NULL)
if (data == NULL) {
{ answer = false;
answer = false; break;
break; }
} /* formating flash area before writing */
/* formating flash area before writing */ if (EE_Format() == false)
if (EE_Format() == false) {
{ answer = false;
answer = false; break;
break; }
} HAL_FLASH_Unlock();
HAL_FLASH_Unlock(); #ifdef HAL_ICACHE_MODULE_ENABLED
#ifdef HAL_ICACHE_MODULE_ENABLED /* disabling ICACHE if enabled*/
/* disabling ICACHE if enabled*/ HAL_ICACHE_Disable();
HAL_ICACHE_Disable(); #endif
#endif #if (defined FLASH_TYPEPROGRAM_HALFWORD)
#if (defined FLASH_TYPEPROGRAM_HALFWORD) /* writing buffer to flash */
/* writing buffer to flash */ for (uint32_t i = 0; i < eeHandle.Size ; i += 2)
for (uint32_t i = 0; i < eeHandle.Size ; i += 2) {
{ uint64_t halfWord;
uint64_t halfWord; memcpy((uint8_t*)&halfWord, data, 2);
memcpy((uint8_t*)&halfWord, data, 2); if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, EE_ADDRESS + i, halfWord) != HAL_OK)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, EE_ADDRESS + i, halfWord) != HAL_OK) {
{ answer = false;
answer = false; break;
break; }
} data += 2;
data += 2; }
} #elif (defined FLASH_TYPEPROGRAM_DOUBLEWORD)
#elif (defined FLASH_TYPEPROGRAM_DOUBLEWORD) /* writing buffer to flash */
/* writing buffer to flash */ for (uint32_t i = 0; i < eeHandle.Size; i += 8)
for (uint32_t i = 0; i < eeHandle.Size; i += 8) {
{ uint64_t doubleWord;
uint64_t doubleWord; memcpy((uint8_t*)&doubleWord, data, 8);
memcpy((uint8_t*)&doubleWord, data, 8); if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, EE_ADDRESS + i, doubleWord) != HAL_OK)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, EE_ADDRESS + i, doubleWord) != HAL_OK) {
{ answer = false;
answer = false; break;
break; }
} data += 8;
data += 8; }
} #elif (defined FLASH_TYPEPROGRAM_QUADWORD)
#elif (defined FLASH_TYPEPROGRAM_QUADWORD) /* writing buffer to flash */
/* writing buffer to flash */ for (uint32_t i = 0; i < eeHandle.Size; i += 16)
for (uint32_t i = 0; i < eeHandle.Size; i += 16) {
{ if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK) {
{ answer = false;
answer = false; break;
break; }
} data += 16;
data += 16; }
} #elif (defined FLASH_TYPEPROGRAM_FLASHWORD)
#elif (defined FLASH_TYPEPROGRAM_FLASHWORD) /* writing buffer to flash */
/* writing buffer to flash */ for (uint32_t i = 0; i < eeHandle.Size; i += FLASH_NB_32BITWORD_IN_FLASHWORD * 4)
for (uint32_t i = 0; i < eeHandle.Size; i += FLASH_NB_32BITWORD_IN_FLASHWORD * 4) {
{ if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK)
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, EE_ADDRESS + i, (uint32_t)data) != HAL_OK) {
{ answer = false;
answer = false; break;
break; }
} data += FLASH_NB_32BITWORD_IN_FLASHWORD * 4;
data += FLASH_NB_32BITWORD_IN_FLASHWORD * 4; }
} #endif
#endif /* verifying Flash content */
/* verifying Flash content */ data = eeHandle.DataPointer;
data = eeHandle.DataPointer; for (uint32_t i = 0; i < eeHandle.Size; i++)
for (uint32_t i = 0; i < eeHandle.Size; i++) {
{ if (*data != (*(__IO uint8_t*) (EE_ADDRESS + i)))
if (*data != (*(__IO uint8_t*) (EE_ADDRESS + i))) {
{ answer = false;
answer = false; break;
break; }
} data++;
data++; }
}
} while (0);
} while (0);
HAL_FLASH_Lock();
HAL_FLASH_Lock(); #ifdef HAL_ICACHE_MODULE_ENABLED
#ifdef HAL_ICACHE_MODULE_ENABLED HAL_ICACHE_Enable();
HAL_ICACHE_Enable(); #endif
#endif return answer;
return answer; }
}
/***********************************************************************************************************/
/***********************************************************************************************************/