Skip to content

Commit

Permalink
hal_usb: add macro for sending zero-length packets during control IN …
Browse files Browse the repository at this point in the history
…transfers
  • Loading branch information
trheinfels committed May 18, 2023
1 parent 3101ad3 commit 4be6e75
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
18 changes: 18 additions & 0 deletions os/hal/include/hal_usb.h
Expand Up @@ -476,6 +476,24 @@ typedef const USBDescriptor * (*usbgetdescriptor_t)(USBDriver *usbp,
#define usbSetupTransfer(usbp, buf, n, endcb) { \
(usbp)->ep0next = (buf); \
(usbp)->ep0n = (n); \
(usbp)->ep0zlp = false; \
(usbp)->ep0endcb = (endcb); \
}

/**
* @brief Request zero-length IN setup transfer.
* @details This macro is used by the request handling callbacks in order to
* prepare a zero-length packet transaction over the endpoint zero.
*
* @param[in] usbp pointer to the @p USBDriver object
* @param[in] endcb callback to be invoked after the transfer or @p NULL
*
* @special
*/
#define usbSetupTransferZLP(usbp, endcb) { \
(usbp)->ep0next = NULL; \
(usbp)->ep0n = 0u; \
(usbp)->ep0zlp = true; \
(usbp)->ep0endcb = (endcb); \
}

Expand Down
4 changes: 4 additions & 0 deletions os/hal/ports/AVR/MEGA/LLD/USBv1/hal_usb_lld.h
Expand Up @@ -287,6 +287,10 @@ struct USBDriver {
* @brief Number of bytes yet to be transferred through endpoint 0.
*/
size_t ep0n;
/**
* @brief Flag indicating that a zero-length IN packet should be sent through endpoint 0.
*/
bool ep0zlp;
/**
* @brief Endpoint 0 end transaction callback.
*/
Expand Down
4 changes: 4 additions & 0 deletions os/hal/ports/STM32/LLD/OTGv1/hal_usb_lld.h
Expand Up @@ -465,6 +465,10 @@ struct USBDriver {
* @brief Number of bytes yet to be transferred through endpoint 0.
*/
size_t ep0n;
/**
* @brief Flag indicating that a zero-length IN packet should be sent through endpoint 0.
*/
bool ep0zlp;
/**
* @brief Endpoint 0 end transaction callback.
*/
Expand Down
4 changes: 4 additions & 0 deletions os/hal/ports/STM32/LLD/USBv1/hal_usb_lld.h
Expand Up @@ -386,6 +386,10 @@ struct USBDriver {
* @brief Number of bytes yet to be transferred through endpoint 0.
*/
size_t ep0n;
/**
* @brief Flag indicating that a zero-length IN packet should be sent through endpoint 0.
*/
bool ep0zlp;
/**
* @brief Endpoint 0 end transaction callback.
*/
Expand Down
4 changes: 4 additions & 0 deletions os/hal/ports/STM32/LLD/USBv2/hal_usb_lld.h
Expand Up @@ -408,6 +408,10 @@ struct USBDriver {
* @brief Number of bytes yet to be transferred through endpoint 0.
*/
size_t ep0n;
/**
* @brief Flag indicating that a zero-length IN packet should be sent through endpoint 0.
*/
bool ep0zlp;
/**
* @brief Endpoint 0 end transaction callback.
*/
Expand Down
5 changes: 3 additions & 2 deletions os/hal/src/hal_usb.c
Expand Up @@ -844,9 +844,9 @@ void _usb_ep0setup(USBDriver *usbp, usbep_t ep) {
}
if ((usbp->setup[0] & USB_RTYPE_DIR_MASK) == USB_RTYPE_DIR_DEV2HOST) {
/* IN phase.*/
if (usbp->ep0n != 0U) {
if (usbp->ep0n != 0U || usbp->ep0zlp) {
/* Starts the transmit phase.*/
usbp->ep0state = USB_EP0_IN_TX;
usbp->ep0state = (usbp->ep0n > 0u) ? USB_EP0_IN_TX : USB_EP0_IN_WAITING_TX0;
osalSysLockFromISR();
usbStartTransmitI(usbp, 0, usbp->ep0next, usbp->ep0n);
osalSysUnlockFromISR();
Expand Down Expand Up @@ -874,6 +874,7 @@ void _usb_ep0setup(USBDriver *usbp, usbep_t ep) {
osalSysUnlockFromISR();
}
else {
osalDbgAssert(!usbp->ep0zlp, "ZLP invalid for EP0 OUT");
/* No receive phase, directly sending the zero sized status
packet.*/
usbp->ep0state = USB_EP0_IN_SENDING_STS;
Expand Down

0 comments on commit 4be6e75

Please sign in to comment.