[SOLVED] Use J-Link commander to flash a file to area that is not erased?

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

  • [SOLVED] Use J-Link commander to flash a file to area that is not erased?

    To flash an GD32 device I'm working with, I use a script that writes a temporary J-Link Commander "command" file (or whatever the correct term is) that includes the LoadFile command to flash the .bin or .hex file. An example command file might look like:

    Source Code

    1. ExitOnError 1
    2. Connect
    3. h
    4. LoadFile C:\devtrees\test-bin\test-bin.bin,0x08080000
    5. r
    6. exit
    The problem is that this fails if there is already data at address 0x08080000. If I erase the flash first it works great, but the script only knows how to erase the entire chip, not just the area of flash that the .bin (or .hex) file corresponds to. That means to update a single binary (this project flashes 3 binaries to various parts of the flash) I have to erase the chip then flash all 3 binaries, which is error prone when I have several versions of each binary that can be flashed.

    The `erase` command can be used to erase only a selected range of flash, but the script file would need to figure out what the appropriate range is and it doesn't do that - it only has the capability to erase the entire chip.


    Is there a way to tell `LoadFile` to erase the area of flash that it will be updating? I couldn't see such an option or setting in wiki.segger.com/J-Link_Commander

    Here is an example session trying to flash a non-erased chip:

    Source Code

    1. c:\devtrees\test-bin>flash-gd32.py C:\devtrees\test-bin\test-bin.bin,08080000
    2. SEGGER J-Link Commander V7.66g (Compiled Jul 7 2022 10:37:30)
    3. DLL version V7.66g, compiled Jul 7 2022 10:35:46
    4. J-Link Command File read successfully.
    5. Processing script file...
    6. J-Link>ExitOnError 1
    7. J-Link Commander will now exit on Error
    8. J-Link>Connect
    9. J-Link connection not established yet but required for command.
    10. Connecting to J-Link via USB...O.K.
    11. Firmware: J-Link V10 compiled Nov 2 2021 12:14:50
    12. Hardware version: V10.10
    13. S/N: 600105391
    14. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    15. VTref=3.310V
    16. Device "GD32F303ZK" selected.
    17. Connecting to target via SWD
    18. Found SW-DP with ID 0x2BA01477
    19. DPIDR: 0x2BA01477
    20. CoreSight SoC-400 or earlier
    21. Scanning AP map to find all available APs
    22. AP[1]: Stopped AP scan as end of AP map has been reached
    23. AP[0]: AHB-AP (IDR: 0x24770011)
    24. Iterating through AP map to find AHB-AP to use
    25. AP[0]: Core found
    26. AP[0]: AHB-AP ROM base: 0xE00FF000
    27. CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
    28. Found Cortex-M4 r0p1, Little endian.
    29. FPUnit: 6 code (BP) slots and 2 literal slots
    30. CoreSight components:
    31. ROMTbl[0] @ E00FF000
    32. [0][0]: E000E000 CID B105E00D PID 000BB00C SCS-M7
    33. [0][1]: E0001000 CID B105E00D PID 003BB002 DWT
    34. [0][2]: E0002000 CID B105E00D PID 002BB003 FPB
    35. [0][3]: E0000000 CID B105E00D PID 003BB001 ITM
    36. [0][4]: E0040000 CID B105900D PID 000BB9A1 TPIU
    37. [0][5]: E0041000 CID 00000000 PID 00000000 ???
    38. Cortex-M4 identified.
    39. J-Link>h
    40. PC = 08027744, CycleCnt = 2AD8BC18
    41. R0 = 00000000, R1 = CDCDCDCD, R2 = 80000000, R3 = 08031999
    42. R4 = 6802B4D0, R5 = 00000001, R6 = 6802B4D0, R7 = CCCC0007
    43. R8 = CCCC0008, R9 = CCCC0009, R10= CCCC000A, R11= CCCC000B
    44. R12= 08031998
    45. SP(R13)= 20003400, MSP= 200033D8, PSP= 20003400, R14(LR) = 21000000
    46. XPSR = 01000000: APSR = nzcvq, EPSR = 01000000, IPSR = 000 (NoException)
    47. CFBP = 02000000, CONTROL = 02, FAULTMASK = 00, BASEPRI = 00, PRIMASK = 00
    48. FPS0 = 4A71DF44, FPS1 = 20925D1B, FPS2 = 2010E87E, FPS3 = F69BF462
    49. FPS4 = 64DB8277, FPS5 = E3EB6079, FPS6 = 23C78804, FPS7 = D577FD7F
    50. FPS8 = DAC9F9F9, FPS9 = 502666CE, FPS10= 5B6F31D3, FPS11= 7A6FC353
    51. FPS12= FE89A50F, FPS13= 842683E1, FPS14= 842FBC66, FPS15= 2F0DAE5F
    52. FPS16= 6E59BC16, FPS17= 5E86E6ED, FPS18= ADB0E7B1, FPS19= 934CF140
    53. FPS20= FFF56F97, FPS21= 00EEBE6F, FPS22= CF3423CD, FPS23= B975F6E5
    54. FPS24= 849C7D2F, FPS25= AEED31C8, FPS26= 1ABDC822, FPS27= 9B88E032
    55. FPS28= 4EBE14CF, FPS29= A0EA7FD2, FPS30= B97B02D2, FPS31= 4ADCCC57
    56. FPSCR= 00000000
    57. J-Link>LoadFile C:\devtrees\test-bin\test-bin.bin,08080000
    58. Downloading file [C:\devtrees\test-bin\test-bin.bin]...
    59. Error while programming flash: Programming failed.
    60. Script processing completed.
    61. resetting...
    62. Done in 0 min 3.574 sec
    Display All

    And here is the J-Link command file that the script produced for that session:

    Source Code

    1. ExitOnError 1
    2. Connect
    3. h
    4. LoadFile C:\devtrees\test-bin\test-bin.bin,08080000
    5. r
    6. exit
  • There is something wrong here…
    A „loadfile“ has always done and should still do erase + verify implicitly, besides programming.

    Could you please provide a log file of a failing session?
    wiki.segger.com/Enable_J-Link_log_file
    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.
  • I should have thought to do this before. Here's an annotated manual jlink session that gets the script out of the picture and just tries to erase a couple sectors of flash:

    C Source Code

    1. C:\devtrees\cpa-ramfunc>jlink -log just-erase.gd32f303zk.jlink.log -device GD32F303ZK -if swd -speed 4000 -autoconnect
    2. 1
    3. SEGGER J-Link Commander V7.66g (Compiled Jul 7 2022 10:37:30)
    4. DLL version V7.66g, compiled Jul 7 2022 10:35:46
    5. Connecting to J-Link via USB...O.K.
    6. Firmware: J-Link V10 compiled Nov 2 2021 12:14:50
    7. Hardware version: V10.10
    8. S/N: 600105391
    9. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    10. VTref=3.315V
    11. Device "GD32F303ZK" selected.
    12. Connecting to target via SWD
    13. Found SW-DP with ID 0x2BA01477
    14. DPIDR: 0x2BA01477
    15. CoreSight SoC-400 or earlier
    16. Scanning AP map to find all available APs
    17. AP[1]: Stopped AP scan as end of AP map has been reached
    18. AP[0]: AHB-AP (IDR: 0x24770011)
    19. Iterating through AP map to find AHB-AP to use
    20. AP[0]: Core found
    21. AP[0]: AHB-AP ROM base: 0xE00FF000
    22. CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
    23. Found Cortex-M4 r0p1, Little endian.
    24. FPUnit: 6 code (BP) slots and 2 literal slots
    25. CoreSight components:
    26. ROMTbl[0] @ E00FF000
    27. [0][0]: E000E000 CID B105E00D PID 000BB00C SCS-M7
    28. [0][1]: E0001000 CID B105E00D PID 003BB002 DWT
    29. [0][2]: E0002000 CID B105E00D PID 002BB003 FPB
    30. [0][3]: E0000000 CID B105E00D PID 003BB001 ITM
    31. [0][4]: E0040000 CID B105900D PID 000BB9A1 TPIU
    32. [0][5]: E0041000 CID 00000000 PID 00000000 ???
    33. Cortex-M4 identified.
    34. // dump some memory
    35. J-Link>mem 08080000 80
    36. 08080000 = 52 46 55 4E FC 1F 16 00 FC 1F 01 20 00 00 01 20 RFUN....... ...
    37. 08080010 = 89 02 01 20 8D 02 01 20 A1 02 01 20 9A 02 00 00 ... ... ... ....
    38. 08080020 = 00 00 00 00 FC 1F 00 00 40 28 23 29 63 70 61 2D ........@(#)cpa-
    39. 08080030 = 66 6C 61 73 68 63 6F 70 69 65 72 20 76 32 30 2D flashcopier v20-
    40. 08080040 = 75 6E 6B 6E 6F 77 6E 20 62 75 69 6C 64 3A 30 20 unknown build:0
    41. 08080050 = 53 65 70 20 20 31 20 32 30 32 32 20 30 31 3A 31 Sep 1 2022 01:1
    42. 08080060 = 36 3A 32 36 00 00 00 00 00 00 00 00 00 00 00 00 6:26............
    43. 08080070 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    44. // erase a range that includes that memory
    45. J-Link>erase 08080000 08081fff
    46. Erasing selected range...
    47. J-Link: Flash download: Total time needed: 0.194s (Prepare: 0.097s, Compare: 0.000s, Erase: 0.083s, Program: 0.000s, Verify: 0.000s, Restore: 0.013s)
    48. J-Link: Flash download:
    49. Flash sectors within Range [0x08080000 - 0x08081FFF] deleted.
    50. Erasing done.
    51. // erase didn't work
    52. J-Link>mem 08080000 80
    53. 08080000 = 52 46 55 4E FC 1F 16 00 FC 1F 01 20 00 00 01 20 RFUN....... ...
    54. 08080010 = 89 02 01 20 8D 02 01 20 A1 02 01 20 9A 02 00 00 ... ... ... ....
    55. 08080020 = 00 00 00 00 FC 1F 00 00 40 28 23 29 63 70 61 2D ........@(#)cpa-
    56. 08080030 = 66 6C 61 73 68 63 6F 70 69 65 72 20 76 32 30 2D flashcopier v20-
    57. 08080040 = 75 6E 6B 6E 6F 77 6E 20 62 75 69 6C 64 3A 30 20 unknown build:0
    58. 08080050 = 53 65 70 20 20 31 20 32 30 32 32 20 30 31 3A 31 Sep 1 2022 01:1
    59. 08080060 = 36 3A 32 36 00 00 00 00 00 00 00 00 00 00 00 00 6:26............
    60. 08080070 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    61. // erase the whole chip
    62. J-Link>erase
    63. Without any given address range, Erase Chip will be executed
    64. Erasing device...
    65. J-Link: Flash download: Total time needed: 0.608s (Prepare: 0.070s, Compare: 0.000s, Erase: 0.501s, Program: 0.000s, Verify: 0.000s, Restore: 0.037s)
    66. J-Link: Flash download: Total time needed: 1.155s (Prepare: 0.055s, Compare: 0.000s, Erase: 1.062s, Program: 0.000s, Verify: 0.000s, Restore: 0.037s)
    67. Erasing done.
    68. // full chip erase worked
    69. J-Link>mem 08080000 80
    70. 08080000 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    71. 08080010 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    72. 08080020 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    73. 08080030 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    74. 08080040 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    75. 08080050 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    76. 08080060 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    77. 08080070 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    78. J-Link>exit
    Display All

    And attached is the log file in case it shows something helpful: just-erase.gd32f303zk.jlink.log

    The post was edited 4 times, last by mwb1100 ().

  • I just tried updating the J-Link software package to v7.70e. This also resulted in the J-Link's firmware being updated.

    The test performs exactly the same (except that the erase command does a reset now as explained in the release notes).

    The erase of a memory range still doesn't actually do the erase.

    Source Code

    1. SEGGER J-Link Commander V7.70e (Compiled Aug 31 2022 17:08:13)
    2. DLL version V7.70e, compiled Aug 31 2022 17:06:50
    3. Connecting to J-Link via USB...O.K.
    4. Firmware: J-Link V10 compiled Jul 22 2022 11:40:29
    5. Hardware version: V10.10
    6. J-Link uptime (since boot): N/A (Not supported by this model)
    7. S/N: 600105391
    8. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    9. VTref=3.315V
    10. Device "GD32F303ZK" selected.
    Display All
  • One more clue:

    Since the GD32F303 is a close clone of the STM32F103 (it's actually more like the STM32F103 than it is the STM32F303), I tried connecting to it as an STM32F103ZG. The F103ZG has 1MB of flash so the flash pages in the range 0x08080000 - 0x08081ffff exist on that chip. Telling jlink.exe that it's connecting to an STM32F103ZG makes the erase operation work:

    C Source Code

    1. C:\temp>jlink -device STM32F103ZG -if swd -speed 4000 -autoconnect 1
    2. SEGGER J-Link Commander V7.66g (Compiled Jul 7 2022 10:37:30)
    3. DLL version V7.66g, compiled Jul 7 2022 10:35:46
    4. Connecting to J-Link via USB...O.K.
    5. Firmware: J-Link V10 compiled Jul 22 2022 11:40:29
    6. Hardware version: V10.10
    7. S/N: 600105391
    8. License(s): RDI, FlashBP, FlashDL, JFlash, GDB
    9. VTref=3.310V
    10. Device "STM32F103ZG" selected.
    11. Connecting to target via SWD
    12. InitTarget() start
    13. InitTarget() end
    14. Found SW-DP with ID 0x2BA01477
    15. DPIDR: 0x2BA01477
    16. CoreSight SoC-400 or earlier
    17. Scanning AP map to find all available APs
    18. AP[1]: Stopped AP scan as end of AP map has been reached
    19. AP[0]: AHB-AP (IDR: 0x24770011)
    20. Iterating through AP map to find AHB-AP to use
    21. AP[0]: Core found
    22. AP[0]: AHB-AP ROM base: 0xE00FF000
    23. CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
    24. Found Cortex-M4 r0p1, Little endian.
    25. Identified core does not match configuration. (Found: Cortex-M4, Configured: Cortex-M3)
    26. FPUnit: 6 code (BP) slots and 2 literal slots
    27. CoreSight components:
    28. ROMTbl[0] @ E00FF000
    29. [0][0]: E000E000 CID B105E00D PID 000BB00C SCS-M7
    30. [0][1]: E0001000 CID B105E00D PID 003BB002 DWT
    31. [0][2]: E0002000 CID B105E00D PID 002BB003 FPB
    32. [0][3]: E0000000 CID B105E00D PID 003BB001 ITM
    33. [0][4]: E0040000 CID B105900D PID 000BB9A1 TPIU
    34. [0][5]: E0041000 CID 00000000 PID 00000000 ???
    35. Cortex-M4 identified.
    36. J-Link>mem 08080000 80
    37. 08080000 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    38. 08080010 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    39. 08080020 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    40. 08080030 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    41. 08080040 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    42. 08080050 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    43. 08080060 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    44. 08080070 = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
    45. J-Link>erase 08080000 08081fff
    46. T-bit of XPSR is 0 but should be 1. Changed to 1.
    47. Erasing selected range...
    48. J-Link: Flash download: Total time needed: 0.288s (Prepare: 0.102s, Compare: 0.000s, Erase: 0.172s, Program: 0.000s, Verify: 0.000s, Restore: 0.014s)
    49. J-Link: Flash download:
    50. Flash sectors within Range [0x08080000 - 0x08081FFF] deleted.
    51. Erasing done.
    52. J-Link>mem 08080000 80
    53. 08080000 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    54. 08080010 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    55. 08080020 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    56. 08080030 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    57. 08080040 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    58. 08080050 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    59. 08080060 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    60. 08080070 = FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ................
    61. J-Link>exit
    Display All

    However, I don't think that's an effective workaround for me because this project uses flash up to the end of the GD32F303ZK's flash address space to store data so I need the tool to be able to write to flash above the 1MB limit that the STM32F103ZG has.
  • We reproduced and fixed the issue.
    The fix is already available in the newly released J-Link V7.80.

    Could you give the latest version a try?

    BR
    Sebastian
    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.