Skip to content

Add support for WCH CH32V303 #87125

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

Merged
merged 3 commits into from
May 24, 2025
Merged

Conversation

miggazElquez
Copy link
Contributor

This PR add support for the SoC CH32V303 from WCH, and for the board CH32V303EVT.

Depends on #85395 for the support of west flash.

@zephyrbot
Copy link
Collaborator

zephyrbot commented Mar 14, 2025

The following west manifest projects have changed revision in this Pull Request:

Name Old Revision New Revision Diff
hal_wch zephyrproject-rtos/hal_wch@1de9d3e zephyrproject-rtos/hal_wch@941f518 (main) zephyrproject-rtos/hal_wch@1de9d3e4..941f5182

All manifest checks OK

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

@zephyrbot zephyrbot added manifest manifest-hal_wch DNM (manifest) This PR should not be merged (controlled by action-manifest) labels Mar 14, 2025
@miggazElquez
Copy link
Contributor Author

@VynDragon if you want to take a look, as this is based on your code for the ch32v208.

CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

CONFIG_FLASH_BASE_ADDRESS=0x08000000
Copy link
Collaborator

Choose a reason for hiding this comment

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

why is this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I don't put that, CONFIG_FLASH_BASE_ADDRESS will be set to the address of the flash in the device tree (currently 0), and minichlink fail to flash at the address 0. But if I change the address in the device tree, I can flash but Zephyr then fails to boot, I don't really know why. @VynDragon you didn't have this issue on the 208 ?

What would be the way to solve this ? I saw that some boards did change CONFIG_FLASH_BASE_ADDRESS in their defconfig, so that's why I put it there, but maybe it should be in another place ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh so that's why the west flash didnt work! Ill look into it, might be a matter of fixing the runner instead of this, otherwise figuring how to make it boot in non aliased space would make it easier to use the full flash so ill see for both issues.

Copy link
Collaborator

@VynDragon VynDragon Mar 14, 2025

Choose a reason for hiding this comment

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

Well it works for me when i set

flash0: flash@8000000 {
				compatible = "soc-nv-flash";
				reg = <0x08000000 DT_SIZE_K(128)>;
			};

after udpating minichlink and using minichlink -w build/zephyr/zephyr.bin 0x0
But west flash still doenst work so no clue what's going on there

Copy link
Collaborator

@VynDragon VynDragon Mar 14, 2025

Choose a reason for hiding this comment

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

a it's the -b for the runner, need to disable reset for ch32v208

Edit:" which is easy to say but almost impossible to do, i'm on my second PR just to be able to pass --no-reset in the board args.

Copy link
Collaborator

@VynDragon VynDragon Mar 19, 2025

Choose a reason for hiding this comment

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

It might be a frequency issue, for some reason on CH32V208 it works at 144MHz, but they 'highly recommend' using 120M in the RM for the F/V20x/30x serie. You added the long jump code in the ASM right?
It's only for the linking mapping within that space, otherwise the space is accessible without booting into it. (eg if your address space in zephyr is 0x08000000 it will only run in non-aliased, but if it's 0x0 it will only run in aliased due to the long jump / short jump things)

For flashing

Me neither, it's supposed to flash to 0x08000000 so with dt-flash=y and the flash address set to that and the long jump code, or 0x0 with no long jump and 0x08000000 flash address still. I think it changed between old and new minichlink version?

Copy link
Contributor Author

@miggazElquez miggazElquez Mar 19, 2025

Choose a reason for hiding this comment

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

So before answering to you I wanted to test it one last time and now it works, I don't know what I did wrong during my tests before... because I'm quite sure I tested with the long jump, but maybe I had changed something else, I don't know. Anyway it works now, so I modified my PR to include it, it seems cleaner to me than passing the flash address directly to minichlink.

I still don't really understand the point of the long jump, even if it's clearly necessary : if the address space in zephyr is at 0x08000000, why do we need the long jump to get into non-aliased mode ? Because when testing it seems like we boot in non-aliased mode with the address space at 0x08000000, but maybe the debuggers are just confused by all this aliasing and are lying to me. (I would like to add a comment explaining this long jump things but as I don't totally understand it yet...)

Copy link
Collaborator

@VynDragon VynDragon Mar 19, 2025

Choose a reason for hiding this comment

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

I still don't really understand the point of the long jump

It's so the pc is in the right zone when the short jumps happen, eg that the pc is in the area expected when it tries to do a short jump, there is also the matter of the gp pointer, and a couple other thing i believe break when operated in not the expected area. In practice it crashes if you run it without that long jump.

Also make sure to set frequency to 120 if you use the full flash! It might be your spurious issue
image

Because when testing it seems like we boot in non-aliased mode

It always boot in the aliased zone?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It always boot in the aliased zone?

I think that I was fooled by the debugger, I'm not sure about what is happening but with the debugger even without the longjump I'm in non aliased space (pc = 0x0800...). I think it's not really executing the first instructions, it seems like it's just jumping to __start directly ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Weird, I dont know what could be going on

@github-actions github-actions bot removed the DNM (manifest) This PR should not be merged (controlled by action-manifest) label Apr 24, 2025
@VynDragon VynDragon added the DNM This PR should not be merged (Do Not Merge) label Apr 24, 2025
@kartben kartben assigned nzmichaelh and unassigned gmarull Apr 25, 2025

Choose a reason for hiding this comment

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

I guess this file should also be located in qinke_v4f-directory

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this file I'm waiting on #87345 to be merged to rebase my code, so this file will end up in a folder for all Qingke-V4

/**
* @brief Type to hold a pin's pinctrl configuration.
*/
struct ch32v303_pinctrl_soc_pin {

Choose a reason for hiding this comment

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

this struct will also be used by ch32v307. This means the naming would be better qingke_v4f_pin....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same for this, it's in the folder for all qingke v4

Choose a reason for hiding this comment

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

as you told, it is in folder for all qinke v4, so a name of a soc (ch32v303) is maybe wrong. better to use a struct qinke_v4_pinvtrl_soc_pin {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the PR deduplicating the files was changed to just focus on the changes for the v208, so I created another PR deduplicating all the files, with the idea that when one of these two PR is merged, I would just rebase the other.

About the specific point, for the pinctrl file, naming it qingke_v4_something is maybe not the best idea, see this comment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to not naming it after the microarchitecture, as the pinctrl is a feature of the SoC, not the processor.

Update hal_wch.

As the hal upstream changed name, there is now a name conflict.
Rename ch32fun.h to hal_ch32fun.h to fix this conflict.

Signed-off-by: Miguel Gazquez <miguel.gazquez@bootlin.com>
Adds support for building an image for the ch32v303.

Signed-off-by: Miguel Gazquez <miguel.gazquez@bootlin.com>
Adds support for the CH32V303EVT board, based on the CH32V303 soc.

Signed-off-by: Miguel Gazquez <miguel.gazquez@bootlin.com>
Copy link

@VynDragon VynDragon removed the DNM This PR should not be merged (Do Not Merge) label May 14, 2025
@miggazElquez
Copy link
Contributor Author

The deduplication of the files is now in #90007; when one of these is merged I will rebase the other.

Overview
********

The `WCH`_ CH3V303VCT6_EVT hardware provides support for QingKe 32-bit RISC-V4F
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you happen to have a link to the schematic for the board? It's hard to check if the pin assignments etc are correct without it.

Overview
********

The `WCH`_ CH3V303VCT6_EVT hardware provides support for QingKe 32-bit RISC-V4F
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: the name seems to be "CH32V303VCT6-EVT": https://www.aliexpress.com/item/1005005257256239.html

Overview
********

The `WCH`_ CH3V303VCT6_EVT hardware provides support for QingKe 32-bit RISC-V4F
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: it's a QingKe-V4F 32-bit RISC-V, not a RISC-V4F.

Hardware
********

The QingKe 32-bit RISC-V4F processor of the WCH CH32V303VCT6_EVT is clocked by an external
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto re: RISC-V4F.

clk_hse: clk-hse {
#clock-cells = <0>;
compatible = "wch,ch32v00x-hse-clock";
clock-frequency = <DT_FREQ_M(32)>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: the HSE clock frequency is board specific, so probably shouldn't be set here.

};

&cpu0 {
clock-frequency = <DT_FREQ_M(144)>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see this was reduced to 120 MHz on the ch32v203 to allow using the extra, higher wait state flash. Either reduce this or shrink the flash to the 256 KiB of low-wait state flash.

/**
* @brief Type to hold a pin's pinctrl configuration.
*/
struct ch32v303_pinctrl_soc_pin {
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to not naming it after the microarchitecture, as the pinctrl is a feature of the SoC, not the processor.

@kartben kartben merged commit a9f2a19 into zephyrproject-rtos:main May 24, 2025
29 of 30 checks passed
nzmichaelh added a commit to nzmichaelh/zephyr that referenced this pull request May 24, 2025
zephyrproject-rtos#87125 renamed the
`ch32vfun.h` header but missed some of the drivers. Fix.

Signed-off-by: Michael Hope <michaelh@juju.nz>
@nzmichaelh
Copy link
Collaborator

I've sent #90438 to fix the other drivers after the ch32fun -> hal_ch32fun update.

nzmichaelh added a commit to nzmichaelh/zephyr that referenced this pull request May 25, 2025
zephyrproject-rtos#87125 renamed the
`ch32vfun.h` header but missed some of the drivers. Fix.

Signed-off-by: Michael Hope <michaelh@juju.nz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants