8000 port/esp32: Add CAN(TWAI) driver. by IhorNehrutsa · Pull Request #12331 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

Conversation

IhorNehrutsa
Copy link
Contributor
@IhorNehrutsa IhorNehrutsa commented Aug 29, 2023

This pull request is based on ESP32: CAN(TWAI) driver #7381

Consistent with the docs\machine_CAN: Add CAN docs. #12330

Will consistent with the New machine.CAN driver #12337

@codecov
Copy link
codecov bot commented Aug 29, 2023

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.39%. Comparing base (00a926e) to head (20bc2f6).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12331   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         171      171           
  Lines       22290    22290           
=======================================
  Hits        21933    21933           
  Misses        357      357           

☔ 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
Copy link
Contributor

As mentioned on the linked PR, this work is much appreciated and we're keen to have esp32 CAN driver support. Implementing a common machine.CAN API will take some time, the new plan for this work is in #12337.

@IhorNehrutsa I hope you're keen to keep this PR open and it can be adapted it to the new API once that's available.

@straga
Copy link
straga commented Nov 8, 2023

esp-idf 5.02
["esp32", "esp32", "1.22.0-preview", "v1.22.0-preview.132.g958c6d917.dirty on 2023-11-08", "with PSRAM"]
Work with some code change.

>>> dev = CAN(0, extframe=False, tx=4, rx=5, mode=CAN.NORMAL, baudrate=500000, auto_restart=False)
>>> dev.recv()
(857, False, False, b'\x00\x00\x00\x00\x03\x00\x00\x00')
>>> dev.recv()
(857, False, False, b'\x00\x00\x00\x00\x03\x00\x00\x00')
>>> dev.recv()
(853, False, False, b'_\x00d\x00')
>>> dev.recv()
(857, False, False, b'\x00\x00\x00\x00\x03\x00\x00\x00')
>>> dev.recv()
(857, False, False, b'\x00\x00\x00\x00\x03\x00\x00\x00')
>>> dev.recv()
(857, False, False, b'\x00\x00\x00\x00\x03\x00\x00\x00')
>>> dev.recv()
(862, False, False, b'hi\x89i\x00\x00  ')
>>> dev.recv()
(857, False, False, b'\x00\x00\x00\x00\x03\x00\x00\x00')

@winnie-xzh
Copy link

@straga Can you share your code changes? This PR may not be completed so quickly.

@straga
Copy link
straga commented Dec 27, 2023

@winnie-xzh
I am use user_modules for CAN, draft version, but work for me. Not need more touch micropython core code.
https://github.com/straga/micropython-esp32-twai

@projectgus
Copy link
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

@IhorNehrutsa IhorNehrutsa force-pushed the esp32_CAN_TWAI branch 6 times, most recently from be0679d to b3394d5 Compare November 2, 2024 19:49
@ValentinBirth
Copy link

Hi, I tried to compile this PR to test it, but I can't seem to enable the idf twai component. While building, I get errors indicating that it doesn't recognize the

#include "driver/twai.h"

as seen in this error:

ports/esp32/machine_can.c:82:15: error: implicit declaration of function 'TWAI_TIMING_CONFIG_25KBITS' [-Werror=implicit-function-declaration]
   82 |     .timing = TWAI_TIMING_CONFIG_25KBITS(),
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~

I've already tried to enable it via idf.py menuconfig, but couldn't find the option there.
I'm using idf version 5.2.2

Would be nice if anyone could help me.

@IhorNehrutsa IhorNehrutsa force-pushed the esp32_CAN_TWAI branch 2 times, most recently from b19f701 to 79ed82e Compare February 18, 2025 16:02
@IhorNehrutsa IhorNehrutsa force-pushed the esp32_CAN_TWAI branch 3 times, most recently from e7cca3d to 8c2132e Compare March 3, 2025 07:26
@sveinungkb
Copy link

I was able to use your branch @IhorNehrutsa , but I had to make some changes to the example code found here:
pyb.CAN or I had misc type expectation errors.

pseudo patch:

buf = bytearray(8)
-lst = [0, 0, 0, 0, memoryview(buf)]
+lst = [0, False, False, memoryview(buf)]
# No heap memory is allocated in the following call
-can.recv(0, lst)
+can.recv(lst)

@IhorNehrutsa IhorNehrutsa force-pushed the esp32_CAN_TWAI branch 2 times, most recently from 6f81e42 to bd3d673 Compare March 10, 2025 13:58
@IhorNehrutsa IhorNehrutsa force-pushed the esp32_CAN_TWAI branch 3 times, most recently from 0792fba to 3380c14 Compare March 13, 2025 07:34
@maruno
Copy link
Contributor
maruno commented Mar 16, 2025

This looks pretty extensive, but I am unsure what the blocker on ESP32 CAN support is right now? It looks to wait on a rewrite of the STM32 implementation?

Is there anything that anyone could help out with? I would love to start a project with CAN in micropython later, but it looks like efforts on it are blocked or halted?

straga added a commit to straga/micropython-esp32-twai that referenced this pull request May 9, 2025
@straga
Copy link
straga commented May 9, 2025

I am updated as USER_C_MODULES
Build: with micropython 1.25 + esp_idf 5.4.1
https://github.com/straga/micropython-esp32-twai

Add some updates for the build as a module and for LoopBack, with the pin connected -> together.

@Ozuba
Copy link
Ozuba commented Jun 24, 2025

Hey im intrasted in this feature, whats missing for it to get merged, is there any point i can help in?

@IhorNehrutsa IhorNehrutsa force-pushed the esp32_CAN_TWAI branch 6 times, most recently from 06ffe44 to 062ff12 Compare October 7, 2025 09:25
@IhorNehrutsa IhorNehrutsa force-pushed the esp32_CAN_TWAI branch 4 times, most recently from 6a4e537 to 268ebc9 Compare October 15, 2025 10:13
Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
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.

9 participants

0