Skip to content

stm32: Fix Classic CAN issue where initialising CAN1 corrupts CAN2.#18992

Draft
projectgus wants to merge 2 commits intomicropython:masterfrom
projectgus:bugfix/stm32_multi_can
Draft

stm32: Fix Classic CAN issue where initialising CAN1 corrupts CAN2.#18992
projectgus wants to merge 2 commits intomicropython:masterfrom
projectgus:bugfix/stm32_multi_can

Conversation

@projectgus
Copy link
Copy Markdown
Contributor

Summary

This PR is marked draft until I get some hardware to address the last item.

Testing

  • Ran the updated unit tests on a PYBV11 (Classic CAN) and a STM32G4 (FDCAN). PYBV11 fails without this fix, passes with this fix.
  • Re-run ports/stm32/pyb_can*.py extmod_hardware/machine_can*.py unit tests, multi_extmod/machine_can_*.py multi_pyb_can/*.py multi-tests.

Trade-offs and Alternatives

  • The way pyb.CAN handles the CAN1/CAN2 filter split is a bit awkward, but as we want to focus on machine.CAN it's probably not worth spending more time thinking about it.

Generative AI

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

@projectgus
Copy link
Copy Markdown
Contributor Author

@chrismas9 if you get a chance then I'd be interested to know if the new unit tests added here pass on your boards (can run with mpremote run ... if you don't want to use the whole run-tests.py runner.)

Still waiting for an F413Z board to arrive so I can finish this.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.46%. Comparing base (5c00edc) to head (6137e28).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #18992   +/-   ##
=======================================
  Coverage   98.46%   98.46%           
=======================================
  Files         176      176           
  Lines       22784    22784           
=======================================
  Hits        22435    22435           
  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.

On Classic CAN hardware only, the problem was CAN1 init would
clear all filters including CAN2 filter range.

Includes expanded unit tests to cover arbitrary pairs of CAN instances.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
@projectgus projectgus force-pushed the bugfix/stm32_multi_can branch from eab0e62 to 1078d7d Compare March 25, 2026 00:08
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 25, 2026

Code size report:

Reference:  tools/ci.sh: Increase qemu_arm test run timeout. [5c00edc]
Comparison: stm32: Don't disable CAN1 clock if CAN2 is still active (Classic CAN). [merge of 6137e28]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:   +40 +0.010% 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

This turned out not to be needed for the bugfix in previous commit,
but seems like a good practice anyway.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
@projectgus projectgus force-pushed the bugfix/stm32_multi_can branch from 1078d7d to 6137e28 Compare March 25, 2026 00:40
@chrismas9
Copy link
Copy Markdown
Contributor

@projectgus pyb_can_instances.py fails on NUCEO_F413ZH, but passes on PYBv1.0. F413 fails three times with old build and two times with latest build. Results below. It's interesting that [1.2] passes on PYB, but fails on F413, even when CAN3 in not used.

I hope I built it properly. I haven't done this before. I cloned your repo, switched to bugfix/stm32_multi_can, make submodules and built PYBv1.0 and NUCLEO_F413HZ.

Tets results - MASTER

MPY: sync filesystems
MPY: soft reboot
test_controller_pairs (__main__.Test) ...testing config A with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
received (123, False, False, 0, b'message1')
testing config B with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
received (3, False, False, 0, b'message2')
testing config A with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
testing config A with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
testing config A with controller CAN(3, CAN.LOOPBACK, auto_restart=False)
received (123, False, False, 0, b'message1')
testing config B with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
received (3, False, False, 0, b'message2')
testing config A with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
testing config A with controller CAN(3, CAN.LOOPBACK, auto_restart=False)
received (123, False, False, 0, b'message1')
testing config B with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
received (3, False, False, 0, b'message2')
 FAIL

======================================================================
FAIL: test_controller_pairs (__main__.Test)  [Testing CAN pair]  (id_a=2, id_b=1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 77, in test_controller_pairs
  File "unittest/__init__.py", line 92, in fail
AssertionError: no rx!

======================================================================
FAIL: test_controller_pairs (__main__.Test)  [Testing CAN pair]  (id_a=1, id_b=3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 77, in test_controller_pairs
  File "unittest/__init__.py", line 92, in fail
AssertionError: no rx!

======================================================================
FAIL: test_controller_pairs (__main__.Test)  [Testing CAN pair]  (id_a=2, id_b=3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 77, in test_controller_pairs
  File "unittest/__init__.py", line 92, in fail
AssertionError: no rx!

----------------------------------------------------------------------
Ran 1 tests

FAILED (failures=3, errors=0)
>>> 

###############################################################################################################################
MicroPython v1.18-5289.g6137e28f3e on 2026-03-26; NUCLEO-F413ZH with STM32F413


MPY: sync filesystems
MPY: soft reboot
test_controller_pairs (__main__.Test) ...testing config A with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
testing config A with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
received [123, False, False, 0, b'message1']
testing config B with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
received [3, False, False, 0, b'message2']
testing config A with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
testing config A with controller CAN(3, CAN.LOOPBACK, auto_restart=False)
received [123, False, False, 0, b'message1']
testing config B with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
received [3, False, False, 0, b'message2']
testing config A with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
received [123, False, False, 0, b'message1']
testing config B with controller CAN(3, CAN.LOOPBACK, auto_restart=False)
received [3, False, False, 0, b'message2']
testing config A with controller CAN(3, CAN.LOOPBACK, auto_restart=False)
received [123, False, False, 0, b'message1']
testing config B with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
received [3, False, False, 0, b'message2']
 FAIL

======================================================================
FAIL: test_controller_pairs (__main__.Test)  [Testing CAN pair]  (id_a=1, id_b=2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 77, in test_controller_pairs
  File "unittest/__init__.py", line 92, in fail
AssertionError: no rx!

======================================================================
FAIL: test_controller_pairs (__main__.Test)  [Testing CAN pair]  (id_a=1, id_b=3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 77, in test_controller_pairs
  File "unittest/__init__.py", line 92, in fail
AssertionError: no rx!

----------------------------------------------------------------------
Ran 1 tests

FAILED (failures=2, errors=0)
>>> 

My tests:
###############################################################################################################################
MicroPython v1.18-5289.g6137e28f3e on 2026-03-26; NUCLEO-F413ZH with STM32F413

------------------------------------------------------------------------------------------------------------------------
Test order:
 [[1, CAN(1, CAN.LOOPBACK, auto_restart=False)], [2, CAN(2, CAN.LOOPBACK, auto_restart=False)]]
------------------------------------------------------------------------------------------------------------------------
can1 receive failed
can2 [123, False, False, 0, b'can2']
------------------------------------------------------------------------------------------------------------------------
Test order:
 [[2, CAN(2, CAN.LOOPBACK, auto_restart=False)], [1, CAN(1, CAN.LOOPBACK, auto_restart=False)]]
------------------------------------------------------------------------------------------------------------------------
can2 [123, False, False, 0, b'can2']
can1 [123, False, False, 0, b'can1']
------------------------------------------------------------------------------------------------------------------------
Test order:
 [[1, CAN(1, CAN.LOOPBACK, auto_restart=False)], [3, CAN(3, CAN.LOOPBACK, auto_restart=False)]]
------------------------------------------------------------------------------------------------------------------------
can1 receive failed
can3 [123, False, False, 0, b'can3']
------------------------------------------------------------------------------------------------------------------------
Test order:
 [[3, CAN(3, CAN.LOOPBACK, auto_restart=False)], [1, CAN(1, CAN.LOOPBACK, auto_restart=False)]]
------------------------------------------------------------------------------------------------------------------------
can3 [123, False, False, 0, b'can3']
can1 [123, False, False, 0, b'can1']
------------------------------------------------------------------------------------------------------------------------
Test order:
 [[2, CAN(2, CAN.LOOPBACK, auto_restart=False)], [3, CAN(3, CAN.LOOPBACK, auto_restart=False)]]
------------------------------------------------------------------------------------------------------------------------
can2 [123, False, False, 0, b'can2']
can3 [123, False, False, 0, b'can3']
------------------------------------------------------------------------------------------------------------------------
Test order:
 [[3, CAN(3, CAN.LOOPBACK, auto_restart=False)], [2, CAN(2, CAN.LOOPBACK, auto_restart=False)]]
------------------------------------------------------------------------------------------------------------------------
can3 [123, False, False, 0, b'can3']
can2 [123, False, False, 0, b'can2']

###########################################################################################################
MicroPython v1.18-5289.g6137e28f3e on 2026-03-26; PYBv1.0 with STM32F405RG

MPY: sync filesystems
MPY: soft reboot
test_controller_pairs (__main__.Test) ...testing config A with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
received [123, False, False, 0, b'message1']
testing config B with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
received [3, False, False, 0, b'message2']
testing config A with controller CAN(2, CAN.LOOPBACK, auto_restart=False)
received [123, False, False, 0, b'message1']
testing config B with controller CAN(1, CAN.LOOPBACK, auto_restart=False)
received [3, False, False, 0, b'message2']
 ok
----------------------------------------------------------------------
Ran 1 tests

OK

@projectgus
Copy link
Copy Markdown
Contributor Author

Huh, thanks @chrismas9 that's interesting. Good that the results from your test code and the new unit test seem to agree that the problem on F413 is orderings 1,2 and 1,3.

My F413 board should arrive soon, so I'll probably hold off on looking at this further until then.

@chrismas9
Copy link
Copy Markdown
Contributor

I have three CANbus drivers I can connect to the F413. I can do an external CAN test when you are ready. It sends on each port and listens on th eother two.

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