aappleby / picorvd

GDB-compatible RISC-V Debugger for CH32V003 that runs on a Raspberry Pi Pico
MIT License
173 stars 11 forks source link

No communication with CH32V003 dev board #5

Closed krasin closed 1 year ago

krasin commented 1 year ago

Hi @aappleby,

thank you for working on this! Very exciting to have all bits of the CH32V003 toolchain open-source.

I am trying to flash the blinky example. I connected my Raspberry Pi Pico dev board over USB, and I made 3 connections between the CH32V003 dev board and the Pico:

  1. GND
  2. 3V3
  3. GP28 on the Raspberry Pi Pico -> D1 on CH32V003 with a 1 KOhm pull-up resistor to 3V3.

I built picorvd.uf2 and flashed it on PicoRVD using USB mass storage device method:

~/src/github.com/aapleby/PicoRVD$ ls -lh bin/picorvd.uf2 
-rw-rw-r-- 1 krasin krasin 184K May 14 15:48 bin/picorvd.uf2

I rebooted my RP2040 dev board and then built and tried to flash the blinky example:

krasin@localhost:~/src/github.com/aapleby/PicoRVD/example$ ./build.sh 
   text    data     bss     dec     hex filename
   7856       0     260    8116    1fb4 bin/blink.elf
krasin@localhost:~/src/github.com/aapleby/PicoRVD/example$ ./flash.sh
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from bin/blink.elf...
Ignoring packet error, continuing...
warning: unrecognized item "timeout" in "qSupported" response
Ignoring packet error, continuing...
gdbinit:2: Error in sourced command file:
Remote replied unexpectedly to 'vMustReplyEmpty': timeout
You can't do that when your target is `exec'
The program is not being run.

Can you please confirm that my wiring setup sounds correct? I would also appreciate any hints about how to debug the current problem.

Thanks again for publishing PicoRVD!

krasin commented 1 year ago

I would also like to add that I am able to successfully flash firmware on the CV32V003 dev board using the provided proprietary dongle, so the board itself is fine.

krasin commented 1 year ago

I started to dive into it and documenting my progress here (I hope that's okay!), so that it's easier for others who might hit the same issue to see what's happening.

In short, I made two small changes to the PicoRVD firmware:

  1. I added swio->dump() in main.cpp right after it's initialized:

    printf_g("// Starting PicoSWIO\n");
    PicoSWIO* swio = new PicoSWIO();
    swio->reset(PIN_SWIO);
    printf_g("// Let's dump SWIO\n");
    swio->dump();
    printf_g("// SWIO dumped!\n");
  2. I uncommented #define DUMP_COMMANDS in PicoSWIO.cpp.

This is the output that I get over the USB serial connected to pins 0 and 1 of the Raspberry Pi Pico, when it's just starting up (no gdb call is in progress):

//==============================================================================
// PicoRVD 0.0.2

// Starting PicoSWIO
set_dbg WCH_DM_SHDWCFGR 0x5aa50400
set_dbg     WCH_DM_CFGR 0x5aa50400
set_dbg    DM_DMCONTROL 0x00000000
set_dbg    DM_DMCONTROL 0x00000001
// Let's dump SWIO
get_dbg     WCH_DM_CPBR 0x00000000
DM_CPBR = 0x00000000
  TDIV:0  SOPN:0  CHECKSTA:0  CMDEXTENSTA:0  OUTSTA:0  IOMODE:0  VERSION:0
get_dbg     WCH_DM_CFGR 0x00000000
DM_CFGR = 0x00000000
  TDIVCFG:0  SOPNCFG:0  CHECKEN:0  CMDEXTEN:0  OUTEN:0  IOMODECFG:0  KEY:0x0000
get_dbg WCH_DM_SHDWCFGR 0x00000000
DM_SHDWCFGR = 0x00000000
  TDIVCFG:0  SOPNCFG:0  CHECKEN:0  CMDEXTEN:0  OUTEN:0  IOMODECFG:0  KEY:0x0000
get_dbg     WCH_DM_PART 0x00000000
DM_PARTID = 0x00000000
// SWIO dumped!
// Starting RVDebug
// Starting WCHFlash
// Starting SoftBreak
get_dbg     DM_DMSTATUS 0x00000000
// Starting GDBServer
// Starting Console
// Everything up and running!

Based on the fact that SWIO dump is all zeros, I suspect that it's not really reading anything from the CH32V003 microcontroller, which would then explain timeouts while trying to flash it with GDB.

I plan to continue diving into it and will update the thread if I find anything useful / noteworthy. But at least, the problem is isolated to RP2040 -> CH32V003 communications; it's not a GDB problem and it's not a PC->RP2040 communication problem.

aappleby commented 1 year ago

Hi, I've been busy with some family medical stuff recently, I'll try and follow up on this later this week.

On Sun, May 21, 2023 at 6:42 PM Ivan Krasin @.***> wrote:

I started to dive into it and documenting my progress here (I hope that's okay!), so that it's easier for others who might hit the same issue to see what's happening.

In short, I made two small changes to the PicoRVD firmware:

1.

I added swio->dump() in main.cpp right after it's initialized:

printf_g("// Starting PicoSWIO\n"); PicoSWIO* swio = new PicoSWIO(); swio->reset(PIN_SWIO); printf_g("// Let's dump SWIO\n"); swio->dump(); printf_g("// SWIO dumped!\n");

2.

I uncommented #define DUMP_COMMANDS in PicoSWIO.cpp.

This is the output that I get over the USB serial connected to pins 0 and 1 of the Raspberry Pi Pico, when it's just starting up (no gdb call is in progress):

//============================================================================== // PicoRVD 0.0.2

// Starting PicoSWIO set_dbg WCH_DM_SHDWCFGR 0x5aa50400 set_dbg WCH_DM_CFGR 0x5aa50400 set_dbg DM_DMCONTROL 0x00000000 set_dbg DM_DMCONTROL 0x00000001 // Let's dump SWIO get_dbg WCH_DM_CPBR 0x00000000 DM_CPBR = 0x00000000 TDIV:0 SOPN:0 CHECKSTA:0 CMDEXTENSTA:0 OUTSTA:0 IOMODE:0 VERSION:0 get_dbg WCH_DM_CFGR 0x00000000 DM_CFGR = 0x00000000 TDIVCFG:0 SOPNCFG:0 CHECKEN:0 CMDEXTEN:0 OUTEN:0 IOMODECFG:0 KEY:0x0000 get_dbg WCH_DM_SHDWCFGR 0x00000000 DM_SHDWCFGR = 0x00000000 TDIVCFG:0 SOPNCFG:0 CHECKEN:0 CMDEXTEN:0 OUTEN:0 IOMODECFG:0 KEY:0x0000 get_dbg WCH_DM_PART 0x00000000 DM_PARTID = 0x00000000 // SWIO dumped! // Starting RVDebug // Starting WCHFlash // Starting SoftBreak get_dbg DM_DMSTATUS 0x00000000 // Starting GDBServer // Starting Console // Everything up and running!

Based on the fact that SWIO dump is all zeros, I suspect that it's not really reading anything from the CH32V003 microcontroller, which would then explain timeouts while trying to flash it with GDB.

I plan to continue diving into it and will update the thread if I find anything useful / noteworthy. But at least, the problem is isolated to RP2040 -> CH32V003 communications; it's not a GDB problem and it's not a PC->RP2040 communication problem.

— Reply to this email directly, view it on GitHub https://github.com/aappleby/PicoRVD/issues/5#issuecomment-1556388426, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAF6BH3DPOH5MCEWIAXWDDLXHLABNANCNFSM6AAAAAAYBN2GZ4 . You are receiving this because you were mentioned.Message ID: @.***>

krasin commented 1 year ago

Hi Austin,

I appreciate a response and please, no worries about this ticket - family medical stuff is way more important.

My current plan is to try esp32s2-funprog and then depending on whether it is successful, take the next steps in debugging my issue.

Btw, your code is very clean and a pleasure to read.

Good luck to you and your family in the current matter, krasin

krasin commented 1 year ago

I was able to get flashing working:

~/src/github.com/aapleby/PicoRVD/example$ ./flash.sh 
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from bin/blink.elf...
InterruptVector () at ch32v003fun.c:717
717             asm volatile( "\n\
Loading section .init, size 0xa0 lma 0x0
Loading section .text, size 0x1e10 lma 0xa0
Start address 0x00000000, load size 7856
Transfer rate: 181 bytes/sec, 3928 bytes/write.
Detaching from program: /home/krasin/src/github.com/aapleby/PicoRVD/example/bin/blink.elf, Remote target
[Inferior 1 (Remote target) detached]

The problem was me - I got lazy and didn't look up the pinout for Raspberry Pi Pico and wrongly connected to the board pin 28 (GND) instead of to the board pin 34 (GP28). Oops!

Now, flashing works, but the blink example from this repo does not. I had to flash blink.elf from ch32v003fun. I also have to reboot the ch32v003 manually, as the flash.sh would not do that. But that's all very minor.

Very excited - I achieved my goal for this weekend, even though it's a bit of Monday already. :)

Thanks again for working on this!