cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-L432KC store data in Flash using mbed

Duy Tran
Associate II
Posted on December 15, 2017 at 05:11

Hello,

I am using my Nucleo-L432KC for a project and my client wants to use mbed online compiler. I am trying to save some variables into the flash memory (because there is no EEPROM) and unfortunately there is no library for this on mbed. Can the FLASH_WriteProtection and FLASH_EraseProgram example of CubeMX help me with this? I saw some functions about writing into the Flash memory but I haven't seen those to read from Flash. Where should I start to write this library for my own?

Can any one please show me some ways or give me some advices? Thank you very much.

\Duy

#nucleo-l432kc-flash #flash #nucleo-l432kc #stm32l4
9 REPLIES 9
Geoffrey1
Associate III
Posted on December 15, 2017 at 12:48

Here's the C code I use (not in mbed) on the stm32l432.  You'll need to include the appropriate header file and change the interface for C++.  I'd view this as a rough starting point and not production code.

#define FLASH_KEY1

               

((uint32_t)0x45670123U)

             

/*!< Flash key1 */

#define FLASH_KEY2

               

((uint32_t)0xCDEF89ABU)

             

/*!< Flash key2: used with FLASH_KEY1 */

static inline uint32_t FLASH_Errors(void) {

 

return FLASH->SR & (FLASH_SR_EOP | FLASH_SR_OPERR | FLASH_SR_PROGERR |

 

   

FLASH_SR_WRPERR | FLASH_SR_PGAERR | FLASH_SR_SIZERR

 

|

 

   

FLASH_SR_PGSERR | FLASH_SR_MISERR | FLASH_SR_FASTERR |

 

     

FLASH_SR_RDERR

 

| FLASH_SR_OPTVERR);

}

static inline void FLASH_ClearAllErrors(void) {

 

SET_BIT(FLASH->SR,

 

   

FLASH_SR_EOP | FLASH_SR_OPERR | FLASH_SR_PROGERR |

 

   

FLASH_SR_WRPERR | FLASH_SR_PGAERR | FLASH_SR_SIZERR

 

|

 

   

FLASH_SR_PGSERR | FLASH_SR_MISERR | FLASH_SR_FASTERR |

 

   

FLASH_SR_RDERR

 

| FLASH_SR_OPTVERR);

 

SET_BIT(FLASH->ECCR, FLASH_ECCR_ECCC | FLASH_ECCR_ECCD);

}

uint32_t flash_err = 0;

void FLASH_PageErase(uint32_t Page) {

 

// Check and clear all error programming flags

 

// due to a previous programming. If not, PGSERR is set.

 

FLASH_ClearAllErrors();

 

// set up write

 

Page &= 255;

 

// clear any invalid bits

 

MODIFY_REG(FLASH->CR, FLASH_CR_PNB, (Page << POSITION_VAL(FLASH_CR_PNB)));

 

SET_BIT(FLASH->CR, FLASH_CR_PER);

 

SET_BIT(FLASH->CR, FLASH_CR_STRT);

 

while (FLASH->SR & FLASH_SR_BSY);

 

CLEAR_BIT(FLASH->CR, FLASH_CR_PER);

 

flash_err =

 

FLASH_Errors();

}

void FLASH_Unlock(void){

 

// Authorize the FLASH Registers access

 

WRITE_REG(FLASH->KEYR, FLASH_KEY1);

 

WRITE_REG(FLASH->KEYR, FLASH_KEY2);

}

void FLASH_Lock(void){

 

// Set the LOCK Bit to lock the FLASH Registers access

 

SET_BIT(FLASH->CR, FLASH_CR_LOCK);

}

// See page 82 of reference manual

void FLASH_Program_DoubleWord(uint32_t Address, uint32_t Data0,

 

     

uint32_t Data1)

{

 

// only aligned addresses

 

if (Address & 0x7)

   

return;

 

FLASH_ClearAllErrors();

 

// Set PG bit

 

SET_BIT(FLASH->CR, FLASH_CR_PG);

 

 

// Program the double word

 

*(__IO uint32_t*)Address = Data0;

 

*(__IO uint32_t*)(Address+4) = Data1;

 

while (FLASH->SR & FLASH_SR_BSY);

 

// Check that EOP flag is set in the FLASH_SR register

 

// (meaning that the programming operation has succeed), and clear it by software.

 

if (FLASH->SR & FLASH_SR_EOP)

   

CLEAR_BIT(FLASH->SR, FLASH_SR_EOP);

 

// Reset PG bit

 

CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

 

flash_err =

 

FLASH_Errors()
RomainR.
ST Employee
Posted on December 15, 2017 at 14:19

Hi Duy Tran,

Take a look at application note AN4894

http://www.st.com/content/ccc/resource/technical/document/application_note/group0/b2/94/a6/62/18/c0/4f/e6/DM00311483/files/DM00311483.pdf/jcr:content/translations/en.DM00311483.pdf

​​, it explains EEPROM emulation techniques and software for STM32L4 and it is a part of X-CUBE-EEPROM.

http://www.st.com/en/embedded-software/x-cube-eeprom.html

With MBed C++ code, you should be able to use X-CUBE-EEPROM library. Then add to your Mbed project:

eeprom_emul.c which contains EE_ReadVariable32bits() and EE_WriteVariable32bits() firmware functions.

I have already mixed, several times, STM32 HAL code with C ++ MBed compiler and all work well.

STM32 support for MBed is already written with the native HAL library.

Best regards

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on December 22, 2017 at 09:12

Thank you so much

reicher.romain

,

Sorry for my late reply, I am trying to mix all the eeprom core and eeprom porting from

X-CUBE-EEPROM and still making making some changes

eeprom_emul.c to make it suitable with my project.

Once again, thank you so much.

Best regards,

Duy

Posted on December 22, 2017 at 09:14

Thank you so much

Brown.Geoffrey

:)

the code is easy to understand

:)

Posted on December 24, 2017 at 09:00

Hello Duy Tran,

Happy that this expansion API fits your need.

Will you agree to share your project? Or write a little tutorial on this forum ?

Thank you in advance.

Best regards.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on December 26, 2017 at 01:54

reicher.romain

‌ sure I will share and make the tutorial for this project on this forum. I will introduce it briefly below:

The project use an Nucleo-L432KC and an ESP8266 to push data to the internet using MQTT. Data is gather from a sensor that readsdissolved oxygen and another reads the pH in the water. User can control the device through the internet or let it run automatically. The data that is uploaded also has some synchronizations like command ID, time data (using the RTC from the STM32)...

User config data is stored in flash so there is no need to worry about configuration when power lost. But unfortunately, the flash hangs in the format and init function and I am trying to fix this. When I finish, I promise there will be a tutorial on this forum

:)

Hope the flash will work, there are some differences between Keil C and mbed online compiler but there is no need to worry about those compilers.

Best regards,

Duy

Posted on January 06, 2018 at 13:41

romain reicher wrote:

Hi Duy Tran,

Take a look at application note AN4894

/external-link.jspa?url=http%3A%2F%2Fwww.st.com%2Fcontent%2Fccc%2Fresource%2Ftechnical%2Fdocument%2Fapplication_note%2Fgroup0%2Fb2%2F94%2Fa6%2F62%2F18%2Fc0%2F4f%2Fe6%2FDM00311483%2Ffiles%2FDM003114pdf%2Fjcr%3Acontent%2Ftranslations%2Fen.DM003114pdf

, it explains EEPROM emulation techniques and software for STM32L4 and it is a part of X-CUBE-EEPROM.

/external-link.jspa?url=http%3A%2F%2Fwww.st.com%2Fen%2Fembedded-software%2Fx-cube-eeprom.html

How do I install this expansion please? I downloaded the package from the cited page, tried to install it in the latest CubeMX version (4.0) and it tells me that 'this package version is not managed on this version of STM32CubeMX'. I cannot download neither another version of the EEPROM expansion nor an older version of CubeMX.

Posted on January 06, 2018 at 17:27

Hi Sebastian K.

This is a software expansion. There is nothing to install with the Cube.

You just have to extract the archie zip and copy it to your computer.

You can either use the examples that are included. Or use the drivers for your own use and include them in your project.

Let me know.

Best regards

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on January 06, 2018 at 17:41

romain reicher wrote:

Hi Sebastian K.

This is a software expansion. There is nothing to install with the Cube.

You just have to extract the archie zip and copy it to your computer.

You can either use the examples that are included. Or use the drivers for your own use and include them in your project.

Ok, thanks for the quick clarification - that's what I ended up doing now. But I still find it odd since the page you quoted and the directory structure inside the archive make it look like it's a CubeMX expansion, and yet I have to hand-pick the files I need to make it work, hoping they are compatible with the firmware version I am using. The EEPROM application example for the F7 series was much easier to use.