tinyusb icon indicating copy to clipboard operation
tinyusb copied to clipboard

Rp2040 - ISO API to make Audio work / Merge to master

Open pschatzmann opened this issue 1 year ago • 1 comments

Follow up of https://github.com/hathach/tinyusb/pull/2844

Following the discussion that the RP2040 is missing the new ISO API, here is my take on it. I added the 2 following methods

  • bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size)
  • bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)

I was keeping the existing logic and I just needed to split up some functions to fit the new logic.

This is addressing this issue. See also the discussion in this https://github.com/hathach/tinyusb/pull/1802#issuecomment-2410581586

I hope that now only the following files will show up as changed:

  • tusb_mcu.h (to activate the ISO API)
  • dcd_rp2040.c (see above)
  • video_device.c (to correct a data cast error because ep_addr was defined as uint16_t instead of uint8_t)

pschatzmann avatar Oct 15 '24 21:10 pschatzmann

Just did a forced push to set you as the author (forgot in clean up) and trigger the CI.

Some simple tests are included in hil_test

@hathach Seems pre-commit test is bugged.

HiFiPhile avatar Oct 15 '24 21:10 HiFiPhile

This PR works great - Pico has been running for days without problems with this patch applied.

Perhaps rebasing and force-pushing will get us around the CI problems?

kholia avatar Nov 27 '24 07:11 kholia

Hi @hathach!

The following patch breaks USB (in particular the CDC+UAC2 composite device example) on a Pico 2 board.

commit 7f61a5a43b4637afc323f6e63cc59330f1b1e5a6
Author: hathach <[email protected]>
Date:   Thu Nov 28 15:56:47 2024 +0700

    made change per reviews, remove dcd_edpt_close(), rename and move thing around

 src/common/tusb_common.h                     |   1 +
 src/portable/raspberrypi/rp2040/dcd_rp2040.c | 167 +++++++++------------------
 2 files changed, 55 insertions(+), 113 deletions(-)

Here is the git bisect log:

[dhiru@zippy tinyusb]$ git bisect good
Bisecting: 57 revisions left to test after this (roughly 6 steps)
[fa523a5682d8dc91cc1c7a63475623743e37ac83] make sure usb buffer occupies whole cache line when DCACHE is enabled for msc,cdc,hid HIL enable device DMA for p4
[dhiru@zippy tinyusb]$ git bisect good
Bisecting: 28 revisions left to test after this (roughly 5 steps)
[9e4b855e5309976ead84d6cc65288e0e03f27ba5] minor clean up
[dhiru@zippy tinyusb]$ git bisect good
Bisecting: 12 revisions left to test after this (roughly 4 steps)
[c6dccffa2d81f692ca3f9729978ee37f7ed6281d] Merge pull request #2847 from pschatzmann/rp2040-iso
[dhiru@zippy tinyusb]$ git bisect bad 
Bisecting: 8 revisions left to test after this (roughly 3 steps)
[5bb90efd5ff59d3b1d67f3c9269ce1aa57a925a6] Merge pull request #2848 from DavidEGrayson/pr_stm32c0
[dhiru@zippy tinyusb]$ git bisect good
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[31429271141e5af70bbcbe28804442d4f2ffb9ba] Merge pull request #2852 from GuavTek/recover_zero_length_desc
[dhiru@zippy tinyusb]$ git bisect good
Bisecting: 2 revisions left to test after this (roughly 1 step)
[970a03e3989b1771db59784cc6343502ba97d9a8] Merge branch 'hathach:master' into rp2040-iso
[dhiru@zippy tinyusb]$ git bisect good
Bisecting: 0 revisions left to test after this (roughly 1 step)
[7f61a5a43b4637afc323f6e63cc59330f1b1e5a6] made change per reviews, remove dcd_edpt_close(), rename and move thing around
[dhiru@zippy tinyusb]$ git bisect bad 
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[c514a8c87993abf481f3eda5f206765e4422571b] Merge branch 'master' into fork/pschatzmann/rp2040-iso
[dhiru@zippy tinyusb]$ git bisect good
7f61a5a43b4637afc323f6e63cc59330f1b1e5a6 is the first bad commit

kholia avatar Dec 06 '24 07:12 kholia

I was testing your changes to the dcd_rp2040.c and noticed that the audio is not working after your correctins. I could trace down the issue to your version of the following method

// New API: Configure and enable an ISO endpoint according to descriptor
bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * ep_desc) {
  (void) rhport;
  const uint8_t ep_addr = ep_desc->bEndpointAddress;
  // Fill in endpoint control register with buffer offset
  struct hw_endpoint* ep = hw_endpoint_get_by_addr(ep_addr);
  TU_ASSERT(ep->hw_data_buf != NULL); // must be inited and buffer allocated
  ep->wMaxPacketSize = ep_desc->wMaxPacketSize;

  hw_endpoint_enable(ep);
  return true;
}

Moving it back to the original version which is calling hw_endpoint_init() makes it work again

// New API: Configure and enable an ISO endpoint according to descriptor
bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * ep_desc) {
  (void) rhport;
  const uint8_t ep_addr = ep_desc->bEndpointAddress;

  // init w/o allocate
  const uint16_t mps = ep_desc->wMaxPacketSize;
  uint16_t size = (uint16_t)tu_div_ceil(mps, 64) * 64u;
  hw_endpoint_init(ep_addr, size, TUSB_XFER_ISOCHRONOUS);

  // Fill in endpoint control register with buffer offset
  struct hw_endpoint* ep = hw_endpoint_get_by_addr(ep_addr);
  TU_ASSERT(ep->hw_data_buf != NULL); // must be inited and buffer allocated
  ep->wMaxPacketSize = ep_desc->wMaxPacketSize;

  hw_endpoint_enable(ep);
  return true;
}

Please advise about the next steps... ps. this also fixes the headest issue mentioned above

pschatzmann avatar Jan 04 '25 20:01 pschatzmann