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

Skip to content

port/esp32: Add CAN(TWAI) driver. #12331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

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.54%. Comparing base (f1018ee) to head (557d5ee).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12331   +/-   ##
=======================================
  Coverage   98.54%   98.54%           
=======================================
  Files         169      169           
  Lines       21877    21877           
=======================================
  Hits        21558    21558           
  Misses        319      319           

☔ 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 8000 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?

IhorNehrutsa and others added 3 commits March 24, 2025 12:06
Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
CONFIG_XTAL_FREQ_40=y
CONFIG_XTAL_FREQ=40

Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
Co-Authored-By: Sveinung Kval Bakken <sveinung.bakken@gmail.com>

Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
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.

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.

8 participants
0