tinyusb
tinyusb copied to clipboard
Add WCH CH32V307 port
Describe the PR
Add initial driver/port for the CH32V307 series from WCH. This is a RISCV (rv32imac) with integrated High Speed USB PHY.
The targets the WCH development board CH32V307V-R1-1v0, which is a lowish cost ~$10 board from LCSC.
Additional context
The xpack-riscv-gcc compiler is used. The official wch packaged gcc appears to have some custom changes to support the interrupt controller in this part that handles hw stack save/restore. Fortunately that's reasonably easy to work around, so that a vanilla compiler works.
Right now I've been testing this port with the cdc_msc example, so I thought I'd open up the PR.
I'd like to help get this merged so that others can use tinyusb on this new IC too, it's a pretty nice combination pairing the high-speed PHY inside a lower cost microcontroller,
could you add a build workflow for this? see .github/workflows/build_riscv
Added. I think just adding to the family will be enough?
But I think they won't run unless a maintainer approves them to run.
Yes, i think so. I can approve.
you need to add the family to the skip files on freertos examples if you don't have the freertos port configured examples/device/hid_composite_freertos/skip examples/device/cdc_msc_freertos/skip
Just for easier reference, you mention initial driver, what is missing currently?
I've not tested with a Full Speed only host/hub. Which I think will be the same as trying to configure the device as Full Speed only. Which should work, but does not appear to right now.
- Only supports bulk endpoints.
- Only supports device mode.
- No host mode.
I've also not done extensive testing yet, I've only had the dev board for <1week.
I should also mention. The CH32V307 has 2 separate USB interfaces/peripherals, One is FS/OTG and then one is HS/FS/Device/Host. These use different pins on the device, and have completely different register sets.
This port is only targeting the High Speed peripheral.
The CH32V307 USB FS/OTG interface looks to be identical to, or else very very similar to, the STM32 USB FS/OTG interface on e.g. the STM32F103, so it would probably be fairly simple to add support. (I know, easy for me to say.) There are also STM32 parts that have basically the same dual USB (FS & HS) as the CH32V307.
On Tue, Mar 22, 2022 at 6:09 PM Greg Davill @.***> wrote:
I should also mention. The CH32V307 has 2 separate USB interfaces/peripherals, One is FS/OTG and then one is HS/FS/Device/Host. These use different pins on the device, and have completely different register sets.
This port is only targeting the High Speed peripheral.
— Reply to this email directly, view it on GitHub https://github.com/hathach/tinyusb/pull/1405#issuecomment-1075770763, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADLYXVRXJSE627I7IEUAXDVBJOKRANCNFSM5RH3PL2A . You are receiving this because you are subscribed to this thread.Message ID: @.***>
sounds like it might have a synopsys usb core, very popular among MCUs, it might be compatible with the dwc2 driver?
I've been digging into this a tad more, there is actually 4 different USB peripherals in the CH32FV32x_V3x Reference manual.
USBDLooks compatible with the STMF103, WCH have a CortexM3 device that is pin compatible with the F103USBHDRegister equivalent to theUSBHSbut FS only.USBHSThis port, Host/Device with integrated HS PHY.OTG_FS/USB_OTGDoesn't match the STM32's USB peripheral.
Config summary
| V203 | V303 | V305* | V307 | V208 | |
|---|---|---|---|---|---|
| USBD | ✔ | ✔ | |||
| USBHD | ✔ | ✔ | ✔ | ||
| USBHS | ✔ | ✔ | |||
| OTG-FS | ✔ | ✔ |
*V305 should be compatible with this port, but hasn't been released yet.
So sounds like it should be possible to support some of these using the dwc2 driver.
Can you point me at the STM32 devices that have a similar HS/FS pair? The only ones I've seen/worked with were definitely a different register set, and needed an external ULPI PHY.
eit, thats a bit of a mess, lets not worry too much for now then.
to clarify, does USBHS in any way look compatible with the STMF103 usb registers?
Great work for the PR, unfortunately I have spent last 2 day with sleep-eat cycle in high fever due to covid-19. I feel much better now, but will take a couple more days to rest. And haven't followed this pr so far, but I will try to review this in the next week or so. Please be patient and keep up the good work. Thank you.
Take all the time you need. Glad to hear you're starting to feel better.
There is no rush on getting this merged on my end.
On Thu, Mar 24, 2022, 06:55 Rafael Silva @.***> wrote:
to clarify, does USBHS in any way look compatible with the STMF103 usb registers?
I don't think so. Judging by a somewhat cursory comparison of the CH32V307 docs to various ST docs:
The CH32V307 USBHS looks like the USBHS on the bigger/newer STM32 parts. AFAIK it doesn't look like any STM32 USBFS controller.
The CH32V307 USBFS looks like the STM32F103 (and many other STM32) USBFS.
Some STM32 have both, in the same way the CH32V307 has both. I think STM32F205 or STM32F207 have both, but I haven't double-checked.
Eric
The CH32V307 USBFS looks like the STM32F103 (and many other STM32) USBFS.
I agree with this, I suspect since WCH has already used this IP on it's STM32 replacements it makes sense.
The CH32V307 USBHS looks like the USBHS on the bigger/newer STM32 parts. AFAIK it doesn't look like any STM32 USBFS controller.
Some STM32 have both, in the same way the CH32V307 has both. I think STM32F205 or STM32F207 have both, but I haven't double-checked.
Looking through the STM32F207 Reference manual the controller it has for OTG_HS peripheral, is in fact from Synopsys, the section has this notice at the start.
# OTG_HS introduction
Portions Copyright (c) 2004, 2005 Synopsys, Inc. All rights reserved. Used with permission.
This section presents the architecture and the programming model of the OTG_HS controller.
Looking over the register map it's not related at all to the the HS controller in the CH32V307. There are no similarities.
Here is the CSR map from the STM32, where it shows that in the CSR there is fixed memory mapped FIFO.

The CH32V307 has a DMA where you're able to specify endpoint buffers anywhere in the memory space, and it also supports more endpoints.
thank @perigoso for helping with the PR so far. @gregdavill I took a quick look at ch32v307
- USBD (FS) is exactly like the stm32 fsdev
- USBHS D/H on the other hand is completely new to me, and definitely not dwc2. Therefore we are good with current file structure.
I still haven't looked at actual code changes, will do that later on.
update: I just ordered ch32v307 / v103 to test with this PR, since they are interested mcu. Interestingly, wch also has one ch595 with USB 3.0 but with rather limited ram/rom but its dev board is not available. Looking forward to test this out.
It is an interesting chip indeed, I have a couple on the cart waiting for an order
There is a dev board you can find on aliexpress, it's not very affordable but it's an option https://aliexpress.com/item/1005003617496688.html
What would need to be done on the usb stack side to get usb 3.0? enumeration is still done on the usb 2.0 right?
The technical director at WCH is also very open, you can contact him through twitter maybe they could even send you one @hathach
https://twitter.com/Patrick_RISCV
It is an interesting chip indeed, I have a couple on the cart waiting for an order
There is a dev board you can find on aliexpress, it's not very affordable but it's an option https://aliexpress.com/item/1005003617496688.html
yeah, I saw that board as well, but I will wait for a cheaper option. I won't have time to do anything with it anything soon anyway.
What would need to be done on the usb stack side to get usb 3.0? enumeration is still done on the usb 2.0 right?
hard to tell, I never look at usb 3.0 specs, may need to update my own knowledge first. Will do that later when I have both the board and time.
The technical director at WCH is also very open, you can contact him through twitter maybe they could even send you one @hathach
https://twitter.com/Patrick_RISCV
that is too much hassle for an dev board, and there is no guarantee that I would spend anytime on it to begin with. Let ch569 have its own PR. We should better focus on v307 on this PR instead.
Oh i wasn't trying to bundle the ch569 on here at all, just a suggestion for a possible way of getting a dev board, since you were interested
I'm planing to poke around with the ch565/ch569 at some point too. Have some of the ICs.
Let me know when you get your CH32V307 devkit. Loading firmware currently requires the use of a "patched" openOCD, which can be downloaded as part of the MounRiver Toolchain (http://www.mounriver.com/download). I hope that eventually support for the WCH-Link and this IC are upstreamed into openOCD.
Oh i wasn't trying to bundle the ch569 on here at all, just a suggestion for a possible way of getting a dev board, since you were interested
thanks for your suggestion.
I'm planing to poke around with the ch565/ch569 at some point too. Have some of the ICs.
Let me know when you get your CH32V307 devkit. Loading firmware currently requires the use of a "patched" openOCD, which can be downloaded as part of the MounRiver Toolchain (http://www.mounriver.com/download). I hope that eventually support for the WCH-Link and this IC are upstreamed into openOCD.
thanks for the hint, it probably got delivered in a week or so, will let you know by then.
my ch32v307 dev board has arrived, however, I am currently busy catching up with paid work and won't probably look at this pr this week. I will try my best to review this as soon as I could. Please be patient.
@hathach any idea if you'd have time to look into this PR in the near future?
this PR is on my TODO, though I am currently busy with other paid work. Please be patient in the meanwhile.
Apparently, the dev board for the CH569 is now available at a reasonable price on LCSC https://www.lcsc.com/product-detail/Development-Boards-Kits_WCH-Jiangsu-Qin-Heng-CH569W-EVT-R0-1V0_C3001176.html
thanks @perigoso (late response again), I actually ordered this, but it is now kept in the custom. and it has been a PITA with custom to get this particular package.
@hathach I'm sure you're busy but I'd love to see this PR progress when you have time! There's a PR that has begun to add support for the v307 to MicroPython and I'd like to add TinyUSB to the mix.
I have a v307 dev board so if I can help test just let me know.
@hathach I'm sure you're busy but I'd love to see this PR progress when you have time! There's a PR that has begun to add support for the v307 to MicroPython and I'd like to add TinyUSB to the mix.
I have a v307 dev board so if I can help test just let me know.
yeah, sure thing. I was waiting for wch devkit to test this out (it takes 2 months to ship) and I totally forgot about this one. Will test this out as soon as I could.