Skip to content

drivers: usb: Add RP2040 USB device support. #42506

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

Merged

Conversation

petejohanson
Copy link
Contributor

Initial work on the USB device driver for RaspberryPi Pico SoC.

I'm quite certain there are some gaps and missing bits, and some that just need cleaning up, perhaps with feedback from others that know the stack better than I.

@github-actions github-actions bot added area: Devicetree area: Devicetree Binding PR modifies or adds a Device Tree binding labels Feb 6, 2022
@jfischer-no jfischer-no added area: Drivers area: USB Universal Serial Bus labels Feb 10, 2022
@jfischer-no jfischer-no added this to the v3.1.0 milestone Feb 10, 2022
@jfischer-no jfischer-no self-assigned this Feb 10, 2022
@jfischer-no jfischer-no self-requested a review February 10, 2022 08:28

/* Helper macros to make it easier to work with endpoint numbers */
#define EP0_IDX 0
#define EP0_IN (EP0_IDX | USB_EP_DIR_IN)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please use USB_CONTROL_EP_OUT, USB_CONTROL_EP_OUT, USB_CONTROL_EP_MPS from include/usb/usb_ch9.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Did keep the separate DATA_BUFFER_SIZE define, since that's used beyond just EP0 usage.

if (status & USB_INTS_SETUP_REQ_BITS) {
handled |= USB_INTS_SETUP_REQ_BITS;
usb_hw_clear->sie_status = USB_SIE_STATUS_SETUP_REC_BITS;
usb_dc_rpi_pico_handle_setup();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Status and endpoint callbacks processing should be offloaded to a thread/workqueue, otherwise device stack, class or application code would be executed in drivers ISR context, see usb_dc_mcux or usb_dc_kinetis driver

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before doing this.... Any reason to use a dedicated thread, versus submitting to the main system work queue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reviewed this, and went w/ a dedicated thread, like the other drivers. Feedback welcome.

@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from 651d0af to 0dd4490 Compare February 10, 2022 19:06
@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch 10 times, most recently from c71e9b9 to 5f0b8d8 Compare February 11, 2022 05:06
@petejohanson petejohanson marked this pull request as ready for review February 11, 2022 05:09
@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch 2 times, most recently from ac931af to 38fb1d0 Compare February 17, 2022 05:04
@jfischer-no jfischer-no self-requested a review February 23, 2022 11:03
Copy link
Collaborator

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

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

Sorry for delay, I finally had time to prepare the hardware so that I can flash it using DAPLink probe and pyOCD and test this driver. Basically it seems to work, let it polish and get in ASAP.

Comment on lines 225 to 218
msg.cb = usb_hw->sie_status & USB_SIE_STATUS_CONNECTED_BITS ?
USB_DC_CONNECTED :
USB_DC_DISCONNECTED;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This, CONNECTED vs. DISCONNECTED, seems to be reversed.

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'm not sure it is? Seems like the connected status bit being present should be USB_DC_CONNECTED.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I observed it like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will swap, I haven't had a chance to test this myself directly.

Comment on lines 799 to 746
} else if (state.status_cb) {
switch (msg.cb) {
case USB_DC_RESET:
state.status_cb(USB_DC_RESET, NULL);
break;
case USB_DC_ERROR:
state.status_cb(USB_DC_ERROR, NULL);
break;
case USB_DC_SUSPEND:
state.status_cb(USB_DC_SUSPEND, NULL);
break;
case USB_DC_RESUME:
state.status_cb(USB_DC_RESUME, NULL);
break;
default:
LOG_ERR("unknown msg");
break;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

diff --git a/drivers/usb/device/usb_dc_rpi_pico.c b/drivers/usb/device/usb_dc_rpi_pico.c
index 84bc8f0cbea..48577e668be 100644
--- a/drivers/usb/device/usb_dc_rpi_pico.c
+++ b/drivers/usb/device/usb_dc_rpi_pico.c
@@ -793,26 +793,12 @@ static void udc_rpi_thread_main(void *arg1, void *unused1, void *unused2)
                                }
                                break;
                        default:
-                               LOG_ERR("unknown msg");
+                               LOG_ERR("unknown ep msg");
                                break;
                        }
-               } else if (state.status_cb) {
-                       switch (msg.cb) {
-                       case USB_DC_RESET:
-                               state.status_cb(USB_DC_RESET, NULL);
-                               break;
-                       case USB_DC_ERROR:
-                               state.status_cb(USB_DC_ERROR, NULL);
-                               break;
-                       case USB_DC_SUSPEND:
-                               state.status_cb(USB_DC_SUSPEND, NULL);
-                               break;
-                       case USB_DC_RESUME:
-                               state.status_cb(USB_DC_RESUME, NULL);
-                               break;
-                       default:
-                               LOG_ERR("unknown msg");
-                               break;
+               } else {
+                       if (state.status_cb) {
+                               state.status_cb(msg.cb, NULL);
                        }
                }
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored this to simplify greatly. Leaving open until you review again.

@yonsch
Copy link
Collaborator

yonsch commented Mar 16, 2022

Please also add USB to the table of supported features in the board docs


if (data) {
memcpy(ep_state->buf, data, len);
val |= USB_BUF_CTRL_FULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

val |= USB_BUF_CTRL_FULL; should go outside of the condition, otherwise, if the packet size is a multiplicity of the buffer size then the transfer will stall. This is a zero-length-packet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved this out. Thanks!

@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from 38fb1d0 to 588084f Compare April 13, 2022 14:44
@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from 53665e1 to 7c73b26 Compare April 13, 2022 15:08
@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from 7c73b26 to bab74cb Compare April 13, 2022 15:20
@jfischer-no jfischer-no added the dev-review To be discussed in dev-review meeting label Apr 13, 2022
@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from bab74cb to 119f26d Compare April 14, 2022 04:00
@jfischer-no
Copy link
Collaborator

jfischer-no commented Apr 14, 2022

Summary for the dev-review:

rpi_pico postfix can be confusing if someone is explicitly looking for the SoC name. Should we keep naming scheme or rename?

dev-review result: overwhelming majority has voted to keep rpi_pico postfix.

@MaureenHelm MaureenHelm removed the dev-review To be discussed in dev-review meeting label Apr 14, 2022
@yonsch yonsch added the platform: Raspberry Pi Pico Raspberry Pi Pico (RPi Pico) label May 1, 2022
@petejohanson
Copy link
Contributor Author

@jfischer-no nudge? Anything else you'd like to see here?

@jfischer-no
Copy link
Collaborator

@jfischer-no nudge? Anything else you'd like to see here?

Sorry, too busy. I will try to finish my review today or tomorrow.

Copy link
Collaborator

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

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

LGTM, just few comments, see petejohanson#4, please squash without my comments if you agree with the changes.

Copy link
Collaborator

@yonsch yonsch left a comment

Choose a reason for hiding this comment

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

Just a few minor things

@jfischer-no
Copy link
Collaborator

@petejohanson today last chance to get it in v3.1.0 (I do not want to push you, just as a hint 😄)

@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from 119f26d to 4872dc7 Compare May 13, 2022 13:21
Copy link
Contributor Author

@petejohanson petejohanson left a comment

Choose a reason for hiding this comment

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

Addressed the comments @jfischer-no

Thanks for the nudge on the timing!

@petejohanson
Copy link
Contributor Author

Actually, missed the other comments. Working on those now.

@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch 2 times, most recently from a414774 to 1d4c746 Compare May 13, 2022 13:32
@petejohanson
Copy link
Contributor Author

@jfischer-no Ok, now all the comments have actually been resolved. Thanks again for the review and the nudge.

@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from 1d4c746 to a8f6bef Compare May 13, 2022 13:37
@jfischer-no jfischer-no requested review from yonsch and jfischer-no May 13, 2022 13:42
Add USB device driver for Rasberry Pico family of controllers.

Signed-off-by: Peter Johanson <[email protected]>
@petejohanson petejohanson force-pushed the drivers/rpi-pico-usb-device branch from a8f6bef to 0593f37 Compare May 13, 2022 13:49
@petejohanson
Copy link
Contributor Author

(my last few pushes were just to make the compliance check happy, FYI)

Copy link
Collaborator

@yonsch yonsch left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Collaborator

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

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

👍

@carlescufi carlescufi merged commit 43b77a2 into zephyrproject-rtos:main May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants