STM32L07 Simple EEPROM read and write without the example code.

Post here first, or if you can't find a relevant section!
Post Reply
TwoArmsTwoLegs
Posts: 16
Joined: Sat Apr 25, 2020 7:14 pm

STM32L07 Simple EEPROM read and write without the example code.

Post by TwoArmsTwoLegs »

Hi Folks,
Following on from the bug bug detailed here (https://github.com/stm32duino/Arduino_C ... ssues/1016) , the example code for EEPROM read and write wont operate on the internal EEPROM, but instead on an emulated EEPROM.

The advice on the bug note says that the HAL API can be used directly from within a sketch. Thinking that this is probably the only way forward for now until there's an update, I've given it a shot. The code to write values with HAL_FLASHEx_DATAEEPROM_Program appears to run through, however when I go to try to check that the values have been written with a different sketch, the program locks up as though I've hit something protected. I'll be honest, that I don't exactly understand the code for reading from EEPROM, as its a scavenged line, however I've seen it in a couple of places. I believe I've got the correct start point for the EEPROM address, but I could always be wrong.

I've attached the minimal code here. if anything obvious jumps out at anybody about the code, or from their experience about accessing the EEPROM, I'd be very grateful. The "Read" code gets as far as printing the first "0" address location, but does not even get as far as printing out the first underscore (just an underscore char to separate the address from the data).

if anybody understands the line from the read code: "tmp = *(__IO uint32_t*)address;" that would be amazing. There are asterisks in places that are unfamiliar to me.

Kindest thanks.

Write:

Code: Select all

#include <EEPROM.h>

#define FLASH_TYPEPROGRAMDATA_BYTE            ((uint32_t)0x00)  /*!< Program byte (8-bit) at a specified address.*/
//#define FLASH_TYPEPROGRAMDATA_HALFWORD        ((uint32_t)0x01)  /*!< Program a half-word (16-bit) at a specified address.*/
//#define FLASH_TYPEPROGRAMDATA_WORD            ((uint32_t)0x02)  /*!< Program a word (32-bit) at a specified address.*/

//#define FLASH_TYPEERASEDATA_BYTE         (0x00U)  // Erase 1 byte
//#define FLASH_TYPEERASEDATA_HALFWORD     (0x01U)  // Erase 2 bytes
//#define FLASH_TYPEERASEDATA_WORD         (0x02U)  // Erase 4 bytes

void setup() {
  
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  HAL_FLASHEx_DATAEEPROM_Unlock();
  for (int i = 0 ; i < 6252 ; i++) {
    HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, i, 63);
    Serial.println(i);
  }
  HAL_FLASHEx_DATAEEPROM_Lock();
}

void loop() {
}

Read:

Code: Select all

#include <EEPROM.h>
#define DATA_EEPROM_START_ADDR     0x08080000
#define DATA_EEPROM_END_ADDR       0x080817FF

// start reading from the first byte (address 0) of the EEPROM
int address = 0;
uint8_t value;

void setup() {
  // initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  //HAL_FLASHEx_DATAEEPROM_Unlock();
}

void loop() {
  // read a byte from the current address of the EEPROM
  if (address < 6252){
    value = readEEPROMByte(address);
  
    Serial.print(address);
    Serial.print('_');
    Serial.print(value, DEC);
    Serial.println();
    
    address = address + 1;
  }
}

uint8_t readEEPROMByte(uint32_t address) {
    uint8_t tmp = 0;
    address = address + 0x08080000;
    tmp = *(__IO uint32_t*)address;
    
    return tmp;
}
TwoArmsTwoLegs
Posts: 16
Joined: Sat Apr 25, 2020 7:14 pm

Re: STM32L07 Simple EEPROM read and write without the example code.

Post by TwoArmsTwoLegs »

I've only just realised that STM32 Cube programmer allows you to view the data from whatever memory location you like. Very useful. EEPROM address (0x08080000) is all 0s, so my suspicion that the write was going through was also wrong.
No read or write protection bits seem to be set, so, back to reading around.
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: STM32L07 Simple EEPROM read and write without the example code.

Post by stas2z »

TwoArmsTwoLegs wrote: Wed May 06, 2020 4:10 pm
if anybody understands the line from the read code: "tmp = *(__IO uint32_t*)address;" that would be amazing. There are asterisks in places that are unfamiliar to me.
it's simple
__IO is an alias for volatile
(__IO uint32_t*)address means you want to convert value of address to a volatile pointer to 32bit value
* before it means you need to take a value placed at address we convereted to pointer above

yr readeeprom function is wrong a bit, cuz it returns an uint8_t implicitely converted from uint32_t value, it's wrong, but can work as stm32 are little endian
but it's better to return *(volatile uint8_t*)address

btw, i can't find erasing in your code
im not sure, as i never tried chips with internal eeprom, but usually flash memory cant be written without erasing just cuz it how it works
TwoArmsTwoLegs
Posts: 16
Joined: Sat Apr 25, 2020 7:14 pm

Re: STM32L07 Simple EEPROM read and write without the example code.

Post by TwoArmsTwoLegs »

Thanks for taking a look at it man.
After trying out various bits of scavenged code without success, I've got the example code snippets off the ST website.
Surprisingly hard to find from google. The reference manual has a phrase to search for on the ST website which opens the secret door behind the waterfall to unlock the treasure chest. "STM32SnippetsL0".

Projects/NVM
and Drivers/CMSIS/Device/ST/STM32L0xx/Include
There's almost 5000 lines total, so I haven't pulled it apart yet, but it seems to have written to the EEPROM.
Post Reply

Return to “General discussion”