Skip to content

STM32 FLASH API : add critical sections #13914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions targets/TARGET_STM/TARGET_STM32F0/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

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

Expand Down Expand Up @@ -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;
}
Expand Down
41 changes: 16 additions & 25 deletions targets/TARGET_STM/TARGET_STM32F1/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

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

Expand Down Expand Up @@ -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;
}
Expand Down
41 changes: 16 additions & 25 deletions targets/TARGET_STM/TARGET_STM32F2/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
41 changes: 16 additions & 25 deletions targets/TARGET_STM/TARGET_STM32F3/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

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

Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions targets/TARGET_STM/TARGET_STM32F4/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
Loading