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

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

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/[email protected]

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

@VynDragon
Copy link
Collaborator

VynDragon commented Mar 14, 2025

Looks perfect to me as far as I can tell beside the nordicjm remarks. Will do regression testing for CH32V003 and CH32V208 soon.
CONFIG_FLASH_BASE_ADDRESS shouldnt be set like this in there. I am not sure how to support the extended flash size in zephyr either.... In the meantime you can just set the address of the flash in memory space in the DTS if you dont want it to use the 0x0 address alias such as:

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

i think

Copy link
Collaborator

@nzmichaelh nzmichaelh left a comment

Choose a reason for hiding this comment

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

LGTM

west.yml Outdated
@@ -258,7 +258,7 @@ manifest:
groups:
- hal
- name: hal_wch
revision: 1de9d3e406726702ce7cfc504509a02ecc463554
revision: pull/1/head
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this correct?

Copy link
Contributor Author

@miggazElquez miggazElquez Mar 17, 2025

Choose a reason for hiding this comment

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

I think, it's to indicate that this PR depend on the PR #1 on the hal_wch repository. The message from zephyrbot at the top of the PR looks good so I think it's correct.

@VynDragon
Copy link
Collaborator

I cant find the other comment so here the PR for fixing CH32V208 with the issue mentionned: #87345, inherently fixing it by adding the full flash feature

SECTION_FUNC(vectors, __start)
li a0, 0xf
csrw mtvec, a0
j __initialize
Copy link
Member

Choose a reason for hiding this comment

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

This file seems to be the same across all qingke socs. Let's de-duplicate it and use a single common one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's the same for the ch32v208 (Qingke V4C), but not for the ch32v003(Qingke V2A) (see #87125 (comment)).

Should I still de-duplicate it ?

Copy link
Collaborator

@VynDragon VynDragon Mar 21, 2025

Choose a reason for hiding this comment

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

Yea, this version is only relevant for CH32V208, and CH32V30x (unless i'm missing some)
Edit: and also CH32V203 but the amount of flash is different (224K), so between all Qingke V4?

This seems to be relevant only for all Qinke V4 CH32 of the 20x and 3xx serie. So currently CH32V208, CH32V303, not CH32V003.

Copy link
Member

Choose a reason for hiding this comment

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

@miggazElquez yeah, let's de-duplicate it :)

Copy link
Contributor Author

@miggazElquez miggazElquez Mar 24, 2025

Choose a reason for hiding this comment

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

yeah, with the new PR introducing the v203 it seems even more logical.

As the new vector.S use the non-aliased space, it needs some changes in the devicetree of the 208 to use the new files, so I didn't remove it from here. These changes are in #87345, which updated the SoC to use the new version of vector.S.

Copy link
Collaborator

@VynDragon VynDragon Mar 24, 2025

Choose a reason for hiding this comment

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

Should i submit the deduplication with the 208 update? Should it be

-qingke_v4/
    \ f
    \ c
    \ b

or stay

- qingke_v4f
- qingke_v4c
- qingke_v4 # common

?

Copy link
Member

Choose a reason for hiding this comment

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

The latter sounds better, and it replicates the convention used for other SoCs (stm, gd, silabs), but I'd rearrange it to something like this:

- chv32 (family)
  - qingke_v4 (series)
    - common
    - qingke_v4f (soc)
    - qingke_v4c (soc)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this be done in this pull request, or is it better to do it with the ch32v208 update ?

Copy link
Member

Choose a reason for hiding this comment

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

We can handle it in the update IMO

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok will update that PR

nordicjm
nordicjm previously approved these changes Mar 24, 2025
fkokosinski
fkokosinski previously approved these changes Mar 28, 2025
@@ -0,0 +1,59 @@
.. zephyr:board:: ch32v303evt

Choose a reason for hiding this comment

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

Here it is not clear, which board is mentioned. WCH have several CH32V303-Boards. They differ in size of flash and if FSMC is there or not. So please specify more clear which type is described (VH32V303CBT6 or VH32V303VCT6 or VH32V303RCT6)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I hadn't noticed that there are multiple development kit named CH32V303EVT. Mine is for the one with the CH32V303VCT6. I'm not sure about the best way to handle it : I can of course add it to the text of the documentation, but for the name of the board I'm not sure of the correct way t handle it (in this line of the documentation but also in general, the name of the board is used by west). Maybe this where I have to use board variant ? I'm not sure

Choose a reason for hiding this comment

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

I am actually working on the 308. There I have the same problem with different naming. Here I guess, we need to add the extension to the naming. I future there will then be 3 boards. If you look here https://www.wch-ic.com/products/CH32V303.html in part Product Selection Guide you can see a lot of diffenerces (number of adc, Uart SPI etc, Flashsize Ram-Size). So my proposale is to add to yout board name the processor extension like WCH to it at the order of dev-boards too.

Copy link
Contributor Author

@miggazElquez miggazElquez Apr 24, 2025

Choose a reason for hiding this comment

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

Ah yes, on their ali express page the name is more complete, I hadn't noticed it. I will rename the board ch32v303vct6_evt.

Choose a reason for hiding this comment

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

thats looks fine from myside

Choose a reason for hiding this comment

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

I will look in the next days over all your files and compare it with my 308-files (so far i have them) to check their compatibility

@miggazElquez
Copy link
Contributor Author

miggazElquez commented Apr 24, 2025

I modified the commit for the hal update, and renamed the board.

Please put a DNM tag on this PR, I'm still waiting for #87345 to be merged before I can rebase my code.

Edit: I also rebased on main to try to fix the errors on the CI.

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 <[email protected]>
All SoCs based on Qingke V4 processors need the same 'vector.S'.

Adds a shared `qingke_v4_vector.S` file to avoid duplication across
multiple SoCs.

Signed-off-by: Miguel Gazquez <[email protected]>
Adds support for building an image for the ch32v303.

Signed-off-by: Miguel Gazquez <[email protected]>
Adds support for the CH32V303EVT board, based on the CH32V303 soc.

Signed-off-by: Miguel Gazquez <[email protected]>
@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
#endif

#if defined(CONFIG_SOC_SERIES_QINGKE_V4C)
#define CH32V20x 1
#include <ch32v003fun.h>

Choose a reason for hiding this comment

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

i am not happy with the filename starting with wch. I have a board using the Qingke-V4F and not from wch. I guess, that the filename should be change to qingkeCh32fun or something like this.

Choose a reason for hiding this comment

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

Sorry for confusion, I see now, that the chip is still from wch. So evrything can stay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Clock Control area: GPIO area: Interrupt Controller area: Pinctrl 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 platform: WinChipHead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants