[SOLVED] Unable to reprogram STM32 flash without full erase

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] Unable to reprogram STM32 flash without full erase

    I am unable to reprogram an STM32 microcontroller unless I perform a full erase before programming. The below code uses CLion, but the same error also happens using the command line version of arm-none-eabi-gdb and JLinkGDBServer.
    GDB: arm-none-eabi-gdb (also occurs with CLion's bundled GDB)
    Compiler: arm-none-eabi-gcc and arm-none-eabi-g++
    Microcontroller: STM32L433CC (also happens with STM32F411RC)
    I am using USB for this. When I use ethernet, I get an error like "Failed to download RAMCode")

    The error is below. The program still starts, but with the previous code. If I attempt to flash it one more time, the flash is corrupted until I perform a full erase.

    Brainfuck Source Code

    1. /opt/SEGGER/JLink_Linux_V700_x86_64/JLinkGDBServerCLExe -singlerun -strict -timeout 0 -nogui -speed 1000 -ir -endian little -vd -device STM32L433CC -if swd
    2. SEGGER J-Link GDB Server V7.00 Command Line Version
    3. JLinkARM.dll V7.00 (DLL compiled Apr 8 2021 14:33:43)
    4. Command line: -singlerun -strict -timeout 0 -nogui -speed 1000 -ir -endian little -vd -device STM32L433CC -if swd
    5. -----GDB Server start settings-----
    6. GDBInit file: none
    7. GDB Server Listening port: 2331
    8. SWO raw output listening port: 2332
    9. Terminal I/O port: 2333
    10. Accept remote connection: yes
    11. Generate logfile: on
    12. Verify download: on
    13. Init regs on start: on
    14. Silent mode: off
    15. Single run mode: on
    16. Target connection timeout: 0 ms
    17. ------J-Link related settings------
    18. J-Link Host interface: USB
    19. J-Link script: none
    20. J-Link settings file: none
    21. ------Target related settings------
    22. Target device: STM32L433CC
    23. Target interface: SWD
    24. Target interface speed: 1000kHz
    25. Target endian: little
    26. Connecting to J-Link...
    27. J-Link is connected.
    28. Firmware: J-Link Pro V4 compiled Feb 16 2021 10:07:18
    29. Hardware: V4.00
    30. S/N: 174505557
    31. Feature(s): RDI, FlashBP, FlashDL, JFlash, GDB
    32. Checking target voltage...
    33. Target voltage: 3.30 V
    34. Listening on TCP/IP port 2331
    35. Connecting to target...
    36. Error during pretty printers setup: Undefined info command: "pretty-printer". Try "help info".
    37. Some features and performance optimizations will not be available.
    38. Connected to target
    39. Waiting for GDB connection...Connected to 127.0.0.1
    40. Reading all registers
    41. Read 2 bytes @ address 0x08013C10 (Data = 0x4B02)
    42. Read 2 bytes @ address 0x08001198 (Data = 0x4770)
    43. Read 4 bytes @ address 0x00000000 (Data = 0x20010000)
    44. Read 2 bytes @ address 0x00000000 (Data = 0x0000)
    45. Downloading 400 bytes @ address 0x08000000 - Verified OK
    46. Downloading 16112 bytes @ address 0x08000190 - Verified OK
    47. Downloading 16048 bytes @ address 0x08004080 - Verified OK
    48. Downloading 16160 bytes @ address 0x08007F30 - Verified OK
    49. Downloading 15984 bytes @ address 0x0800BE50 - Verified OK
    50. Downloading 16080 bytes @ address 0x0800FCC0 - Verified OK
    51. Downloading 15984 bytes @ address 0x08013B90 - Verified OK
    52. Downloading 7464 bytes @ address 0x08017A00 - Verified OK
    53. Downloading 6856 bytes @ address 0x08019728 - Verified OK
    54. Downloading 1680 bytes @ address 0x0801B1F0 - Verified OK
    55. Downloading 1512 bytes @ address 0x0801B880 - Verified OK
    56. Downloading 40 bytes @ address 0x0801BE68 - Verified OK
    57. Downloading 8 bytes @ address 0x0801BE90 - Verified OK
    58. Downloading 632 bytes @ address 0x0801BE98 - Verified OK
    59. Comparing flash [....................] Done.
    60. Erasing flash [....................] Done.
    61. ERROR: Failed to erase sectors 0 @ address 0x08000000 (Algo87: Unspecified error #1)
    62. Failed to erase sectors.
    63. Writing register (PC = 0x 800f0dc)
    64. Read 2 bytes @ address 0x08013C10 (Data = 0x4B02)
    65. Read 2 bytes @ address 0x08001198 (Data = 0x4770)
    66. Debugger connected to localhost:2331
    67. Starting target CPU...
    Display All
  • I believe I have the same or a similar issue. My setup consists of a custom PCB with an STM32L476RG as the target. I am also using CLion, but am using v7.22 of the Segger suite of tools. This error seems to occur on several versions as I have tried several others with the same result. My output looks like this:

    Source Code

    1. C:\Users\jspecht\SEGGER\JLink\JLinkGDBServerCL.exe -device STM32L476RG -endian little -speed 1000 -if swd -vd -rtos GDBServer\RTOSPlugin_FreeRTOS.dll
    2. SEGGER J-Link GDB Server V7.22 Command Line Version
    3. JLinkARM.dll V7.22 (DLL compiled Jun 2 2021 10:03:15)
    4. Command line: -device STM32L476RG -endian little -speed 1000 -if swd -vd -rtos G
    5. DBServer\RTOSPlugin_FreeRTOS.dll
    6. -----GDB Server start settings-----
    7. GDBInit file: none
    8. GDB Server Listening port: 2331
    9. SWO raw output listening port: 2332
    10. Terminal I/O port: 2333
    11. Accept remote connection: localhost only
    12. Generate logfile: off
    13. Verify download: on
    14. Init regs on start: off
    15. Silent mode: off
    16. Single run mode: off
    17. Target connection timeout: 0 ms
    18. ------J-Link related settings------
    19. J-Link Host interface: USB
    20. J-Link script: none
    21. J-Link settings file: none
    22. ------Target related settings------
    23. Target device: STM32L476RG
    24. Target interface: SWD
    25. Target interface speed: 1000kHz
    26. Target endian: little
    27. Connecting to J-Link...
    28. J-Link is connected.
    29. Firmware: J-Link V10 compiled Apr 27 2021 16:35:48
    30. Hardware: V10.10
    31. S/N: 50121668
    32. Feature(s): GDB
    33. Checking target voltage...
    34. Target voltage: 3.34 V
    35. Listening on TCP/IP port 2331
    36. Connecting to target...
    37. Connected to target
    38. Waiting for GDB connection...Connected to 127.0.0.1
    39. Reading all registers
    40. Read 4 bytes @ address 0x080366AC (Data = 0x8F6FF3BF)
    41. Loading RTOS plugin: GDBServer\RTOSPlugin_FreeRTOS.dll...
    42. RTOS plugin (API v1.0) loaded successfully
    43. RTOS plugin: Loaded
    44. Received symbol: pxCurrentTCB (0x200163D0)
    45. Received symbol: pxReadyTasksLists (0x200163D4)
    46. Received symbol: xDelayedTaskList1 (0x2001649C)
    47. Received symbol: xDelayedTaskList2 (0x200164B0)
    48. Received symbol: pxDelayedTaskList (0x200164C4)
    49. Received symbol: pxOverflowDelayedTaskList (0x200164C8)
    50. Received symbol: xPendingReadyList (0x200164CC)
    51. Received symbol: xTasksWaitingTermination (0x200164E0)
    52. Received symbol: xSuspendedTaskList (0x200164F8)
    53. Received symbol: uxCurrentNumberOfTasks (0x2001650C)
    54. Received symbol: uxTopUsedPriority (0x00000000)
    55. Received symbol: uxTopReadyPriority (0x20016514)
    56. Received symbol: vPortEnableVFP (0x080366F8)
    57. Received symbol: FreeRTOSDebugConfig (0x00000000)
    58. All mandatory symbols successfully loaded.
    59. Downloading 392 bytes @ address 0x0800C000 - Verified OK
    60. Downloading 16144 bytes @ address 0x0800C1C0 - Verified OK
    61. Downloading 16096 bytes @ address 0x080100D0 - Verified OK
    62. Downloading 16128 bytes @ address 0x08013FB0 - Verified OK
    63. Downloading 16128 bytes @ address 0x08017EB0 - Verified OK
    64. Downloading 16048 bytes @ address 0x0801BDB0 - Verified OK
    65. Downloading 16016 bytes @ address 0x0801FC60 - Verified OK
    66. Downloading 15952 bytes @ address 0x08023AF0 - Verified OK
    67. Downloading 16000 bytes @ address 0x08027940 - Verified OK
    68. Downloading 16048 bytes @ address 0x0802B7C0 - Verified OK
    69. Downloading 15968 bytes @ address 0x0802F670 - Verified OK
    70. Downloading 15936 bytes @ address 0x080334D0 - Verified OK
    71. Downloading 16096 bytes @ address 0x08037310 - Verified OK
    72. Downloading 15936 bytes @ address 0x0803B1F0 - Verified OK
    73. Downloading 16064 bytes @ address 0x0803F030 - Verified OK
    74. Downloading 576 bytes @ address 0x08042EF0 - Verified OK
    75. Downloading 16192 bytes @ address 0x08043130 - Verified OK
    76. Downloading 14992 bytes @ address 0x08047070 - Verified OK
    77. Downloading 8 bytes @ address 0x0804AB00 - Verified OK
    78. Downloading 8 bytes @ address 0x0804AB08 - Verified OK
    79. Downloading 4 bytes @ address 0x0804AB10 - Verified OK
    80. Downloading 3308 bytes @ address 0x0804AB14 - Verified OK
    81. ERROR: Failed to erase sectors 24 @ address 0x0800C000 (Algo87: Unspecified erro
    82. r #1)
    83. Failed to erase sectors.
    84. Writing register (PC = 0x 802aeb0)
    85. Received monitor command: reset
    86. Resetting target
    87. Debugger connected to localhost:2331
    88. Resetting target
    89. Starting target CPU...
    Display All


    I did some experimenting and found a workaround that allows me to continue development:
    1. Open up a Jlink Commander window and connect to the board.
    2. Reset the target with 'r'...this also halts the processor.
    3. Debug/Reflash the application, it should succeed this time.


    I would leave the Commander window open and just reset the board anytime I wanted to re-flash the device.

    I also tried running the same code via an ST-Link and using the STLink-gdb server that is packaged with the STM32CubeIDE. In CLion I added the STLink-gdb server as a external gdb server. I was able to successfully program and debug my application multiple times without a programming error. Sadly my application is based on freeRTOS and is multithreaded(the st-link + stlink-gdb server doesn't support multi-thread applications) so I need to continue using my workaround. My team has experienced a similar issue with CLion before but we able to fix it by modifying the .gdbinit script that CLion uses. More info on that here.
  • Hi all,

    We are not aware of such an issue in recent versions.
    I just gave it a quick try here using two different evaluation boards but I cannot reproduce any problems.

    Test setup:
    • NUCLEO-L432KC / NUCLEO-F411RE
    • J-Link software V7.22
    • arm-none-eabi-gdb (8.0.50.20171128)
    • J-Link GDB Server CL started via command line (included in the test suite linked below)

    • Embedded Studio project (included in the test suite linked below)
    The test suite is available for download here:
    download.segger.com/Erik/210609_ST_STM32L4_TestSuite.zip

    1) Could you please give the project / files above a quick try and provide feedback?
    2) Could you please check if adding a "monitor reset" right before loading the application via GDB resolves the reported issues in your custom setups?


    Short feedback would be appreciated.


    Thanks
    Erik
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • After some debugging and head bashing I figured out what my problem was. CLion has a menu for modifying the debug configurations for a given target. In the "Advanced GDB Server Options" I tried adding the monitor reset command but that did not function as expected. I had to use the following Clion gdb init script to get the jlink to actually run the monitor reset command.

    Clion gdb-monitor commands

    In my project directory I have a file titled ".gdbinit" that contains the same commands listed in the above link. For Clion to recognize these commands I also had to add a ".gdbinit" file to my C:\Users\me\ directory with the line:

    add-auto-load-safe-path C:\path\to\project\.gdbinit

    I have this same setup on several other projects each with their own line in the file and it works perfectly. The reason it was not working on this project was because of a typo in the path. After fixing the typo, the script was properly executed and it all worked well.

    Thanks for your help!
  • Hi,
    Thank you for taking the time to share your solution to the problem with the community.

    We will close this thread now, as the issue is resolved.

    Best regards,
    Fabian
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.