From a8ad10b9a0f16854de0cc17974d3a2e2fd8a66ff Mon Sep 17 00:00:00 2001 From: cfif Date: Wed, 28 May 2025 15:16:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BD=D0=B0=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=B8=D0=BD=D0=BE=D0=B2=20=D0=B8=20=D1=83=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=B9=D1=81=D1=82=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/at32f435_437_flash.c | 77 ++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/src/at32f435_437_flash.c b/src/at32f435_437_flash.c index ab23a50..8bed15c 100644 --- a/src/at32f435_437_flash.c +++ b/src/at32f435_437_flash.c @@ -558,51 +558,46 @@ flash_status_type flash_bank2_erase(void) */ flash_status_type flash_user_system_data_erase(void) { - flash_status_type status = FLASH_OPERATE_DONE; - uint16_t fap_val = FAP_RELIEVE_KEY; - /* get the flash access protection status */ - if(flash_fap_status_get() != RESET) - { - fap_val = 0x0000; - } - - /* wait for last operation to be completed */ - status = flash_operation_wait_for(ERASE_TIMEOUT); - - if(status == FLASH_OPERATE_DONE) - { + flash_status_type status = FLASH_OPERATE_DONE; + uint16_t fap_val = FAP_RELIEVE_KEY; + /* get the flash access protection status */ + if(flash_fap_status_get() != RESET) + { + fap_val = 0x0000; + } + /* unlock the user system data */ FLASH->usd_unlock = FLASH_UNLOCK_KEY1; FLASH->usd_unlock = FLASH_UNLOCK_KEY2; while(FLASH->ctrl_bit.usdulks==RESET); - + /* erase the user system data */ FLASH->ctrl_bit.usders = TRUE; FLASH->ctrl_bit.erstr = TRUE; - + /* wait for operation to be completed */ status = flash_operation_wait_for(ERASE_TIMEOUT); - + /* disable the usders bit */ FLASH->ctrl_bit.usders = FALSE; if((status == FLASH_OPERATE_DONE) && (fap_val == FAP_RELIEVE_KEY)) { - /* enable the user system data programming operation */ - FLASH->ctrl_bit.usdprgm = TRUE; - - /* restore the last flash access protection value */ - USD->fap = (uint16_t)fap_val; - - /* wait for operation to be completed */ - status = flash_operation_wait_for(PROGRAMMING_TIMEOUT); - - /*disable the usdprgm bit */ - FLASH->ctrl_bit.usdprgm = FALSE; + /* enable the user system data programming operation */ + FLASH->ctrl_bit.usdprgm = TRUE; + + /* restore the last flash access protection value */ + USD->fap = (uint16_t)fap_val; + + /* wait for operation to be completed */ + status = flash_operation_wait_for(PROGRAMMING_TIMEOUT); + + /*disable the usdprgm bit */ + FLASH->ctrl_bit.usdprgm = FALSE; } - } - /* return the status */ - return status; + + /* return the status */ + return status; } /** @@ -623,34 +618,30 @@ flash_status_type flash_user_system_data_erase(void) */ flash_status_type flash_eopb0_config(flash_usd_eopb0_type data) { - flash_status_type status = FLASH_OPERATE_DONE; - - /* wait for last operation to be completed */ - status = flash_operation_wait_for(ERASE_TIMEOUT); - - if(status == FLASH_OPERATE_DONE) - { + flash_status_type status = FLASH_OPERATE_DONE; + /* unlock the user system data */ FLASH->usd_unlock = FLASH_UNLOCK_KEY1; FLASH->usd_unlock = FLASH_UNLOCK_KEY2; while(FLASH->ctrl_bit.usdulks==RESET); - + /* enable the user system data programming operation */ FLASH->ctrl_bit.usdprgm = TRUE; /* restore the default eopb0 value */ USD->eopb0 = (uint16_t)data; - + /* wait for operation to be completed */ status = flash_operation_wait_for(PROGRAMMING_TIMEOUT); - + /*disable the usdprgm bit */ FLASH->ctrl_bit.usdprgm = FALSE; - } - /* return the status */ - return status; + + /* return the status */ + return status; } + /** * @brief program a word at a specified address. * @param address: specifies the address to be programmed, word alignment is recommended.