Skip to content

stm32: Update pyb.CAN loopback unit tests for FDCAN, fix CAN2 filter bug#18946

Merged
projectgus merged 4 commits into
micropython:masterfrom
projectgus:bugfix/stm32_pyb_can_tests
Mar 18, 2026
Merged

stm32: Update pyb.CAN loopback unit tests for FDCAN, fix CAN2 filter bug#18946
projectgus merged 4 commits into
micropython:masterfrom
projectgus:bugfix/stm32_pyb_can_tests

Conversation

@projectgus
Copy link
Copy Markdown
Contributor

Summary

  • Updates the loopback unit tests in tests/ports/stm32/can*.py to pass on CANFD hardware, renames them to pyb_can*.py to account for incoming machine.CAN functionality.
  • As part of updating pyb_can2.py to work with FDCAN, was also able to fix STM32 pyb.CAN: Instantiating one CAN instance can corrupts settings of another instance. #18922 and make that unit test a regression test for it.
  • Cleaned up some parameter names and types for can_clearfilter() as they weren't very clear.
  • Enabled CAN2 peripheral on the NUCLEO_G474RE board, as used for testing.

This work is to support #18572 - now tests pass for pyb.CAN we can verify there's no regression when adding machine.CAN. Have made into a separate PR as the other is already too big!

Testing

  • Ran ports/stm32/pyb_can*.py unit tests on PYBDV11 (Classic CAN), NUCLEO_G474RE and NUCLEO_H723ZG boards (both flavours of FDCAN). All now passing.
  • Connected a CAN transceiver to CAN2 on NUCLEO_G474RE and confirmed the CAN bus also works on the configured pins in non-loopback mode.
  • NUCLEO_H723ZG doesn't have externally usable FDCAN2 pins without changing solder jumper settings (and possibly other things), so I didn't commit the change to this PR as it's only immediately usable for loopback tests. For refernece, the patch for loopback testing is:
--- a/ports/stm32/boards/NUCLEO_H723ZG/mpconfigboard.h
+++ b/ports/stm32/boards/NUCLEO_H723ZG/mpconfigboard.h
@@ -97,6 +97,10 @@
 #define MICROPY_HW_CAN1_TX                  (pin_D1)
 #define MICROPY_HW_CAN1_RX                  (pin_D0)
 
+#define MICROPY_HW_CAN2_NAME                "FDCAN2"
+#define MICROPY_HW_CAN2_TX                  (pin_B13) // B13, B6
+#define MICROPY_HW_CAN2_RX                  (pin_B12) // B12, B5
+
 // SD card detect switch
 #define MICROPY_HW_SDCARD_DETECT_PIN        (pin_G2)
 #define MICROPY_HW_SDCARD_DETECT_PULL       (GPIO_PULLUP)

Generative AI

I did not use generative AI tools when creating this PR.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.45%. Comparing base (df9b714) to head (22ee568).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #18946   +/-   ##
=======================================
  Coverage   98.45%   98.45%           
=======================================
  Files         175      175           
  Lines       22635    22635           
=======================================
  Hits        22286    22286           
  Misses        349      349           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@projectgus projectgus force-pushed the bugfix/stm32_pyb_can_tests branch from 28061d9 to 68aa819 Compare March 18, 2026 05:14
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 18, 2026

Code size report:

Reference:  esp32: Only check the lockfile currently used by the build. [82c6b0e]
Comparison: stm32: Expose FDCAN2 on board NUCLEO_G474RE. [merge of 22ee568]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:   +12 +0.003% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Comment thread tests/ports/stm32/pyb_can_classic_rx.py Outdated
Comment thread ports/stm32/can.h Outdated
Comment thread tests/ports/stm32/can2.py
@dpgeorge
Copy link
Copy Markdown
Member

Apart from the comments above, this looks really good, thanks! I tested on PYBV10 and PYBD_SF6, running the new tests both before and after the bug fix to the firmware in this PR. The expected part of the test fails prior to fixing the CAN2 filter issue, and they all pass with the fix (ie with firmware compiled from this PR).

@projectgus projectgus force-pushed the bugfix/stm32_pyb_can_tests branch from 68aa819 to 05744b0 Compare March 18, 2026 06:08
Also rename the prefix from can to pyb_can, in anticipation
of machine.CAN tests.

Signed-off-by: Angus Gratton <[email protected]>
The function arguments mean totally different things for Classic vs FDCAN
hardware, but the argument name wasn't particularly clear for either.

This commit shouldn't really change the binary firmware at all.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <[email protected]>
This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <[email protected]>
@projectgus projectgus force-pushed the bugfix/stm32_pyb_can_tests branch from 05744b0 to 22ee568 Compare March 18, 2026 06:15
Copy link
Copy Markdown
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

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

Retested on PYBD_SF2 and PYBD_SF6 and the updated tests work.

@projectgus projectgus merged commit d1c936d into micropython:master Mar 18, 2026
41 checks passed
@projectgus projectgus deleted the bugfix/stm32_pyb_can_tests branch March 18, 2026 22:37
@projectgus
Copy link
Copy Markdown
Contributor Author

Thanks for the quick review, @dpgeorge! Have merged so I can more easily rebase the machine.CAN PR.

@dpgeorge
Copy link
Copy Markdown
Member

Excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STM32 pyb.CAN: Instantiating one CAN instance can corrupts settings of another instance.

2 participants