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
971 stars 148 forks source link

blink not working on WeAct Studio CH32X035F8U6 board #412

Closed nanikjava closed 1 month ago

nanikjava commented 1 month ago

Hi,

I've got this board and compiled ch32v003fun/examples_x035/blink/blink.c modified the code a bit like the following:

#include "ch32v003fun.h"
#include <stdio.h>

int main()
{
    SystemInit();

    funGpioInitAll();

    funPinMode( PB12, GPIO_CFGLR_OUT_10Mhz_PP );

    while(1)
    {
        funDigitalWrite( PB12, FUN_HIGH );   // Turn on GPIO
        Delay_Ms( 1000 );
        funDigitalWrite( PB12, FUN_LOW );    // Turn off GPIO
        Delay_Ms( 1000 );
    }
}

I've cross-compiled the toolchain on my Linux machine and was able to compile the sample code and got the following output:

riscv32-unknown-elf-gcc -E -P -x c -DTARGET_MCU=CH32X035 -DMCU_PACKAGE=1 -DTARGET_MCU_LD=4       -DTARGET_MCU_MEMORY_SPLIT= ../../ch32v003fun//ch32v003fun.ld > ../../ch32v003fun//generated_CH32X035F8U6_.ld
riscv32-unknown-elf-gcc -o blink.elf ../../ch32v003fun//ch32v003fun.c blink.c   -g -Os -flto -ffunction-sections -fdata-sections -fmessage-length=0 -msmall-data-limit=8 -march=rv32imac -mabi=ilp32 -DCH32X03x=1 -static-libgcc -I/usr/include/newlib -I../../ch32v003fun//../extralibs -I../../ch32v003fun/ -nostdlib -I. -Wall  -Wl,--print-memory-usage -lgcc -T ../../ch32v003fun//generated_CH32X035F8U6_.ld -Wl,--gc-sections
Memory region         Used Size  Region Size  %age Used
           FLASH:         524 B        62 KB      0.83%
             RAM:           0 B        20 KB      0.00%
riscv32-unknown-elf-objdump -S blink.elf > blink.lst
riscv32-unknown-elf-objdump -t blink.elf > blink.map
riscv32-unknown-elf-objcopy -O binary blink.elf blink.bin
riscv32-unknown-elf-objcopy -O ihex blink.elf blink.hex
make -C ../../ch32v003fun//../minichlink all
make[1]: Entering directory '/home/nanik/Downloads/devboards/ch32v003fun/minichlink'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/nanik/Downloads/devboards/ch32v003fun/minichlink'
../../ch32v003fun//../minichlink/minichlink -w blink.bin flash -b
WARNING: You are not in the plugdev group, the canned udev rules will not work on your system.
Error: Could not initialize any supported programmers
Error: Could not initialize any supported programmers
make: *** [../../ch32v003fun/ch32v003fun.mk:241: cv_flash] Error 224

The changes made was the pin PB12 as per the schematic the LED is at PB12 (not sure if I did it right or wrong, appreciate any feedback) on the code.

I'm using the wchisp project to flash the board using the following command

wchisp  -v flash ./blink.elf -V

which I got the following response

12:28:45 [INFO] Opening USB device #0
12:28:45 [DEBUG] (1) wchisp::transport::usb: Found USB Device Bus 001 Device 085: ID 1a86:55e0
12:28:45 [DEBUG] (1) wchisp::transport: => a11200   00004d4355204953502026205743482e434e
12:28:45 [DEBUG] (1) wchisp::transport: <= a1000200 5e23
12:28:45 [DEBUG] (1) wchisp::transport: => a11200   00004d4355204953502026205743482e434e
12:28:45 [DEBUG] (1) wchisp::transport: <= a1000200 5e23
12:28:45 [DEBUG] (1) wchisp::flashing: found chip: CH32X035F8U6[0x5e23]
12:28:45 [DEBUG] (1) wchisp::transport: => a70200   1f00
12:28:45 [DEBUG] (1) wchisp::transport: <= a7001a00 1f00ff001fe000ff00ffffffffff00020600cdaba1716cbcdad9
12:28:45 [DEBUG] (1) wchisp::flashing: read_config: ff001fe000ff00ffffffffff00020600cdaba1716cbcdad9
12:28:45 [INFO] Chip: CH32X035F8U6[0x5e23] (Code Flash: 64KiB)
12:28:45 [INFO] Chip UID: CD-AB-A1-71-6C-BC-DA-D9
12:28:45 [INFO] BTVER(bootloader ver): 02.60
12:28:45 [DEBUG] (1) wchisp::transport: => a70200   1f00
12:28:45 [DEBUG] (1) wchisp::transport: <= a7001a00 1f00ff001fe000ff00ffffffffff00020600cdaba1716cbcdad9
12:28:45 [INFO] Current config registers: ff001fe000ff00ffffffffff00020600cdaba1716cbcdad9
RDPR_USER: 0xE01F00FF
  [7:0]   RDPR 0xFF (0b11111111)
    `- Protected
  [16:16] IWDG_SW 0x1 (0b1)
    `- IWDG enabled by the software, and disabled by hardware
  [17:17] STOP_RST 0x1 (0b1)
    `- Disable
  [18:18] STANDBY_RST 0x1 (0b1)
    `- Disable, entering standby-mode without RST
  [20:19] RST_MOD 0x3 (0b11)
    `- Disable RST alternative function, use PA21/PC3/PB7 as GPIO
DATA: 0xFF00FF00
  [7:0]   DATA0 0x0 (0b0)
  [23:16] DATA1 0x0 (0b0)
WRP: 0xFFFFFFFF
  `- Unprotected
12:28:45 [INFO] Read /home/nanik/Downloads/devboards/ch32v003fun/examples_x035/blink/blink.elf as ELF format
12:28:45 [INFO] Found loadable segment, physical address: 0x00000000, virtual address: 0x00000000, flags: 0x5
12:28:45 [DEBUG] (1) wchisp::format: Matching section: ".init" offset: 0x1000 size: 0xe0
12:28:45 [DEBUG] (1) wchisp::format: Matching section: ".text" offset: 0x10e0 size: 0x12c
12:28:45 [INFO] Section names: [".init", ".text"]
12:28:45 [DEBUG] (1) wchisp::format: found 1 sections
12:28:45 [INFO] Firmware size: 1024
12:28:45 [INFO] Erasing...
12:28:45 [WARN] erase_code: set min number of erased sectors to 8
12:28:45 [DEBUG] (1) wchisp::transport: => a40400   08000000
12:28:45 [DEBUG] (1) wchisp::transport: <= a4000200 0000
12:28:45 [INFO] Erased 8 code flash sectors
12:28:46 [INFO] Erase done
12:28:46 [INFO] Writing to code flash...
12:28:46 [DEBUG] (1) wchisp::transport: => a31e00   000000000000000000000000000000000000000000000000000000000000
12:28:46 [DEBUG] (1) wchisp::transport: <= a3000200 8600
12:28:46 [DEBUG] (1) wchisp::transport: => a53d00   000000005b0a65656b656565c303646565016465c365656565016465c365656565656565c301646565016465c365656565656565c301646565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
█████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 56/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   38000000cc01646565656565c301646565016465c301646565016465c301646565016465c301646565016465c301646565016465c301646565016465c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
███████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 112/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   700000008701646565016465c301646565016465c301646565016465c301646565016465c301646565016465c301646565016465c301646565016465c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
█████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 168/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   a80000004801646565016465c301646565016465c301646565016465c301646565016465c301646565016465c301646565016465c301646565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
███████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 224/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   e000000028f2646545f6e464b1723465457664e432f667e56d16c567f3c82716f527e5e885726065657660603330e81675305572c665457660408af2c6
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
█████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 280/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   18010000376545f6e0c08b64850633d06575a474c6862bd09b7660a5e3f2606545f6e0602e726365457663e32f06eda0657124f1027460f46086ffa03d
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
███████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 336/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   5001000060d29265856022fd00f662057f16f572f71665455564c552d467257922d263e5c3b0ea79a2d24267836c22fda6d272678352626d6546c762c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
█████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 392/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   88010000307662726dfda652c4fa65fda2d262652346c7625d766265cb46c5825de7e524d263a38c5252726783792ad263949a98d5f682b26479aa52d4
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
███████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 448/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   c0010000846425766222a57980600d52606575904ca403b0ea79a6d2553e65d2726425f64462a552926585f64563bd46cd62646984d3f0752268eb868d
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
█████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 504/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   f8010000ff639bedae6922d356752268eb862b633da8d26565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
██████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 560/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   300200005065656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 616/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   680200005b65656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 672/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   a00200005d65656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 728/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   d80200001665656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 784/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   100300004265656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 840/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   480300004965656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░ 896/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   800300002265656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░░░░░░░░░░░ 952/102412:28:46 [DEBUG] (1) wchisp::transport: => a53d00   b80300008465656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████░░░ 1008/102412:28:46 [DEBUG] (1) wchisp::transport: => a51500   f00300006e65656565656565c365656565656565c3
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ 1024/102412:28:46 [DEBUG] (1) wchisp::transport: => a50500   0004000086
12:28:46 [DEBUG] (1) wchisp::transport: <= a5000200 0000
████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ 1024/102412:28:46 [INFO] Code flash 1024 bytes written
12:28:47 [WARN] Skipping verify
12:28:47 [INFO] Now reset device and skip any communication errors
12:28:47 [DEBUG] (1) wchisp::transport: => a20100   01
12:28:47 [DEBUG] (1) wchisp::transport: <= a2000200 0000
12:28:47 [INFO] Device reset

The problem is the LED is not flashing as per what the code is doing

cnlohr commented 1 month ago

Would you mind trying to use minichlink to flash instead? There is a canned windows .exe, and if you add yourself to the plugdev group and use sudo make install_udev_rules you can try that.

nanikjava commented 1 month ago

Would you mind trying to use minichlink to flash instead? There is a canned windows .exe, and if you add yourself to the plugdev group and use sudo make install_udev_rules you can try that.

I'm using Linux machine does it has Linux version ?

cnlohr commented 1 month ago

Yes. make by default will flash the part as long as your user is allowed to talk to the device.

nanikjava commented 1 month ago

Running make it shows the following errors

WARNING: You are not in the plugdev group, the canned udev rules will not work on your system.
Error: Could not initialize any supported programmers
Error: Could not initialize any supported programmers

The board is connected only via USB there is no external programmer available btw

TommyMurphyTM1234 commented 1 month ago

Running make it shows the following errors

WARNING: You are not in the plugdev group, the canned udev rules will not work on your system.
Error: Could not initialize any supported programmers
Error: Could not initialize any supported programmers

The board is connected only via USB there is no external programmer available btw

You need to install the udev rules and add the user to the plugdev group as @cnlohr said here:

cnlohr commented 1 month ago

You need to add your user to plugdev,

sudo usermod -aG plugdev YOUR_USERNAME

If you get an error about plugdev not being a group, you may need to add it:

sudo groupadd plugdev

But you will definitely need to have the rules install.

sudo make install_udev_rules 

Only do this after your user is in the plugdev group.

You may need to log out and back in before the OS picks up you're in plugdev.

nanikjava commented 1 month ago

Thanks. I've followed the instruction and getting the following error.

riscv32-unknown-elf-gcc -E -P -x c -DTARGET_MCU=CH32X035 -DMCU_PACKAGE=1 -DTARGET_MCU_LD=4       -DTARGET_MCU_MEMORY_SPLIT= ../../ch32v003fun//ch32v003fun.ld > ../../ch32v003fun//generated_CH32X035F8U6_.ld
riscv32-unknown-elf-gcc -o blink.elf ../../ch32v003fun//ch32v003fun.c blink.c   -g -Os -flto -ffunction-sections -fdata-sections -fmessage-length=0 -msmall-data-limit=8 -march=rv32imac -mabi=ilp32 -DCH32X03x=1 -static-libgcc -I/usr/include/newlib -I../../ch32v003fun//../extralibs -I../../ch32v003fun/ -nostdlib -I. -Wall  -Wl,--print-memory-usage -lgcc -T ../../ch32v003fun//generated_CH32X035F8U6_.ld -Wl,--gc-sections
Memory region         Used Size  Region Size  %age Used
           FLASH:         592 B        62 KB      0.93%
             RAM:           0 B        20 KB      0.00%
riscv32-unknown-elf-objdump -S blink.elf > blink.lst
riscv32-unknown-elf-objdump -t blink.elf > blink.map
riscv32-unknown-elf-objcopy -O binary blink.elf blink.bin
riscv32-unknown-elf-objcopy -O ihex blink.elf blink.hex
make -C ../../ch32v003fun//../minichlink all
make[1]: Entering directory '/home/nanik/Downloads/devboards/ch32v003fun/minichlink'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/nanik/Downloads/devboards/ch32v003fun/minichlink'
../../ch32v003fun//../minichlink/minichlink -w blink.bin flash -b
Error: Could not initialize any supported programmers
Error: Could not initialize any supported programmers
make: *** [../../ch32v003fun/ch32v003fun.mk:241: cv_flash] Error 224
nanikjava commented 1 month ago

Following info from dmesg

[122202.795514] usb 1-2: new full-speed USB device number 107 using xhci_hcd
[122203.072929] usb 1-2: New USB device found, idVendor=1a86, idProduct=55e0, bcdDevice=26.00
[122203.072938] usb 1-2: New USB device strings: Mfr=0, Product=0, SerialNumber=0

and from lsusb

Bus 001 Device 108: ID 1a86:55e0 QinHeng Electronics 
cnlohr commented 1 month ago

What distro are you on @nanikjava ?

nanikjava commented 1 month ago

What distro are you on @nanikjava ?

Here you go

image

maxgerhardt commented 1 month ago

As I understand, minichlink does not yet have the capability to flash an CH32X035 via the built-in USB bootloader. It's for using a programmer like the WCH-LinkE. So best to modify the makefile to really invoke wchisp.