cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect the flash size at compile time ?

Katzenberger.Michael
Associate III

Is there a preprocessor define of the flash size available at compile time ?

I know that the flash size can be read at run-time from the FLASH_SIZE_DATA_REGISTER but I need the value for a constexpr.

I found only the following definitions - but this is not sufficient to calculate the flash size (examples for a STM32F103 device):

FLASH_BASE:      0x08000000UL
FLASH_BANK1_END: 0x0801FFFFUL
FLASH_PAGE_SIZE: 0x400

e.g. the number of pages would be nice 😊

Thanks!

6 REPLIES 6

>>Is there a preprocessor define of the flash size available at compile time ?

No but the compiler is often told which part via the command line. Check project settings.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
prain
Senior III

​number of pages = (end-base)/pagesize

Also if you are using GCC, you can read memory section info in linker script:

https://mcuoneclipse.com/2017/09/06/using-the-gnu-linker-script-to-know-the-flash-and-ram-areas-in-the-application/amp/

Been a long time since I used F1 parts, but as I recall the flash code could determine what hardware and flash capacity was at runtime, via DEVID and FLASH_KB (0x1FFFF7E0)

Could figure page size (1K, 2KB) and page count

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

On the F4 family, and others, FLASH_END is available in the CMSIS board include file, but something similar doesn't appear to exist on the F1 series.

If you feel a post has answered your question, please click "Accept as Solution".

The F1 has smaller and uniform flash sectors/pages, but only has 8-bit as I recall, so 1MB part had 2-banks, with 256 * 2KB pages

STM32Cube_FW_F1_V1.8.0\Drivers\STM32F1xx_HAL_Driver\Inc\stm32f1xx_hal_flash_ex.h

/** @defgroup FLASHEx_Page_Size Page Size

 * @{

 */

#if (defined(STM32F101x6) || defined(STM32F102x6) || defined(STM32F103x6) || defined(STM32F100xB) || defined(STM32F101xB) || defined(STM32F102xB) || defined(STM32F103xB))

#define FLASH_PAGE_SIZE     0x400U

#endif /* STM32F101x6 || STM32F102x6 || STM32F103x6 */

    /* STM32F100xB || STM32F101xB || STM32F102xB || STM32F103xB */

#if (defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F103xE) || defined(STM32F101xG) || defined(STM32F103xG) || defined(STM32F105xC) || defined(STM32F107xC))

#define FLASH_PAGE_SIZE     0x800U

#endif /* STM32F100xB || STM32F101xB || STM32F102xB || STM32F103xB */

    /* STM32F101xG || STM32F103xG */

    /* STM32F105xC || STM32F107xC */

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Katzenberger.Michael
Associate III

Hi Guys,

thanks for your feedback.

It seams that for F1 devices there is no "easy way" to get the flash size of the selected device.

To keep the effort low I'll create a define it in the Eclipse project settings (although I don't like "redundant" settings :( ) ...