add first rev of eeprom

and ref voltage in varhandler
This commit is contained in:
2022-12-13 14:30:40 +01:00
parent bf7cde9730
commit 7cadcdbc28
47 changed files with 30486 additions and 1377 deletions

View File

@ -19,23 +19,11 @@
//
//-------------------------------------------------------------------------------------------------
//
// Description: This source file contains all functions dealing with internal flash for User Settings
// Description: This source file contains all functions dealing with the virtual eeprom
//
// STM32L432KBUX_FLASH.ld
// More info in the AN4894 or this link:
// https://www.st.com/en/embedded-software/x-cube-eeprom.html
//
// DATA (rwx) : ORIGIN = 0x801F800, LENGTH = 2K
//
// /* Sections */
// SECTIONS
// {
// /* NOLOAD is required for not ereasing this block */
// .user_data (NOLOAD) :
// {
// . = ALIGN(4);
// *(.user_data)
// . = ALIGN(4);
// } > DATA*/
// ...
//=================================================================================================
@ -47,26 +35,18 @@
#include "USFL_UserFlash.h"
// Toolbox
#include "../Application/VARH_VariableHandler.h"
// include STM32 drivers
#include "stm32l4xx_hal.h"
#include "cmsis_os2.h"
#include "eeprom_emul.h"
//=================================================================================================
// Section: DEFINITIONS
// Description: Definition of local constants (visible by this module only).
//=================================================================================================
#define USERFLASHSIZE (2000/4) // Bytes -> 64 Bits
#define USERFLASHPAGE (63)
#define VARDEF 0xABCDEF
#define STARTDEF (((U64)0xAA01F055 << 32) + (VARDEF << 2))
//=================================================================================================
@ -88,12 +68,6 @@
// Description: Definition of local Structures (visible by this module only).
//=================================================================================================
FLASH_EraseInitTypeDef stEreaseInit = {
FLASH_TYPEERASE_PAGES,
FLASH_BANK_1,
USERFLASHPAGE,
1
};
//=================================================================================================
@ -101,24 +75,29 @@ FLASH_EraseInitTypeDef stEreaseInit = {
// Description: Definition of local variables (visible by this module only).
//=================================================================================================
U32 u32VarPointer = 0;
__IO uint32_t ErasingOnGoing = 0;
LOCAL osMutexId_t m_pstMutexID = NULL;
//=================================================================================================
// Section: LOCAL CONSTANTS
// Description: Definition of local constants (visible by this module only).
//=================================================================================================
__attribute__((__section__(".user_data"))) const U64 UserFlash[USERFLASHSIZE];
LOCAL CONST osMutexAttr_t m_stMutexAttr =
{
"USFL_Mutex", // human readable mutex name
osMutexRecursive | osMutexPrioInherit, // attr_bits
NULL, // memory for control block
0U // size for control block
};
//=================================================================================================
// Section: LOCAL FUNCTIONS (PROTOTYPES)
// Description: Definition of local functions (visible by this module only).
//=================================================================================================
BOOL vEreaseUserFlash( void );
U32 vFindNextFreePointer( void );
U32 u32FindLastPointer( void );
U8 u8ConvertWordsToDoubleWords( U8 u8Words );
//=================================================================================================
// Section: GLOBAL FUNCTIONS
@ -131,28 +110,87 @@ U8 u8ConvertWordsToDoubleWords( U8 u8Words );
// Parameters: None
// Returns: Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL USFL_boInitializeModule( VOID )
{
BOOL boOK = TRUE;
if( UserFlash[0] != STARTDEF ){
boOK &= vEreaseUserFlash();
}
boOK &= ( ( m_pstMutexID = osMutexNew( &m_stMutexAttr )) == NULL) ? FALSE : TRUE;
boOK &= EE_Init(EE_FORCED_ERASE) == EE_OK ? TRUE : FALSE;
return( boOK );
}
VARH_UVariable USFL_uGetVariable ( void ){
//-------------------------------------------------------------------------------------------------
// Function: USFL_boGetVariable
// Description: Gets a variable out of the virtual eeprom
// Parameters: U8 u8Variable -> virtual adress of variable
// U32 * u32Variable -> pointer to data
// Returns: Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL USFL_boGetVariable ( U8 u8Variable, U32 * u32Variable ){
BOOL boOK = TRUE;
if( u32VarPointer == 0 ) u32VarPointer = u32FindLastPointer();
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
boOK &= EE_ReadVariable32bits( (U16) u8Variable, u32Variable ) == EE_OK ? TRUE : FALSE;
osMutexRelease( m_pstMutexID ); // release mutex
return boOK;
}
//-------------------------------------------------------------------------------------------------
// Function: USFL_boSetVariable
// Description: Writes a variable into the virtual eeprom
// Parameters: U8 u8Variable -> virtual adress of variable
// U32 u32Variable -> data to write
// Returns: Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL USFL_boSetVariable ( U8 u8Variable, U32 u32Variable ){
BOOL boOK = TRUE;
EE_Status ee_status = EE_OK;
osMutexAcquire( m_pstMutexID, osWaitForever ); // aquire mutex
boOK &= (ee_status = EE_WriteVariable32bits( (U16) u8Variable, u32Variable )) == EE_OK ? TRUE : FALSE;
/* Start cleanup IT mode, if cleanup is needed */
if ((ee_status & EE_STATUSMASK_CLEANUP) == EE_STATUSMASK_CLEANUP) {
ErasingOnGoing = 1;
boOK &= (ee_status |= EE_CleanUp()) == EE_OK ? TRUE : FALSE;
}
if ((ee_status & EE_STATUSMASK_ERROR) == EE_STATUSMASK_ERROR) {boOK &= FALSE;}
osMutexRelease( m_pstMutexID ); // release mutex
return boOK;
}
//-------------------------------------------------------------------------------------------------
// Function: USFL_vLock
// Description: Locks the Flash, no more clearing and programming possible
// Parameters: None
// Returns: Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL USFL_vLock( VOID ){
BOOL boOK = TRUE;
boOK &= HAL_FLASH_Lock() == HAL_OK ? TRUE : FALSE;
return boOK;
}
//-------------------------------------------------------------------------------------------------
// Function: USFL_vUnlock
// Description: Unlocks the Flash, clearing and programming possible
// Parameters: None
// Returns: Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL USFL_vUnlock( VOID ){
BOOL boOK = TRUE;
boOK &= HAL_FLASH_Unlock() == HAL_OK ? TRUE : FALSE;
return boOK;
}
//=================================================================================================
@ -160,116 +198,3 @@ VARH_UVariable USFL_uGetVariable ( void ){
// Descriptionn: Definition (implementation) of local functions.
//=================================================================================================
//-------------------------------------------------------------------------------------------------
// Function: u32FindNextFreePointer
// Description: Finds the next free sector in the flash for saving variables
// Parameters: None
// Returns: U32 next free pointer
//-------------------------------------------------------------------------------------------------
U32 u32FindNextFreePointer( void ){
BOOL boFound = FALSE;
U32 u32Pointer = u32VarPointer;
while(!boFound){
if( ( ( UserFlash[u32Pointer] >> 8 ) & 0xFFFFFF ) == VARDEF ){
U8 u8Size = UserFlash[u32Pointer] & 0xFF;
if( u8Size == 0 ){
boFound = TRUE;
} else {
u32Pointer += u8ConvertWordsToDoubleWords(u8Size);
}
} else {
u32Pointer += 1;
}
if( u32Pointer >= USERFLASHSIZE ){
u32Pointer = 1;
break;
}
}
return u32Pointer;
}
//-------------------------------------------------------------------------------------------------
// Function: u32FindLastPointer
// Description: Finds the next free sector in the flash for saving variables
// Parameters: None
// Returns: U32 next free pointer
//-------------------------------------------------------------------------------------------------
U32 u32FindLastPointer( void ){
BOOL boFound = FALSE;
U32 u32Pointer = 0;
U8 u8LastSize = 0;
while(!boFound){
if( ( UserFlash[u32Pointer] >> 40) == VARDEF ){
U8 u8Size = UserFlash[u32Pointer] & 0xFF;
if( u8Size == 0 ){
boFound = TRUE;
u32Pointer -= u8ConvertWordsToDoubleWords(u8LastSize);
} else {
u32Pointer += u8ConvertWordsToDoubleWords(u8Size);
u8LastSize = u8Size;
}
} else {
u32Pointer += 1;
}
if( u32Pointer >= USERFLASHSIZE ){
u32Pointer = 1;
break;
}
}
return u32Pointer;
}
//-------------------------------------------------------------------------------------------------
// Function: u8ConvertWordsToDoubleWords
// Description: Converts 32Bit Word size to 64 Bit Double Word size for saving Vars
// Parameters: U8 u8Words
// Returns: U8 Double Words
//-------------------------------------------------------------------------------------------------
U8 u8ConvertWordsToDoubleWords( U8 u8Words ) {
U8 u8DWords;
u8Words += 1; // + VARDEF
u8DWords = u8Words / 2;
u8DWords += u8Words % 2;
return u8DWords;
}
//-------------------------------------------------------------------------------------------------
// Function: vEreaseUserFlash
// Description: Ereases the User Flash Sector
// Parameters: None
// Returns: Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL vEreaseUserFlash( void ){
uint32_t u32PageError = 0;
BOOL boOK = TRUE;
HAL_FLASH_Unlock();
boOK &= HAL_FLASHEx_Erase(&stEreaseInit, &u32PageError) == HAL_OK ? TRUE : FALSE;
if( !boOK ){
return FALSE;
}
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, (U32) &UserFlash[0], STARTDEF);
HAL_FLASH_Lock();
return TRUE;
}