[GD32VF103][Platformio][Jlink] Przerabiamy skrypty żeby olać OpenOCD

Płytki i moduły oparte o układy Giga Device GD32VF103
ODPOWIEDZ
Awatar użytkownika
Nefarious19
Magyster
Posty: 46
Rejestracja: 17 paź 2017, 10:03
Lokalizacja: Łódź

[GD32VF103][Platformio][Jlink] Przerabiamy skrypty żeby olać OpenOCD

Post autor: Nefarious19 » 22 mar 2020, 20:09

Siemanko,

Lubie open source, używam Linuxa, ale to jak działa OpenOCD z Jlinkiem woła o pomste do nieba.

Przysłowie mówi: szukajcie a znajdziecie. Ja znalazałem to czego potrzebowałem w dokumentacji Platformio oraz JLink'a.

1. Wgrywanie softu

Żeby nasz soft wgrywał się po ludzku i sam startował należy w pliku platformio.ini w pole upload_protocol dać wartość custom, o tak:

Kod: Zaznacz cały

upload_protocol = custom
dodatkowo, musimy napisać mały skrypcik w pytongu który w locie podmieni nam komendę dla jlinka.

W platformio.ini dodajemy linijke:

Kod: Zaznacz cały

extra_scripts = extra_script.py
w tym samym miejscu gdzie mamy plik platformio.ini tworzymy skrypt o podanej nazwie i wypełniamy go zawartością:

Kod: Zaznacz cały

from os import makedirs
from os.path import isdir, join
Import('env')

def _jlink_cmd_script(env, source):
    build_dir = env.subst("$BUILD_DIR")
    if not isdir(build_dir):
        makedirs(build_dir)
    dupa = str(source)
    # dupa = dupa.replace(".bin", ".hex")
    script_path = join(build_dir, "upload.jlink")
    commands = ["rx 10\n", "erase\n", "loadbin %s,0x08000000\n" % dupa, "rx 10\n", "setPC 0x08000000\n", "go\n", "q\n"]
    with open(script_path, "w") as fp:
        fp.truncate(0)
        for x in commands:
            fp.write(x)
    return script_path

env.Replace(
    __jlink_cmd_script=_jlink_cmd_script,
    UPLOADER="C:/Program Files (x86)/SEGGER/JLink/JLink.exe",
    UPLOADERFLAGS=[
        "-device", "GD32VF103CBT6",
        "-speed", "2000",
        "-if", "jtag",
        "-autoconnect", "1"
    ],
    UPLOADCMD='"$UPLOADER" $UPLOADERFLAGS -CommandFile ${__jlink_cmd_script(__env__, SOURCE)}')
Teraz możemy wypróbowac nasz skrypcik. Zapsiujemy wszystko, zamykamy terminale, i dajemy Upload na dolnym pasku platformio.

Kod: Zaznacz cały

> Executing task in folder adasd: C:\Users\rafal\.platformio\penv\Scripts\platformio.exe run --target upload <


Processing jlink_debug_and_upload (platform: gd32v; board: sipeed-longan-nano; framework: gd32vf103-sdk)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/gd32v/sipeed-longan-nano.html
PLATFORM: GigaDevice GD32V 1.1.2 > Sipeed Longan Nano
HARDWARE: GD32VF103CBT6 108MHz, 32KB RAM, 128KB Flash
DEBUG: Current (custom) External (altera-usb-blaster, gd-link, jlink, rv-link, sipeed-rv-debugger, um232h)
PACKAGES:
 - framework-gd32vf103-sdk 1.0.0
 - tool-gd32vflash 0.1.0
 - tool-openocd-gd32v 0.1.1
 - toolchain-gd32v 9.2.0
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\jlink_debug_and_upload\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.1% (used 2314 bytes from 32768 bytes)
Flash: [=         ]   5.6% (used 7278 bytes from 131072 bytes)
Configuring upload protocol...
AVAILABLE: altera-usb-blaster, custom, gd-link, jlink, rv-link, serial, sipeed-rv-debugger, um232h
CURRENT: upload_protocol = custom
Uploading .pio\build\jlink_debug_and_upload\firmware.bin
C:/Users/rafal/.platformio/penv/Scripts/activate.bat
SEGGER J-Link Commander V6.64b (Compiled Mar 20 2020 10:05:37)
DLL version V6.64b, compiled Mar 20 2020 10:05:06


J-Link Command File read successfully.
Processing script file...

J-Link connection not established yet but required for command.
Connecting to J-Link via USB...O.K.
Firmware: J-Link V10 compiled Mar 18 2020 17:38:51
Hardware version: V10.10
S/N: 260108299
License(s): FlashBP, GDB
OEM: SEGGER-EDU
VTref=3.322V
Target connection not established yet but required for command.
Device position in JTAG chain (IRPre,DRPre) <Default>: -1,-1 => Auto-detect
JTAGConf>ERROR while parsing value for IRPre. Using default: -1.
ERROR while parsing value for DRPre. Using default: -1.
Device "GD32VF103CBT6" selected.


Connecting to target via JTAG
ConfigTargetSettings() start
Selecting reset pin as default strategy for this device because <ndmreset> bit does not work properly.
ConfigTargetSettings() end
TotalIRLen = 10, IRPrint = 0x0021
JTAG chain detection found 2 devices:
 #0 Id: 0x1000563D, IRLen: 05, RV32
 #1 Id: 0x790007A3, IRLen: 05, Unknown device
Debug architecture:
  RISC-V debug: 0.13
  AddrBits: 7
  DataBits: 32
  IdleClks: 7
Memory access:
  Via system bus: No
  Via ProgBuf: Yes (2 ProgBuf entries)
DataBuf: 4 entries
  autoexec[0] implemented: Yes
Detected: RV32 core
CSR access via abs. commands: No
Temp. halted CPU for NumHWBP detection
HW instruction/data BPs: 4
Support set/clr BPs while running: No
HW data BPs trigger before execution of inst
RISC-V identified.
Reset delay: 10 ms
Reset type Normal: Resets core & peripherals using <ndmreset> bit in <dmcontrol> debug register.
RISC-V: Performing reset via reset pin

Erasing device...
J-Link: Flash download: Total time needed: 0.572s (Prepare: 0.250s, Compare: 0.000s, Erase: 0.304s, Program: 0.000s, Verify: 0.000s, Restore: 0.017s)
Erasing done.

Downloading file [.pio\build\jlink_debug_and_upload\firmware.bin]...
J-Link: Flash download: Bank 0 @ 0x08000000: 1 range affected (8192 bytes)
J-Link: Flash download: Total time needed: 1.033s (Prepare: 0.559s, Compare: 0.078s, Erase: 0.134s, Program: 0.175s, Verify: 0.019s, Restore: 0.065s)
O.K.

Reset delay: 10 ms
Reset type Normal: Resets core & peripherals using <ndmreset> bit in <dmcontrol> debug register.
RISC-V: Performing reset via reset pin




Script processing completed.

================================================================================================================ [SUCCESS] Took 5.25 seconds ================================================================================================================ 

Terminal will be reused by tasks, press any key to close it.
V'oila, FW wgrany błyskawicznie!
► Pokaż Spoiler

2. Debugowanie

Tu sprawa wygląda tak że nie będziemy używać protezy OpenOCD która nie widzi JLinka pod Windowsem na natywnym Driverze. Żeby widziała trzeba zadigiem podmienić stery na libusub0-win32 ale jest to ble i niedobre. Cóż więc począć? Odpalić natywny GDB server jlinka który rozumie niektóre komendy zgoła inaczej więc trzeba to jakoś potłumaczyć. Nie wdając się w szczegóły, dodajemy do pliku platformio.ini takie oto dodatkowe linijki:

Kod: Zaznacz cały

debug_tool = custom
debug_server = 
    C:\Program Files (x86)\SEGGER\JLink\JLinkGDBServerCL.exe 
    -singlerun 
    -if 
    JTAG 
    -select 
    USB 
    -port 
    2331 
    -device 
    GD32VF103CBT6
    -speed
    4000

debug_init_cmds =
  define pio_reset_halt_target
      monitor reset
      monitor halt
  end
  define pio_reset_run_target
      monitor clrbp
      monitor reset
      monitor go
  end
  target extended-remote $DEBUG_PORT
  monitor clrbp
  monitor speed auto
  pio_reset_halt_target
  $LOAD_CMDS
  $INIT_BREAK
  break main

debug_extra_cmds = set $pc=0x08000000
cały nasz plik platformio.ini powinien tak wyglądać:

Kod: Zaznacz cały

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:jlink_debug_and_upload]
platform = gd32v
board = sipeed-longan-nano
framework = gd32vf103-sdk
extra_scripts = extra_script.py
upload_protocol = custom
debug_tool = custom
debug_server = 
    C:\Program Files (x86)\SEGGER\JLink\JLinkGDBServerCL.exe 
    -singlerun 
    -if 
    JTAG 
    -select 
    USB 
    -port 
    2331 
    -device 
    GD32VF103CBT6
    -speed
    4000

debug_init_cmds =
  define pio_reset_halt_target
      monitor reset
      monitor halt
  end
  define pio_reset_run_target
      monitor clrbp
      monitor reset
      monitor go
  end
  target extended-remote $DEBUG_PORT
  monitor clrbp
  monitor speed auto
  pio_reset_halt_target
  $LOAD_CMDS
  $INIT_BREAK
  break main

debug_extra_cmds = set $pc=0x08000000
zapisujemy, dajemy debug i po chwili cichej modlitwy widzimy taki obraz:
Obrazek

no i możemy debugować jak dzicy. Powodzonka!

ODPOWIEDZ

Wróć do „GD32VF103”