Merge pull request #14469 from jeromecoutant/PR_FLASH_PROGRAM

STM32: remove critical sections in flash_program_page
pull/14631/head
Martin Kojtal 2021-04-14 20:15:11 +02:00 committed by GitHub
commit dd1da3da40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 48 additions and 105 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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));