MicroPython CAN bus for ESP32

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
aklein9999
Posts: 2
Joined: Sat Sep 21, 2019 2:07 pm

MicroPython CAN bus for ESP32

Post by aklein9999 » Sat Sep 21, 2019 2:19 pm

I have been searching up and down for a solid MicroPython library that allows the use of the internal CAN bus controller of an ESP32 board.

Unfortunately I couldn't find anything.

CAN bus is a very essential must have function, and I am genuinely surprised that is is not included in the machine library for the ESP32 hardware.

Does anyone have a solution? I would donate to this forum a reasonable sum if someone would implement it.

Additionally, I also couldn't find a MicroPython library to control an external CAN controller (e.g. for the MCP2515)…

I would appreciate any help.

Alex :D

beyonlo
Posts: 58
Joined: Thu Nov 26, 2015 8:03 pm

Re: MicroPython CAN bus for ESP32

Post by beyonlo » Mon Sep 23, 2019 1:59 pm

Hello.

I agree, CAN Bus, like as MOD bus are very essential, and I am surprised too those it is not included in the micropython for ESP32.

For the CAN Bus, ESP32 has the MAC/Controller, but not the transceiver. But if you need just to communicate from a microcontroller to microcontroller, like as ESP32 to ESP32, or ESP32 to STM, and so on, you do not need the transceiver.

aklein9999
Posts: 2
Joined: Sat Sep 21, 2019 2:07 pm

Re: MicroPython CAN bus for ESP32

Post by aklein9999 » Mon Sep 23, 2019 2:39 pm

Dear beyonlo,

Thank you for your reply.

At this point I am not worried about the bus driver, this I would do externally of course.

Maybe someone can tell me if and how it would be possible to make an official request directly to the MicroPython developers who could be in charge of this.

beyonlo
Posts: 58
Joined: Thu Nov 26, 2015 8:03 pm

Re: MicroPython CAN bus for ESP32

Post by beyonlo » Mon Sep 23, 2019 11:22 pm

Hi, you can to try to open a issue in the https://github.com/micropython/micropython

There has many open requests, and maybe more developers can see your request. I don't know if there is the best way, but is another place that you can to try.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: MicroPython CAN bus for ESP32

Post by jimmo » Tue Sep 24, 2019 12:11 am

aklein9999 wrote:
Mon Sep 23, 2019 2:39 pm
Maybe someone can tell me if and how it would be possible to make an official request directly to the MicroPython developers who could be in charge of this.
beyonlo wrote:
Mon Sep 23, 2019 11:22 pm
Hi, you can to try to open a issue in the https://github.com/micropython/micropython
Yup, this is the best way. Unfortunately the word "CAN" isn't very easy to search for in the existing bugs.

The feature you want is not terribly difficult to implement (compared to some of the other features that people are looking for!) and is supported by the ESP-IDF (see https://docs.espressif.com/projects/esp ... s/can.html ).

MicroPython is an open source project and the ESP32 port is almost entirely contributed by volunteers. CAN would obviously be a great feature. If you need this feature to be implemented then finding a way to fund someone else to do it might be a good option to get it done sooner. Whomever you find to implement it probably doesn't need that much MicroPython experience -- CAN and ESP-IDF experience is probably far more useful, so you could probably search outside of the MicroPython community.

I personally would love to see more of this "sponsored development of features" so I'm glad to see you offering. (But I understand that other people feel differently).

User avatar
roland_vs
Posts: 89
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

Re: MicroPython CAN bus for ESP32

Post by roland_vs » Tue Sep 24, 2019 7:35 am

For the STM32 port there is CAN support from the beginning and recently FDCAN is added for the H743. The python class/bindings with C are already there so that any effort for the ESP32 can get a head start!

R

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: MicroPython CAN bus for ESP32

Post by jimmo » Tue Sep 24, 2019 11:52 am

roland_vs wrote:
Tue Sep 24, 2019 7:35 am
For the STM32 port there is CAN support from the beginning and recently FDCAN is added for the H743. The python class/bindings with C are already there so that any effort for the ESP32 can get a head start!
Yeah, that's a great point. And possibly some additional inspiration, the MCPWM implementation from this thread viewtopic.php?f=18&t=6998 is a great example of a self-contained implementation of making an ESP-IDF feature available to Python. (Specifically this commit: https://github.com/bskp/micropython_esp ... 4425fc7928 )


triumvus
Posts: 1
Joined: Wed Aug 28, 2019 11:29 pm

Re: MicroPython CAN bus for ESP32

Post by triumvus » Tue Jun 09, 2020 5:46 am

Seems like development stopped. Is anyone aware of a functional CAN implementation on esp32?

wthie
Posts: 4
Joined: Sun Feb 16, 2020 9:30 pm

Re: MicroPython CAN bus for ESP32

Post by wthie » Thu Mar 04, 2021 1:42 am

Just brought the file machine_can.c from the fork

https://github.com/nos86/micropython/tr ... can-driver

into the MicroPython V1.13 project compiled with esp-idf-v4.0.1.

There were some minor adaptions to macro usage around error strings with MP_ERROR_TEXT necessary and I also changed the constant mentioned in the single PR 'Fix CAN std id length 11 bit' on the project of nos86. Also don't forget to adjust the modmachine.c/h files with the incantation for the CAN module like

Code: Select all

for modmachine.c
                 :
    { MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) },
    { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
    #if MICROPY_HW_ENABLE_CAN
    { MP_ROM_QSTR(MP_QSTR_CAN), MP_ROM_PTR(&machine_can_type) },
    #endif
    { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
    { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
               :
for modmachine.h
               :
extern const mp_obj_type_t machine_rtc_type;
extern const mp_obj_type_t machine_sdcard_type;
extern const mp_obj_type_t machine_can_type;
               :
Enabling the compilation and inclusion of the CAN module can then be managed by having

Code: Select all

#define MICROPY_HW_ENABLE_CAN (1)
in mpconfigboard.h for whatever board you compile MicroPython.

After that the CAN module comes up when issuing

Code: Select all

from machine import CAN
Further testing is needed for sure, but so far so good, I'll report back when I can connect to my Sol-Ark 12K inverter via CAN.

Post Reply