Skip to content

feat(stm32): USB register runtimes (OTG, FSDEV) and a CubeMX post-generation step - #36

Open
Jiu-xiao wants to merge 4 commits into
devfrom
feat/usb-otg-register-runtime
Open

Jiu-xiao wants to merge 4 commits into
devfrom
feat/usb-otg-register-runtime

Conversation

@Jiu-xiao

Copy link
Copy Markdown
Member

STM32 USB code generation for the register runtimes of LibXR. It pairs with xrobot-org/libxr#362: after that change the STM32 USB backends no longer use the HAL PCD handle, so projects generated by the current CodeGenerator do not build against it.

What changes:

  • OTG devices: the generated app_main constructs STM32USBDeviceOtgFS or STM32USBDeviceOtgHS with the PHY, DMA, VBUS sensing and speed taken from the CubeMX configuration, and the endpoint buffers in the slot layout. OTG_x_IRQn has to be enabled in NVIC; when CubeMX does not generate the handler, the generator writes one that calls STM32USBOtgDevice::Dispatch().
  • FSDEV devices (the PMA-based full-speed controller): the same for STM32USBDeviceDevFs.
  • The code CubeMX generates stays as it is: LibXR takes over HAL_PCD_Init and HAL_PCD_IRQHandler at link time.
  • libxr stm32 sync and a CubeMX post-generation step: libxr stm32 setup installs cmake/libxr_after_cubemx.cmd as the project's "after code generation" user action, so generating from CubeMX also regenerates the LibXR code. Errors go to .libxr/after_cubemx.log and fail the CMake configure step.
  • README: the USB section and the post-generation step.

Tests: analyzer, generator, CMake generator and the sync command have unit tests (tests/test_stm32_cubemx_sync.py is new). ruff format --check and ruff check pass.

Merge together with xrobot-org/libxr#362.

…n step

USB code generation follows the LibXR OTG register runtime:
- The OTG device takes a hardware configuration (PHY, DMA, VBUS sensing,
  high speed, EP0 OUT DMA buffer) derived from the .ioc mode and
  parameters instead of the PCD handle.
- Endpoint buffers are 32-byte aligned, data endpoints are two slots
  (512-byte slots at high speed), and a raised OTG buffer size is
  written back to libxr_config.yaml with a notice.
- CubeMX keeps its default settings; only a disabled OTG interrupt in
  NVIC is an error. When the generated sources define no OTG interrupt
  handler, app_main.cpp gets the vector function.

libxr stm32 sync parses the project, generates app_main.cpp, turns off
the CubeMX initialization call and interrupt handler of the OTG
instances LibXR takes over, and updates the CMake integration. setup
registers cmake/libxr_after_cubemx.cmd, a script that runs under cmd on
Windows and under sh elsewhere, as the CubeMX post-generation script, so
every code generation in CubeMX runs sync. Its output also goes to
.libxr/after_cubemx.log; app_main.cpp records the digest of the .ioc
file, and LibXR.CMake fails the configuration with that log and the
command to run when the .ioc file changed without the code being
generated again.
Emit the RX/TX list device constructor without any hpcd, generate the
FSDEV/DRD vector function when CubeMX provides no IRQ handler, and number the
data OUT endpoints EP(i+1) on both OTG and FSDEV.
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 2/5

[Medium impact] The changes affect STM32 USB projects using LibXR and their CubeMX build workflow.

Fix the setup digest order, FSDEV interrupt settings, and shell interpreter selection before merging.

Fix All in Claude CodeFindings

  1. P1 First setup blocks the build ▶
  2. P1 FSDEV interrupt handlers stay missing ▶
  3. P1 Hook skips the chosen Python ▶
Fix with agent prompt
### Issue 1
src/libxr/config_cubemx_project.py:784
On first setup, `install_hook()` changes `ProjectManager.UAScriptAfterPath` after the final `generate()` call. The digest in `app_main.cpp` therefore describes the old `.ioc` file. The next CMake configure fails with a stale-code error even though setup reported success. Install the hook before parsing, or regenerate after registering it.

### Issue 2
src/libxr/peripheral_analyzer_stm32.py:1257-1258
The parser records `Vectors` only for `USB_OTG_*`, but the new `_fsdev_vector()` needs that field to choose an interrupt handler. For `USB` and `USB_DRD_FS`, it always returns before checking the generated sources. With the USB interrupt enabled but CubeMX's Generate IRQ handler turned off, no replacement handler is emitted, so USB interrupts never reach `STM32USBDeviceDevFs::Dispatch()`. Record the FSDEV interrupt settings too.

### Issue 3
src/libxr/stm32_cubemx_sync.py:91-92
If `LIBXR_PYTHON` names an interpreter whose path contains spaces, the shell hook splits it into separate words because the expansion in the `for` list is unquoted. A path such as `/home/user/My Project/.venv/bin/python` is never tried as one interpreter. If the fallback interpreters lack libxr, automatic sync fails despite a valid explicit interpreter. Probe the quoted `LIBXR_PYTHON` value separately before trying `python3` and `python`.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Moves STM32 USB generation to LibXR's register-based drivers and adds a CubeMX post-generation sync step.

  • STM32 USB devices use LibXR’s register drivers and matching interrupts.
  • USB endpoint buffers fit the controller’s slots and alignment.
  • CubeMX regenerations refresh LibXR code and flag stale projects.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[STM32 setup] --> B[Parse ioc and record digest]
    B --> C[Generate app_main.cpp]
    C --> D[Apply OTG settings]
    D -->|Changed| B
    D -->|Unchanged| E[Write CMake integration]
    E --> F[Register CubeMX hook in ioc]
    F --> G[CMake compares digests]
    G -->|First registration changes digest| H[Configuration fails]
Loading

Reviews (1) · Last reviewed commit: "style(stm32): format the FSDEV endpoint ..." · Reviewed by Greptile

Comment thread src/libxr/config_cubemx_project.py Outdated
generate(yaml_output, cpp_output, xrobot_enable)

integrate(project_dir)
install_hook(project_dir, ioc_file)

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 First setup blocks the build

On first setup, install_hook() changes ProjectManager.UAScriptAfterPath after the final generate() call. The digest in app_main.cpp therefore describes the old .ioc file. The next CMake configure fails with a stale-code error even though setup reported success. Install the hook before parsing, or regenerate after registering it.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/libxr/config_cubemx_project.py
Line: 784

Comment:
**First setup blocks the build**

On first setup, `install_hook()` changes `ProjectManager.UAScriptAfterPath` after the final `generate()` call. The digest in `app_main.cpp` therefore describes the old `.ioc` file. The next CMake configure fails with a stale-code error even though setup reported success. Install the hook before parsing, or regenerate after registering it.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Comment on lines +1257 to +1258
if usb_name.startswith("USB_OTG_"):
self._record_otg_code_generation(usb_name, cfg)

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 FSDEV interrupt handlers stay missing

The parser records Vectors only for USB_OTG_*, but the new _fsdev_vector() needs that field to choose an interrupt handler. For USB and USB_DRD_FS, it always returns before checking the generated sources. With the USB interrupt enabled but CubeMX's Generate IRQ handler turned off, no replacement handler is emitted, so USB interrupts never reach STM32USBDeviceDevFs::Dispatch(). Record the FSDEV interrupt settings too.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/libxr/peripheral_analyzer_stm32.py
Line: 1257-1258

Comment:
**FSDEV interrupt handlers stay missing**

The parser records `Vectors` only for `USB_OTG_*`, but the new `_fsdev_vector()` needs that field to choose an interrupt handler. For `USB` and `USB_DRD_FS`, it always returns before checking the generated sources. With the USB interrupt enabled but CubeMX's Generate IRQ handler turned off, no replacement handler is emitted, so USB interrupts never reach `STM32USBDeviceDevFs::Dispatch()`. Record the FSDEV interrupt settings too.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

Comment on lines +91 to +92
for py in ${LIBXR_PYTHON:-} python3 python; do
if command -v "$py" >/dev/null 2>&1 && "$py" -c "import libxr.stm32_cubemx_sync" >/dev/null 2>&1; then

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 Hook skips the chosen Python

If LIBXR_PYTHON names an interpreter whose path contains spaces, the shell hook splits it into separate words because the expansion in the for list is unquoted. A path such as /home/user/My Project/.venv/bin/python is never tried as one interpreter. If the fallback interpreters lack libxr, automatic sync fails despite a valid explicit interpreter. Probe the quoted LIBXR_PYTHON value separately before trying python3 and python.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/libxr/stm32_cubemx_sync.py
Line: 91-92

Comment:
**Hook skips the chosen Python**

If `LIBXR_PYTHON` names an interpreter whose path contains spaces, the shell hook splits it into separate words because the expansion in the `for` list is unquoted. A path such as `/home/user/My Project/.venv/bin/python` is never tried as one interpreter. If the fallback interpreters lack libxr, automatic sync fails despite a valid explicit interpreter. Probe the quoted `LIBXR_PYTHON` value separately before trying `python3` and `python`.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

setup wrote the script name into the .ioc file after app_main.cpp had recorded the
digest of the .ioc file, so a project failed to configure right after setup until
STM32CubeMX generated the code again.

This branch has not been deployed

No deployments
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.

1 participant