Showing results for 
Search instead for 
Did you mean: 

Determine flash page size at run-time

Associate II

STM32G4 devices are split into 2 flash categories per the datasheet: Category 2 devices (STM32G43x/G44x) which have only single-bank capable flash with a capacity up to 128kB and Category 3 devices which are dual-bank capable and have up to 512kB of flash.

I'm trying to figure out at run-time what the flash page (sector) size is, but find it difficult to do so as there is no clear distinguishing factor between category 2 and 3 devices that can be used/read at run-time.

Based on my understanding and what I can find online, both category 2 AND 3 devices can have flash sizes of 128kB, so they overlap. Therefore, I can't use the flash size indicator...

There also doesn't seem to be a clear way to indicate whether a device is dual-bank capable. The missing bits in the flash registers don't really help.

Further, category 2 devices have 2kb page size, while category 3 devices are 2kb in DUAL-bank mode, but 4kB in single-bank mode, so there's just not a lot of commonality.

Does anyone know how I can elegantly figure out at run-time (NOT compile time, that's what I'm doing now...) to accurately determine the flash page size?

Jack Peacock_2
Senior III

Check the reference manual at the end, Electronic Signature. There is an entry for total flash size. You might try combining that with information from the DBGMCU->IDCODE register for device ID and rev ID. Not sure on G parts if the device ID distinguishes between cat 2 and cat 3, but if it does that will tell you if DBANK in the OPT bytes is valid for dual bank detection.

Jack Peacock

Associate II

Ha, you hit the jackpot! The DBGMCU_IDCODE register is exactly what I was looking for. In fact, for the G4 series, there are only 2 values (IDs) mentioned. One for each category.

I totally forgot that about the IDCODE reg, so thank you for the reminder!

There should also be a word in the System memory indicating the size of the flash array tested

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

Could you elaborate on this a bit more?

With "size of the flash array tested" do you mean this is a "test report"? How can this help in finding the page size? I thought I looked through the datasheet pretty carefully, but I couldn't find anything related to this.

On a similar topic, there was a question about finding the size of the embedded SRAM, which I was wondering about too. If I recall correctly, you actually mentioned something about there being some information in the system memory about that too... Is this documented anywhere? Based on the reference manual, the system memory contains only the ST bootloader...