cancel
Showing results for 
Search instead for 
Did you mean: 

How to used internal EEPROM(read and write operation) of STM32L011F3?

kalpeshpatil102
Associate III
Posted on January 16, 2017 at 17:09

Hi, i am using STM32L011F3 . I am using cube mx and keil mdk 5 ide for programming. In my project I want to write and read  data on internal  EEPROM of stm32L0 11F3.  

I didn't get any library or example tutorial for this . I am new in stm32L011F3 .

please help me.  

thank you 

6 REPLIES 6
Imen.D
ST Employee

Posted on January 16, 2017 at 18:18

Hello

Patil.Kalpesh,

I recommend you to have a look to the "Flash program memory and data EEPROM (FLASH)" section in your related STM32L0 product reference manual

http://www.st.com/content/ccc/resource/technical/document/reference_manual/21/bd/0f/bd/1c/88/40/f0/DM00108282.pdf/files/DM00108282.pdf/jcr:content/translations/en.DM00108282.pdf

and get idea about read-write from EEPROM.

You can have a look to this example under

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubel0.html

which describes how to configure and use the Flash to enable and disable the write protection of the internal FLASH memory:

STM32Cube_FW_L0_V1.7.0\Projects\STM32L031K6-Nucleo\Examples\FLASH\FLASH_WriteProtection

You can inspire from this example to achieve your goal and modify it or add your own code.

Also, you can found a full set of running examples with a EEPROM under ST web page within other STM32CubeXX firmware package (like STM32CubeL4), which can help you as an implementation example to build your application on L0. 

Regards

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on January 16, 2017 at 18:58

Isn't the EEPROM just mapped into regular address space, can't you read it using pointers, or memcpy() ?

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

Posted on January 17, 2017 at 10:05

thank you @ DAHMEN.IMEN, Turvey.Clive.002 for reply.

I go through STM32L0 product reference manual

http://www.st.com/content/ccc/resource/technical/document/reference_manual/21/bd/0f/bd/1c/88/40/f0/DM00108282.pdf/files/DM00108282.pdf/jcr:content/translations/en.DM00108282.pdf.

on page no. 822 found write function like this '

Write to data EEPROM code example

/*/*/*
 
*(uint8_t *)(DATA_E2_ADDR+i) = DATA_BYTE;
 
*(uint16_t *)(DATA_E2_ADDR+j) = DATA_16B_WORD;
 
*(uint32_t *)(DATA_E2_ADDR) = DATA_32B_WORD;
 
DATA_E2_ADDR is an aligned address in the data EEPROM area. i can be any integer. j must be an even integer. */*/
 
in this function i wrote like this 
 
/*/*/* write function of eeprom write/*/*/*/*/*/*/**
 
unsigned long int DATA_E2_ADDR=8080100;// EEPROM address of stm32L0
 
 
 
int i=0,j=0, DATA_BYTE=3;//
 
*(uint8_t *)(DATA_E2_ADDR+i) = DATA_BYTE;
 
*/*/*/*/*

but didn't get it working or not . and how to perform read operation ?

let me know where i am wrong ?

please some one give example on internal eeprom read and write function that will helpfully for me. 

sorry for poor English 

please help me .

thank you

kalpeshpatil102
Associate III
Posted on January 18, 2017 at 15:58

Hello , thank you to all. please any one can give  simple example  to write and read data on internal EEPROM of stm32L0.  please help me .

thank you 

Posted on January 18, 2017 at 16:57

One has to be careful with the Cortex-M0 as it doesn't not support unaligned memory operations, it will Hard Fault. ie reading a 32-bit value from an odd address

I'm not using the L0, my expectation is the FLASH writing examples will be a good place to start.

For reading I'd suggest using structures, either with pointers to them, or copying the content to a RAM holding buffer using memcpy()

You are familiar with typedef, struct and memcpy(), right?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Alan Doncaster
Associate
Posted on January 19, 2017 at 13:27

Hi Patil

This works for the STM32l051 on Eclipse, just migrating to Keil so haven't tried it on that yet:

#define DATA_EEPROM_BASE_ADDR ((uint32_t)0x08080000) /* Data EEPROM base address */

#define DATA_EEPROM_END_ADDR ((uint32_t)0x080807FF) /* Data EEPROM end address */

Simple write sequence:

void Write_Registers_to_Eeprom(void)

{

uint32_t i;

uint32_t* p_tFlashRegs;

   i = 0;

   p_tFlashRegs = (uint32_t*)&data_to_write;   /* Point to data to write */

   UnlockEeprom(); /* Unlock the EEPROM and enable flash interrupts */

   FLASH->PECR = FLASH->PECR & ~(FLASH_PECR_ERASE | FLASH_PECR_DATA); /* Reset the ERASE and DATA                                                                               bits in the FLASH_PECR register to disable any residual erase */

   /* Put the next line in a loop if sequential bits to be written with i as loop counter */

   EepromProgram(DATA_EEPROM_BASE_ADDR + (4 * i), *(p_tFlashRegs + i)); /* Increase eeprom address by 4 for each                                                                word to write.  */

   LockEeprom(); /* Lock the EEPROM */

}

Called routines:

/* Unlock the EEPROM: */

void UnlockEeprom(void)

{

   while ((FLASH->SR & FLASH_SR_BSY) != 0) /* Wait for FLASH to be free */

   {

/*   insert timeout test */

   }

   if ((FLASH->PECR & FLASH_PECR_PELOCK) != 0) /* If PELOCK is locked */

   {

      FLASH->PEKEYR = FLASH_PEKEY1; /* Unlock PELOCK */

      FLASH->PEKEYR = FLASH_PEKEY2;

   }

   FLASH->PECR = FLASH->PECR | (FLASH_PECR_ERRIE | FLASH_PECR_EOPIE); /* enable flash interrupts */

}

/**

* Brief This function programs a word of data EEPROM.

* The ERASE bit and DATA bit are cleared in PECR at the beginning

* words are automatically erased if required before programming

* Param addr is the 32-bit EEPROM address to program, data is the 32 bit word to program

* Retval None

*/

void EepromProgram(uint32_t addr, uint32_t ee_data)

{

/* NOTE: The EEPROM must be unlocked and the flash interrupts must have been enabled prior to calling this function.*/

   *(uint32_t *)(addr) = ee_data; /* write data to EEPROM */

   __WFI();

   if (*(uint32_t *)(addr) != ee_data)

   {

      error |= ERROR_PROG_32B_WORD;

   }

}

/* Lock the EEPROM: */

void LockEeprom(void)

{

   while ((FLASH->SR & FLASH_SR_BSY) != 0) /* Wait for FLASH to be free */

   {

/*   insert timeout test */

   }

   FLASH->PECR = FLASH->PECR & ~(FLASH_PECR_ERRIE | FLASH_PECR_EOPIE); /* disable flash interrupts */

   FLASH->PECR = FLASH->PECR | FLASH_PECR_PELOCK; /* Lock memory with PELOCK */

}

To read you can use a simple structure (eg called Eeprom_registers) and have something like:

#define CONFIG ((Eeprom_registers *) DATA_EEPROM_BASE_ADDR)

then get data using something like

Eeprom_data1 = CONFIG-> xxxx ; /* where xxxx is the entry in the Eeprom registers */

either that or something like

Eeprom_data1 = *(uint32_t *)(addr); /* where addr is the EEPROM address you want to read */

Hope this helps.