tinyusb icon indicating copy to clipboard operation
tinyusb copied to clipboard

DCD SAM3U Support

Open perigoso opened this issue 4 years ago • 36 comments

Signed-off-by: Rafael Silva [email protected]

Describe the PR Implementation of a DCD driver for the SAM3U.

Additional context The SAM3U seems to have a similar USB core to the SAMx7x family, for which support is being added in #693, but the register map seems to be completely different. That said, thanks to that it should be easier to port.

Datasheet for reference.

  • [x] Port dcd based on SAM7X;
  • [x] Successful build;
  • [ ] Working examples (cdc_msc, hid_composite);

perigoso avatar Jun 11 '21 23:06 perigoso

if it is the same controller, it should be merged together, instead of having 2 different dcds. I have done something similar even across family line for LPC, iMXRT https://github.com/hathach/tinyusb/blob/master/src/portable/nxp/transdimension/dcd_transdimension.c#L30 It is more work initially, but much less work later on when maintaining and fixing bugs.

hathach avatar Jun 12 '21 08:06 hathach

I have to look further into it, the register map seems very different but it still might make sense to join them

perigoso avatar Jun 13 '21 23:06 perigoso

yeah, I really prefer to join them if possible, or at least extract/abstract transfer handling into common API.

hathach avatar Jun 14 '21 04:06 hathach

After reading SAM3U's manual, it seems they are very similar in functionality.

But there are too much differences in register map, I'm afraid we need to spam #ifdef in order to merge them.

HiFiPhile avatar Jun 14 '21 14:06 HiFiPhile

After reading SAM3U's manual, it seems they are very similar in functionality.

But there are too much differences in register map, I'm afraid we need to spam #ifdef in order to merge them.

It is doable with by extracting function to common API and/or some other works. There is no rush to support SAM3U, we do it little by little.

hathach avatar Jun 14 '21 15:06 hathach

The reason I added this is because I actually need a usb driver for the sam3u (as well as the sams70), so im planning on working on this

perigoso avatar Jun 14 '21 22:06 perigoso

The reason I added this is because I actually need a usb driver for the sam3u (as well as the sams70), so im planning on working on this

If you feel adding new dcd is quicker, feel free to do so in this PR to test with your board. Once it is ready, I could help with the merging.

PS: just placed an order for SAM3U to help with this PR, the official EK board from atmel is out of stock everywhere. https://vi.aliexpress.com/item/4001163149713.html?spm=a2g0o.store_pc_groupList.8148356.2.41612820wy8Fks

hathach avatar Jun 15 '21 02:06 hathach

how do i approach the cmsis @hathach ? should i merge into the microchip driver repository? right now I'm relying on this https://github.com/cmsis-packs/cmsis-dfp-sam3u

perigoso avatar Jul 10 '21 23:07 perigoso

I ported the initial driver from the samx7x, without dual port and dma implementation to reduce complexity, I have yet to test compile, but everything should be more or less ready.

I'm going to be out for a week, I won't pick this up again until then.

perigoso avatar Jul 16 '21 20:07 perigoso

@perigoso #693 is finally merged, I've added several fixes later on mostly focused on EP0. I can help review but since I don't have the board I can't test with.

HiFiPhile avatar Jul 21 '21 18:07 HiFiPhile

Waiting on #985 to redo this

perigoso avatar Aug 04 '21 16:08 perigoso

This is still something I want and have to do, but I don't have time right now (due to new job) so If anyone can pick this up for me I would appreciate it (should be only needed to write the header with the registers)

perigoso avatar Dec 02 '21 10:12 perigoso

Started to look at this again, unfortunately, the USB cores are very similar, but just different enough to make it quite hard to make compatible, so I just forked the original samx7x driver and made the necessary changes.

I simply translated as best I could the register from the samx7x to the 3u, but there are a couple features that apparently have slightly different implementations, so I don't believe this is going to work out of the box.

also unfortunately, I don't have any boards with this MCU assembled at the moment. @hathach did you ever get the SAM3U development board?

Any help testing this and finalizing the port would be very appreciated.

perigoso avatar Apr 14 '22 16:04 perigoso

cmsis PR: hathach/microchip_driver#9

Board driver is a todo

perigoso avatar Apr 15 '22 01:04 perigoso

also unfortunately, I don't have any boards with this MCU assembled at the moment. @hathach did you ever get the SAM3U development board?

I got the board, and would love to help but I am way behind my work schedule now, and couldn't have the time. I will revisit this whenever I could.

hathach avatar Apr 16 '22 05:04 hathach

I understand, i the meantime I'll try to assemble some boards with it

@HiFiPhile you think you could take a look? I am certain there are things that beed to be done differently, i recall a couple flags to trigger things in the endpoints status register are very different, i did a naive translation so far

perigoso avatar Apr 16 '22 12:04 perigoso

@HiFiPhile you think you could take a look?

Yes I could help you if you are going to do some testing, but I haven't used sam3u so my assumption won't be accurate :)

HiFiPhile avatar Apr 17 '22 07:04 HiFiPhile

finally tried running this

when I connected in high speed I got constant bus resets to high speed

USBD init on controller 0
sizeof(usbd_device_t) = 47
CDC init
MSC init
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed
USBD Bus Reset : High Speed

when forcing full speed i got this from sam:

USBD init on controller 0
sizeof(usbd_device_t) = 47
CDC init
MSC init
USBD Bus Reset : Full Speed

USBD Setup Received 80 06 00 01 00 00 40 00 
  Get Descriptor Device
  Queue EP 80 with 18 bytes ...
USBD Xfer Complete on EP 80 with 18 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes
USBD Bus Reset : Full Speed

USBD Setup Received 00 05 52 00 00 00 00 00 
  Set Address
USBD Xfer Complete on EP 80 with 0 bytes
    dcd_edpt0_status_complete: complete set address

USBD Setup Received 80 06 00 01 00 00 12 00 
  Get Descriptor Device
  Queue EP 80 with 18 bytes ...
USBD Xfer Complete on EP 80 with 18 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 06 00 00 0A 00 
  Get Descriptor Device Qualifier
  Queue EP 80 with 10 bytes ...
USBD Xfer Complete on EP 80 with 10 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 02 00 00 09 00 
  Get Descriptor Configuration[0]
  Queue EP 80 with 9 bytes ...
USBD Xfer Complete on EP 80 with 9 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 02 00 00 62 00 
  Get Descriptor Configuration[0]
  Queue EP 80 with 64 bytes ...
USBD Xfer Complete on EP 80 with 64 bytes
  Queue EP 80 with 34 bytes ...
USBD Xfer Complete on EP 80 with 34 bytes
  Queue EP 00 with 0 bytes ...
USBD Bus Reset : Full Speed

USBD Setup Received 80 06 00 01 00 00 40 00 
  Get Descriptor Device
  Queue EP 80 with 18 bytes ...
USBD Xfer Complete on EP 80 with 18 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes
USBD Bus Reset : Full Speed

USBD Setup Received 00 05 53 00 00 00 00 00 
  Set Address
USBD Xfer Complete on EP 80 with 0 bytes

USBD Setup Received 80 06 00 01 00 00 12 00 
  Get Descriptor Device
  Queue EP 80 with 18 bytes ...
USBD Xfer Complete on EP 80 with 18 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 06 00 00 0A 00 
  Get Descriptor Device Qualifier
  Queue EP 80 with 10 bytes ...
USBD Xfer Complete on EP 80 with 10 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 02 00 00 09 00 
  Get Descriptor Configuration[0]
  Queue EP 80 with 9 bytes ...
USBD Xfer Complete on EP 80 with 9 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 02 00 00 62 00 
  Get Descriptor Configuration[0]
  Queue EP 80 with 64 bytes ...
USBD Xfer Complete on EP 80 with 64 bytes
  Queue EP 80 with 34 bytes ...
USBD Xfer Complete on EP 80 with 34 bytes
  Queue EP 00 with 0 bytes ...

from pc:

[ter jun 28 23:56:29 2022] usb 1-3: new full-speed USB device number 122 using xhci_hcd
[ter jun 28 23:56:29 2022] usb 1-3: not running at top speed; connect to a high speed hub
[ter jun 28 23:56:29 2022] usb 1-3: unable to read config index 0 descriptor/all
[ter jun 28 23:56:29 2022] usb 1-3: can't read configurations, error -71
[ter jun 28 23:56:29 2022] usb usb1-port3: unable to enumerate USB device

perigoso avatar Jun 28 '22 23:06 perigoso

after usb high speed reset it doesnt seem to get any endpoint interrupts, while it does on full

anyone think tthey can help me diagnose this?

perigoso avatar Jun 28 '22 23:06 perigoso

The host seems to be requesting more data than expected for config descriptos, 0x8d/141 vs 77 for dual cdc, curiously on hid_composite it enumerated

perigoso avatar Jun 28 '22 23:06 perigoso

Full speed HID composite

USBD init on controller 0
sizeof(usbd_device_t) = 47
HID init
USBD Bus Reset : Full Speed

USBD Setup Received 80 06 00 01 00 00 40 00 
  Get Descriptor Device
!USBD Xfer Complete on EP 80 with 18 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes
USBD Bus Reset : Full Speed

USBD Setup Received 00 05 7D 00 00 00 00 00 
  Set Address
USBD Xfer Complete on EP 80 with 0 bytes
  dcd_edpt0_status_complete: complete set address

USBD Setup Received 80 06 00 01 00 00 12 00 
  Get Descriptor Device
!USBD Xfer Complete on EP 80 with 18 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 06 00 00 0A 00 
  Get Descriptor Device Qualifier
!USBD Xfer Complete on EP 80 with 10 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 02 00 00 09 00 
  Get Descriptor Configuration[0]
  Queue EP 80 with 9 bytes ...
USBD Xfer Complete on EP 80 with 9 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 02 00 00 22 00 
  Get Descriptor Configuration[0]
!USBD Xfer Complete on EP 80 with 34 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 00 03 00 00 FF 00 
  Get Descriptor String[0]
  Queue EP 80 with 4 bytes ...
USBD Xfer Complete on EP 80 with 4 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 02 03 09 04 FF 00 
  Get Descriptor String[2]
USBD Xfer Complete on EP 80 with 30 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 01 03 09 04 FF 00 
  Get Descriptor String[1]
!USBD Xfer Complete on EP 80 with 16 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 80 06 03 03 09 04 FF 00 
  Get Descriptor String[3]
!USBD Xfer Complete on EP 80 with 14 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 00 09 01 00 00 00 00 00 
  Set Configuration
�        �
         S�        HID opened
  Bind EP 81 to driver id 0
  Queue EP 80 with 0 bytes ...
USBD Xfer Complete on EP 80 with 0 bytes

USBD Setup Received 80 06 03 03 09 04 FF 00 
  Get Descriptor String[3]
USBD Xfer Complete on EP 80 with 14 bytes
  Queue EP 00 with 0 bytes ...
USBD Xfer Complete on EP 00 with 0 bytes

USBD Setup Received 21 0A 00 00 00 00 00 00 
  HID control request
  Queue EP 80 with 0 bytes ...
USBD Xfer Complete on EP 80 with 0 bytes

USBD Setup Received 81 06 00 22 00 00 EF 00 
  Get Descriptor  HID control request
!)
  ( !�   USBD Xfer Complete on EP 80 with 64 bytes
  Queue EP 80 with 64 bytes ...
USBD Xfer Complete on EP 80 with 64 bytes
  Queue EP 80 with 64 bytes ...
USBD Xfer Complete on EP 80 with 64 bytes
  Queue EP 80 with 47 bytes ...
USBD Xfer Complete on EP 80 with 47 bytes
  HID control complete
  Queue EP 00 with 0 bytes ...
[qua jun 29 00:47:01 2022] usb 1-3: new full-speed USB device number 85 using xhci_hcd
[qua jun 29 00:47:01 2022] usb 1-3: not running at top speed; connect to a high speed hub
[qua jun 29 00:47:01 2022] usb 1-3: New USB device found, idVendor=cafe, idProduct=4004, bcdDevice= 1.00
[qua jun 29 00:47:01 2022] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[qua jun 29 00:47:01 2022] usb 1-3: Product: TinyUSB Device
[qua jun 29 00:47:01 2022] usb 1-3: Manufacturer: TinyUSB
[qua jun 29 00:47:01 2022] usb 1-3: SerialNumber: 123456
[qua jun 29 00:47:22 2022] usbhid 1-3:1.0: can't add hid device: -110
[qua jun 29 00:47:22 2022] usbhid: probe of 1-3:1.0 failed with error -110

perigoso avatar Jun 28 '22 23:06 perigoso

@hathach / @HiFiPhile Do you have any tips on what to look for?

on high speed it seems to get stuck on the event bus reset on the queue, like it doesnt clear, it did not get more that the first interrupt for the bus reset

perigoso avatar Jul 01 '22 23:07 perigoso

I'm suspecting some hardware issue, which board are you using ? Which board is Russian Woodpecker ?

HiFiPhile avatar Jul 02 '22 10:07 HiFiPhile

It's a board by me, i don't have access to any dev boards or premade hardware with one, but it should be good, in theory

https://github.com/openinput-fw/russian-woodpecker

perigoso avatar Jul 02 '22 14:07 perigoso

It's a board by me

Okay, could you share a pdf version ? I don't have kicad :)

Which ESD diode are you using ? image

HiFiPhile avatar Jul 02 '22 14:07 HiFiPhile

There's a PDF in the releases https://github.com/openinput-fw/russian-woodpecker/releases/tag/r0.1

perigoso avatar Jul 04 '22 08:07 perigoso

the ESD is a USBLC6-2SC6

perigoso avatar Jul 04 '22 08:07 perigoso

Nice layout :) When I have time I'll compare with ASF3 to see if there is any difference.

HiFiPhile avatar Jul 04 '22 09:07 HiFiPhile

What do you mean by that? the asf code for the USB peripheral? do you know of an example by them relatively ready to go for this device? I can't find one suitable

perigoso avatar Jul 05 '22 19:07 perigoso

the asf code for the USB peripheral

Yes ASF3 udphs driver.

I can't find one suitable

Yes they lack a basic example !

When I wrote the driver for SAMx7x I found its USB signal is weaker than other MCUs, @hathach had to use a 30cm cable to let USB work. I don't know if it is the same for SAM3U.

HiFiPhile avatar Jul 06 '22 07:07 HiFiPhile

Hm, actually, when i used a longer cable it showed a different behavior, didn't even get to a bus reset, but I'm using a rather short cable right now, could look for something shorter

perigoso avatar Jul 06 '22 12:07 perigoso