用于 EEPROM 写入的 stm8flash .bin 文件
stm8flash .bin file for EEPROM write
我正在尝试将值直接写入 stm8 微控制器上的 eeprom space。我不想编写一个程序来执行此操作并将其闪存到芯片上。但我想直接写信给它。执行此操作的命令在 unix 中是这样的:
./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -w myfile.bin
我搜索过的问题是如何制作 myfile.bin
以及它会是什么样子,这只是我编写的 C 代码,它将值分配给我选择的寄存器然后使用一些可以输出到 .bin 文件的编译器?我在程序中完成了eeprom read/writes 但从未直接写入eeprom space.The 我想存储的唯一信息是有关产品的信息,可以在之后查找的使用信息。我猜最多 50 字节的数据。
最简单的方法是先像这样读取文件:
./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -r myfile.bin
完成后,我使用@thebusybee 的想法,使用十六进制编辑器,打开读取文件(GHex 是我使用的编辑器)。使用编辑器我进行了更改并使用以下方式编写了文件:
./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -w myfile.bin
这似乎运行良好,但第二次读取后数据不完全匹配,嗯,可能是另一个问题。
我相信只有解锁并验证DATA区是否没有写保护后才能正常写入EEPROM区
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/
不幸的是,您可能真的需要一个程序来填充 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);
//[...]
}
我正在尝试将值直接写入 stm8 微控制器上的 eeprom space。我不想编写一个程序来执行此操作并将其闪存到芯片上。但我想直接写信给它。执行此操作的命令在 unix 中是这样的:
./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -w myfile.bin
我搜索过的问题是如何制作 myfile.bin
以及它会是什么样子,这只是我编写的 C 代码,它将值分配给我选择的寄存器然后使用一些可以输出到 .bin 文件的编译器?我在程序中完成了eeprom read/writes 但从未直接写入eeprom space.The 我想存储的唯一信息是有关产品的信息,可以在之后查找的使用信息。我猜最多 50 字节的数据。
最简单的方法是先像这样读取文件:
./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -r myfile.bin
完成后,我使用@thebusybee 的想法,使用十六进制编辑器,打开读取文件(GHex 是我使用的编辑器)。使用编辑器我进行了更改并使用以下方式编写了文件:
./stm8flash -c stlinkv2 -p stm8l151f2 -s eeprom -w myfile.bin
这似乎运行良好,但第二次读取后数据不完全匹配,嗯,可能是另一个问题。
我相信只有解锁并验证DATA区是否没有写保护后才能正常写入EEPROM区
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/
不幸的是,您可能真的需要一个程序来填充 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);
//[...]
}