STM32的OTP写入64bits数据

Writing 64bits data in STM32's OTP

我正在尝试将数据写入STM32L4芯片的闪存中。这将在 编程上下文中完成,在该上下文中我使用 C 调用 ST-Link 实用程序命令行 .

下面是闪存的示例:

               0             4             8             C
0x1FFF7000  FFFFFFFF     FFFFFFFF       FFFFFFFF       FFFFFFFF

到目前为止,我只能在 64 位数据上写入 32 位 space :

               0             4             8             C
0x1FFF7000  AAAAAAAF     FFFFFFFF       BBBCCCDD       FFFFFFFF

写入前32位时,后32位被锁定。所以鉴于内存处于以前的状态,下面的写法是不允许的:

               0             4             8             C
0x1FFF7000  AAAAAAAF     00000001       BBBCCCDD       FFFFFFFF
                            ^^                            ^^
                         Not allowed                     Locked

我只能在 64 位数据中写入一次大小受 32 位限制的数据 space。 我发现这很奇怪,因为没有理由以这种方式处理内存。

编辑:这只是因为我想在 OTP 中写入,当我在其他地方写入时它似乎工作正常

第一个可能的解决方案

这个 我之前的一个问题引用了这个:

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.

有没有什么方法可以启用 2 级 允许我在 64 位内存上写入两次 32 位 space?

第二种可能的解决方案

查看芯片的documentation,我找到了闪存编程序列(第102页):

  1. Check that no Flash main memory operation is ongoing by checking the BSY bit in the Flash status register (FLASH_SR).
  2. Check and clear all error programming flags due to a previous programming. If not, PGSERR is set.
  3. Set the PG bit in the Flash control register (FLASH_CR).
  4. Perform the data write operation at the desired memory address, inside main memory block or OTP area. Only double word can be programmed. – Write a first word in an address aligned with double word – Write the second word
  5. Wait until the BSY bit is cleared in the FLASH_SR register.
  6. Check that EOP flag is set in the FLASH_SR register (meaning that the programming operation has succeed), and clear it by software.
  7. Clear the PG bit in the FLASH_SR register if there no more programming request anymore.

我一直无法真正理解步骤 3. 中的 PG 位 是什么,但是当我尝试设置它时, 不允许书写。

我使用 ST-Link Utility 进行写入和命令 -w32,根据手册,应该允许我在寄存器上写入。

-w32 supports writing to Flash memory, OTP, SRAM and R/W registers

此外,步骤 4. 似乎准确描述了我所反对的问题,但我无法理解它提出的解决方案。

任何关于如何使用任何解决方案解决问题的提示都是一种祝福。另外,由于我对嵌入式低级词汇不是很熟悉,请不要犹豫编辑我的问题以使其更清楚。

您观察到此行为的原因如下(来自 STM32L4x5 STM32L4x6 RM):

1 Kbyte (128 double word) OTP (one-time programmable) bytes for user data. The OTP area is available in Bank 1 only. The OTP data cannot be erased and can be written only once. If only one bit is at 0, the entire double word cannot be written anymore, even with the value 0x0000 0000 0000 0000.

这意味着 OTP 区域被组织为 64 位值,写入它应该是 "atomic",这意味着您一次写入所有 64 位。将 64 位中的任何一位设置为 0 会锁定整个双字以供进一步写入。在您的示例中,这意味着,如果您尝试使用 -w8 以 8 位批次写入值,您将只能在给定的双字(64 位)中写入 8 个中的第一个字节。

反过来这意味着即使 ST-Link Utility manual 声明 -w32 允许对 OTP 区域进行编程,这仅意味着可以将 OTP 指定为目标地址以写入但不一定能正确编程每个双字的所有位。这看起来像是一个疏忽,尽管您对此无能为力。

我个人会尝试使用 -P 命令对其进行编程:

Description: Loads binary, Intel Hex or Motorola S-record file into device memory without
verification. For hex and srec format, the address is relevant.
Syntax: -P <File_Path> [<Address>]
Examples: -P C:\file.srec
-P C:\file.bin 0x08002000
-P C:\file.hex

我自己还没有测试过,但目前对我来说这似乎是允许您通过 ST-Link"atomically" 将 64 位值编程到 OTP 中的唯一可能性。