HAL FLASH How to Use ¶
- group FLASH_How_To_Use
-
The Flash memory interface manages CPU AHB C-Bus accesses to the Flash memory. It implements the erase and program Flash memory operations and the read and write protection mechanisms.
FLASH main features ¶
The FLASH memory organization is based on a main area and an information block.
The main flash memory block organized as two banks of up to 1 Mbyte each containing up to 128 pages of 8 Kbytes.
The information block contains :
112 Kbytes for system memory. It contains the bootloader that is used to reprogram the flash memory.
1 kbyte OTP (one-time programmable). The OTP data cannot be erased and can be written only once.
Option bytes for user configuration. The FLASH_ITF module provides option bytes configuration functions.
Programming operation ¶
The flash memory program operation can be performed 128, 64, 32, 16 or 8 bits at a time within the flash user memory areas.
The flash memory program operation can be performed 32 or 16 bits at a time within the flash data and OTP memory areas.
Erasing operation ¶
The flash memory erase operation can be performed at page level.
The flash memory erase operation can be performed at bank level.
The flash memory erase operation can be performed on the whole flash memory.
How to use the FLASH HAL module driver ¶
Initialization and De-initialization functions : ¶
For a given instance, use the HAL_FLASH_Init() function to initialize the FLASH handle and associate the physical instance.
Use the HAL_FLASH_DeInit() function to de-initialize FLASH. When called, the FLASH must be in reset.
Configuration functions ¶
Use the HAL_FLASH_SetProgrammingMode() function to set the programming mode for both banks.
Use the HAL_FLASH_GetProgrammingMode() function to get the programming mode retrieved from bank 1, use HAL_FLASH_SetBankProgrammingMode() and HAL_FLASH_GetBankProgrammingMode() to use different programming modes per bank.
Use the HAL_FLASH_SetBankProgrammingMode() function to set the programming mode for the selected bank.
Use the HAL_FLASH_GetBankProgrammingMode() function to get the programming mode for the selected bank.
Use the HAL_FLASH_SetCRCBurstSize() function to set the CRC burst size.
Use the HAL_FLASH_GetCRCBurstSize() function to get the CRC burst size.
Process operation functions : ¶
Polling mode operations : ¶
Use the HAL_FLASH_ProgramByAddr() function to program by address any area of FLASH USER memory in polling mode.
Use the HAL_FLASH_ProgramByAddrAdapt() function to program by address using adaptive width any area of FLASH USER memory in polling mode.
Use the HAL_FLASH_OTP_ProgramByAddr() function to program by address any area of OTP area in polling mode.
Use the HAL_FLASH_OTP_ProgramByAddrAdapt() function to program by address using adaptive width any area of OTP area in polling mode.
Use the HAL_FLASH_EraseByAddr() function to erase by address any area of FLASH USER memory in polling mode.
Use the HAL_FLASH_ErasePage() function to erase a specific set of pages of FLASH USER memory in polling mode.
Use the HAL_FLASH_EraseBank() function to erase a specific bank of FLASH memory in polling mode.
Use the HAL_FLASH_MassErase() function to erase all the FLASH memory in polling mode.
Use the HAL_FLASH_CalculateCRCByAddr() function to calculate the CRC by address in polling mode.
Use the HAL_FLASH_CalculateCRCByPage() function to calculate the CRC by page in polling mode.
Use the HAL_FLASH_CalculateCRCByBank() function to calculate the CRC by bank in polling mode.
Interrupt mode operations ¶
Configure the FLASH interrupt priority using HAL_CORTEX_NVIC_SetPriority() function
Enable the FLASH IRQ handler using HAL_CORTEX_NVIC_EnableIRQ() function
Use the HAL_FLASH_ProgramByAddr_IT() function to program by address any area of FLASH USER memory in interrupt mode.
Use the HAL_FLASH_ProgramByAddrAdapt_IT() function to program by address using adaptive width any area of FLASH USER memory in interrupt mode.
Use the HAL_FLASH_OTP_ProgramByAddr_IT() function to program by address any area of OTP area in interrupt mode.
Use the HAL_FLASH_OTP_ProgramByAddrAdapt_IT() function to program by address using adaptive width any area of OTP area in interrupt mode.
Use the HAL_FLASH_EraseByAddr_IT() function to erase by address the FLASH USER memory in interrupt mode.
Use the HAL_FLASH_ErasePage_IT() function to erase by page the FLASH USER memory in interrupt mode.
Use the HAL_FLASH_EraseBank_IT() function to erase a bank of FLASH memory in interrupt mode.
Use the HAL_FLASH_MassErase_IT() function to erase all the FLASH memory in interrupt mode.
Use the HAL_FLASH_CalculateCRCByAddr_IT() function to calculate the CRC by address in interrupt mode.
Use the HAL_FLASH_CalculateCRCByPage_IT() function to calculate the CRC by page in interrupt mode.
Use the HAL_FLASH_CalculateCRCByBank_IT() function to calculate the CRC by bank in interrupt mode.
Use the HAL_FLASH_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any FLASH interrupt.
Use the HAL_FLASH_ProgramByAddr_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any FLASH program interrupt.
Use the HAL_FLASH_EraseByAddr_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any FLASH erase by address interrupt.
Use the HAL_FLASH_ErasePage_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any FLASH erase page interrupt.
Use the HAL_FLASH_EraseBank_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any FLASH erase bank interrupt.
Use the HAL_FLASH_MassErase_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any FLASH mass erase interrupt.
Use the HAL_FLASH_CalculateCRC_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any FLASH CRC calculation interrupt.
Use the HAL_FLASH_ECC_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any ECC single error interrupt.
Use the HAL_FLASH_BusFault_IRQHandler() function called under BusFault_Handler interrupt subroutine to handle any Bus Fault FLASH interrupt.
Callback registration ¶
When the compilation flag USE_HAL_FLASH_REGISTER_CALLBACKS is set to 1, the following functions allow to register the different FLASH callbacks:
Use the HAL_FLASH_RegisterProgramCpltCallback() function to register the FLASH program complete callback.
Use the HAL_FLASH_RegisterEraseByAddrCpltCallback() function to register the FLASH erase by address complete callback.
Use the HAL_FLASH_RegisterErasePageCpltCallback() function to register the FLASH erase by page complete callback.
Use the HAL_FLASH_RegisterEraseBankCpltCallback() function to register the FLASH bank erase complete callback.
Use the HAL_FLASH_RegisterMassEraseCpltCallback() function to register the FLASH mass erase complete callback.
Use the HAL_FLASH_RegisterCalculateCRCCpltCallback() function to register the FLASH CRC calculation complete callback.
Use the HAL_FLASH_RegisterErrorCallback() function to register the FLASH error callback.
Use the HAL_FLASH_RegisterECCErrorCallback() function to register the FLASH ECC error callback.
When the compilation flag USE_HAL_FLASH_REGISTER_CALLBACKS is undefined or set to 0, the callback registration feature is not available and all callbacks are set to the following weak functions:
The HAL_FLASH_ProgramCpltCallback() function for the program complete callback.
The HAL_FLASH_EraseByAddrCpltCallback() function for the erase by address complete callback.
The HAL_FLASH_ErasePageCpltCallback() function for the erase by page complete callback.
The HAL_FLASH_EraseBankCpltCallback() function for the erase bank complete callback.
The HAL_FLASH_MassEraseCpltCallback() function for the mass erase complete callback.
The HAL_FLASH_CalculateCRCCpltCallback() function for the CRC calculation complete callback.
The HAL_FLASH_ErrorCallback() function for the error callback.
The HAL_FLASH_ECC_ErrorCallback() function for the ECC error callback.
These weak functions can be redefined within user application if the callbacks are needed.
Status functions ¶
Use the HAL_FLASH_GetCurrentOperation() function to get the current flash operation.
Use the HAL_FLASH_GetCurrentProgrammedAddr() function to get the currently being programmed flash address.
Use the HAL_FLASH_GetCurrentErasedAddr() function to get the currently being erased flash address.
Use the HAL_FLASH_GetCurrentErasedPage() function to get the currently being erased flash page.
Use the HAL_FLASH_GetInfo() function to get the FLASH information.
Use the HAL_FLASH_GetSizeByte() function to get the FLASH total size.
Use the HAL_FLASH_GetBankNbr() function to get the FLASH number of banks.
Use the HAL_FLASH_GetBankSizeByte() function to get the FLASH bank size.
Use the HAL_FLASH_GetUserFlashSizeByte() function to get the FLASH USER area size within a bank.
Use the HAL_FLASH_GetUserFlashPageNbr() function to get the number of pages in FLASH USER area.
Use the HAL_FLASH_GetUserFlashPageSizeByte() function to get the size of a given page in FLASH USER area.
Use the HAL_FLASH_GetUserFlashAddrOffset() function to get the address offset of a specific page in FLASH USER area.
Use the HAL_FLASH_ECC_GetInfo() function to get the FLASH ECC information.
Use the HAL_FLASH_GetInstance() function to get the HAL FLASH instance.
Use the HAL_FLASH_GetLLInstance() function to get the hardware FLASH instance.
Use the HAL_FLASH_GetState() function to get the flash global state.
Use the HAL_FLASH_GetBankState() function to get the flash bank state.
Use the HAL_FLASH_GetLastErrorCodes() function to get the last error codes.
Use the HAL_FLASH_SetUserData() function to set user data within the FLASH handle.
Use the HAL_FLASH_GetUserData() function to get the user data from the FLASH handle.