Skip to content

add ch32v303cbt6 - #239

Open
kingovchouffe wants to merge 5 commits into
openwch:mainfrom
kingovchouffe:add_CH32V302CBT6_variant
Open

add ch32v303cbt6#239
kingovchouffe wants to merge 5 commits into
openwch:mainfrom
kingovchouffe:add_CH32V302CBT6_variant

Conversation

@kingovchouffe

Copy link
Copy Markdown

Add support for CH32V303CBT6 generic board

@maxint-rd

Copy link
Copy Markdown
Contributor

Excellent! (Unfortunately I don't have a V303 in my collection to test this. Perhaps others can help out...)

@BitByteBeetle

Copy link
Copy Markdown

Max, do you have CH570D in your collection ?
For the moment I have not much time to test/play with it and can send some over if you want.

@maxgerhardt

Copy link
Copy Markdown
Contributor

The PR has a variants/CH32V30x/CH32V303CBT6/boards_entry.txt but the boards.txt file of this core remains unmodified? So it won't show up in the Arduino IDE as a selectable board, right?

@kingovchouffe

Copy link
Copy Markdown
Author

There is no ch32v303cbt6 official eval board, only ch32v303vct6, I just wanted to add the cbt6 as a general mcu like you can do with the smt32duino where you are not stuck with only EVT boards.

Anyway I can add it as a generic board if that makes sense.

@maxint-rd

Copy link
Copy Markdown
Contributor

Max, do you have CH570D in your collection ? For the moment I have not much time to test/play with it and can send some over if you want.

Hallo @BitByteBeetle , no I don't have any CH57x yet. If you've any spares I'd be happy to receive one. I've continued my response here in your issue about the CH570D.

@kingovchouffe

Copy link
Copy Markdown
Author

Hi @maxint-rd, I'm ordering V303 devboard from official WCH ali store. Do you want one for the testing ?

How can I generate a working Arduino test environnement?

@maxint-rd

Copy link
Copy Markdown
Contributor

Hello @kingovchouffe ,

Hi @maxint-rd, I'm ordering V303 devboard from official WCH ali store. Do you want one for the testing?

Thank you for that offer. If it's not too costly for you I'd very much appreciate your contribution to my collection! Unfortunately I cannot commit to spend much time, but I'd try my best to test things and look into issues. You can get my address details via e-mail: info [at] maxint.nl

My electronics hobby would quickly ran out of budget if I were to buy every WCH dev board, so I only bought the V003 EVT board and a bunch of the most affordable bare chips, e.g. V003, V002, V006. If you're interested I can send you some of those in return.

(BTW. On the WCH AliExpress store I saw the V303CBT6-EVT-R0 board and also the V303VCT6-EVT-R0 and V303RCT6-EVT-R1. I presume you're ordering the CBT6 board, since that's the topic of your PR. Are you making your own CBT6 board, perhaps something audio related? ;-)

How can I generate a working Arduino test environment?

I know that in PR #121 the makeboards.py python script was added to the tools, but I didn't use that. What I did for the V002/V006 (in PR #200) and for the X033 (in PR #171) was basically:

  • make a copy of the existing board definition,
  • change the file names to match the new MCU
  • add the new MCU into the generic boards.txt file
  • compile the blink example to see what works
  • make MCU specific changes in the variant files
  • make changes as needed in other files (e.g. fix SPI/I2C, set memory sizes in Link.ld)

From looking at the changed files in your PR, you already did much of this already.
For testing I also made a fresh installation in a Windows VM, but on my development PC I just changed the existing installation.

@kingovchouffe

Copy link
Copy Markdown
Author

Hello @maxint-rd

I already have a CH32V303CBT6 board with the goal to do audio effects. I can't test on it the ADC, DAC, LED, and GPIO reading but not much. I don't know if it could be sufficient for this PR or if it needs to be tested on the devboard.

Thank you for the guidelines I'll try to do the testing when I have the time.

@maxint-rd

Copy link
Copy Markdown
Contributor

I think using that board could be sufficient to test this PR. If the PR works for you, then that's already something. In the board definitions I added, I found it sufficient to have the peripherals working on the available pins. I only have the V003 EVT board and use the others as bare chips on breakout boards, (or directly on project specific PCB's).

Please note that like other PRs this PR will not be merged into a new release by WCH, unless they dedicate some manpower to do so, or delegate their authorization to the community. However, knowing this PR works might help others to include it in their board files.

Personally I still like using the CH32 platform for my Arduino projects. Mainly for its affordability, but also for features such as easy debugging and the ease of upgrading when flash gets too low. My main goal is to be able to use these processors in a wide range of Arduino projects, so I mainly worked on getting basic Arduino functionality working and add support to common Arduino libraries.

@mickabrig7

Copy link
Copy Markdown

Hey and thanks a lot for your PR !

I've been trying it on my own chip, but seem to be hitting a HardFault within the startup script, on line 365.
Do you have the same issue on your side ?

I'll be looking a bit more in detail later and report back 😄

@mickabrig7 mickabrig7 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Found it !
Fixing CH32V303CBT6.build.chip + setting the correct sizes in system/CH32V30x/SRC/Ld/Link.ld made it work (the latter should maybe not be applied in this PR since it would break compatibility with the other chips with more memory, I'm not sure how PlatformIO is supposed to handle this normally).

Comment thread boards.txt Outdated
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.board=CH32V303CBT6
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.series=CH32V30x
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.variant=CH32V30x/CH32V303CBT6
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.chip=CH32V30x_C

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.chip=CH32V30x_C
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.chip=CH32V30x

@maxint-rd

maxint-rd commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Congratulations @mickabrig7

FYI: in PR #200 to add support for CH32V002 I added a link file to specifically match the V002 characteristics. In the board.txt file I used the ldscript field to make it used that link file:
CH32VM00X_EVT.menu.pnum.CH32V002F4.build.ldscript=LinkV002.ld

Perhaps you can use that method to avoid breaking compatibility?
(Mind you, I only use the Arduino IDE, not Plaformio...)

@kingovchouffe

Copy link
Copy Markdown
Author

Hey @mickabrig7 !

They are a lot of issues with this PR and I submitted it a bit too early sorry for that without having the time to properly test itsorry for that.

Thanks you for this modification, have you test the PR with a blinky ? I will test this modification on my board after work (UE time).

@maxint-rd I will do another link file again after work.

Again thanks for your support :)

@mickabrig7

Copy link
Copy Markdown

@kingovchouffe Avec plaisir copain, merci a toi ! 🙆‍♂️

So after some testing, I was able to enumerate my chip as a USB device by defining PIO_FRAMEWORK_ARDUINO_USBFS which is awesome.
However GPIOs seem broken as I don't get any state change with a simple blinky on PB2 (the same code works on my CH32V203).

It's bed time in my time zone so I'll check this out more in depth tomorrow, in the meantime have a good evening 😊

@kingovchouffe

Copy link
Copy Markdown
Author

I made another link file, and I checked other configuration file but I'm not able to do some basic blinky also. But it's nice that the USB work ! maybe the end of the tunnel isn't that far.

I'll work on that this week-end :)

@mickabrig7 mickabrig7 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Okay so turns out I'm stupid and was just accidentally calling Arduino GPIO functions with the PinNames.h pin definitions (the ones with an underscore like PB_2) instead of the ones defined in the variant_*.h files 🫠

I just found 2 small mistakes that I'm suggesting changes for in this review.
However, even after fixing the ldscript typo and clean building my project, the linker file doesn't seem properly applied: I still need to edit the default Link.ld for the chip to boot properly.
I've been looking at #200 that @maxint-rd linked and I honestly cannot figure out what we're doing differently here 🤷

Otherwise everything works nicely, I got my project running just like on a CH32V203 with more memory which is exactly what I wanted, and it uses Adafruit TinyUSB + Adafruit NeoPixel + SimpleFOC (with an I2C sensor using TwoWire) + HX711_ADC.

Thanks again !

Comment thread variants/CH32V30x/CH32V303CBT6/variant_CH32V303CBT6.h Outdated
Comment thread boards.txt Outdated
kingovchouffe and others added 2 commits March 7, 2026 18:46
Co-authored-by: ガラベロ ミカエル <mickaelgarabello7@gmail.com>
Co-authored-by: ガラベロ ミカエル <mickaelgarabello7@gmail.com>
@kingovchouffe

Copy link
Copy Markdown
Author

WOW thanks a lot @mickabrig7 it works on my side too ! it was a stupid mistake but good catch !

@maxint-rd

maxint-rd commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

Hello again @mickabrig7 and @kingovchouffe ,

Although I can't help you in testing (yet), I do wish you both all the luck in getting everything working. Always nice to have support for more CH32 chips. Congratulations on the results of your efforts so far!

@mickabrig7, can I presume you were using the Arduino IDE v2.x in your latest test? (I'm not familiar with the way Platformio does the linking).

I wonder why you still need to change the regular link file. I saw that you fixed the ldscript typo in your builld.txt. This didn't fix it?
Could it be that the alternative link file is not being used? Can you check the command line where the link file is specified?

If you are using the IDE; I remember that when changing things in the board,txt, these changes were not always followed by the IDE. Adding things such as the peripheral menu required clearing the IDE cache. It's a known issue that bugged me enough to write a section about dealing with it.

If you're using Platformio: I guess you still need to look at the command line that is used at the final step of linking and make sure your alternative link file is used. For the IDE the ldscript value in build.txt overwrites a similar value in platform.txt:

# These can be overridden in boards.txt
build.extra_flags=
build.bootloader_flags=
build.ldscript=Link.ld
build.variant_h=

I don't know if those files are used the same way in Platformio. The IDE uses that value later in platform.txt to do the linking:

## Combine gc-sections, archives, and objects
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.extra_flags} -T "{build.system.path}/{build.series}/SRC/Ld/{build.ldscript}" -nostartfiles -Xlinker --gc-sections "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} {build.flags.ldflags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -Wl,--start-group {object_files} {build.ch_extra_lib} -Wl,--whole-archive "{archive_file_path}" -Wl,--no-whole-archive -lc -Wl,--end-group 

@mickabrig7 mickabrig7 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@maxint-rd Thanks for the info !
I am indeed using PlatformIO exclusively and have never tried using this core with the Arduino IDE.

After looking around to see how multiple memory sizes were handled for the CH32V20x (which has plenty of variants), I went for what I believe is the cleanest way to do it: using the __FLASH_SIZE, __RAM_SIZE and __STACK_SIZE defines that are linked to the upload.maximum_size / upload.maximum_data_size properties within platform.txt.

So our Link.ld becomes this:

__flash_size = DEFINED(__FLASH_SIZE) ? __FLASH_SIZE : 256K;
__ram_size = DEFINED(__RAM_SIZE) ? __RAM_SIZE : 64K;
__stack_size = DEFINED(__STACK_SIZE) ? __STACK_SIZE : 2048;


ENTRY( _start )
PROVIDE( _stack_size = __stack_size );


MEMORY
{
/* CH32V30x_D8C - CH32V305RB-CH32V305FB
   CH32V30x_D8 - CH32V303CB-CH32V303RB
*/
/*
	FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 128K
	RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
*/
    
/* CH32V30x_D8C - CH32V307VC-CH32V307WC-CH32V307RC
   CH32V30x_D8 - CH32V303VC-CH32V303RC
   FLASH + RAM supports the following configuration
   FLASH-192K + RAM-128K
   FLASH-224K + RAM-96K
   FLASH-256K + RAM-64K  
   FLASH-288K + RAM-32K  
*/
	FLASH (rx) : ORIGIN = 0x00000000, LENGTH = __flash_size
	RAM (xrw) : ORIGIN = 0x20000000, LENGTH = __ram_size
}

It works properly on my side and I would imagine it does on Arduino IDE too ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Delete this file

Comment thread boards.txt
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.math_lib_gcc=-lm
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.IQ_math_RV32=
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.ch_extra_lib=-lprintfloat
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.ldscript=Link_v303.ld

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
CH32V30x_EVT.menu.pnum.CH32V303CBT6.build.ldscript=Link_v303.ld

@maxint-rd

Copy link
Copy Markdown
Contributor

It works properly on my side and I would imagine it does on Arduino IDE too ?

Sounds like a great solution. I haven't tried using these definitions, but it's definitely cleaner than using a blunt modification of a copied link file like I did.

Unfortunately I don't have the spare time now, but as soon as I get the opportunity, I'd like to test this for my V002 PR. Thank you for your help and for giving this suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants