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, an EDATA area and an information block.
The main flash memory block organized as two banks of up to 512 Kbytes each containing up to 64 pages of 8 Kbytes.
The EDATA memory block can be configured
to extend the user area, with two banks of 32 Kbytes each containing 16 pages of 2 Kbytes.
or to offer data flash area, with two banks of 24 Kbytes each containing 16 pages of 1.5 Kbytes.
The information block contains:
32 Kbytes for system memory. It contains the bootloader that is used to reprogram the flash memory.
32 Kbytes immutable secure area containing the root security services (RSS and RSS library).
512 bytes 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.
Programming in a previously programmed address is not allowed except if the data to write is full zero.
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.
Use the HAL_FLASH_GetProgrammingMode() function to get the programming mode.
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_EDATA_ProgramByAddr() function to program by address any area of FLASH EDATA memory in polling mode.
Use the HAL_FLASH_EDATA_ProgramByAddrAdapt() function to program by address using adaptive width any area of FLASH EDATA memory 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_EDATA_EraseByAddr() function to erase by address any area of FLASH EDATA 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_EDATA_ErasePage() function to erase a specific set of pages of FLASH EDATA memory in polling mode.
Use the HAL_FLASH_EraseBank() function to erase a specific bank of FLASH memory (USER and EDATA) in polling mode.
Use the HAL_FLASH_MassErase() function to erase all the FLASH memory (USER and EDATA) 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_EDATA_ProgramByAddr_IT() function to program by address any area of FLASH EDATA memory in interrupt mode.
Use the HAL_FLASH_EDATA_ProgramByAddrAdapt_IT() function to program by address using adaptive width any area of FLASH EDATA memory 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_EDATA_EraseByAddr_IT() function to erase by address the FLASH EDATA 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_EDATA_ErasePage_IT() function to erase by page the FLASH EDATA memory in interrupt mode.
Use the HAL_FLASH_EraseBank_IT() function to erase a bank of FLASH memory (USER and EDATA) in interrupt mode.
Use the HAL_FLASH_MassErase_IT() function to erase all the FLASH memory (USER and EDATA) 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_ECC_IRQHandler() function called under FLASH_IRQHandler interrupt subroutine to handle any ECC single error interrupt.
Use the HAL_FLASH_NMI_IRQHandler() function called under NMI_Handler interrupt subroutine to handle any NMI 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_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_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_GetInterruptedByResetOperationInfo() function to get the interrupted flash operation information.
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_EDATA_GetSizeByte() function to get the FLASH EDATA area size within a bank.
Use the HAL_FLASH_GetExtUserFlashSizeByte() function to get the FLASH Extended 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_EDATA_GetPageNbr() function to get the number of pages in FLASH EDATA area.
Use the HAL_FLASH_GetExtUserFlashPageNbr() function to get the number of pages in FLASH extended USER area.
Use the HAL_FLASH_GetUserFlashPageSizeByte() function to get the size of a given page in FLASH USER area.
Use the HAL_FLASH_EDATA_GetPageSizeByte() function to get the size of a given page in FLASH EDATA area.
Use the HAL_FLASH_GetExtUserFlashPageSizeByte() function to get the size of a given page in FLASH extended 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_EDATA_GetAddrOffset() function to get the address offset of a specific page in FLASH EDATA area.
Use the HAL_FLASH_GetExtUserFlashAddrOffset() function to get the address offset of a specific page in FLASH extended USER area. The offset is computed from the beginning of the FLASH extended 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_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.