Skip to content

fix: USB OTG FS PHY reset for reliable re-enumeration after st-flash#35

Closed
Amperstrand wants to merge 1 commit into
mainfrom
fix/usb-phy-reset
Closed

fix: USB OTG FS PHY reset for reliable re-enumeration after st-flash#35
Amperstrand wants to merge 1 commit into
mainfrom
fix/usb-phy-reset

Conversation

@Amperstrand

Copy link
Copy Markdown
Owner

Summary

Fixes #34 — USB OTG FS doesn't re-enumerate after st-flash soft reset.

Adds an explicit PHY reset sequence between embassy_stm32::init() and Driver::new_fs():

  1. RCC AHB2ENR.OTGFSEN clock cycle (disable → enable)
  2. RCC AHB2RSTR.OTGFSRST peripheral reset (assert → deassert)
  3. GRSTCTL.CSRST core soft reset
  4. GCCFG.PWRDWN PHY power cycle (clear → set)

Why This Is Needed

After st-flash (SYSRESETREQ without NRST), the USB PHY retains stale state. Neither embassy-usb-synopsys-otg nor embassy-stm32's RCC init performs the full reset sequence needed to unwedge the PHY.

Verification

Same fix hardware-verified on STM32F746-DISCO in ccid-firmware-rs (commit 28e6fee, issue #15). The STM32F469 uses an identical OTG FS peripheral at the same base address (0x5000_0000).

Related

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b1b99d6ba9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread firmware/src/main.rs Outdated
unsafe {
// GRSTCTL = CSRST (bit 0)
otg_global.add(0x010 / 4).write_volatile(1);
while otg_global.add(0x010 / 4).read_volatile() & 1 != 0 {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Bound CSRST wait loop to avoid boot hang

This loop waits indefinitely for GRSTCTL.CSRST to clear, so if the OTG core never reports reset completion (for example when the AHB state is not idle after an unusual reset path), firmware boot will block forever before USB initialization. Because this code now runs on every boot, a single stuck condition turns into a hard startup hang with no recovery path; adding a timeout/fallback (and logging) would prevent bricking the boot sequence.

Useful? React with 👍 / 👎.

… soft reset

After a soft reset (SYSRESETREQ from st-flash), the USB OTG FS PHY
can be left in an inconsistent state where it doesn't re-enumerate on
the host. This cycles the RCC clock, asserts peripheral reset, performs
a core soft reset (GRSTCTL.CSRST), and power-cycles the PHY via
GCCFG.PWRDWN before the USB driver initializes.

Mirrors the fix in ccid-firmware-rs commit 28e6fee (issue #15).
@Amperstrand

Copy link
Copy Markdown
Owner Author

Closing in favor of direct commit to main. The code here uses the pre-fix PAC method names (set_otgfsen/set_otgfsrst) which were corrected to set_usb_otg_fsen/set_usb_otg_fsrst in gm65-scanner f24ea99. The version going into main includes the corrected names and SAFETY comments. Also closing issue #34 as the fix will be in the next commit.

@Amperstrand Amperstrand closed this May 4, 2026
@Amperstrand Amperstrand deleted the fix/usb-phy-reset branch May 4, 2026 17:55
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.

USB OTG FS doesn't re-enumerate after soft reset (st-flash)

1 participant