Files
tinygo/src/machine/machine_stm32f7_otgfs_vbus.go
T
Konstantin Sharlaimov c1a4ed1489 machine/stm32: add OTG FS USB driver for F4/F7
STM32F4 and F7 share the same OTG FS IP but have no USB driver in
TinyGo. This adds a full device-mode driver covering CDC, HID, and
MSC with working examples.

- OTG FS has 4 physical EPs (0–3); virtual indices 4–7 fold onto
  them via physEP() so the existing machine/usb API is unchanged
- F4 bypasses VBUS sensing via GCCFG.NOVBUSSENS; F7 uses the USB
  voltage regulator + GOTGCTL B-valid override instead
- STM32F7 PLL_Q changed 2→9 to produce the 48 MHz clock required
  by USB/RNG/SDMMC; CK48MSEL cleared to select main PLL as source
- HID and MSC descriptors remapped at init() to physical endpoint
  addresses (EP2/EP1 for HID, EP2/EP3 for MSC)
- usb-storage example replaced machine.Flash with a FAT12 RAM disk
  so the host mounts without reformatting
- MSC sendCSW sets queuedBytes before state transition to fix a
  missed byte-count on the status phase
2026-07-12 10:45:49 +02:00

35 lines
1.0 KiB
Go

//go:build stm32f7
package machine
import (
"device/arm"
"device/stm32"
)
// initOTGFSPHY enables the STM32F7 OTG FS PHY and overrides B-session VBUS
// detection via GOTGCTL so boards without VBUS sensing still enumerate.
func initOTGFSPHY() {
// Enable USB voltage regulator (specific to F72x/F73x).
// Bit 14 in PWR_CR2 is USBREGEN.
stm32.PWR.CR2.SetBits(1 << 14)
// Stabilization delay for the regulator (~100us is plenty).
for i := 0; i < 10000; i++ {
arm.Asm("nop")
}
// Enable FS PHY.
stm32.OTG_FS_GLOBAL.GCCFG.SetBits(stm32.USB_OTG_FS_GCCFG_PWRDWN)
// Disable hardware VBUS detection (F7 uses VBDEN, opposite polarity to F4's NOVBUSSENS).
// Clearing this prevents the peripheral from gating enumeration on PA9 VBUS level.
stm32.OTG_FS_GLOBAL.GCCFG.ClearBits(stm32.USB_OTG_FS_GCCFG_VBDEN)
// Override B-session valid so GOTGCTL-based detection reports device connected.
stm32.OTG_FS_GLOBAL.GOTGCTL.SetBits(
stm32.USB_OTG_FS_GOTGCTL_BVALOEN | // enable B-valid override
stm32.USB_OTG_FS_GOTGCTL_BVALOVAL, // set B-valid = 1
)
}