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 tinyusb builtin-core support with menu selection #121

Merged
merged 6 commits into from
Jun 20, 2024

Conversation

hathach
Copy link
Contributor

@hathach hathach commented Jun 17, 2024

This PR add TinyUSB library support for ch32 core with minimal changes as possible. These are tested and worked with Ch32v203 with both USBD and USBFS, and ch32v307 both USBFS and USBHS (need 144Mhz HSE since I am familliar with highspeed clock setup).

  • add TinyUSB to libraries/ as submodules, this is required for having Serial to include and declare as USB virtual come instead of UART. (this require git submodule update --init), and the zip package should also clone this for a new core release
  • Add Menu selection for TinyUSB with: None (default), TinyUSB with USBD, USBFS, USBHS. Therefore by default, this should not affect existing code, in the future if you guys want to add your own usb driver, we can just add another entry to the menu for user to select.
  • To make it easier to add new boards and usb menu to boards.txt, I have written tools/makeboards.py (this is how we manage boards in Adafruit repo), this help to ease the new boards addition, and fix typo issue, just update the script then run python tools/makeboards.py > boards.txt. It does catch some of your existing typo, please let us know if this looks good to you. Otherwise we can skip this, and just copy/paste & modify manually.
  • If TinyUSB is selected: main() will call TinyUSB_Device_Init() before setup() and task()/cdc flush() in the loop. This ease user from manually calling these to handle usb communication.
  • WSerial.h include tinyusb header and define Serial as TinyUSB's virtual com if selected

Ch32v203: only USBD and USBFS are available
image

Ch32v307: only USBFS and USBHS are available
Screenshot from 2024-06-17 11-33-55

Limitation:

Note: this uses adafruit/Adafruit_TinyUSB_Arduino#429, once this PR is approved and merged, we will release a new version for tinyusb and make an following up PR so that this core use an official new release.

@ladyada

@@ -4,6 +4,10 @@
#include "variant.h"
#include "HardwareSerial.h"

#if defined(USE_TINYUSB)
#include "Adafruit_USBD_CDC.h"
#define Serial SerialTinyUSB
Copy link
Contributor Author

Choose a reason for hiding this comment

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

define Serial as TinyUSB's virtual com

@@ -79,7 +79,8 @@ compiler.elf2lst.extra_flags=

# USB Flags
# ---------
build.usb_flags=-DUSBCON {build.usb_speed} -DUSBD_VID={build.vid} -DUSBD_PID={build.pid} -DHAL_PCD_MODULE_ENABLED
#build.usb_flags=-DUSBCON {build.usb_speed} -DUSBD_VID={build.vid} -DUSBD_PID={build.pid} -DHAL_PCD_MODULE_ENABLED
build.usb_flags=
Copy link
Contributor Author

Choose a reason for hiding this comment

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

build.usb_flags is definned by menu selection, user can choose which usb controller they want to use as device: usbd, usbfs, usbhs

CH32X035_EVT.menu.pnum.CH32X035G8U=CH32X035G8U EVT
CH32X035_EVT.menu.pnum.CH32X035G8U.node=NODE_X035G8
CH32X035_EVT.menu.pnum.CH32X035G8U.node=NODE_X035G8U
Copy link
Contributor Author

Choose a reason for hiding this comment

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

node typo ? is NODE_X035G8U correct ?

CH32X035_EVT.menu.clock.24MHz_HSI.build.flags.clock=-DSYSCLK_FREQ_16MHz_HSI=16000000
CH32X035_EVT.menu.clock.24MHz_HSI=12MHz Internal
CH32X035_EVT.menu.clock.24MHz_HSI.build.flags.clock=-DSYSCLK_FREQ_12MHz_HSI=12000000
CH32X035_EVT.menu.clock.16MHz_HSI=16MHz Internal
Copy link
Contributor Author

Choose a reason for hiding this comment

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

16mhz_hsi typo catched by script

CH32V10x_EVT.menu.clock.72MHz_HSI=72MHz Internal
CH32V10x_EVT.menu.clock.72MHz_HSI.build.flags.clock=-DSYSCLK_FREQ_72MHz_HSI=72000000
CH32V10x_EVT.menu.clock.56MHz_HSI=56MHz Internal
CH32V10x_EVT.menu.clock.56MHz_HSI.build.flags.clock=-DSYSCLK_FREQ_56MHz_HSI=56000000
CH32V10x_EVT.menu.clock.48MHz_HSI=48MHz Internal
CH32V10x_EVT.menu.clock.48MHz_HSI.build.flags.clock=-DSYSCLK_FREQ_48MHz_HSI=48000000
CH32V10x_EVT.menu.clock.8MHz_HSI=8MHz Internal
CH32V10x_EVT.menu.clock.8MHz_HSI.build.flags.clock=-DSYSCLK_FREQ_HSI=8000000
CH32V10x_EVT.menu.clock.8MHz_HSI.build.flags.clock=-DSYSCLK_FREQ_8MHz_HSI=8000000
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this typo? should it be -DSYSCLK_FREQ_HSI/HSE=8000000 or -DSYSCLK_FREQ_8MHz_HSI/HSE=8000000 to be consistent with above defines

CH32V20x_EVT.menu.pnum.CH32V203C6.build.IQ_math_RV32=
CH32V20x_EVT.menu.pnum.CH32V203C6.build.ch_extra_lib=-lprintf


# USB support
Copy link
Contributor Author

@hathach hathach Jun 17, 2024

Choose a reason for hiding this comment

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

usb menu selection for v20x



# USB support
Copy link
Contributor Author

Choose a reason for hiding this comment

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

usb selection for v307

@DeqingSun
Copy link
Contributor

This is awesome!

@TianpeiLee
Copy link
Collaborator

Special thanks for your contribution, it's awesome!!

  • To make it easier to add new boards and usb menu to boards.txt, I have written tools/makeboards.py (this is how we manage boards in Adafruit repo), this help to ease the new boards addition, and fix typo issue, just update the script then run python tools/makeboards.py > boards.txt. It does catch some of your existing typo, please let us know if this looks good to you. Otherwise we can skip this, and just copy/paste & modify manually.

This is really convenient and very friendly to me. But what I'm worried about is that if someone doesn't have a py environment, he might add a board by directly copying it.

  • ch32v307 usb highspeed only work when compiling with HSE (144mhz), I tried with HSI but it does not work, this is probably due to usbhs clock setup. I am not too familliar with this, if possible, pleaes help us out with this

What I know is that if you want to use USBHS for ch32v307, you must use HSE, because the internal HSI has frequency offset and may not be applicable to all

  • ch32v103 isn't working, I have no idea why, I tried to add it to tinyusb core, but it doesnn't work there as well. Any helps is appreciated

I haven't found out what the problem is yet.

@TianpeiLee TianpeiLee merged commit 0d344fa into openwch:main Jun 20, 2024
@hathach
Copy link
Contributor Author

hathach commented Jun 20, 2024

This is really convenient and very friendly to me. But what I'm worried about is that if someone doesn't have a py environment, he might add a board by directly copying it.

@TianpeiLee thank you for the merge, no worry for python it is rather universal and easy to install for windows (for linux/mac it comes with the OS).

What I know is that if you want to use USBHS for ch32v307, you must use HSE, because the internal HSI has frequency offset and may not be applicable to all

Ah, glad to know that, I will do more test with other freq configuration (96/48 etc ..) so far I only tested with 144Mhz HSE. Will put some error/note message for this later on

I haven't found out what the problem is yet.

feel free to make an PR if you figure out anything.

@hathach hathach deleted the add-tinyusb-menu branch June 27, 2024 04:27
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.

3 participants