Skip to content

fix(MAXUSB): Service EP0 (control) OUT & SETUP transactions#1523

Merged
ttmut merged 3 commits intoanalogdevicesinc:mainfrom
Brandon-Hurst:maxusb/fix-ep0-out-txns
Mar 13, 2026
Merged

fix(MAXUSB): Service EP0 (control) OUT & SETUP transactions#1523
ttmut merged 3 commits intoanalogdevicesinc:mainfrom
Brandon-Hurst:maxusb/fix-ep0-out-txns

Conversation

@Brandon-Hurst
Copy link
Copy Markdown
Contributor

Description

MXC_USB_ReadEndpoint currently only services non-control endpoints. The justification for this was that EP0 out doesn't have an OUT interrupt. The issue with this is that ReadEndpoint is just not servicing Control OUT transactions as a result, which breaks the USB enumeration / configuration process.

This PR adds handling for EP0 OUT so that control transactions can proceed properly without manual intervention. Otherwise some manual servicing of EP0 out transactions would be needed on the part of higher-level drivers.

An additional fix was added to fix a problem where a new SETUP event would not be handled if it was aborting other DATA / STATUS transactions on EP0. This commit switches the order of checks in the IRQ Handler so that the new SETUP is serviced before the (!aborted) check for e.g. a DATA transfer.

Originally submitted to hal_adi: zephyrproject-rtos/hal_adi#31

The motivation is to prepare for a rework to the Zephyr USB UDC driver; these changes are necessary to assist that development.

I tested the MAXUSB CDC_ACM example with these changes on the APARD32690, and they worked. I also tested the Zephyr cdc_acm and mass samples with the driver rework that depends on these.

Checklist Before Requesting Review

  • PR Title follows correct guidelines.
  • Description of changes and all other relevant information.
  • (Optional) Link any related GitHub issues using a keyword
  • (Optional) Provide info on any relevant functional testing/validation. For API changes or significant features, this is not optional.

@MaureenHelm MaureenHelm requested a review from hfakkiz February 26, 2026 21:14
@MaureenHelm
Copy link
Copy Markdown
Collaborator

MaureenHelm commented Feb 26, 2026

@Brandon-Hurst Why is this a draft?

@ttmut this is a dependency for the max32 driver in zephyrproject-rtos/zephyr#103493, which we're trying to complete for zephyr 4.4

@Brandon-Hurst
Copy link
Copy Markdown
Contributor Author

Brandon-Hurst commented Feb 27, 2026

Ran into an issue retesting on raw MSDK examples with my APARD board which I am now comfortable chalking up to hardware problems. The board would suspend and the USB PHY clock was not coming up. However, I have tested this change on both MAX32690FTHR and MAX32666FTHR. I also tested old working code using TinyUSB and my APARD board still wouldn't work. So, I think my board was damaged.

I tested USB_CDCACM on both boards and USB_MassStorage on MAX32690FTHR (not supported at the moment on MAX32666FTHR). I was seeing these use-cases work in Zephyr with my reworked changes as well.

Anyway, opening it now. Just wanted to get it into a draft and then do a couple tests to rule out user error here.

@Brandon-Hurst Brandon-Hurst marked this pull request as ready for review February 27, 2026 00:29
Copy link
Copy Markdown
Contributor

@hfakkiz hfakkiz left a comment

Choose a reason for hiding this comment

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

Could you please share your development on the Zephyr UDC driver?

@Brandon-Hurst
Copy link
Copy Markdown
Contributor Author

@tmon-nordic
Copy link
Copy Markdown

UDC Rework is here: https://github.com/Brandon-Hurst/zephyr/blob/max32/udc-rework/drivers/usb/udc/udc_max32.c

Can you update it to latest rework-udc? drop_control_transfers() and pending setup handling was moved out to common code. All the driver has to do is to call udc_setup_received().

@Brandon-Hurst
Copy link
Copy Markdown
Contributor Author

Brandon-Hurst commented Mar 3, 2026

UDC Rework is here: https://github.com/Brandon-Hurst/zephyr/blob/max32/udc-rework/drivers/usb/udc/udc_max32.c

Can you update it to latest rework-udc? drop_control_transfers() and pending setup handling was moved out to common code. All the driver has to do is to call udc_setup_received().

I rebased it, thanks for the heads up. I'll change it and test later today.

@Brandon-Hurst Brandon-Hurst force-pushed the maxusb/fix-ep0-out-txns branch 2 times, most recently from 48d743d to f3a70f7 Compare March 10, 2026 19:53
MXC_USB_ReadEndpoint currently only services non-control
endpoints. The justification for this was that EP0 out
doesn't have an OUT interrupt. There is some ambiguity in the UG
about this, but the key issue is that ReadEndpoint is just
not servicing Control OUT transactions as a result.

Currently, if a new SETUP event arrives when a DATA / STATUS
is in progress, all transfers are aborted, and the SETUP event
is dropped. The new SETUP event should be handled, which this
commit addresses.

- Add handling for EP0 OUT so that control transactions
can proceed properly without manual intervention.
- Handle a new SETUP event interrupting a control Data
or Status stage.
- Add CSR0_DATA_END flag set to end of data transactions
in MXC_USB_ReadEndpoint

Signed-off-by: Brandon Hurst <brandon.hurst@analog.com>
Signed-off-by: Brandon Hurst <brandon.hurst@analog.com>
The #ifndef removed in this commit is unnecessary for Zephyr USB.

For reference, the stack is being reworked so that all that is
needed is the following:

```C
static int udc_event_setup(const struct device *dev)
{
	int ret = 0;
	MXC_USB_SetupPkt setup_pkt;

	/* Get setup data from FIFO */
	ret = MXC_USB_GetSetup(&setup_pkt);
	if (ret != 0) {
		LOG_ERR("Failed to get setup data");
		return ret;
	}

	/* Explicit ACK for setup if no follow-on data
           (checked by MXC_USB) */
	MXC_USB_Ackstat(0);

	/* Clear EP0 previous requests */
	ret = MXC_USB_ResetEp(0);
	if (ret != 0) {
		LOG_ERR("Failed to reset EP0");
		return ret;
	}

	udc_setup_received(dev, &setup_pkt, true);

	return 0;
}
```

As is mentioned in musbhsfc/usb.c, SETUP transactions with no
follow-on data are ACK'd explicitly with MXC_USB_AckStat. In
the case where follow-on data is required, this line can ACK
where needed.

Testing:
- MAX32690FTHR, Zephyr USB `cdc-acm` and `mass` examples.
- MAX32690FTHR, MSDK USB_CDC-ACM sample.

Signed-off-by: Brandon Hurst <brandon.hurst@analog.com>
@Brandon-Hurst Brandon-Hurst force-pushed the maxusb/fix-ep0-out-txns branch from f3a70f7 to bbb1d22 Compare March 12, 2026 19:12
@Brandon-Hurst
Copy link
Copy Markdown
Contributor Author

Changes completed as requested. Re-tested on MAX32690FTHR, CDC and Mass use-cases in Zephyr and MSDK examples.

There is still one CI task failing but it doesn't appear related to something I can fix on my side:

Run tj-actions/changed-files@v45
changed-files
  Using local .git directory
  Running on a pull_request (synchronize) event...
  Repository is shallow, fetching more history...
  Completed fetching more history.
  Warning: Unable to find merge base between a96bb212f22b6952c5c39699de012945f7b75a7e and bbb1d2210630c634e14f6ffa8daa276d22b93a69
  Error: Unable to locate the commit sha: a96bb212f22b6952c5c39699de012945f7b75a7e
  Error: Please verify that the commit sha is correct, and increase the 'fetch_depth' input if needed
  Warning: fatal: bad object a96bb212f22b6952c5c39699de012945f7b75a7e
  
  Warning: If this pull request is from a forked repository, please set the checkout action `repository` input to the same repository as the pull request.
  Warning: This can be done by setting actions/checkout `repository` to ${{ github.event.pull_request.head.repo.full_name }}
  Error: Unable to determine a difference between a96bb212f22b6952c5c39699de012945f7b75a7e..bbb1d2210630c634e14f6ffa8daa276d22b93a69

Copy link
Copy Markdown
Contributor

@hfakkiz hfakkiz left a comment

Choose a reason for hiding this comment

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

Thanks!

@hfakkiz hfakkiz requested a review from ttmut March 13, 2026 10:05
@ttmut ttmut merged commit 8577479 into analogdevicesinc:main Mar 13, 2026
9 of 10 checks passed
@Brandon-Hurst Brandon-Hurst deleted the maxusb/fix-ep0-out-txns branch March 18, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants