CherryUSB / cherryusb_wch

CherryUSB demo for WCH
19 stars 9 forks source link

ch32v103 #8

Closed nikitos1550 closed 1 year ago

nikitos1550 commented 1 year ago

Hello! Seems ch32v103 has same usb IP as ch32v307 USBFS. On ch32v307 it is mapped on 0x50000000, while on ch32v103 it is mapped on 0x40023400. Going through datasheets I did not find any difference in registers list.

I made this change to usb_ch32_usbfs_reg.h

//#define USBFS_BASE ((uint32_t)0x50000000)
#define USBFS_BASE ((uint32_t)0x40023400)

And trying run some device example (i have startup code for ch32v103 and other things related) like cdc. Unfortunately My Linux host cannot enumerate device. I added some ugly log to the cherryusb and port to understand what is going on. Seem first I get SETUP request

[I/USB] Setup: bmRequestType 0x80, bRequest 0x06, wValue 0x0100, wIndex 0x0000, wLength 0x0040

Then cherryusb try to send 18 bytes descriptor to EP0 and then I get invoke of usbd_event_ep0_in_complete_handler. it decrement g_usbd_core.ep0_data_buf_residue and start reading usbd_ep_start_read(USB_CONTROL_OUT_EP0, NULL, 0);. Here I expect to get some out interrupt, but for some reason I still get IN IRQ and go into usbd_event_ep0_in_complete_handler again and again, it decrement g_usbd_core.ep0_data_buf_residue again and again (i think developers were not expected such thing and there is no any protection about it), anyway I make check if there is no any bytes to send, then do not decrement it, but I still getting this function invocation.

How can I debug this? Maybe it is some common problem? I do not expect any hardware issue, as ch32v103 usb examples works fine.

nikitos1550 commented 1 year ago

some log, not very useful, but anyway:

[I/USB] Setup: bmRequestType 0x80, bRequest 0x06, wValue 0x0100, wIndex 0x0000, wLength 0x0040
!!! 64, 64                        // usbd_event_ep0_setup_complete_handler
int 18 64                        //  setuping 18 bytes to send
send data or status 18  //  and invoke port send function

usbd_ep_start_write l 18  //
ep_idx = 0                        // sending 18 bytes, comparing to 64 max
data_len 18 64                 //
finish                                //

in0                                    // this is from IRQ handler I got it because data sent

usbd_event_ep_in_complete_handler        //
usbd_event_ep0_in_complete_handler 18 // all data sent, start reading
+++18 536871130 0                                    //
sent                                                             //
start read                                                     //

usbd_ep_start_read l 0                               // port read func invoke

in0                                                               // again getting in irq for ep0
usbd_event_ep_in_complete_handler       //
usbd_event_ep0_in_complete_handler 18 //
!!EEEE                                                         // this is message that we try decrement 0 for 18 bytes
+++18 536871148 0
sent
start read
usbd_ep_start_read l 0
in0
... again and again same 
nikitos1550 commented 1 year ago

After deep evaluation, I can see that ch32v103 has cuted IP similar as ch32v307 USBFS. Adjustment needed.

nikitos1550 commented 1 year ago

Finally after regs file adjustment I got

CherryUSB device cdc acm example
3.3V
ENABLE
cdc_acm_init() done
usbd_set_address 0
[I/USB] Setup: bmRequestType 0x80, bRequest 0x06, wValue 0x0100, wIndex 0x0000, wLength 0x0040
[D/USB] EP0 send 18 bytes, 0 remained
[D/USB] EP0 recv out status
usbd_set_address 0
[I/USB] Setup: bmRequestType 0x00, bRequest 0x05, wValue 0x0027, wIndex 0x0000, wLength 0x0000
usbd_set_address 39
[D/USB] EP0 send 0 bytes, 0 remained
[I/USB] Setup: bmRequestType 0x80, bRequest 0x06, wValue 0x0100, wIndex 0x0000, wLength 0x0012
[D/USB] EP0 send 18 bytes, 0 remained
[D/USB] EP0 recv out status
[D/USB] EP0 send 18 bytes, -18 remained
[D/USB] EP0 send 64 bytes, -82 remained
[D/USB] EP0 send 64 bytes, -146 remained
[D/USB] EP0 send 64 bytes, -210 remained
[D/USB] EP0 send 64 bytes, -274 remained
[D/USB] EP0 send 64 bytes, -338 remained
[D/USB] EP0 send 64 bytes, -402 remained
[D/USB] EP0 send 64 bytes, -466 remained
[D/USB] EP0 send 64 bytes, -530 remained
[D/USB] EP0 send 64 bytes, -594 remained
[D/USB] EP0 send 64 bytes, -658 remained
[D/USB] EP0 send 64 bytes, -722 remained
usbd_set_address 0
nikitos1550 commented 1 year ago

dmesg

[189831.352753] usb 3-13.2: new full-speed USB device number 101 using xhci_hcd
[189831.580926] usb 3-13.2: unable to read config index 0 descriptor/start: -75
[189831.580930] usb 3-13.2: can't read configurations, error -75
[189831.684676] usb 3-13.2: new full-speed USB device number 102 using xhci_hcd
[189847.092758] usb 3-13.2: device descriptor read/64, error -110
sakumisu commented 1 year ago

Only support v307, thanks.

sakumisu commented 1 year ago

Although its ip is the same, but some registers will be not the same we do not know, and we will not support other chips.

nikitos1550 commented 1 year ago

Although its ip is the same, but some registers will be not the same we do not know, and we will not support other chips.

why you do not want support other WCH chips? Are there some issues with WCH?

sakumisu commented 1 year ago

No official support, i do not want to waste my time on this.And their usb ips are the same but not the same.

nikitos1550 commented 1 year ago

Got it. Thanks. If I manage get it working on ch32v103, I let you know.