2025-02-12 01:26 AM
I have a project that needs standby mode for switching the device on/off and stop mode to save energy between measurements. I use the IWDG for safety, but it restarts the board while in standby mode...
I used this code to switch on the IWDG:
IWDG->KR = 0x0000CCCC;
IWDG->KR = 0x00005555; //Enable the IWDG Registry access
IWDG->PR = 0x06; //Prescaler 256
IWDG->RLR = 0x0FFF; //Reload value 4095, around 30s
while (IWDG->SR != 0) {} //Wait for the registers to be updated
IWDG->KR = 0x0000AAAA; //Reload/start the IWDG*/
To switch off the IWDG_STBY bit, I had to unlock flash and the option bits for flash, as recommended by AN0434 page 98. And the CPU has to restart to take in effect. I made this code, started before initializing the IWDG:
bool mustRestart = false;
if (FLASH->OPTR & FLASH_OPTR_IWDG_STDBY) mustRestart = true;
FLASH->KEYR = FLASH_KEY1; //Flash unlock sequence (pg 98 of the reference manual)
FLASH->KEYR = FLASH_KEY2; //First unlock flash, then option bytes
FLASH->OPTKEYR = FLASH_OPTKEY1;
FLASH->OPTKEYR = FLASH_OPTKEY2;
FLASH->CR |= FLASH_CR_OBL_LAUNCH; //Launch the option byte loader
FLASH->OPTR &= ~FLASH_OPTR_IWDG_STDBY; //Disable IWDG in standby mode
while (FLASH->SR & FLASH_SR_BSY) {} //Wait for the registers to be updated
while (FLASH->C2SR & FLASH_C2SR_BSY) {} //Wait for the registers to be updated
//check pesd bit
if (FLASH->SR & FLASH_SR_PESD) //Check if the PESD bit is set
{
FLASH->SR &= ~FLASH_SR_PESD; //Clear the PESD bit
while (FLASH->SR != FLASH_SR_BSY) {} //Wait for the registers to be updated
while (FLASH->C2SR != FLASH_C2SR_BSY) {}//Wait for the registers to be updated
}
FLASH->CR |= FLASH_CR_OPTSTRT; //Start the option byte programming
while (FLASH->SR & FLASH_SR_BSY) {} //Wait for the registers to be updated
if (mustRestart) NVIC_SystemReset(); //If the IWDG was enabled in standby mode, restart the device
After uploading this code to my STM32, I can't program it anymore. It gets stuck erasing the flash:
[2025-02-12T09:16:45.451Z] SERVER CONSOLE DEBUG: onBackendConnect: gdb-server session connected. You can switch to "DEBUG CONSOLE" to see GDB interactions.
/opt/ST/STM32CubeCLT/STLink-gdb-server/bin/ST-LINK_gdbserver -p 50000 -cp /opt/ST/STM32CubeCLT/STM32CubeProgrammer/bin --swd --halt -m 0
STMicroelectronics ST-LINK GDB server. Version 7.5.0
Copyright (c) 2023, STMicroelectronics. All rights reserved.
Starting server with the following options:
Persistent Mode : Disabled
Logging Level : 31
Listen Port Number : 50000
Status Refresh Delay : 15s
Verbose Mode : Disabled
SWD Debug : Enabled
COM frequency = 24000 kHz
Target connection mode: Default
Reading ROM table for AP 0 @0xe00fffd0
Hardware watchpoint supported by the target
ST-LINK Firmware version : V3J15M7B5S1
Device ID: 0x495
PC: 0xfffffffe
ST-LINK device status: HALT_MODE
ST-LINK detects target voltage = 3.34 V
ST-LINK device status: HALT_MODE
ST-LINK device initialization OK
Stm32Device, pollAndNotify running...
SwvSrv state change: 0 -> 1
Waiting for connection on port 50001...
Waiting for debugger connection...
Waiting for connection on port 50000...
Accepted connection on port 50000...
Debugger connected
Waiting for debugger connection...
Waiting for connection on port 50000...
GDB session thread running
GdbSessionManager, session started: 1
Enter STM32_SystemReset() function
Enter STM32_InitAfterReset() function
NVIC_DFSR_REG = 0x00000008
NVIC_CFGFSR_REG = 0x00000000
Stm32Device, closeDevice() entry
GDB session, device event: 5
Stm32Device, pollAndNotify stopped
Stm32Device, closeDevice() exit
------ Switching to STM32CubeProgrammer -----
-------------------------------------------------------------------
STM32CubeProgrammer v2.15.0
-------------------------------------------------------------------
Log output file: /tmp/STM32CubeProgrammer_OjCAya.log
ST-LINK SN : 003F00433532510531333430
ST-LINK FW : V3J15M7B5S1
Board : STLINK-V3SET
Voltage : 3.34V
SWD freq : 8000 KHz
Connect mode: Under Reset
Reset mode : Hardware reset
Device ID : 0x495
Revision ID : Rev X
Device name : STM32WB5x/35xx
Flash size : 1 MBytes
Device type : MCU
Device CPU : Cortex-M4
BL Version : 0xD5
Debug in Low Power mode enabled
Memory Programming ...
Opening and parsing file: ST-LINK_GDB_server_BCuh2p.srec
File : ST-LINK_GDB_server_BCuh2p.srec
Size : 104.54 KB
Address : 0x08000000
Erasing memory corresponding to segment 0:
Erasing internal memory sectors [0 26]
Error: failed to erase memory
Error: failed to erase memory
Encountered Error when opening /opt/ST/STM32CubeCLT/STM32CubeProgrammer/bin/STM32_Programmer_CLI
------ Switching context -----
Error in STM32CubeProgrammer
Enter STM32_SystemReset() function
Failed to reset target.
Stopping port 50000
Received stop for port 50000, not accepting new connections.
GDB session, device event: 4
GdbSrv, deInit entry.
Shutting down...
GdbSessionManager, deInit entry.
GDB session, device event: 3
Error in executing 'detach' command ...
GDB session terminated: Disposed
GdbSessionManager, session terminated: 1
GDB session disposed: 1
GdbSessionManager, deInit exit
SwvSrv deInit entry
Stopping port 50001
Received stop for port 50001, not accepting new connections.
SwvSrv deInit exit
Stm32Device, closeDevice() entry
Stm32Device, closeDevice() exit
Stm32Device, deInit success
GdbSrv, deInit exit.
Exit.
[2025-02-12T09:16:47.099Z] SERVER CONSOLE DEBUG: onBackendConnect: gdb-server session closed
GDB server session ended. This terminal will be reused, waiting for next session to start...
Then I checked with STMCubeProgrammer, it can connect. Sometimes it gives a read error. I can set optionbytes, but can't read/write the FUS or erase flash:
When opening up PRG:
10:20:01 : UR connection mode is defined with the HWrst reset mode
10:20:01 : ST-LINK SN : 003F00433532510531333430
10:20:01 : ST-LINK FW : V3J15M7B5S1
10:20:01 : Board : STLINK-V3SET
10:20:01 : Voltage : 3.38V
10:20:01 : SWD freq : 8000 KHz
10:20:01 : Connect mode: Under Reset
10:20:01 : Reset mode : Hardware reset
10:20:01 : Device ID : 0x495
10:20:01 : Revision ID : Rev X
10:20:01 : Debug in Low Power mode enabled.
10:20:02 : UPLOADING OPTION BYTES DATA ...
10:20:02 : Bank : 0x00
10:20:02 : Address : 0x58004020
10:20:02 : Size : 96 Bytes
10:20:02 : Bank : 0x01
10:20:02 : Address : 0x58004080
10:20:02 : Size : 8 Bytes
10:20:02 : UPLOADING ...
10:20:02 : Size : 4 Bytes
10:20:02 : Address : 0x58004080
10:20:02 : Read progress:
10:20:02 : Data read successfully
10:20:02 : Time elapsed during the read operation is: 00:00:00.033
10:20:02 : UPLOADING ...
10:20:02 : Size : 1024 Bytes
10:20:02 : Address : 0x8000000
10:20:02 : Read progress:
10:20:02 : Data read failed
When trying to clean the flash:
10:22:08 : MASS ERASE ...
10:22:08 : Mass erase operation failed.Please verify flash protection
When reading the option bytes:
I tried in PRG to write the IWDGSTBY and IWDGSTOP bits, that worked. RDP = AA, PCROP and Write protect seem on first sight ok. ESE is not possible to set/reset. (Like not present).
Help.