From 16e63dc108f0cd123b9942356555fba2c29bfde0 Mon Sep 17 00:00:00 2001 From: reme Date: Fri, 13 Nov 2020 16:31:03 +0100 Subject: [PATCH] STM32 FLASH API : add critical sections See PR #13802 (for F4 board) Concerned boards are STM32F0 STM32F1 STM32F2 STM32F3 STM32F4 STM32F7 STM32G0 STM32G4 STM32H7 STM32L0 STM32L1 STM32L4 STM32L5 Adding test of return code of HAL_FLASH_Lock() function Adding board STM32F4 Running AStyle --- targets/TARGET_STM/TARGET_STM32F0/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32F1/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32F2/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32F3/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32F4/flash_api.c | 9 +++- targets/TARGET_STM/TARGET_STM32F7/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32G0/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32G4/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32H7/flash_api.c | 17 +++++++- targets/TARGET_STM/TARGET_STM32L0/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32L1/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32L4/flash_api.c | 41 ++++++++----------- targets/TARGET_STM/TARGET_STM32L5/flash_api.c | 41 +++++++------------ 13 files changed, 197 insertions(+), 280 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/flash_api.c b/targets/TARGET_STM/TARGET_STM32F0/flash_api.c index 8ed1709a1ce..a764a3af071 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F0/flash_api.c @@ -30,27 +30,6 @@ // Minimum number of bytes to be programmed at a time #define MIN_PROG_SIZE (4U) -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Flash */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_init(flash_t *obj) { return 0; @@ -71,10 +50,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + // Clear Flash status register's flags __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); @@ -93,7 +74,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -111,10 +96,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Program the user Flash area word by word */ StartAddress = address; @@ -145,7 +132,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32F1/flash_api.c b/targets/TARGET_STM/TARGET_STM32F1/flash_api.c index 8fdb457332d..705802934a8 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F1/flash_api.c @@ -30,27 +30,6 @@ // Minimum number of bytes to be programmed at a time #define MIN_PROG_SIZE (4U) -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Flash */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_init(flash_t *obj) { return 0; @@ -71,10 +50,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + // Clear Flash status register's flags __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR | FLASH_FLAG_OPTVERR); @@ -93,7 +74,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -111,10 +96,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Program the user Flash area word by word */ StartAddress = address; @@ -145,7 +132,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32F2/flash_api.c b/targets/TARGET_STM/TARGET_STM32F2/flash_api.c index 421d6bcc435..cfc53e9462f 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F2/flash_api.c @@ -25,27 +25,6 @@ static uint32_t GetSector(uint32_t Address); static uint32_t GetSectorSize(uint32_t Sector); -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_init(flash_t *obj) { return 0; @@ -67,10 +46,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Get the 1st sector to erase */ FirstSector = GetSector(address); @@ -84,7 +65,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -97,10 +82,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, you have to make sure that these data are rewritten before they are accessed during code execution. If this cannot be done safely, it is recommended to flush the caches by setting the @@ -124,7 +111,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32F3/flash_api.c b/targets/TARGET_STM/TARGET_STM32F3/flash_api.c index 8ed1709a1ce..a764a3af071 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F3/flash_api.c @@ -30,27 +30,6 @@ // Minimum number of bytes to be programmed at a time #define MIN_PROG_SIZE (4U) -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Flash */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_init(flash_t *obj) { return 0; @@ -71,10 +50,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + // Clear Flash status register's flags __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR); @@ -93,7 +74,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -111,10 +96,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Program the user Flash area word by word */ StartAddress = address; @@ -145,7 +132,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c index 5fa6bd397f7..a2e88962491 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c @@ -65,7 +65,9 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) core_util_critical_section_exit(); - HAL_FLASH_Lock(); + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -83,6 +85,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } core_util_critical_section_enter(); + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, you have to make sure that these data are rewritten before they are accessed during code execution. If this cannot be done safely, it is recommended to flush the caches by setting the @@ -108,7 +111,9 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, core_util_critical_section_exit(); - HAL_FLASH_Lock(); + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32F7/flash_api.c b/targets/TARGET_STM/TARGET_STM32F7/flash_api.c index 7b6bfeac8ec..14191def769 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F7/flash_api.c @@ -61,27 +61,6 @@ int32_t flash_free(flash_t *obj) return 0; } -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_erase_sector(flash_t *obj, uint32_t address) { /* Variable used for Erase procedure */ @@ -94,10 +73,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, you have to make sure that these data are rewritten before they are accessed during code execution. If this cannot be done safely, it is recommended to flush the caches by setting the @@ -122,7 +103,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) SCB_CleanInvalidateDCache_by_Addr((uint32_t *)GetSectorBase(SectorId), GetSectorSize(SectorId)); SCB_InvalidateICache(); - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -138,10 +123,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache, you have to make sure that these data are rewritten before they are accessed during code execution. If this cannot be done safely, it is recommended to flush the caches by setting the @@ -164,7 +151,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, SCB_CleanInvalidateDCache_by_Addr((uint32_t *)StartAddress, FullSize); SCB_InvalidateICache(); - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32G0/flash_api.c b/targets/TARGET_STM/TARGET_STM32G0/flash_api.c index be89f7ec9b6..6c90ddd6914 100644 --- a/targets/TARGET_STM/TARGET_STM32G0/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32G0/flash_api.c @@ -34,27 +34,6 @@ int32_t flash_free(flash_t *obj) return 0; } -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_erase_sector(flash_t *obj, uint32_t address) { uint32_t PAGEError = 0; @@ -66,10 +45,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear OPTVERR bit set on virgin samples */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); @@ -88,7 +69,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; @@ -109,10 +94,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Program the user Flash area word by word */ StartAddress = address; @@ -143,7 +130,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32G4/flash_api.c b/targets/TARGET_STM/TARGET_STM32G4/flash_api.c index 4b12ce0b9c3..e3229569eff 100644 --- a/targets/TARGET_STM/TARGET_STM32G4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32G4/flash_api.c @@ -91,27 +91,6 @@ int32_t flash_free(flash_t *obj) return 0; } -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - /** Erase one sector starting at defined address * * The address should be at sector boundary. This function does not do any check for address alignments @@ -131,10 +110,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -158,7 +139,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -189,10 +174,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -229,7 +216,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32H7/flash_api.c b/targets/TARGET_STM/TARGET_STM32H7/flash_api.c index 920fe182ac2..9c2df12af52 100644 --- a/targets/TARGET_STM/TARGET_STM32H7/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32H7/flash_api.c @@ -62,6 +62,7 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) while (LL_HSEM_1StepLock(HSEM, CFG_HW_FLASH_SEMID)) { } #endif /* DUAL_CORE */ + if (HAL_FLASH_Unlock() != HAL_OK) { #if defined(DUAL_CORE) LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, HSEM_CR_COREID_CURRENT); @@ -69,6 +70,8 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } + core_util_critical_section_enter(); + /* Fill EraseInit structure */ EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS; #if defined (FLASH_CR_PSIZE) @@ -97,9 +100,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) SCB_InvalidateICache(); #endif /* DUAL_CORE */ + core_util_critical_section_exit(); + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } - HAL_FLASH_Lock(); #if defined(DUAL_CORE) LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, HSEM_CR_COREID_CURRENT); #endif /* DUAL_CORE */ @@ -128,6 +134,8 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } + core_util_critical_section_enter(); + StartAddress = address; while ((address < (StartAddress + size)) && (status == 0)) { if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, address, (uint32_t)data) == HAL_OK) { @@ -148,7 +156,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, SCB_InvalidateICache(); #endif /* DUAL_CORE */ - HAL_FLASH_Lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } + #if defined(DUAL_CORE) LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, HSEM_CR_COREID_CURRENT); #endif /* DUAL_CORE */ diff --git a/targets/TARGET_STM/TARGET_STM32L0/flash_api.c b/targets/TARGET_STM/TARGET_STM32L0/flash_api.c index 0ed01d1425e..85a42753c36 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L0/flash_api.c @@ -36,27 +36,6 @@ int32_t flash_free(flash_t *obj) return 0; } -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_erase_sector(flash_t *obj, uint32_t address) { uint32_t PAGEError = 0; @@ -68,10 +47,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear OPTVERR bit set on virgin samples */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); @@ -90,7 +71,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; @@ -111,10 +96,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Program the user Flash area word by word */ StartAddress = address; @@ -145,7 +132,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32L1/flash_api.c b/targets/TARGET_STM/TARGET_STM32L1/flash_api.c index cae36c8c0ba..454d0ae8a54 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/flash_api.c @@ -36,27 +36,6 @@ int32_t flash_free(flash_t *obj) return 0; } -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - int32_t flash_erase_sector(flash_t *obj, uint32_t address) { uint32_t PAGEError = 0; @@ -68,10 +47,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR | FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR); /* MBED HAL erases 1 sector at a time */ /* Fill EraseInit structure*/ @@ -88,7 +69,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; @@ -109,10 +94,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Program the user Flash area word by word */ StartAddress = address; @@ -143,7 +130,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32L4/flash_api.c b/targets/TARGET_STM/TARGET_STM32L4/flash_api.c index 9457d28e491..49fc2729290 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L4/flash_api.c @@ -94,27 +94,6 @@ int32_t flash_free(flash_t *obj) return 0; } -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - /** Erase one sector starting at defined address * * The address should be at sector boundary. This function does not do any check for address alignments @@ -134,10 +113,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -161,7 +142,11 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) status = -1; } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } @@ -192,10 +177,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -232,7 +219,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - flash_lock(); + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { + return -1; + } return status; } diff --git a/targets/TARGET_STM/TARGET_STM32L5/flash_api.c b/targets/TARGET_STM/TARGET_STM32L5/flash_api.c index e8a9eba0d06..f1b1431bb93 100644 --- a/targets/TARGET_STM/TARGET_STM32L5/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L5/flash_api.c @@ -72,7 +72,7 @@ static uint32_t GetBank(uint32_t Addr) int32_t flash_init(flash_t *obj) { #ifdef TARGET_TFM -/* TFM implementation needs dual bank configuration */ + /* TFM implementation needs dual bank configuration */ if (READ_BIT(FLASH->OPTR, FLASH_OPTR_DBANK) != 0U) { return 0; } else { @@ -93,27 +93,6 @@ int32_t flash_free(flash_t *obj) return 0; } -static int32_t flash_unlock(void) -{ - /* Allow Access to Flash control registers and user Falsh */ - if (HAL_FLASH_Unlock()) { - return -1; - } else { - return 0; - } -} - -static int32_t flash_lock(void) -{ - /* Disable the Flash option control register access (recommended to protect - the option Bytes against possible unwanted operations) */ - if (HAL_FLASH_Lock()) { - return -1; - } else { - return 0; - } -} - /** Erase one sector starting at defined address * * The address should be at sector boundary. This function does not do any check for address alignments @@ -131,10 +110,12 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -154,7 +135,9 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - if (flash_lock() != HAL_OK) { + core_util_critical_section_exit(); + + if (HAL_FLASH_Lock() != HAL_OK) { return -1; } @@ -186,10 +169,12 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - if (flash_unlock() != HAL_OK) { + if (HAL_FLASH_Unlock() != HAL_OK) { return -1; } + core_util_critical_section_enter(); + /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -226,7 +211,11 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - status = flash_unlock(); + if (HAL_FLASH_Unlock() != HAL_OK) { + return -1; + } + + core_util_critical_section_enter(); return status; }