STM32 HAL FLASH - Page 1
Forum section: electronics / general

author: conducteur
author_rank: Regular Contributor
timestamp: September 17, 2022, 09:03:16 pm
content: Hi all,I'm working on a Nucleo-32L432KC. I want to save one variable (two bytes) in the flash memory and read it back to that variable at power-on. (There's no EEPROM internally in the uc, or externally on my project).There's very basic information about STM32 HAL FLASH library in the HAL user manualhttps://www.st.com/resource/en/user_manual/um1725-description-of-stm32f4-hal-and-lowlayer-drivers-stmicroelectronics.pdfI found thishttps://www.st.com/resource/en/application_note/an4894-eeprom-emulation-techniques-and-software-for-stm32-microcontrollers-stmicroelectronics.pdfIt's all a bit daunting to me, so I'm looking for clearer instructions to get started. Any tips or advice where to start actually?

author: nctnico
author_rank: Super Contributor
timestamp: September 17, 2022, 09:08:26 pm
content: Use the flash controller directly. If it is the same as on the STM32F103, then it is easier to just read/write to the registers compared to using the HAL.

author: conducteur
author_rank: Regular Contributor
timestamp: September 17, 2022, 09:14:49 pm
content: And is there any documentation for that available?

author: eutectique
author_rank: Frequent Contributor
timestamp: September 17, 2022, 09:26:33 pm
content: Make sure you write 8 bytes at a time, seeSection 3.3.7of the reference manual.

author: eutectique
author_rank: Frequent Contributor
timestamp: September 17, 2022, 09:30:42 pm
content: Quote from: conducteur on September 17, 2022, 09:14:49 pmVendor'sweb siteis quite handy.

author: eutectique
author_rank: Frequent Contributor
timestamp: September 18, 2022, 02:57:16 pm
content: You can find some examples of accessing flash via STM32 HAL here:https://cpp.hotexamples.com/examples/-/-/HAL_FLASH_Lock/cpp-hal_flash_lock-function-examples.htmlBasically, you would need three functions:Code:HAL_FLASH_Unlock()HAL_FLASH_Program()HAL_FLASH_Lock()

author: conducteur
author_rank: Regular Contributor
timestamp: September 18, 2022, 05:09:33 pm
content: Quote from: eutectique on September 17, 2022, 09:30:42 pmPersonal opinion, but silicon makers don't really excel in clear easy-to-understand documentation, especially if you want to know how to start using the part (for the first time). They are usually more of a listing of features and capabilities, rather than a manual explaining how to use/connect it and how to use it in specific situations (with appropriate examples).

author: eutectique
author_rank: Frequent Contributor
timestamp: September 18, 2022, 06:58:34 pm
content: Quote from: conducteur on September 18, 2022, 05:09:33 pmIt depends on the vendor. Some have rather clear documentation, like Nordic Semi. Some have ambiguous one written in poor English and lacking crucial details.You would look for at least areference manualfor your part. STM32L432 is 1600 pages long, compared to 150-odd pages of data sheet. Then, there are application notes. Again, of varying quality.STM suggests the developer to use their STM32Cube to configure your project and generate code. Corresponding documentation can be foundhere.There isSTM repositoryon the github.Yes, there is always learning curve, and the amount of time you spend learning depends greatly on your prior experience. "First ever MCU" -- you could easily be lost in details and frustrated by lack of examples. "Yet another MCU" -- you know exactly which section to navigate to for the clock tree or DMA channel mapping.

author: nctnico
author_rank: Super Contributor
timestamp: September 18, 2022, 07:36:45 pm
content: From what I've seen ST definitely isn't in the bracket of 'clear documentation'. HAL documentation seems to be Doxygen crap so not very useful. And it looks like the API has changed a few times so many examples are outdated / irrelevant. My method for writing code for STM32 parts is to look at the HAL code for the details and then write my own 'driver' in a simple and clear way.If you want excellent documentation, switch to the LPC ARM microcontrollers from NXP. ST's documentation is a cold shower in comparison; a lot of details are missing.

author: DavidAlfa
author_rank: Super Contributor
timestamp: September 19, 2022, 08:07:43 am
content: Remember you also have the RTC backup registers, 128bytes that are kept by Vbat.In low pin count stm32s Vbat and vdd are internally connected, so you would have to add a small battery or supercap with a series charging resistor, I think the user must detect external VDD somehow and enter Vbat mode by SW, that way only the RTC circuitry is kept.Not sure, never used it, but it's a userful feature!

author: conducteur
author_rank: Regular Contributor
timestamp: September 19, 2022, 08:43:45 pm
content: Thanks. Don't think it's an option for my current project, but something to keep in mind. (Vbat is indeed not an external pin on my device and the entire design is not really made to be very low power)....