- Added Verify after Writing
 - Added Checking Pointer in Reading/Writing
- Removed erasing buffer and formatting before write
This commit is contained in:
Nima Askari (نیما عسکری) 2024-05-04 06:34:29 +02:00
parent 460d569b92
commit 810dd7c944
3 changed files with 50 additions and 25 deletions

View File

@ -95,7 +95,7 @@ int main(void)
ee.val6 = 6; ee.val6 = 6;
ee.val7 = 7; ee.val7 = 7;
ee.val8 = 8; ee.val8 = 8;
EE_Write(true); EE_Write();
while (1) while (1)
{ {

60
ee.c
View File

@ -169,8 +169,11 @@ bool EE_Init(void *StoragePointer, uint32_t Size)
bool answer = false; bool answer = false;
do do
{ {
/* checking size of eeprom area*/
if (Size > EE_SIZE) if (Size > EE_SIZE)
{ {
eeHandle.Size = 0;
eeHandle.DataPointer = NULL;
break; break;
} }
eeHandle.Size = Size; eeHandle.Size = Size;
@ -199,15 +202,11 @@ uint32_t EE_Capacity(void)
/** /**
* @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,
* optionally erasing its content.
* @param EraseBuffer Indicates whether to erase the content of the EEPROM emulation area:
* - true: Erase the content of the EEPROM emulation area(In RAM).
* - false: Do not erase the content (only format Flash).
* @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(bool EraseBuffer) bool EE_Format(void)
{ {
bool answer = false; bool answer = false;
uint32_t error; uint32_t error;
@ -216,6 +215,7 @@ bool EE_Format(bool EraseBuffer)
{ {
HAL_FLASH_Unlock(); HAL_FLASH_Unlock();
#ifdef HAL_ICACHE_MODULE_ENABLED #ifdef HAL_ICACHE_MODULE_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
@ -237,10 +237,12 @@ bool EE_Format(bool EraseBuffer)
#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 */
if (HAL_FLASHEx_Erase(&flashErase, &error) != HAL_OK) if (HAL_FLASHEx_Erase(&flashErase, &error) != HAL_OK)
{ {
break; break;
} }
/* checking result */
if (error != 0xFFFFFFFF) if (error != 0xFFFFFFFF)
{ {
break; break;
@ -266,10 +268,14 @@ bool EE_Format(bool EraseBuffer)
void EE_Read(void) void EE_Read(void)
{ {
uint8_t *data = eeHandle.DataPointer; uint8_t *data = eeHandle.DataPointer;
for (uint32_t i = 0; i < eeHandle.Size; i++) if (data != NULL)
{ {
*data = (*(__IO uint8_t*) (EE_ADDRESS + i)); /* reading flash */
data++; for (uint32_t i = 0; i < eeHandle.Size; i++)
{
*data = (*(__IO uint8_t*) (EE_ADDRESS + i));
data++;
}
} }
} }
@ -278,31 +284,33 @@ void EE_Read(void)
/** /**
* @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.
* Optionally, the area can be formatted first before writing.
* @param FormatFirst: Indicates whether to format the EEPROM emulation area before writing:
* - true: Format the Flash area before writing.
* - false: Do not format the Flash area before writing.
* @retval true if the write operation is successful, false otherwise. * @retval true if the write operation is successful, false otherwise.
*/ */
bool EE_Write(bool FormatFirst) bool EE_Write(void)
{ {
bool answer = true; bool answer = true;
uint8_t *data = eeHandle.DataPointer; uint8_t *data = eeHandle.DataPointer;
do do
{ {
if (FormatFirst) /* checking eeprom is initialize correctly */
if (data == NULL)
{ {
if (EE_Format(false) == false) answer = false;
{ break;
answer = false; }
break; /* formating flash area before writing */
} if (EE_Format() == false)
{
answer = false;
break;
} }
HAL_FLASH_Unlock(); HAL_FLASH_Unlock();
#ifdef HAL_ICACHE_MODULE_ENABLED #ifdef HAL_ICACHE_MODULE_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 */
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;
@ -315,6 +323,7 @@ bool EE_Write(bool FormatFirst)
data += 2; data += 2;
} }
#elif (defined FLASH_TYPEPROGRAM_DOUBLEWORD) #elif (defined FLASH_TYPEPROGRAM_DOUBLEWORD)
/* 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;
@ -327,6 +336,7 @@ bool EE_Write(bool FormatFirst)
data += 8; data += 8;
} }
#elif (defined FLASH_TYPEPROGRAM_QUADWORD) #elif (defined FLASH_TYPEPROGRAM_QUADWORD)
/* 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)
@ -337,6 +347,7 @@ bool EE_Write(bool FormatFirst)
data += 16; data += 16;
} }
#elif (defined FLASH_TYPEPROGRAM_FLASHWORD) #elif (defined FLASH_TYPEPROGRAM_FLASHWORD)
/* 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)
@ -347,6 +358,17 @@ bool EE_Write(bool FormatFirst)
data += FLASH_NB_32BITWORD_IN_FLASHWORD * 4; data += FLASH_NB_32BITWORD_IN_FLASHWORD * 4;
} }
#endif #endif
/* verifying Flash content */
data = eeHandle.DataPointer;
for (uint32_t i = 0; i < eeHandle.Size; i++)
{
if (*data != (*(__IO uint8_t*) (EE_ADDRESS + i)))
{
answer = false;
break;
}
data++;
}
} while (0); } while (0);

13
ee.h
View File

@ -9,9 +9,14 @@
Youtube: https://www.youtube.com/@nimaltd Youtube: https://www.youtube.com/@nimaltd
Instagram: https://instagram.com/github.NimaLTD Instagram: https://instagram.com/github.NimaLTD
Version: 3.0.2 Version: 3.1.0
History: History:
3.1.0
- Added Verify after Writing
- Added Checking Pointer in Reading/Writing
- Removed erasing buffer and formating before write
3.0.2 3.0.2
- Fixed Writing for H7B - Fixed Writing for H7B
@ -47,10 +52,8 @@ extern "C"
typedef struct typedef struct
{ {
uint32_t MagicWord;
uint8_t *DataPointer; uint8_t *DataPointer;
uint32_t Size; uint32_t Size;
uint32_t Lock;
} EE_HandleTypeDef; } EE_HandleTypeDef;
@ -60,9 +63,9 @@ typedef struct
bool EE_Init(void *StoragePointer, uint32_t Size); bool EE_Init(void *StoragePointer, uint32_t Size);
uint32_t EE_Capacity(void); uint32_t EE_Capacity(void);
bool EE_Format(bool EraseBuffer); bool EE_Format(void);
void EE_Read(void); void EE_Read(void);
bool EE_Write(bool FormatFirst); bool EE_Write(void);
#ifdef __cplusplus #ifdef __cplusplus
} }