HAL FLASH How to Use

group FLASH_How_To_Use

This file provides firmware functions to manage the following functionalities of the Flash memory area

  • Initialization and De-initialization functions

  • Configuration functions

  • Process operation functions

  • IRQHandler functions

  • Callback functions

  • Status functions

Flash main features

The flash memory interface manages accesses to flash memory, maximizing throughput to CPU, instruction cache and DMAs. It implements the flash memory erase and program operations as well as the read and write protection mechanisms. It also implements the security and privilege access control features. It is optimized in terms of power consumption with dedicated modes when the MCU is in low-power modes.

Up to 4 Mbytes of flash memory supporting read-while-write capability (RWW).

  • Dual bank architecture (bank 1 and bank 2)

  • Main memory: up to 2 Mbytes per bank

  • Information block: 64.5 Kbytes in bank 1 128-bit wide data read with prefetch

  • Standard and burst programming modes

  • Read, program and erase operations in all voltage ranges

  • 10 kcycles endurance on all flash memory. 100 kcycles on up to 256 Kbytes per bank

  • Page erase, bank erase and mass erase (both banks)

  • Bank swapping: the user flash memory address mapping of each bank can be swapped.

  • Product security activated by TrustZone option bit (TZEN)

  • Device life cycle managed by readout protection option byte (RDP)

  • Four write protection areas (two per bank)

  • TrustZone support:

  • Two secure areas (1 per bank)

  • Two secure HDP (hide protection) areas part of the secure areas (one per bank)

  • Configurable protection against unprivileged accesses with flash page granularity

  • Error code correction: 9-bit ECC per 128-bit quad-word allowing two bits error detection and one bit error correction

  • Option-byte loader

  • Advanced low-power modes (low-power read mode, bank power-down mode)

Flash memory organization

The flash memory has the following main features:

  • Capacity up to 4 Mbytes

  • Dual-bank mode:

  • up to 2 Mbytes per bank for main memory

  • 8 Kbytes page size

  • 137 bits wide data read and write (128 effective bits plus 9 ECC bits)

  • Page, bank and mass erase

  • Support read-while-write feature

  • Support bank shutdown feature for power consumption saving The flash memory is organized as follows:

  • Main memory block organized as two banks of up to 2 Mbytes each containing up to 256 pages of 8 Kbytes

  • An information block containing:

  • 32 Kbytes for system memory. This area is immutable and reserved for use by STMicroelectronics. It contains the bootloader that is used to reprogram the flash memory through one of the user communication interfaces such as USB (DFU). The system memory is programmed by STMicroelectronics when the device is manufactured. For further details, refer to the application note STM32 microcontroller system memory boot mode (AN2606).

  • 32 Kbytes immutable secure area containing the root security services (RSS and RSS library) developed by ST.

  • 512 bytes OTP (one-time programmable) bytes for user data (32 quad-words). The OTP data cannot be erased and can be written only once.

  • option bytes for user configuration. Unlike user flash memory and system memory, it is not mapped to any memory address and can be accessed only through the flash register interface

How to use the Flash HAL module driver

Initialization and De-initialization functions :

  • Use HAL_FLASH_Init() to initialize the Flash handle and associate the physical instance.

  • Use HAL_FLASH_DeInit() to de-initialize the Flash instance, When called, the API wait for end of ongoing process and put the Flash state machine in idle state.

Configuration functions :

Process operation functions :

Polling mode operations :

  • Use HAL_FLASH_ProgramByAddr() to program any Flash memory User area in polling mode using flash address according to selected programming mode.

  • Use HAL_FLASH_ProgramByAddrAdapt() to program any Flash memory User area in polling mode using flash address with adaptive programming mode. The adaptive mode allows to optimize Flash programming access versus remaining data size with ignoring Flash programming mode selected by HAL_FLASH_SetProgrammingMode() API.

  • Use HAL_FLASH_OTP_ProgramByAddr() to program any Flash memory OTP area in polling mode using flash OTP address according to selected programming mode.

  • Use HAL_FLASH_OTP_ProgramByAddrAdapt() to program any Flash memory OTP area in polling mode using flash OTP address with adaptive programming mode. The adaptive mode allows to optimize Flash programming access versus remaining data size with ignoring Flash programming mode selected by HAL_FLASH_SetProgrammingMode() API.

  • Use HAL_FLASH_EraseByAddr() to erase by address any Flash memory area in polling mode.

  • Use HAL_FLASH_ErasePage() to erase by page any Flash memory area in polling mode.

  • Use HAL_FLASH_EraseBank() to erase by bank any Flash memory area in polling mode.

  • Use HAL_FLASH_MassErase() to mass erase the entire Flash memory area in polling mode.

  • Use HAL_FLASH_NS_ProgramByAddr() to program any NSecure Flash memory User area from Secure context in polling mode using flash address according to selected programming mode.

  • Use HAL_FLASH_NS_ProgramByAddrAdapt() to program any NSecure Flash memory User area from Secure context in polling mode using flash address with adaptive programming mode. The adaptive mode allows to optimize Flash programming access versus remaining data size with ignoring Flash programming mode selected by HAL_FLASH_SetProgrammingMode() API.

  • Use HAL_FLASH_NS_OTP_ProgramByAddr() to program any Flash memory OTP area from Secure context in polling mode using flash OTP address according to selected programming mode.

  • Use HAL_FLASH_NS_OTP_ProgramByAddrAdapt() to program any Flash memory OTP area from Secure context in polling mode using flash OTP address with adaptive programming mode. The adaptive mode allows to optimize Flash programming access versus remaining data size with ignoring Flash programming mode selected by HAL_FLASH_SetProgrammingMode() API.

  • Use HAL_FLASH_NS_EraseByAddr() to erase by address any NSecure Flash memory area from Secure context in polling mode.

  • Use HAL_FLASH_NS_ErasePage() to erase by page any NSecure Flash memory area from Secure context in polling mode.

  • Use HAL_FLASH_NS_EraseBank() to erase by bank any NSecure Flash memory area from Secure context in polling mode.

  • Use HAL_FLASH_NS_MassErase() to mass erase the entire NSecure Flash memory area from Secure context in polling mode.

Interrupt mode operations :

Status functions :