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 WCH CH32V307 port #1405

Merged
merged 22 commits into from Jan 12, 2023
Merged

Add WCH CH32V307 port #1405

merged 22 commits into from Jan 12, 2023

Conversation

gregdavill
Copy link
Contributor

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,

Add WCH mcu submodule
Add CH32V307V-R1-1v0 bsp
hw/bsp/ch32v307/Link.ld Outdated Show resolved Hide resolved
@perigoso
Copy link
Collaborator

perigoso commented Mar 22, 2022

could you add a build workflow for this?
see .github/workflows/build_riscv

@gregdavill
Copy link
Contributor Author

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.

@perigoso
Copy link
Collaborator

Yes, i think so. I can approve.

@perigoso
Copy link
Collaborator

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

USBHSD->HOST_CTRL = USBHS_PHY_SUSPENDM;

USBHSD->CONTROL = 0;
#if 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be behing the BOARD_DEVICE_RHPORT_SPEED variable?

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've just tested, and the device doesn't correctly enumerate when set to Full Speed mode.
Something to look into, as a placeholder I'll have it throw an error if BOARD_DEVICE_RHPORT_SPEED==OPT_MODE_FULL_SPEED

src/common/tusb_mcu.h Outdated Show resolved Hide resolved
src/common/tusb_mcu.h Outdated Show resolved Hide resolved
Copy link
Collaborator

@perigoso perigoso left a comment

Choose a reason for hiding this comment

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

looks good overall, only missing the docs, but that can be added after

@perigoso
Copy link
Collaborator

Just for easier reference, you mention initial driver, what is missing currently?

@gregdavill
Copy link
Contributor Author

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.

Copy link
Collaborator

@perigoso perigoso left a comment

Choose a reason for hiding this comment

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

Looks good to me, let's wait for @hathach to look at this

docs/reference/supported.rst Outdated Show resolved Hide resolved
@gregdavill
Copy link
Contributor Author

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.

@brouhaha
Copy link

brouhaha commented Mar 23, 2022 via email

@perigoso
Copy link
Collaborator

sounds like it might have a synopsys usb core, very popular among MCUs, it might be compatible with the dwc2 driver?

@gregdavill
Copy link
Contributor Author

I've been digging into this a tad more, there is actually 4 different USB peripherals in the CH32FV32x_V3x Reference manual.

  • USBD Looks compatible with the STMF103, WCH have a CortexM3 device that is pin compatible with the F103
  • USBHD Register equivalent to the USBHS but FS only.
  • USBHS This port, Host/Device with integrated HS PHY.
  • OTG_FS/USB_OTG Doesn'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.

@perigoso
Copy link
Collaborator

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

@hathach
Copy link
Owner

hathach commented Mar 29, 2022

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.

@perigoso
Copy link
Collaborator

perigoso commented Mar 29, 2022

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

@gregdavill
Copy link
Contributor Author

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.

@hathach
Copy link
Owner

hathach commented Mar 30, 2022

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.

@hathach
Copy link
Owner

hathach commented Apr 5, 2022

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.

@arturo182
Copy link
Contributor

@hathach any idea if you'd have time to look into this PR in the near future?

@hathach
Copy link
Owner

hathach commented May 21, 2022

this PR is on my TODO, though I am currently busy with other paid work. Please be patient in the meanwhile.

@perigoso
Copy link
Collaborator

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

@hathach
Copy link
Owner

hathach commented Jul 18, 2022

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.

@mattytrentini
Copy link

@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
Copy link
Owner

hathach commented Jan 6, 2023

@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.

Copy link
Owner

@hathach hathach left a comment

Choose a reason for hiding this comment

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

special thanks to @gregdavill for this brilliant PR and others joining this discussio, and sorry for my huge delay in reviewing and testing this. I struggled a bit with openocd on linux, but finally got everything running. (also put some doc to the family.mk for building it). USB highspeed work well with cdc and msc (hid example has some hiccups but we will fix it later on).

CH32V307 is really an interesting mcu with dual usb controllers, one of which is highspeed, and bundle lots of other great peripherals as well. I am not too familiar with the chip and only make some minor tweak (mostly bsp and rename). Hopefully we could fully support this mcu in the future with more follow-up PRs.

@hathach hathach merged commit fa9d190 into hathach:master Jan 12, 2023
@gregdavill
Copy link
Contributor Author

Thanks @hathach! I think I might have found/fixed some of those issues.. I know I hit some issues trying to use multiple IN/OUT endpoints, after some adjustments to the endpoint handling code it was working nicely on a local project branch.
So I'll sync those changes and take a look at merging them in.

It's been a bit of time since I did the initial work, but supporting the FS PHY I think should be easy, since it's similar/the same as the STM's one.

@hathach
Copy link
Owner

hathach commented Jan 12, 2023

Thanks @hathach! I think I might have found/fixed some of those issues.. I know I hit some issues trying to use multiple IN/OUT endpoints, after some adjustments to the endpoint handling code it was working nicely on a local project branch. So I'll sync those changes and take a look at merging them in.

That is great to hear, I wished I could have more time to looked into ch32v307 dcd code and do more testing with other class drivers as well.

It's been a bit of time since I did the initial work, but supporting the FS PHY I think should be easy, since it's similar/the same as the STM's one.

Yeah, you are right. I just checked, it is similar to the stm fsdev driver. Ideally we should generalize the driver ( shared .c file like dwc2). Though I think we can start with duplicating driver first to make sure hardware is all working. I can actually help with this if I manage the time. Though I guess there is no rush to support FS since most people would prefer to use the HS port for device.

PS: Thanks again for the great work on this.
PS2: fsdev is also used by some of gd32f10x as well, so I guess make it generic would make it easier to add support for similar mcus.

@aurimasniekis
Copy link

Hi, I am curious if there is any progress on working on both ports at the same time? I have really interesting case where I need host on fs, and device on hs

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.

None yet

7 participants