From d0ea0466ad5286a1bded9c03e87c4498b1713d47 Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Wed, 24 Mar 2021 16:46:26 +0100 Subject: [PATCH] STM32: remove critical sections in flash_program_page --- targets/TARGET_STM/TARGET_STM32F0/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32F1/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32F2/flash_api.c | 4 - targets/TARGET_STM/TARGET_STM32F3/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32F4/flash_api.c | 4 - targets/TARGET_STM/TARGET_STM32F7/flash_api.c | 4 - targets/TARGET_STM/TARGET_STM32G0/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32G4/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32H7/flash_api.c | 4 - targets/TARGET_STM/TARGET_STM32L0/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32L1/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32L4/flash_api.c | 6 +- targets/TARGET_STM/TARGET_STM32L5/flash_api.c | 5 +- targets/TARGET_STM/TARGET_STM32WL/flash_api.c | 84 +++++++++---------- 14 files changed, 48 insertions(+), 105 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/flash_api.c b/targets/TARGET_STM/TARGET_STM32F0/flash_api.c index 5cc4185d98..7f2bba60bf 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F0/flash_api.c @@ -14,7 +14,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -97,8 +97,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - core_util_critical_section_enter(); - /* Program the user Flash area word by word */ StartAddress = address; @@ -129,8 +127,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32F1/flash_api.c b/targets/TARGET_STM/TARGET_STM32F1/flash_api.c index 705802934a..f8edb0394e 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F1/flash_api.c @@ -17,7 +17,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -100,8 +100,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - core_util_critical_section_enter(); - /* Program the user Flash area word by word */ StartAddress = address; @@ -132,8 +130,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32F2/flash_api.c b/targets/TARGET_STM/TARGET_STM32F2/flash_api.c index cfc53e9462..8cc93dfbbb 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F2/flash_api.c @@ -86,8 +86,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, 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 @@ -111,8 +109,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32F3/flash_api.c b/targets/TARGET_STM/TARGET_STM32F3/flash_api.c index a764a3af07..3df75ddbdc 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F3/flash_api.c @@ -17,7 +17,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -100,8 +100,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - core_util_critical_section_enter(); - /* Program the user Flash area word by word */ StartAddress = address; @@ -132,8 +130,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c index 92e7d73850..cd43653aaa 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F4/flash_api.c @@ -81,8 +81,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, 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 @@ -106,8 +104,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32F7/flash_api.c b/targets/TARGET_STM/TARGET_STM32F7/flash_api.c index 14191def76..e21b042779 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32F7/flash_api.c @@ -127,8 +127,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, 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 @@ -151,8 +149,6 @@ 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(); - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32G0/flash_api.c b/targets/TARGET_STM/TARGET_STM32G0/flash_api.c index 6c90ddd691..496e6e7c4f 100644 --- a/targets/TARGET_STM/TARGET_STM32G0/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32G0/flash_api.c @@ -17,7 +17,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -98,8 +98,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - core_util_critical_section_enter(); - /* Program the user Flash area word by word */ StartAddress = address; @@ -130,8 +128,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32G4/flash_api.c b/targets/TARGET_STM/TARGET_STM32G4/flash_api.c index 7a07e3f6da..0528a28eeb 100644 --- a/targets/TARGET_STM/TARGET_STM32G4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32G4/flash_api.c @@ -14,7 +14,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -177,8 +177,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - core_util_critical_section_enter(); - /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -215,8 +213,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32H7/flash_api.c b/targets/TARGET_STM/TARGET_STM32H7/flash_api.c index 9c2df12af5..179f9e3b05 100644 --- a/targets/TARGET_STM/TARGET_STM32H7/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32H7/flash_api.c @@ -134,8 +134,6 @@ 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) { @@ -156,8 +154,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, SCB_InvalidateICache(); #endif /* DUAL_CORE */ - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32L0/flash_api.c b/targets/TARGET_STM/TARGET_STM32L0/flash_api.c index 85a42753c3..3c6cc71c9d 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L0/flash_api.c @@ -17,7 +17,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -100,8 +100,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - core_util_critical_section_enter(); - /* Program the user Flash area word by word */ StartAddress = address; @@ -132,8 +130,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32L1/flash_api.c b/targets/TARGET_STM/TARGET_STM32L1/flash_api.c index 857716cc09..66c0418eb6 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/flash_api.c @@ -14,7 +14,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -95,8 +95,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - core_util_critical_section_enter(); - /* Program the user Flash area word by word */ StartAddress = address; @@ -127,8 +125,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32L4/flash_api.c b/targets/TARGET_STM/TARGET_STM32L4/flash_api.c index 49fc272929..81d502871a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L4/flash_api.c @@ -16,7 +16,7 @@ */ #include "flash_api.h" -#include "mbed_critical.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH #include "mbed_assert.h" @@ -181,8 +181,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - core_util_critical_section_enter(); - /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -219,8 +217,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, } } - core_util_critical_section_exit(); - if (HAL_FLASH_Lock() != HAL_OK) { return -1; } diff --git a/targets/TARGET_STM/TARGET_STM32L5/flash_api.c b/targets/TARGET_STM/TARGET_STM32L5/flash_api.c index f1b1431bb9..790ddbf5be 100644 --- a/targets/TARGET_STM/TARGET_STM32L5/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32L5/flash_api.c @@ -14,6 +14,7 @@ */ #include "flash_api.h" +#include "platform/mbed_critical.h" #if DEVICE_FLASH @@ -173,8 +174,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - core_util_critical_section_enter(); - /* Clear error programming flags */ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS); @@ -215,8 +214,6 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, return -1; } - core_util_critical_section_enter(); - return status; } diff --git a/targets/TARGET_STM/TARGET_STM32WL/flash_api.c b/targets/TARGET_STM/TARGET_STM32WL/flash_api.c index b5e59e1017..2c0e92d7a4 100644 --- a/targets/TARGET_STM/TARGET_STM32WL/flash_api.c +++ b/targets/TARGET_STM/TARGET_STM32WL/flash_api.c @@ -16,9 +16,7 @@ #if DEVICE_FLASH #include "flash_api.h" -#include "mbed_critical.h" -#include "mbed_assert.h" -#include "cmsis.h" +#include "platform/mbed_critical.h" /** * @brief Gets the page of a given address @@ -73,22 +71,22 @@ int32_t flash_erase_sector(flash_t *obj, uint32_t address) return -1; } - core_util_critical_section_enter(); + core_util_critical_section_enter(); - /* Clear OPTVERR bit set on virgin samples */ - __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); + /* Clear OPTVERR bit set on virgin samples */ + __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); - /* Get the page number associated to the address */ - PageNumber = GetPage(address); - EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; - EraseInitStruct.Page = PageNumber; - EraseInitStruct.NbPages = 1; + /* Get the page number associated to the address */ + PageNumber = GetPage(address); + EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; + EraseInitStruct.Page = PageNumber; + EraseInitStruct.NbPages = 1; - if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) { - status = -1; - } + if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) { + status = -1; + } - core_util_critical_section_exit(); + core_util_critical_section_exit(); while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)); @@ -129,37 +127,33 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, return -1; } - core_util_critical_section_enter(); + /* Program the user Flash area word by word */ + StartAddress = address; - /* Program the user Flash area word by word */ - StartAddress = address; - - /* HW needs an aligned address to program flash, which data parameters doesn't ensure */ - if ((uint32_t) data % 8 != 0) { // Data is not aligned, copy data in a temp buffer before programming it - volatile uint64_t data64; - while ((address < (StartAddress + size)) && (status == 0)) { - for (uint8_t i = 0; i < 8; i++) { - *(((uint8_t *) &data64) + i) = *(data + i); - } - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data64) == HAL_OK) { - address = address + 8; - data = data + 8; - } else { - status = -1; - } - } - } else { // Data is aligned, so let's avoid any copy - while ((address < (StartAddress + size)) && (status == 0)) { - if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, *((uint64_t *) data)) == HAL_OK) { - address = address + 8; - data = data + 8; - } else { - status = -1; - } - } - } - - core_util_critical_section_exit(); + /* HW needs an aligned address to program flash, which data parameters doesn't ensure */ + if ((uint32_t) data % 8 != 0) { // Data is not aligned, copy data in a temp buffer before programming it + volatile uint64_t data64; + while ((address < (StartAddress + size)) && (status == 0)) { + for (uint8_t i = 0; i < 8; i++) { + *(((uint8_t *) &data64) + i) = *(data + i); + } + if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data64) == HAL_OK) { + address = address + 8; + data = data + 8; + } else { + status = -1; + } + } + } else { // Data is aligned, so let's avoid any copy + while ((address < (StartAddress + size)) && (status == 0)) { + if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, *((uint64_t *) data)) == HAL_OK) { + address = address + 8; + data = data + 8; + } else { + status = -1; + } + } + } while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY));