C语言写入STM32L4x1闪存
Writing in STM32L4x1 flash memory in C
我正在尝试使用 JTAG ST-Link/V2 在 Windows 上写入 STM32L476 的闪存 7. 无需上传软件,我只需要将数据写入非易失性位置它可以被读取和删除。
作为硬件新手,只有在非嵌入式正则C编程时才能高效,恐怕会损坏或修改闪存不可挽回。另外,我不确定我能做什么或不能做什么。
我读懂了 manual that writing in 0x08000000 memory place seemed to be a good idea. Using C code to make calls to ST-Link_Utility :
const char CMD_ACCESS_ST_UTILITY[] = "cd C:/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility&&ST-LINK_CLI.exe ";
bool STLINKWriteSystemCalls(void)
{
char cmd[200] = "";
strcpy(cmd, CMD_ACCESS_ST_UTILITY); // cmd to access utility
strcat(cmd, "-c"); // Then connect with ST-Link
if (system(cmd) != 0)
return false; // If failed exit
strcpy(cmd, CMD_ACCESS_ST_UTILITY);
strcat(cmd, "-r8 0x08000000 0x100"); // Read to check if there is data already
// I haven't managed yet how I'll compare the result of read
// To FFFF but that's not the main issue
if (system(cmd) != 0)
return false; // If failed exit
strcpy(cmd, CMD_ACCESS_ST_UTILITY);
strcat(cmd, "-w8 0x08000000 0x00"); // Write data into 0x080000000
if (system(cmd) != 0)
return false; // If failed exit
return true;
}
是否有更好的方法来解决写到哪里以及如何写(错误检查、资源使用等)?
好消息!由于您使用的是 ST Link 实用程序,因此您无法破坏芯片。它只会拒绝在没有可访问内存的地方写入。
将自己永久锁定在芯片之外的唯一方法是启用最大 2 级 的读取保护。为此,您必须写入选项字节。而且您不会不小心将 0xCC33 写入锁定字节。
需要考虑的一件事是芯片仅写入半字(16 位)并且仅在 FLASH 仍为 0xFFFF(已擦除状态)时写入。否则会return写入错误
更新:来自 RDP 的手动片段。代码读取保护。
Level 2: No debug
In this level, the protection level 1 is guaranteed.
In addition, the Cortex®-M4 debug port, the
boot from RAM (boot RAM mode) and the boot from System memory (boot loader mode)
are no more available.
In user execution mode (boot FLASH mode), all operations are
allowed on the Flash Main memory.
...
The level 2 cannot be removed at all:
it is an irreversible operation.
简而言之:不要写入 0x1FFF7800。
关于闪存的主要知识:
- 闪存是页面可擦除的,在您的情况下页面大小为
2K
。这是什么意思?您必须确保您的代码不在页面的 2k
范围内。
- 擦除后内存中所有字节的状态为0xFF。
- 写入闪存字节意味着,在原始级别,将设置为
1
的位修改为设置为 0
的位。如果你想从 0
修改一点到 1
你必须 擦除整页。
ST-Link_Utility
采用嵌入式方式,因此当您写入闪存时,它会擦除整个扇区然后写入数据。如果您的代码需要在使用后简化数据,则相同。
默认情况下,您的 mCU 在启动时使用 0x0000 0000
0x0800 0000
的别名地址。
第一个词应包含重置向量和默认向量table。重置向量始终是第一个指令 executed.The,此 table 中的重置向量将包含指向包含重置代码的地址的分支。
所以,换句话说,
上电时,处理器跳转到固定位置0x0,也就是跳转到0x0800 0000
。显然你选择的地址不正确;)
我正在尝试使用 JTAG ST-Link/V2 在 Windows 上写入 STM32L476 的闪存 7. 无需上传软件,我只需要将数据写入非易失性位置它可以被读取和删除。
作为硬件新手,只有在非嵌入式正则C编程时才能高效,恐怕会损坏或修改闪存不可挽回。另外,我不确定我能做什么或不能做什么。
我读懂了 manual that writing in 0x08000000 memory place seemed to be a good idea. Using C code to make calls to ST-Link_Utility :
const char CMD_ACCESS_ST_UTILITY[] = "cd C:/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility&&ST-LINK_CLI.exe ";
bool STLINKWriteSystemCalls(void)
{
char cmd[200] = "";
strcpy(cmd, CMD_ACCESS_ST_UTILITY); // cmd to access utility
strcat(cmd, "-c"); // Then connect with ST-Link
if (system(cmd) != 0)
return false; // If failed exit
strcpy(cmd, CMD_ACCESS_ST_UTILITY);
strcat(cmd, "-r8 0x08000000 0x100"); // Read to check if there is data already
// I haven't managed yet how I'll compare the result of read
// To FFFF but that's not the main issue
if (system(cmd) != 0)
return false; // If failed exit
strcpy(cmd, CMD_ACCESS_ST_UTILITY);
strcat(cmd, "-w8 0x08000000 0x00"); // Write data into 0x080000000
if (system(cmd) != 0)
return false; // If failed exit
return true;
}
是否有更好的方法来解决写到哪里以及如何写(错误检查、资源使用等)?
好消息!由于您使用的是 ST Link 实用程序,因此您无法破坏芯片。它只会拒绝在没有可访问内存的地方写入。
将自己永久锁定在芯片之外的唯一方法是启用最大 2 级 的读取保护。为此,您必须写入选项字节。而且您不会不小心将 0xCC33 写入锁定字节。
需要考虑的一件事是芯片仅写入半字(16 位)并且仅在 FLASH 仍为 0xFFFF(已擦除状态)时写入。否则会return写入错误
更新:来自 RDP 的手动片段。代码读取保护。
Level 2: No debug
In this level, the protection level 1 is guaranteed.
In addition, the Cortex®-M4 debug port, the boot from RAM (boot RAM mode) and the boot from System memory (boot loader mode) are no more available.
In user execution mode (boot FLASH mode), all operations are allowed on the Flash Main memory. ...
The level 2 cannot be removed at all: it is an irreversible operation.
简而言之:不要写入 0x1FFF7800。
关于闪存的主要知识:
- 闪存是页面可擦除的,在您的情况下页面大小为
2K
。这是什么意思?您必须确保您的代码不在页面的2k
范围内。 - 擦除后内存中所有字节的状态为0xFF。
- 写入闪存字节意味着,在原始级别,将设置为
1
的位修改为设置为0
的位。如果你想从0
修改一点到1
你必须 擦除整页。
ST-Link_Utility
采用嵌入式方式,因此当您写入闪存时,它会擦除整个扇区然后写入数据。如果您的代码需要在使用后简化数据,则相同。
默认情况下,您的 mCU 在启动时使用 0x0000 0000
0x0800 0000
的别名地址。
第一个词应包含重置向量和默认向量table。重置向量始终是第一个指令 executed.The,此 table 中的重置向量将包含指向包含重置代码的地址的分支。
所以,换句话说,
上电时,处理器跳转到固定位置0x0,也就是跳转到0x0800 0000
。显然你选择的地址不正确;)