Skip to main content
Duy Tran
Associate III
December 15, 2017
Question

Nucleo-L432KC store data in Flash using mbed

  • December 15, 2017
  • 9 replies
  • 2721 views
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
This topic has been closed for replies.

9 replies

Geoffrey1
Associate III
December 15, 2017
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()
Duy Tran
Duy TranAuthor
Associate III
December 22, 2017
Posted on December 22, 2017 at 09:14

Thank you so much

Brown.Geoffrey

:)

the code is easy to understand

:)

RomainR.
ST Employee
December 15, 2017
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.
Duy Tran
Duy TranAuthor
Associate III
December 22, 2017
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

RomainR.
ST Employee
December 24, 2017
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.