2017-01-16 08:09 AM
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
2017-01-16 09:18 AM
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
and get idea about read-write from EEPROM.
You can have a look to this example under
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
2017-01-16 09:58 AM
Isn't the EEPROM just mapped into regular address space, can't you read it using pointers, or memcpy() ?
2017-01-17 01:05 AM
Posted on January 17, 2017 at 10:05
thank you @ DAHMEN.IMEN, Turvey.Clive.002 for reply.
I go through STM32L0 product reference manual
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
2017-01-18 06:58 AM
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
2017-01-18 08:57 AM
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?
2017-01-19 04:27 AM
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.