0

I am attempting to write values directly to the eeprom space on a stm8 micro controller. I don't want to write a program that does this that I flash onto the chip. But i want to write directly to it. The command to do this is in unix is such:

./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -w myfile.bin

My question that I have searched high and low for is how do I make the myfile.bin and what would it look like, is this just C code that I write that assigns the value to the register I pick and then use some compiler that can output to a .bin file? I have done eeprom read/writes within programs but never directly written to eeprom space.The only information I would like to store is information about the product, usage information that can be looked up after. 50 Bytes of data max I would guess.

cameroony
  • 67
  • 1
  • 10
  • How you produce the binary file, is up to you, because it depends *on the kind of data* you want in the EEPROM. You can use any binary (hex) editor, an assembler that can output binary, a C compiler with a special linker script together with an object converter. There are thousands of ways. -- Please provide more information on the topic, and tell us what you have tried so far. Please remember to [edit] your question, don't add informations in the comments. – the busybee Apr 09 '20 at 05:46
  • You will have an option in your linker which output file to generate. bin hex or s19 are the most common by far, it will support at least one of those but perhaps not all. Your in-circuit debugger/adapter will in turn support some of those too. – Lundin Apr 09 '20 at 06:45

2 Answers2

0

The easiest thing to do was to first read the file like so:

./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -r myfile.bin

Then once that was done I used @thebusybee idea of using a hex editor, I opened the read file (GHex was the editor I used). Using the editor I made my changes and wrote the file using:

./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -w myfile.bin

This seems to be working well but the data doesn't match perfectly after a second read, hmm may be a different problem.

cameroony
  • 67
  • 1
  • 10
0

I believe that you can only write to EEPROM area properly after unlocking it and verifying if the DATA area is not write protected.

By default, EEPROM is write protected and a specific sequence is required in order to unlock it: two hardware keys have to be written into FLASH_DUKR register. The first time I tried programming EEPROM it didn’t work. The reason was me ignoring the following statement in the reference manual: “before starting programming, the application must verify that the DATA area is not write protected”. I interpreted it as “you shouldn’t write into write-protected areas” while the real meaning was “it takes some time to unlock EEPROM”.

https://lujji.github.io/blog/bare-metal-programming-stm8-part2/

Unfortunately maybe you really need a program just to fill the EEPROM:

//[...]     
#define FLASH_IAPSR *(unsigned char*)0x505F
#define FLASH_DUKR *(unsigned char*)0x5064
#define _MEM_(mem_addr) (*(volatile unsigned char*)(mem_addr))
    
//[...]

void main(){
   //[...]       
   info1 = 127;
   info2 = 32767;
        
   FLASH_DUKR = 0xAE;
   FLASH_DUKR = 0x56;
   while (!(FLASH_IAPSR & (1 << 3)));

   _MEM_(0x4000) = info1;
   while (!(FLASH_IAPSR & (1 << 2)));
   _MEM_(0x4001) = (unsigned char) (info2 & 0xFF);
   while (!(FLASH_IAPSR & (1 << 2)));
   _MEM_(0x4002) = (unsigned char) ((info2 >> 8) & 0xFF);
   while (!(FLASH_IAPSR & (1 << 2)));
        
   FLASH_IAPSR &= ~(1 << 3);
   //[...]
}