mirror of
https://github.com/ArteryTek/AT32F415_Firmware_Library.git
synced 2026-05-21 09:22:11 +00:00
update version to v2.0.4
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
/**
|
||||
**************************************************************************
|
||||
* @file at32f415_flash.c
|
||||
* @version v2.0.3
|
||||
* @date 2022-02-11
|
||||
* @version v2.0.4
|
||||
* @date 2022-04-02
|
||||
* @brief contains all the functions for the flash firmware library
|
||||
**************************************************************************
|
||||
* Copyright notice & Disclaimer
|
||||
*
|
||||
* The software Board Support Package (BSP) that is made available to
|
||||
* download from Artery official website is the copyrighted work of Artery.
|
||||
* Artery authorizes customers to use, copy, and distribute the BSP
|
||||
* software and its related documentation for the purpose of design and
|
||||
* development in conjunction with Artery microcontrollers. Use of the
|
||||
* The software Board Support Package (BSP) that is made available to
|
||||
* download from Artery official website is the copyrighted work of Artery.
|
||||
* Artery authorizes customers to use, copy, and distribute the BSP
|
||||
* software and its related documentation for the purpose of design and
|
||||
* development in conjunction with Artery microcontrollers. Use of the
|
||||
* software is governed by this copyright notice and the following disclaimer.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
|
||||
@@ -45,11 +45,11 @@
|
||||
* @brief check whether the specified flash flag is set or not.
|
||||
* @param flash_flag: specifies the flash flag to check.
|
||||
* this parameter can be one of flash flag status:
|
||||
* - FLASH_OBF_FLAG
|
||||
* - FLASH_ODF_FLAG
|
||||
* - FLASH_PRGMERR_FLAG
|
||||
* - FLASH_EPPERR_FLAG
|
||||
* - FLASH_USDERR_FLAG
|
||||
* - FLASH_OBF_FLAG
|
||||
* - FLASH_ODF_FLAG
|
||||
* - FLASH_PRGMERR_FLAG
|
||||
* - FLASH_EPPERR_FLAG
|
||||
* - FLASH_USDERR_FLAG
|
||||
* @retval the new state of flash_flag (SET or RESET).
|
||||
*/
|
||||
flag_status flash_flag_get(uint32_t flash_flag)
|
||||
@@ -79,9 +79,9 @@ flag_status flash_flag_get(uint32_t flash_flag)
|
||||
* @brief clear the flash flag.
|
||||
* @param flash_flag: specifies the flash flags to clear.
|
||||
* this parameter can be any combination of the following values:
|
||||
* - FLASH_ODF_FLAG
|
||||
* - FLASH_PRGMERR_FLAG
|
||||
* - FLASH_EPPERR_FLAG
|
||||
* - FLASH_ODF_FLAG
|
||||
* - FLASH_PRGMERR_FLAG
|
||||
* - FLASH_EPPERR_FLAG
|
||||
* @retval none
|
||||
*/
|
||||
void flash_flag_clear(uint32_t flash_flag)
|
||||
@@ -92,7 +92,7 @@ void flash_flag_clear(uint32_t flash_flag)
|
||||
/**
|
||||
* @brief return the flash operation status.
|
||||
* @param none
|
||||
* @retval status: the returned value can be: FLASH_OPERATE_BUSY,
|
||||
* @retval status: the returned value can be: FLASH_OPERATE_BUSY,
|
||||
* FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
|
||||
*/
|
||||
flash_status_type flash_operation_status_get(void)
|
||||
@@ -121,7 +121,7 @@ flash_status_type flash_operation_status_get(void)
|
||||
/**
|
||||
* @brief wait for flash operation complete or timeout.
|
||||
* @param time_out: flash operation timeout
|
||||
* @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
|
||||
* @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
|
||||
* FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
|
||||
*/
|
||||
flash_status_type flash_operation_wait_for(uint32_t time_out)
|
||||
@@ -167,7 +167,7 @@ void flash_lock(void)
|
||||
/**
|
||||
* @brief erase a specified flash sector.
|
||||
* @param sector_address: the sector address to be erased.
|
||||
* @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
|
||||
* @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
|
||||
* FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
|
||||
*/
|
||||
flash_status_type flash_sector_erase(uint32_t sector_address)
|
||||
@@ -175,20 +175,20 @@ flash_status_type flash_sector_erase(uint32_t sector_address)
|
||||
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)
|
||||
{
|
||||
/* if the previous operation is completed, continue to erase the sector */
|
||||
FLASH->ctrl_bit.secers = TRUE;
|
||||
FLASH->addr = sector_address;
|
||||
FLASH->ctrl_bit.erstr = TRUE;
|
||||
|
||||
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(ERASE_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the secers bit */
|
||||
FLASH->ctrl_bit.secers = FALSE;
|
||||
}
|
||||
}
|
||||
/* return the erase status */
|
||||
return status;
|
||||
}
|
||||
@@ -204,16 +204,16 @@ flash_status_type flash_internal_all_erase(void)
|
||||
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)
|
||||
{
|
||||
/* if the previous operation is completed, continue to erase */
|
||||
FLASH->ctrl_bit.bankers = TRUE;
|
||||
FLASH->ctrl_bit.erstr = TRUE;
|
||||
|
||||
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(ERASE_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the bankers bit */
|
||||
FLASH->ctrl_bit.bankers = FALSE;
|
||||
}
|
||||
@@ -238,10 +238,10 @@ flash_status_type flash_user_system_data_erase(void)
|
||||
{
|
||||
fap_val = 0x0000;
|
||||
}
|
||||
|
||||
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(ERASE_TIMEOUT);
|
||||
|
||||
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* unlock the user system data */
|
||||
@@ -252,24 +252,24 @@ flash_status_type flash_user_system_data_erase(void)
|
||||
/* 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;
|
||||
}
|
||||
@@ -290,17 +290,17 @@ flash_status_type flash_word_program(uint32_t address, uint32_t data)
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
FLASH->ctrl_bit.fprgm = TRUE;
|
||||
*(__IO uint32_t*)address = data;
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the fprgm bit */
|
||||
FLASH->ctrl_bit.fprgm = FALSE;
|
||||
}
|
||||
}
|
||||
/* return the program status */
|
||||
return status;
|
||||
}
|
||||
@@ -317,14 +317,14 @@ flash_status_type flash_halfword_program(uint32_t address, uint16_t data)
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
FLASH->ctrl_bit.fprgm = TRUE;
|
||||
*(__IO uint16_t*)address = data;
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the fprgm bit */
|
||||
FLASH->ctrl_bit.fprgm = FALSE;
|
||||
}
|
||||
@@ -344,14 +344,14 @@ flash_status_type flash_byte_program(uint32_t address, uint8_t data)
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
FLASH->ctrl_bit.fprgm = TRUE;
|
||||
*(__IO uint8_t*)address = data;
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the fprgm bit */
|
||||
FLASH->ctrl_bit.fprgm = FALSE;
|
||||
}
|
||||
@@ -376,14 +376,14 @@ flash_status_type flash_user_system_data_program(uint32_t address, uint8_t 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;
|
||||
*(__IO uint16_t*)address = data;
|
||||
|
||||
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the usdprgm bit */
|
||||
FLASH->ctrl_bit.usdprgm = FALSE;
|
||||
}
|
||||
@@ -393,8 +393,8 @@ flash_status_type flash_user_system_data_program(uint32_t address, uint8_t data)
|
||||
|
||||
/**
|
||||
* @brief config erase/program protection for the desired sectors.
|
||||
* @param sector_bits:
|
||||
* the pointer of the address of the sectors to be erase/program protected.
|
||||
* @param sector_bits:
|
||||
* the pointer of the address of the sectors to be erase/program protected.
|
||||
* the first 31 bits general every bit is used to protect 2 sectors. the bit
|
||||
* 31 is used to protect the rest sectors and extension memory.
|
||||
* @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
|
||||
@@ -402,7 +402,7 @@ flash_status_type flash_user_system_data_program(uint32_t address, uint8_t data)
|
||||
*/
|
||||
flash_status_type flash_epp_set(uint32_t *sector_bits)
|
||||
{
|
||||
uint16_t epp_data[4] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
|
||||
uint16_t epp_data[4] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
sector_bits[0] = (uint32_t)(~sector_bits[0]);
|
||||
epp_data[0] = (uint16_t)((sector_bits[0] >> 0) & 0xFF);
|
||||
@@ -411,19 +411,19 @@ flash_status_type flash_epp_set(uint32_t *sector_bits)
|
||||
epp_data[3] = (uint16_t)((sector_bits[0] >> 24) & 0xFF);
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
if(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);
|
||||
|
||||
|
||||
FLASH->ctrl_bit.usdprgm = TRUE;
|
||||
USD->epp0 = epp_data[0];
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
USD->epp1 = epp_data[1];
|
||||
@@ -451,7 +451,7 @@ flash_status_type flash_epp_set(uint32_t *sector_bits)
|
||||
|
||||
/**
|
||||
* @brief return the flash erase/program protection status.
|
||||
* @param sector_bits: pointer to get the epps register.
|
||||
* @param sector_bits: pointer to get the epps register.
|
||||
* @retval none
|
||||
*/
|
||||
void flash_epp_status_get(uint32_t *sector_bits)
|
||||
@@ -479,15 +479,15 @@ flash_status_type flash_fap_enable(confirm_state new_state)
|
||||
FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
|
||||
FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
|
||||
while(FLASH->ctrl_bit.usdulks==RESET);
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
if(new_state == FALSE)
|
||||
@@ -495,10 +495,10 @@ flash_status_type flash_fap_enable(confirm_state new_state)
|
||||
/* enable the user system data programming operation */
|
||||
FLASH->ctrl_bit.usdprgm = TRUE;
|
||||
USD->fap = FAP_RELIEVE_KEY;
|
||||
|
||||
|
||||
/* Wait for operation to be completed */
|
||||
status = flash_operation_wait_for(ERASE_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the usdprgm bit */
|
||||
FLASH->ctrl_bit.usdprgm = FALSE;
|
||||
}
|
||||
@@ -537,30 +537,30 @@ flash_status_type flash_fap_high_level_enable(confirm_state new_state)
|
||||
FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
|
||||
FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
|
||||
while(FLASH->ctrl_bit.usdulks==RESET);
|
||||
|
||||
|
||||
if(new_state == FALSE)
|
||||
{
|
||||
FLASH->ctrl_bit.fap_hl_dis = TRUE;
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(ERASE_TIMEOUT);
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
/* enable the user system data programming operation */
|
||||
FLASH->ctrl_bit.usdprgm = TRUE;
|
||||
USD->fap = FAP_RELIEVE_KEY;
|
||||
|
||||
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(ERASE_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the usdprgm bit */
|
||||
FLASH->ctrl_bit.usdprgm = FALSE;
|
||||
}
|
||||
@@ -571,19 +571,19 @@ flash_status_type flash_fap_high_level_enable(confirm_state new_state)
|
||||
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)
|
||||
{
|
||||
/* enable the user system data programming operation */
|
||||
FLASH->ctrl_bit.usdprgm = TRUE;
|
||||
USD->fap = FAP_HIGH_LEVEL_KEY;
|
||||
|
||||
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the usdprgm bit */
|
||||
FLASH->ctrl_bit.usdprgm = FALSE;
|
||||
}
|
||||
@@ -606,8 +606,8 @@ flag_status flash_fap_high_level_status_get(void)
|
||||
/**
|
||||
* @brief program the flash system setting byte in usd: wdt_ato_en / depslp_rst / stdby_rst .
|
||||
* @param usd_ssb: the system setting byte
|
||||
* @note this parameter usd_ssb must contain a combination of all the following 3 types of data
|
||||
* type 1: wdt_ato_en, select the wdt auto start
|
||||
* @note this parameter usd_ssb must contain a combination of all the following 3 types of data
|
||||
* type 1: wdt_ato_en, select the wdt auto start
|
||||
* this data can be one of the following values:
|
||||
* - USD_WDT_ATO_DISABLE: disable wdt auto start
|
||||
* - USD_WDT_ATO_ENABLE: enable wdt auto start
|
||||
@@ -627,21 +627,21 @@ flash_status_type flash_ssb_set(uint8_t usd_ssb)
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
if(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;
|
||||
|
||||
|
||||
USD->ssb = usd_ssb;
|
||||
/* wait for operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
/* disable the usdprgm bit */
|
||||
FLASH->ctrl_bit.usdprgm = FALSE;
|
||||
}
|
||||
@@ -665,8 +665,8 @@ uint8_t flash_ssb_status_get(void)
|
||||
* @brief enable or disable the specified flash interrupts.
|
||||
* @param flash_int: specifies the flash interrupt sources to be enabled or disabled.
|
||||
* this parameter can be any combination of the following values:
|
||||
* - FLASH_ERR_INT
|
||||
* - FLASH_ODF_INT
|
||||
* - FLASH_ERR_INT
|
||||
* - FLASH_ODF_INT
|
||||
* @param new_state: new state of the specified flash interrupts.
|
||||
* this parameter can be: TRUE or FALSE.
|
||||
* @retval none
|
||||
@@ -694,12 +694,12 @@ flash_status_type flash_slib_enable(uint32_t pwd, uint16_t start_sector, uint16_
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
/*check range param limits*/
|
||||
if((start_sector>=data_start_sector) || ((data_start_sector > end_sector) && \
|
||||
(data_start_sector != 0x7FF)) || (start_sector > end_sector))
|
||||
return FLASH_PROGRAM_ERROR;
|
||||
|
||||
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* unlock slib cfg register */
|
||||
@@ -728,7 +728,7 @@ error_status flash_slib_disable(uint32_t pwd)
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
/* write password to disable slib */
|
||||
FLASH->slib_pwd_clr = pwd;
|
||||
|
||||
|
||||
status = flash_operation_wait_for(ERASE_TIMEOUT);
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
@@ -826,13 +826,13 @@ flash_status_type flash_extension_memory_slib_enable(uint32_t pwd, uint16_t data
|
||||
flash_status_type status = FLASH_OPERATE_DONE;
|
||||
/* wait for last operation to be completed */
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
|
||||
if(status == FLASH_OPERATE_DONE)
|
||||
{
|
||||
/* unlock slib cfg register */
|
||||
FLASH->slib_unlock = SLIB_UNLOCK_KEY;
|
||||
while(FLASH->slib_misc_sts_bit.slib_ulkf==RESET);
|
||||
|
||||
|
||||
/* configure slib, set pwd and range */
|
||||
FLASH->slib_set_pwd = pwd;
|
||||
status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
|
||||
|
||||
Reference in New Issue
Block a user