【发布时间】:2020-11-17 09:42:46
【问题描述】:
我正在尝试写来更新我在 STM32G0 上的工程字节,但我遇到了困难。我需要通过写入 FLASH_KEYR 寄存器来解锁闪存。手册是这样说的:
After reset, write into the FLASH control register (FLASH_CR) is not allowed so as to protect the Flash memory against possible unwanted operations due, for example, to electric disturbances. The following sequence unlocks these registers:
1. Write KEY1 = 0x4567 0123 in the FLASH key register (FLASH_KEYR)
2. Write KEY2 = 0xCDEF 89AB in the FLASH key register (FLASH_KEYR).
Any wrong sequence will lock the FLASH_CR registers until the next system reset. In the case of a wrong key sequence, a bus error is detected and a Hard Fault interrupt is generated.
The FLASH_CR registers can be locked again by software by setting the LOCK bit in one of these registers.
Note: The FLASH_CR register cannot be written when the BSY1 bit of the FLASH status register (FLASH_SR) is set. Any attempt to write to this register with the BSY1 bit set causes the AHB bus to stall until the BSY1 bit is cleared.
我确保在尝试写入时我的 BSY1 位是清晰的,我相信这是唯一的条件。
这就是我的写作方式:
#define KEY1 0x45670123
#define KEY2 0xCDEF89AB
...
FLASH->KEYR = KEY1;
FLASH->KEYR = KEY2;
此时,我的 FLASH_KEYR 寄存器仍然是 0x0。有谁知道为什么?
编辑:这是我的代码
#define TEST_FLASH_BYTES ((uint32_t)0x0801FF00)
...
FLASH->CR &= FLASH_CR_LOCK;
FLASH->KEYR = KEY1;
FLASH->KEYR = KEY2;
while(__HAL_FLASH_GET_FLAG(FLASH_SR_BSY1) != 0) {}
program_flash(TEST_FLASH_BYTES, 0x1234567887654321);
while (__HAL_FLASH_GET_FLAG(FLASH_SR_BSY1) != 0) {}
FLASH->SR &= FLASH_SR_EOP;
FLASH->CR &= FLASH_CR_PG;
这里是program_flash:
static void program_flash(uint32_t address, uint64_t data)
{
*(uint32_t *)address = (uint32_t)data;
(independently of compiler optimization behavior) */
__ISB();
*(uint32_t *)(address + 4U) = (uint32_t)(data >> 32U);
}
编辑:它是一个只写寄存器,所以我无法读回它。我想出了如何成功写入内存。
【问题讨论】:
-
FLASH_CR 默认值为 0x0000 0080.Bit 7 在检测到解锁序列后由硬件复位。所以期望值为 0x00。
-
@Babajan 您在哪里看到默认 val 是 0x0000 0080?地址偏移量为 0x008,其重置值为 0x0000 0000,但我不确定该默认值来自何处。
-
默认寄存器值为 0x8000 0000 。这是一个链接st.com/resource/en/programming_manual/…参考第2.8.5章
-
@Babajan 用于 STM32F 系列
标签: memory embedded microcontroller stm32 flash-memory