cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U5 unable to read and write data to the second bank of the internal flash

G_Anastasopoulos
Associate III

Dear All,

I am working with a STM32U599NJH6Q.
If I understand things correctly it has 4MB of flash divided on 2 banks.
Based on the sample in STM32Cube_FW_U5_V1.6.0\Projects\STM32U5x9J-DK\Examples\FLASH\FLASH_EraseProgram packet, in order to write to the flash you need to disable the icache, unlock the flash, determine the page you need to write to, the number of pages where to write and finally the bank where these pages are located. Then you erase the pages that you have determined and write the data in the address in the flash where the page starts. Finally you lock the flash and enable back the icache.

In the example there is a limited number of pages where you can actually write to. Furthermore, the bank is also hard-coded. For my use case, I need to be able to write to the last pages of the flash.

If I understand things correctly, since there are 2 banks and since the size of all pages is 8kB there 512 pages in total.
The starting address of each page is 0x08000000 + (n * 0x2000), where n is the number of the page.

So, if I want to write a few bytes on page 254 of the second bank, I would need to populate the FLASH_EraseInitTypeDef like this:

 

FLASH_EraseInitTypeDef erase_init =
        {
          .TypeErase = FLASH_TYPEERASE_PAGES,
          .Banks     = FLASH_BANK_2,
          .Page      = 254,
          .NbPages   = 1};

 


Then after erasing the page, if I want to write to it, I will need to calculate the address of the page based on the formula from before 0x08000000 + (510 * 0x2000) = 0x83fc000.

But when I call the HAL_FLASH_Program, I am getting a hardfault at this point, because I am trying to write to write to the address calculated above:

G_Anastasopoulos_0-1721896618843.png

If I try the same sequence for page 254, bank 1 then the code works just fine.
Is there any limitation on accessing the second bank? Is there any step that I am missing?

Thanks a lot.

 

2 REPLIES 2
TDK
Guru

After erasing, can you read from 0x83fc000 and does it show all 0xFFs? Can you write to that location using STM32CubeProgrammer? No MPU settings or option byte settings preventing access?

What is the nature of the hard fault? There's a hard fault analyzer window (Window -> Show View -> Fault Analyzer) which can shed more light.

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

From the Fault Analyzer I am getting this:

G_Anastasopoulos_5-1721913925554.png

Does this mean that I am not supposed to write directly to that address?

While testing with the Programmer I am observing something strange:

From the programmer I see this info for my device:

G_Anastasopoulos_0-1721912582838.png

From this I assume that my device has 4MB of Flash.
If I perform a full erase all of the Flash should be FFFFs.

If I attempt to read the first 2MB only, like this:

G_Anastasopoulos_1-1721912783540.png

I see indeed FFFFs everywhere.

When I try to read all 4MB, like this:

G_Anastasopoulos_2-1721912840156.png

I see AAAAs, which I am not sure what this means. I am not getting any warning that I am attempting to read past the size of the flash or anything like that.

The option bytes as read from the Programmer look like this:

G_Anastasopoulos_3-1721913000972.png

In the user configuration OB I see this option:

G_Anastasopoulos_4-1721913124277.png

Not sure what does it mean for my case though. I tried to unset it and run the code with the same results