boards: wch: add support for ch32v003f4p6 development board

Added support for the CH32V003F4P6 Development Board, which
features QingKe RISC-V2A MCU.

Signed-off-by: Andrei-Edward Popa <andrei.popa105@yahoo.com>
This commit is contained in:
Andrei-Edward Popa 2025-03-13 20:14:15 +02:00 committed by Benjamin Cabé
commit b1833e8588
13 changed files with 276 additions and 0 deletions

View file

@ -0,0 +1,5 @@
# Copyright (c) 2025 Andrei-Edward Popa <andrei.popa105@yahoo.com>.
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources(board.c)

View file

@ -0,0 +1,5 @@
# Copyright (c) 2025 Andrei-Edward Popa
# SPDX-License-Identifier: Apache-2.0
config BOARD_CH32V003F4P6_DEV_BOARD
select BOARD_LATE_INIT_HOOK

View file

@ -0,0 +1,5 @@
# Copyright (c) 2025 Andrei-Edward Popa
# SPDX-License-Identifier: Apache-2.0
config BOARD_CH32V003F4P6_DEV_BOARD
select SOC_CH32V003

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2025 Andrei-Edward Popa <andrei.popa105@yahoo.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/init.h>
#include <zephyr/drivers/gpio.h>
#include <ch32fun.h>
#define AFIO_SWCFG_MASK 0x07000000
#define AFIO_SWCFG_SWD_OFF 0x04000000
void board_late_init_hook(void)
{
#if DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(gpiod))
const struct device *gpiod = DEVICE_DT_GET(DT_NODELABEL(gpiod));
uint32_t pcfr1;
/*
* PD1 is wired to led and to SWDIO pin.
* If PD1 is not put to ground, change the function to GPIO to use
* the led as a user led.
* If PD1 is put to ground, let the pin as SWDIO pin (default) to be
* able to program the board.
*/
if (gpio_pin_get(gpiod, 1) == 1) {
RCC->APB2PCENR |= RCC_AFIOEN;
pcfr1 = AFIO->PCFR1;
pcfr1 = (pcfr1 & ~AFIO_SWCFG_MASK) | (AFIO_SWCFG_SWD_OFF & AFIO_SWCFG_MASK);
AFIO->PCFR1 = pcfr1;
}
#endif
}

View file

@ -0,0 +1,8 @@
# Copyright (c) 2025 Andrei-Edward Popa
# SPDX-License-Identifier: Apache-2.0
board_runner_args(minichlink)
include(${ZEPHYR_BASE}/boards/common/minichlink.board.cmake)
board_runner_args(openocd "--use-elf" "--cmd-reset-halt" "halt")
include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)

View file

@ -0,0 +1,6 @@
board:
name: ch32v003f4p6_dev_board
full_name: WCH CH32V003F4P6 Development Board
vendor: wch
socs:
- name: ch32v003

View file

@ -0,0 +1,22 @@
/*
* Copyright (c) 2025 Andrei-Edward Popa <andrei.popa105@yahoo.com>
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/dt-bindings/pinctrl/ch32v003-pinctrl.h>
&pinctrl {
usart1_default: usart1_default {
group1 {
pinmux = <USART1_TX_PD5_0>;
output-high;
drive-push-pull;
slew-rate = "max-speed-10mhz";
};
group2 {
pinmux = <USART1_RX_PD6_0>;
bias-pull-up;
};
};
};

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2025 Andrei-Edward Popa <andrei.popa105@yahoo.com>
* SPDX-License-Identifier: Apache-2.0
*/
/dts-v1/;
#include <wch/ch32v0/ch32v003f4p.dtsi>
#include "ch32v003f4p6_dev_board-pinctrl.dtsi"
/ {
model = "ch32v003f4p6_dev_board";
compatible = "wch,ch32v003";
chosen {
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,console = &usart1;
zephyr,shell-uart = &usart1;
};
leds {
compatible = "gpio-leds";
red_led: led0 {
gpios = <&gpiod 1 GPIO_ACTIVE_LOW>;
label = "LED";
};
};
aliases {
led0 = &red_led;
};
};
&clk_hsi {
status = "okay";
};
&pll {
clocks = <&clk_hsi>;
status = "okay";
};
&rcc {
clocks = <&pll>;
};
&gpiod {
status = "okay";
};
&usart1 {
status = "okay";
current-speed = <115200>;
pinctrl-0 = <&usart1_default>;
pinctrl-names = "default";
};

View file

@ -0,0 +1,12 @@
identifier: ch32v003f4p6_dev_board
name: WCH CH32V003F4P6 Development Board
type: mcu
arch: riscv
toolchain:
- cross-compile
- zephyr
ram: 2
flash: 16
supported:
- gpio
- uart

View file

@ -0,0 +1,8 @@
# Copyright (c) 2025 Andrei-Edward Popa
# SPDX-License-Identifier: Apache-2.0
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View file

@ -0,0 +1,98 @@
.. zephyr:board:: ch32v003f4p6_dev_board
Overview
********
The `WCH`_ CH32V003F4P6 Development Board hardware provides support for
QingKe 32-bit RISC-V2A processor and the following devices:
* CLOCK
* :abbr:`GPIO (General Purpose Input Output)`
* :abbr:`NVIC (Nested Vectored Interrupt Controller)`
The board is equipped with one LED, wired to PD1 and SWIO pins.
The `WCH webpage on CH32V003`_ contains the processor's information
and the datasheet.
Hardware
********
The QingKe 32-bit RISC-V2A processor of the WCH CH32V003F4P6_DEV_BOARD is clocked
by an internal RC oscillator and runs at 24 MHz.
Supported Features
==================
.. zephyr:board-supported-hw::
Connections and IOs
===================
LED
---
* LED1 (red) = PD1
Programming and Debugging
*************************
Applications for the ``ch32v003f4p6_dev_board`` board target can be built and
flashed in the usual way (see :ref:`build_an_application` and :ref:`application_run`
for more details); however, an external programmer is required since the board
does not have any built-in debug support.
The following pins of the external programmer must be connected to the
following pins on the PCB (see image):
* VCC = VCC (do not power the board from the USB port at the same time)
* GND = GND
* SWIO = PD1
Flashing
========
SWIO pin is connected to PD1 and also to led on the board. By default, the function
of PD1 is SWD. To use the led on the board, the function of PD1 is changed to GPIO
if PD1 is not put to ground (see :zephyr_file:`boards/wch/ch32v003f4p6_dev_board/board.c`).
In order to be able to reflash the board, if some zephyr application is already loaded
in flash, power on the board with PD1 put to ground to let PD1 as SWIO. After that,
reconnect the external debugger SWDIO pin to PD1/SWIO to be able to flash the board
using the external debugger.
You can use ``minichlink`` to flash the board. Once ``minichlink`` has been set
up, build and flash applications as usual (see :ref:`build_an_application` and
:ref:`application_run` for more details).
Here is an example for the :zephyr:code-sample:`blinky` application.
.. zephyr-app-commands::
:zephyr-app: samples/basic/blinky
:board: ch32v003f4p6_dev_board
:goals: build flash
Debugging
=========
This board can be debugged via OpenOCD or ``minichlink``.
Testing the LED on the WCH CH32V003F4P6_DEV_BOARD
*************************************************
There is 1 sample program that allow you to test that the LED on the board is
working properly with Zephyr:
.. code-block:: console
samples/basic/blinky
You can build and flash the examples to make sure Zephyr is running
correctly on your board. The button and LED definition can be found
in :zephyr_file:`boards/wch/ch32v003f4p6_dev_board/ch32v003f4p6_dev_board.dts`.
References
**********
.. target-notes::
.. _WCH: http://www.wch-ic.com
.. _WCH webpage on CH32V003: https://www.wch-ic.com/products/CH32V003.html

View file

@ -0,0 +1,14 @@
adapter driver wlink
wlink_set
set _CHIPNAME riscv
jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x00001
set _TARGETNAME $_CHIPNAME.cpu
target create $_TARGETNAME.0 riscv -chain-position $_TARGETNAME
$_TARGETNAME.0 configure -work-area-phys 0x80000000 -work-area-size 10000 -work-area-backup 1
set _FLASHNAME $_CHIPNAME.flash
flash bank $_FLASHNAME wch_riscv 0x00000000 0 0 0 $_TARGETNAME.0
echo "Ready for Remote Connections"