cancel
Showing results for 
Search instead for 
Did you mean: 

how to use flash memory "the bare metal way".

HellasT
Associate III

Hello, everybody! I'm relatively new to STM32 microcontrollers and have a moderate knowledge of C programming and a fair understanding of electronics.

Over the past few months, I've been learning to properly set up STM32F4 series MCUs, specifically the F401RET6U (Nucleo board) and the F411CEU6 (Black Pill board), using bare-metal approach. I've spent considerable time watching tutorial videos and reading online guides to help me get started.

I'm now at a point where I can set up the clock and read from and write to GPIOs.

From what I understand, setting up peripherals involves configuring the correct values in the appropriate registers in the right order.

I would like to learn how to use embedded flash memory to store information that needs to be retained after a power loss. So far, I've struggled to find a guide or tutorial that I can fully understand. Could someone please assist me with this or point me in the right direction?

Thank you very much, and have a blessed week, everyone!

Wise is not he who knows many. Wise is he who knows useful. Aeschylus.
15 REPLIES 15

Bare metal means you read the Reference Manual (RM) and protocol docs related to the bus in question.

You can check the HAL / CubeF4 examples for writing the Internal FLASH, once you have that working you can review the library source to confirm the mechanics described in the RM

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

 how to use embedded flash memory to store information that needs to be retained after a power loss. 

Can you describe your requirements with more details? Do you want to save small amounts of data not frequently, for example calibration or configuration data - or make something like logger or recorder?  For the former you will find many examples on github and youtube.  But for the latter use the internal flash of STM32 is not optimal. 

 


@HellasT wrote:

From what I understand, setting up peripherals involves configuring the correct values in the appropriate registers in the right order.


Yes, that's a very good summary!

 


@HellasT wrote:

I would like to learn how to use embedded flash memory to store information that needs to be retained after a power loss. So far, I've struggled to find a guide or tutorial that I can fully understand.!


As @Tesla DeLorean said, the whole point of "The Bare Metal Way" is that you commit to studying the device documentation (Datasheet, Reference Manual, Application Notes, etc)  in detail, understanding it, and then implementing what it tells you.

"The HAL Way" exists so that you don't have to go into all that arcane detail.

The HAL is all open-source - so you can always look into how it does its stuff at the register level.

See this current thread on using Flash for non-volatile storage on STM32F4:

https://community.st.com/t5/stm32-mcus-products/use-stm32f4-flash-for-non-volatile-data-storage-eeprom/m-p/744042

 


@HellasT wrote:

F411CEU6 (Black Pill board)


Note that there is a high likelihood that the STM32 on a Black (or Blue or other coloured) Pill board will not be genuine ...

Sorry for my late response. Yes that i do. I read the reference manuals. I've learned for example how to setup the latency and prefetch. Im not finished with the reference manual yet though.

Wise is not he who knows many. Wise is he who knows useful. Aeschylus.

That's right. To save small amount of data like for example a user defined pin number for an alarm system.

Wise is not he who knows many. Wise is he who knows useful. Aeschylus.

As @Tesla DeLorean said, the whole point of "The Bare Metal Way" is that you commit to studying the device documentation (Datasheet, Reference Manual, Application Notes, etc)  in detail, understanding it, and then implementing what it tells you.

I sure try hard.


"The HAL Way" exists so that you don't have to go into all that arcane detail.

The HAL is all open-source - so you can always look into how it does its stuff at the register level

 HAL is awesome. Its just me thinking that if i try hard to learn "bare metal techniques" it will be easier and more clear for me to use HAL later on.

 

https://community.st.com/t5/stm32-mcus-products/use-stm32f4-flash-for-non-volatile-data-storage-eeprom/m-p/744042

Im going to check it out soon ! 


Note that there is a high likelihood that the STM32 on a Black (or Blue or other coloured) Pill board will not be genuine ...


 Agreed but to play with and learn, i use what i can get my hands on. The original gear which by the way i usually get from reputable dealers like mouser etc, i keep for projects to be developed and implemented 

 

Wise is not he who knows many. Wise is he who knows useful. Aeschylus.
HellasT
Associate III

Thank you all for taking the time to reply. I truly value your input and understand how precious your time is. Although I’m not a professional, I’m putting a lot of effort into learning how to work with STM32s properly. Your guidance and suggestions mean a great deal to me. Thank you again for your support!

Wise is not he who knows many. Wise is he who knows useful. Aeschylus.

For the F4's use some of the earlier sectors for smaller data.

The primary sector needs to manage the initial boot, but subsequent 16KB and 64KB are better than 128KB

Sectors need to be erased before use. The writes need to be aligned, here at least 32-bits, other architectures it's wider.

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

Well said !

I am looking for ways to bind the sector that i will be using  so that the compiler will not use it for any other purpose. 

Im planning to use F401RET6U's sector 3 which is the last 16k sector. I have actually been thinking to declare an array that it's start address would be the start address of sector 3 and initialize it with 0xFF. something like this :

const char data[16*1024] = {0xFF}; But i have no idea how to implement it.

Wise is not he who knows many. Wise is he who knows useful. Aeschylus.