Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Zephyr RTOS support for WCH CH32V003 #73761

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

kholia
Copy link

@kholia kholia commented Jun 5, 2024

Details

This PR adds Zephyr RTOS support for WCH CH32V003 RISC-V MCU.

https://www.wch-ic.com/products/CH32V003.html

Usage

west update

west build -p always -b wch_ch32v003evt samples/basic/blinky

west flash -r minichlink

Tested On

  • Bare CH32V003J4M6 SOP-8 chip

  • WCH CH32V003EVT board (CH32V003 Evaluation Kit)

Blinky Build

$ west build -p always -b wch_ch32v003evt samples/basic/blinky
...
-- Zephyr version: 3.6.99 (zephyr), build: v3.6.0-5403-g90b43c51ff3f
[101/101] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
             ROM:       10056 B        16 KB     61.38%
             RAM:        1524 B         2 KB     74.41%
        IDT_LIST:          0 GB         2 KB      0.00%
Generating files from zephyr/build/zephyr/zephyr.elf for board: wch_ch32v003evt

It is fun to see Zephyr RTOS running on a "10 cent" MCU.

Copy link

github-actions bot commented Jun 5, 2024

Hello @kholia, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@zephyrbot
Copy link
Collaborator

zephyrbot commented Jun 5, 2024

The following west manifest projects have been modified in this Pull Request:

Name Old Revision New Revision Diff
hal_wch N/A zephyrproject-rtos/hal_wch@1de9d3e (main) N/A

Note: This message is automatically posted and updated by the Manifest GitHub Action.

soc/wch/ch32v00x/Kconfig Show resolved Hide resolved
soc/wch/ch32v00x/soc.yml Outdated Show resolved Hide resolved
soc/wch/ch32v00x/Kconfig.soc Show resolved Hide resolved
boards/wch/ch32v003evt/Kconfig Outdated Show resolved Hide resolved
boards/wch/ch32v003evt/Kconfig.board Outdated Show resolved Hide resolved
boards/wch/ch32v003evt/Kconfig.defconfig Outdated Show resolved Hide resolved
dts/bindings/pinctrl/wch,afio.yaml Outdated Show resolved Hide resolved
drivers/gpio/Kconfig.ch32v00x Outdated Show resolved Hide resolved
@kholia kholia force-pushed the ch32v003-for-upstream branch 2 times, most recently from 39630a6 to 2ca52da Compare October 7, 2024 03:24
@zephyrbot zephyrbot added the DNM This PR should not be merged (Do Not Merge) label Oct 7, 2024
@cnlohr
Copy link

cnlohr commented Oct 7, 2024

@kholia do you have a summary for what you changed? Or who's if any's quests have been changed?

@kholia
Copy link
Author

kholia commented Oct 8, 2024

@cnlohr I just added select ATOMIC_OPERATIONS_C line and rebased the PR on top of latest upstream main.

The current build failure does NOT seem related to this work.

Screenshot_2024-10-08_07-51-07

I will rebase and re-push the commits.

@kholia kholia force-pushed the ch32v003-for-upstream branch 2 times, most recently from f87099a to aa19307 Compare October 9, 2024 07:37
@kholia
Copy link
Author

kholia commented Oct 9, 2024

I have rebased and re-pushed the same commits in the hope of passing the CI checks!

agatti added a commit to agatti/micropython that referenced this pull request Oct 10, 2024
The current situation about CH32V307 header files licensing got a bit
confusing, and this is an attempt to sort things out.

Originally the WCH BSP files coming from the initial CH32V307 port
commit came with an Apache 2.0 licence, which then it disappeared from
newer BSP file versions.  However, this prevented other projects such as
Zephyr to actually support this MCU (and others from the same family).

This prompted a request to WCH to relicense the BSP files under a more
permissive licence such as MIT.  A specific set of headers is currently
available under the MIT licence after explicit relicensing from WCH,
which is what this commit adds as a submodule.

For reference, see openwch/ch32v003#45 and
zephyrproject-rtos/zephyr#73761 (comment)

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
agatti added a commit to agatti/micropython that referenced this pull request Oct 10, 2024
The current situation about CH32V307 header files licensing got a bit
confusing, and this is an attempt to sort things out.

Originally the WCH BSP files coming from the initial CH32V307 port
commit came with an Apache 2.0 licence, which then it disappeared from
newer BSP file versions.  However, this prevented other projects such as
Zephyr to actually support this MCU (and others from the same family).

This prompted a request to WCH to relicense the BSP files under a more
permissive licence such as MIT.  A specific set of headers is currently
available under the MIT licence after explicit relicensing from WCH,
which is what this commit adds as a submodule.

For reference, see openwch/ch32v003#45 and
zephyrproject-rtos/zephyr#73761 (comment)

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@cnlohr
Copy link

cnlohr commented Oct 11, 2024

There are still 4 changes requested - are they just waiting to be checked off by those reviewers? I wonder if someone should/could ping them?

Copy link
Member

@cfriedt cfriedt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't manage to finish my last review. Just a small suggestion - not sure if it's still relevant

RCC_TypeDef *regs = config->regs;
uint8_t id = (uintptr_t)sys;

*(&regs->AHBPCENR + WCH_RCC_CLOCK_ID_OFFSET(id)) |= 1 << WCH_RCC_CLOCK_ID_BIT(id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*(&regs->AHBPCENR + WCH_RCC_CLOCK_ID_OFFSET(id)) |= 1 << WCH_RCC_CLOCK_ID_BIT(id);
sys_write32(BIT(WCH_RCC_CLOCK_ID_BIT(id), &regs->AHBPCENR + WCH_RCC_CLOCK_ID_OFFSET(id));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in:

clock_control_wch_rcc.c:31:68: warning: passing argument 2 of 'sys_write32' makes integer from pointer without a cast [-Wint-conversion]
   31 |         sys_write32(BIT(WCH_RCC_CLOCK_ID_BIT(id)), &regs->AHBPCENR + WCH_RCC_CLOCK_ID_OFFSET(id));
      |                                                                    ^
      |                                                                    |
      |                                                                    volatile uint32_t * {aka volatile unsigned int *}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the following change make sense?

+#define ENABLE_RCC_CLOCK(regs, id)                                                                 \
+       (*(&(regs)->AHBPCENR + WCH_RCC_CLOCK_ID_OFFSET(id)) |= BIT(WCH_RCC_CLOCK_ID_BIT(id)))
+
 #define WCH_RCC_CLOCK_ID_OFFSET(id) (((id) >> 5) & 0xFF)
 #define WCH_RCC_CLOCK_ID_BIT(id)    ((id) & 0x1F)
 
@@ -28,7 +31,8 @@ static int clock_control_wch_rcc_on(const struct device *dev, clock_control_subs
        RCC_TypeDef *regs = config->regs;
        uint8_t id = (uintptr_t)sys;
 
-       *(&regs->AHBPCENR + WCH_RCC_CLOCK_ID_OFFSET(id)) |= 1 << WCH_RCC_CLOCK_ID_BIT(id);
+       ENABLE_RCC_CLOCK(regs, id);

@nzmichaelh
Copy link
Collaborator

Looking forward to this landing! Here's a short video of 5 CH32V003 based boards running Zephyr and at least blinking a LED: https://juju.nz/michaelh/post/2024/all_so_far/

Once this PR lands, I'll send through PRs for I2C, SPI, and other drivers.

@cfriedt
Copy link
Member

cfriedt commented Oct 12, 2024

Looking forward to this landing! Here's a short video of 5 CH32V003 based boards running Zephyr and at least blinking a LED: https://juju.nz/michaelh/post/2024/all_so_far/

Awesome! 🪁

Comment on lines +24 to +25
config PINCTRL
default y
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not allowed, drivers must select this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roger.

A bunch of existing code is doing the same thing though.

$ cat soc/atmel/sam/Kconfig.defconfig
# Atmel SAM MCU family default configuration options

# Copyright (c) 2016 Piotr Mienkowski
# Copyright (c) 2024 Gerson Fernando Budke <nandojve@gmail.com>
# SPDX-License-Identifier: Apache-2.0

if SOC_FAMILY_ATMEL_SAM

rsource "*/Kconfig.defconfig"

config CLOCK_CONTROL
	default y

config GPIO
	default y

config PINCTRL
	default y

config SYS_CLOCK_HW_CYCLES_PER_SEC
	default $(dt_node_int_prop_int,/cpus/cpu@0,clock-frequency)

config WATCHDOG
	default y

endif # SOC_FAMILY_ATMEL_SAM

Also there are other examples:

$ rg "config PINCTRL" | grep soc     
soc/infineon/cat3/Kconfig.defconfig:config PINCTRL
soc/nxp/imxrt/Kconfig.defconfig:config PINCTRL_IMX
soc/microchip/mec/mec175x/Kconfig.defconfig.mec1753qlj:config PINCTRL
soc/microchip/mec/mec175x/Kconfig.defconfig.mec1753qsz:config PINCTRL
soc/microchip/mec/mec174x/Kconfig.defconfig.mec1743qsz:config PINCTRL
soc/microchip/mec/mec174x/Kconfig.defconfig.mec1743qlj:config PINCTRL
soc/microchip/mec/mech172x/Kconfig.defconfig.mech1723nlj:config PINCTRL
soc/microchip/mec/mech172x/Kconfig.defconfig.mech1723nsz:config PINCTRL
soc/gd/gd32/Kconfig.defconfig:config PINCTRL
soc/nxp/imx/imx8m/Kconfig.defconfig.mimx8ml8_a53:config PINCTRL_IMX
soc/nxp/imx/imx8m/Kconfig.defconfig.mimx8ml8_m7:config PINCTRL_IMX
soc/nxp/imx/imx8m/Kconfig.defconfig.mimx8mq6_m4:config PINCTRL_IMX
soc/nxp/imx/imx8m/Kconfig.defconfig.mimx8mn6_a53:config PINCTRL_IMX
soc/nxp/imx/imx8m/Kconfig.defconfig.mimx8mm6_m4:config PINCTRL_IMX
soc/nxp/imx/imx8m/Kconfig.defconfig.mimx8mm6_a53:config PINCTRL_IMX
soc/nxp/imx/imx7d/Kconfig.defconfig:config PINCTRL_IMX
soc/nxp/imx/imx6sx/Kconfig.defconfig:config PINCTRL_IMX
soc/nxp/imx/imx9/imx93/Kconfig.defconfig.mimx93.a55:config PINCTRL_IMX
soc/ti/k3/am6x/Kconfig.defconfig:config PINCTRL
soc/ite/ec/it8xxx2/Kconfig.defconfig.series:config PINCTRL
soc/atmel/sam/Kconfig.defconfig:config PINCTRL
soc/atmel/sam0/Kconfig.defconfig:config PINCTRL
soc/wch/ch32v00x/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra6m5/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra6e2/Kconfig.defconfig:config PINCTRL
soc/raspberrypi/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra4m3/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra8m1/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra6e1/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra2a1/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra8d1/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra6m3/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra6m2/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra8t1/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra6m1/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra4w1/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra4e2/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra6m4/Kconfig.defconfig:config PINCTRL
soc/renesas/ra/ra4m2/Kconfig.defconfig:config PINCTRL
soc/telink/tlsr/tlsr951x/Kconfig.defconfig:config PINCTRL
soc/renesas/rcar/rcar_gen4/Kconfig.defconfig.r8a779f0:config PINCTRL
soc/renesas/rcar/rcar_gen3/Kconfig.defconfig:config PINCTRL

@nzmichaelh
Copy link
Collaborator

nzmichaelh commented Oct 20, 2024

@kholia I noticed a bug in the GPIO input mode configuration code. See nzmichaelh@e32d9ac for a fix that also adds pullup / pulldown support.

kholia and others added 11 commits October 20, 2024 21:04
This is used for WCH chips including the CH32V003.

Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the dtsi and bindings for the WCH CH32V003 which is a
32-bit general-purpose RISC-V MCU.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the soc support for WCH CH32V003.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the pinctrl driver for WCH CH32V003.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the clock driver for WCH CH32V003.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the systick driver for WCH CH32V003.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the usart driver for WCH CH32V003.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the pfic interrupt controller driver for WCH CH32V003.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds the gpio driver for WCH CH32V003.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds support for the CH32V003EVT board which features a
32-bit general-purpose RISC-V MCU.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
This commit adds a runner wrapper for the 'minichlink' program which
offers a free, open mechanism to use the CH-LinkE programming dongle for
the CH32V003.

https://github.com/cnlohr/ch32v003fun/tree/master/minichlink

Signed-off-by: Michael Hope <michaelh@juju.nz>
Signed-off-by: Dhiru Kholia <dhiru.kholia@gmail.com>
@kholia
Copy link
Author

kholia commented Oct 20, 2024

@kholia I noticed a bug in the GPIO input mode configuration code. See nzmichaelh@e32d9ac for a fix...

Applied and pushed - thanks!

@zhang-wenchao
Copy link
Contributor

I have purchased the WCH board and can't wait to run Zephyr on it. What is I can do? Help to accelerate the merger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Clock Control area: Devicetree Binding PR modifies or adds a Device Tree binding area: GPIO area: Interrupt Controller area: Pinctrl area: Process area: RISCV RISCV Architecture (32-bit & 64-bit) area: Timer Timer area: UART Universal Asynchronous Receiver-Transmitter area: West West utility DNM This PR should not be merged (Do Not Merge) manifest manifest-hal_wch
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.