cnlohr / ch32v003fun

Open source minimal stack for the ch32 line of WCH processors, including the ch32v003, a 10¢ 48 MHz RISC-V Microcontroller - as well as many other chips within the ch32v/x line.
MIT License
893 stars 142 forks source link

Can't load, set up breakpoint, reset from GDB + minichlink #201

Open maxgerhardt opened 1 year ago

maxgerhardt commented 1 year ago

Very simple example: Load a blinky.elf file into the chip, make it start from 0 and break in the main() function. Chip: CH32V003, Debugger: WCH-Link(E), firmware version 2.9 (latest).

I can't get this to work at all. Producure:

  1. Erase chp (minichlink -E)
  2. Unplug and replug minichlink adapter
  3. Start minichlink in GDB mode (minichlink -G)
  4. Tell GDB to connect to the GDB server, load the binary, rest + halt the chip, set up the breakpoint, continue by using
riscv-none-embed-gdb.exe -x my_gdbinit.txt  firmware.elf

with my_gdbinit.txt

target extended-remote :2000
load
monitor reset
break main
continue

Output:

GNU gdb (xPack GNU RISC-V Embedded GCC, 32-bit) 8.3
Copyright (C) 2019 Free Software Foundation, Inc.
[...]

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from C:\Users\Max\Desktop\ch32_dev\platform-ch32v\examples\blinky-none-os\.pio\build\ch32v003f4p6_evt_r0\firmware.elf...
_start () at C:\Users\Max\.platformio\packages\framework-wch-noneos-sdk\Startup\startup_ch32v00x.S:18
18          j       handle_reset
Loading section .init, size 0xa0 lma 0x0
Loading section .text, size 0x570 lma 0xc0
Loading section .data, size 0x18 lma 0x630
Start address 0x0, load size 1576
Transfer rate: 2 KB/sec, 525 bytes/write.
Breakpoint 1 at 0x482: file src\main.c, line 31.
Note: automatically using hardware breakpoints for read-only addresses.
<HANGS FOREVER HERE>

On minichlink side:

>minichlink.exe -G
Found WCH Link
WCH Programmer is LinkE version 2.9
Chip Type: 003
Setup success
Flash Storage: 16 kB
Part UUID    : ab-c8-ab-cd-13-de-bc-49
PFlags       : ff-ff-ff-ff
Part Type (B): 07-13-bb-91
Read protection: disabled
Interface Setup
GDBServer Running
UNKNOWN PACKET: 3 (!#)
23 00 73
Write
LEN: 00000000 176 160 o
Write OK
ESCAPED @ 25 -> * [42]
ESCAPED @ 49 -> } [125]
ESCAPED @ 119 -> # [35]
ESCAPED @ 135 -> # [35]
ESCAPED @ 2b1 -> # [35]
ESCAPED @ 2c3 -> # [35]
ESCAPED @ 2de -> # [35]
ESCAPED @ 30b -> # [35]
ESCAPED @ 32b -> # [35]
ESCAPED @ 33d -> # [35]
ESCAPED @ 373 -> # [35]
ESCAPED @ 38b -> # [35]
ESCAPED @ 3ad -> # [35]
ESCAPED @ 3ae -> $ [36]
ESCAPED @ 3ec -> * [42]
ESCAPED @ 432 -> # [35]
ESCAPED @ 481 -> # [35]
ESCAPED @ 490 -> # [35]
ESCAPED @ 4eb -> # [35]
ESCAPED @ 4ff -> # [35]
ESCAPED @ 51d -> } [125]
ESCAPED @ 55d -> $ [36]
ESCAPED @ 56f -> $ [36]
Write
LEN: 000000c0 1409 1392 Q
Write OK
Write
LEN: 00000630 42 24 
Write OK

When I Ctrl+C in GDB:

Program received signal SIGTRAP, Trace/breakpoint trap.
HardFault_Handler () at src\main.c:61
61              }
(gdb) where
#0  HardFault_Handler () at src\main.c:61
#1  0x00000212 in handle_reset ()
    at C:\Users\Max\.platformio\packages\framework-wch-noneos-sdk\Startup\startup_ch32v00x.S:170

it just hangs in the hardfault handler there.

Note that when I then exit minichlink and just do minichlink.exe -b to reboot the chip, the LED does blink normally.

So something is wrong with the reset procedure?

Firmware is attached here, blinky on PC1: firmware.zip

cnlohr commented 1 year ago

Now that I am getting back into the swing of things, I can take another look at this. That said, I really don't ever use/like GDB, so it's been hard getting myself to actually test and make this move forward.