Skip to content
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

[ESP32] CAN bus and MOD bus master RTU. #5087

Open
beyonlo opened this issue Sep 9, 2019 · 49 comments
Open

[ESP32] CAN bus and MOD bus master RTU. #5087

beyonlo opened this issue Sep 9, 2019 · 49 comments

Comments

@beyonlo
Copy link

beyonlo commented Sep 9, 2019

Hello.

  1. Are there support or plans to support CAN bus on ESP32? I see that there are a implementation for pyboard https://docs.micropython.org/en/latest/library/pyb.CAN.html - is that a similar implementation on the ESP32?

  2. And about the MOD bus master RTU on micropython?
    I think that, differently of CAN bus, MOD bus implementation is not a microcontroller dependent, just works over UART, is that correct?
    I not found a implementation for the modbus master RTU in micropython libs and so on, anyone know if are there a third lib for that?

Thank you so much.

@rolandvs
Copy link
Sponsor Contributor

rolandvs commented Sep 11, 2019

There has been some discussion on the Micropython forum about modbus:
https://forum.micropython.org/viewtopic.php?t=5453

You can get some more info on ESP32 CAN use via this:
https://hackaday.io/project/20204-esp32-can-driver

@nos86
Copy link

nos86 commented Sep 23, 2019

Hello,
I'm interested on CAN communication as well.
I know that Pumbaa (a fork of Micropython) exists, but its creator said me it is no longer mantained.

@nos86
Copy link

nos86 commented Oct 6, 2019

Hello,
I just give an update to everyone is interested.
Since I was not able to find nothing on the web, I've decided to write the module by myself.
A very first version has been published on my repository. If anyone is interested, here the link of it: https://github.com/nos86/micropython/tree/esp32-can-driver-v1
Since the branch is in continuous development, I prefer to give you the link to the tag of first stable version.

Do not hesitate to open an issue for any new request or just to highlight any bug on the code.

@coffice12
Copy link

Hello,
I just give an update to everyone is interested.
Since I was not able to find nothing on the web, I've decided to write the module by myself.
A very first version has been published on my repository. If anyone is interested, here the link of it: https://github.com/nos86/micropython/tree/esp32-can-driver-v1
Since the branch is in continuous development, I prefer to give you the link to the tag of first stable version.

Do not hesitate to open an issue for any new request or just to highlight any bug on the code.

hava a example?

@nos86
Copy link

nos86 commented Oct 9, 2019

I have updated the Quick Ref file. Here the link to CAN Bus section: https://github.com/nos86/micropython/blob/esp32-can-driver-v1/docs/esp32/quickref.rst#can-bus

@beyonlo
Copy link
Author

beyonlo commented Oct 9, 2019

@nos86

Great work. Maybe you need to contact the @dpgeorge to commit this CAN Bus driver on the master libs. :)

By the way, did you wrote or something with a MOD bus RTU Master on MicroPython? I need to use that to read/write from ESP32 to a slave devices that implement MOD bus RTU.

Thank you.

@nos86
Copy link

nos86 commented Oct 10, 2019

Actually not, my goal was to develop the driver for CAN hardware for my project.
Regarding the merge in master, I'd like to add some additional basic function before request a pull.

@camosoul
Copy link

camosoul commented Nov 7, 2019

Things go slow when not incentivized... Last post 29 days old...

Might a modest bounty speed things up? I'd really like a proper CAN implementation (with proper documentation to go with it) for ESP32... It's very odd that PyBoard and other MCUs with CAN are supported in uPython, but somehow ESP32 got a miss on it...

How it got to a 1.0 release with this glaring omission is kinda ... wut? Car v1.0 doesn't have tires or a gas tank... [confusion]

I can't code worth a damn, but I can spend money...

@nos86 nos86 mentioned this issue Nov 8, 2019
@mlokhandwala
Copy link

Hi @nos86,
So how does one install the CAN bus driver? Is it install-able by pip or does one have to compile the source code? I am desperately looking to use the CAN bus in my project and currently having to make do using the UART although the PCB is intended to communicate via CAN.

@camosoul
Copy link

camosoul commented Dec 12, 2019

Hi @nos86,
So how does one install the CAN bus driver? Is it install-able by pip or does one have to compile the source code? I am desperately looking to use the CAN bus in my project and currently having to make do using the UART although the PCB is intended to communicate via CAN.

It's about to be a native part of MicroPython thanks to this excellent fellow.

See here: #5310

@sandervandegeijn
Copy link

Great work! I really could use this for a project I'm doing since I don't care much for writing all this C-code. How does the ESP32 hold up with CAN & micropython when the can bus is quite busy?

@nos86
Copy link

nos86 commented Dec 17, 2019

Hello @neographikal,
for the first release, I'm not sure I'm able to include the configuration of the filter.
If so, the software filtering at uPython level is not suitable for high bus-load conditions.

For the next releases, I'm planning to incorporate the configuration of hardware filter (best option for very high bus load) and some software filter handled at low level by C in order to improve the flexibility of the library.

@sandervandegeijn
Copy link

Good to know :) Thanks!

@sandervandegeijn
Copy link

I have updated the Quick Ref file. Here the link to CAN Bus section: https://github.com/nos86/micropython/blob/esp32-can-driver-v1/docs/esp32/quickref.rst#can-bus

Going to ask some stupid questions;

Is this lib meant for the onboard can bus controller on the ESP32? The SDA/SDL references threw me off a bit, or is the internal one also i2c based? I'm using the internal controller with an 2551 transceiver.

I have an proof of concept working in C now;

void setup() {
  Serial.begin(115200);
  CAN_cfg.speed = CAN_SPEED_250KBPS;
  CAN_cfg.tx_pin_id = GPIO_NUM_5;
  CAN_cfg.rx_pin_id = GPIO_NUM_4;
  CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
  // Init CAN Module
  ESP32Can.CANInit();
}

is the baudrate parameter 250 for 250kbps?

Thanks :) Going to try this, nice to have some spare time in the holidays ;)

@nos86
Copy link

nos86 commented Dec 23, 2019

Driver is designed for internal controller with external transceiver (likes mcp2551).

Please find the example inside my repo (example/esp32_can.py) https://github.com/nos86/micropython/blob/esp32-can-driver/examples/esp32_can.py

There you can find how to initialize the driver. More details on https://github.com/nos86/micropython/blob/esp32-can-driver/docs/library/esp32.CAN.rst

Let me know if you need more help

@riegaz
Copy link

riegaz commented Dec 26, 2019

@nos86 Thanks a lot. I'm really looking forward that you CAN driver is included into Micropython. Do you have a build ready to be tested?

Another question I have is: What about sniffing a CAN bus that has both, standard and extended frames. Would it work? Is it possible to send both types on the same bus one after the other?

I can see from your example that it is possible to switch but is it also possible to detect extended frames when receiving them in standard mode?

@riegaz
Copy link

riegaz commented Dec 26, 2019

Can there be two CAN bus at the same time? What are the available pin combinations?

@nos86
Copy link

nos86 commented Dec 27, 2019

According to my knowledge, esp32 has only one can driver.
Currently I don't have a build available on GitHub, but you can build it from source.

I didn't try to use mixed messages, I'll try it as soon as a return from vacation.
In any case it depends on the firmware implementation at esp-idf level. No limitations are present at micropython level

@mlokhandwala
Copy link

mlokhandwala commented Jan 2, 2020

Hello @nos86 can you release a build? You have forked from a reasonably new version of repository and in fact I (possibly many others) are using an older MicroPython release. So we could avoid setting up the whole build system if you could make a recent build available. In a sense I've already been waiting for the last 2 months for your driver to get integrated with the main branch but it's not moving forward.

@nos86
Copy link

nos86 commented Jan 2, 2020

I'm on vacation without internet. I'll be able to upload a build by end of next week.

@jbakuwel
Copy link

Hi @nos86. First of all, thanks for all the work on this!

While I consider myself experienced in these matters, I'm new to the whole ESP32 ecosystem. Following these instructions, it's not clear where to run "make" without a configured ESPIDF. After cloning the esp-idf repository, there is no Makefile in that directory.

It would be great if I could download a build rather than setting up the whole build system.

@nos86
Copy link

nos86 commented Jan 16, 2020

@jbakuwel
Copy link

Hi @nos86,
Firmware loaded on the board successfully. Many thanks for your help!

@mlokhandwala
Copy link

Hi @nos86 @jbakuwel

Sorry for this noobish question but how can I integrate the firmware.bin with the Micropython installation on the ESP32? Is this to be copied to a specific location there? How will the API get integrated and exposed?

@nos86
Copy link

nos86 commented Jan 17, 2020

@mlokhandwala, firmware.bin is the hex file to be loaded in ESP32. It is a Mycropython installation that includes the CAN driver. Please refer to https://docs.micropython.org/en/latest/esp32/tutorial/intro.html#deploying-the-firmware for the installation. Clearly, you have to use my firmware instead the official one.

@camosoul
Copy link

camosoul commented Jan 17, 2020

Hi @nos86 @jbakuwel

Sorry for this noobish question but how can I integrate the firmware.bin with the Micropython installation on the ESP32? Is this to be copied to a specific location there? How will the API get integrated and exposed?

esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash

curl -LO https://github.com/nos86/micropython/releases/download/esp32-can-driver-v2/firmware.bin

esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 firmware.bin

If I could find some DOCs, I'd be sending and receiving over CANBUS /w muh SN65HVD230s, MCP2551s, and TJA1051s right now!

@mlokhandwala
Copy link

Hi @nos86

@mlokhandwala, firmware.bin is the hex file to be loaded in ESP32. It is a Mycropython installation that includes the CAN driver. Please refer to https://docs.micropython.org/en/latest/esp32/tutorial/intro.html#deploying-the-firmware for the installation. Clearly, you have to use my firmware instead the official one.

My apologies! I was expecting it to be the full installation but when I saw the size of the firmware.bin for some reason I thought it was too small and probably just the CAN lib.

@mlokhandwala
Copy link

mlokhandwala commented Jan 29, 2020

Hi @nos86 ,

I suppose the build you have uploaded is for modules with SPI. After I flash it I get the following error (loop):

rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee

I am using the following chip with 4MB flash and no SPI

Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 160MHz,
None
Crystal is 40MHz
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB

@nos86
Copy link

nos86 commented Feb 1, 2020

I made no changes in Makefile. it is the same of the mainstream.
I developed on my ESP-WROOM-32 board. I'm not sure what could be the problem in you case.

Have you tried to flash again the official micropython firmware to check if it works?

@mlokhandwala
Copy link

Hi,
The official MicroPython firmware works perfectly.

I guess there must be some make file switch for SPI / No SPI Flash. Even the official firmware is available in SPI / No SPI Flash versions.

@skpang
Copy link

skpang commented Feb 19, 2020

Hi,
I've just flash the firmware with the date 2020-01-0 and tried the example but getting this error:

MicroPython esp32-can-driver-v2-4-gc8afef6e8-dirty on 2020-01-09; ESP32 module with ESP32
Type "help()" for more information.

from machine import CAN
bus = CAN(tx=4, rx=2, baudrate=500, mode=CAN.MODE_NO_ACK)
Traceback (most recent call last):
File "", line 1, in
AttributeError: type object 'CAN' has no attribute 'MODE_NO_ACK'

Any idea of how to fix this error ?

@nos86
Copy link

nos86 commented Feb 20, 2020

Hello skpang,
that constant has been changed to CAN.SILENT
Please refer to docs/esp32/quickref.rst

Can you try it and tell me if this fixes your problem?

@skpang
Copy link

skpang commented Feb 20, 2020

CAN.SILENT didn't work either.

bus = CAN(tx=4, rx=2, baudrate=500, mode=CAN.SILENT)
Traceback (most recent call last):
File "", line 1, in
TypeError: function missing 1 required positional arguments

I am looking at the example at:
https://github.com/nos86/micropython/blob/esp32-can-driver-v1/docs/esp32/quickref.rst

Also at:
https://github.com/nos86/micropython/blob/esp32-can-driver-v1/ports/esp32/machine_can.c
There doesn't seems to be a SILENT defined anywhere.

@skpang
Copy link

skpang commented Feb 20, 2020

I also tried:

bus = CAN(0, extframe=True, mode=CAN.LOOPBACK, baudrate=CAN.BAUDRATE_500k)
Traceback (most recent call last):
File "", line 1, in
AttributeError: type object 'CAN' has no attribute 'BAUDRATE_500k'

Is there a newer binary image available for the ESP32 with CAN ?

@nos86
Copy link

nos86 commented Feb 22, 2020

Please, download latest version: https://github.com/nos86/micropython/releases/tag/esp32-can-driver-v3

Refer to https://github.com/nos86/micropython/blob/esp32-can-driver-v3/docs/library/machine.CAN.rst as documentation for usage.

Several changes has been done since first version in order to harmonize the driver across different hardwares.

Moreover, pay attention, in order to save space, several constants have been removed from code. If you want to use it, you have to declare in your script.

@skpang
Copy link

skpang commented Feb 22, 2020

Hi nos86,

I've just tried the new v3 binary but after flashed the board I'm getting this error:
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3f400020,len:228892

Is this the correct binary for an Adafruit HUZZAH32 – ESP32 Feather Board ?

@Civilduino
Copy link

Hello @nos86, first great work implementing the CAN drivers. Did you finish the complete implementation with filters? How can I use it with the lastest MicroPython and not your bin file?

@Tbruno25
Copy link

Tbruno25 commented Jun 6, 2020

@nos86 Anything I can do to help with the development for this?

@nos86
Copy link

nos86 commented Jun 10, 2020

Hello @Tbruno25,
thank you for your interest.

The module is already ready for integration and I'm still waiting for merging.

I saw several people that are not able to use the HEX file I've compiled. Unfortunately at this time, I don't have an hardware ready for test and I cannot check a new compiled file.

Are you able to do it? If yes, it would be very useful for the community until my implementation is not merged in main project.

@skpang
Copy link

skpang commented Jun 10, 2020

Hi nos86,
I have the hardware to test. Do you have a hex file the ESP-WROOM-32D ?

@nos86
Copy link

nos86 commented Jun 10, 2020

Hi @skpang,
thank you for your helping but this kind of loop (where i compile the file and you test it) is very time consuming, because it's not so easy to understand why it is not working.

The best option is that someone has the envirmoment setup and the hardware so he will be sure that the image works without problem and then I can release it for all.

@camosoul
Copy link

camosoul commented Jun 10, 2020

@nos86

The previous .bin you provided worked for me... I've got about 20 ESP32 boards laying about... They're not expensive. What happened to yours? Are they hard to get where you live?

I can send you some...
Or, you can send me knowledge.
Or, we can do nothing and just sit around some more...

I've got some machines running Arch Linux that could be used, ssh, etc... Even if I don't understand, you can ssh and I'll plug an ESP32 into the USB port...

Willing to do stuff, but not good at this #LearnToCode stuff since nobody ever writes intelligible docs or wants to explain anything...

@Tbruno25
Copy link

Tbruno25 commented Jun 10, 2020

@nos86 I can definitely help with that.

First I compiled a new .bin from your master and have encountered the same issue @skpang reported above.

it seems to go into an infinite boot loop with the following error:

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3f400020,len:234340
ets Jun 8 2016 00:22:57

I then realized the master branch has no machine_can.c so I checked out esp32-can-driver and tried to compile again. However I'm unable to complete as it gets interrupted by the following error:

CC machine_can.c
machine_can.c: In function 'machine_hw_can_recv':
machine_can.c:301:51: error: 'MP_ROM_NONE' undeclared
{ MP_QSTR_list, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
machine_can.c:301:51: note: each undeclared identifier is reported only once for each function it appears in make: *** [../../py/mkrules.mk:47: build-GENERIC/machine_can.o] Error 1

@jbakuwel
Copy link

jbakuwel commented Jun 22, 2020

@nos86, @Tbruno25 for what it's worth: v2 of the firmware loads fine on the ESP32 board I have here but v3 goes into the mentioned infinite boot loop. What has changed between v2 and v3 that could cause this?

@nos86, The board I have has three CAN buses, do you think it's feasible to make all those accessible with uPython?

@ChessSpider
Copy link

hi @nos86 just wanted to leave behind some positivity by telling you I appreciate all the hard work you've put into this. I really hope you are able to continue the work.

@Tbruno25
Copy link

While we are waiting for this to be added to the official micropython repo, if you need CAN bus you can give this firmware a try. It's a modified version of Pycom's WIPY firmware which implements CAN -- confirmed working with a sn65hvd230 on an esp32-wroom-32.

@newbie-gk
Copy link

I see that it's been a while since the last update....
Any updates on the request being implemented in the official micropython?

@IhorNehrutsa
Copy link
Contributor

WIP: esp32 CAN(TWAI) driver
#7381

@FeroxTL
Copy link

FeroxTL commented Sep 21, 2022

Is there any progress with this PR?

I was able to build it with latest master version (1.19.1) with replacing definition of const mp_obj_type_t machine_can_type to

MP_DEFINE_CONST_OBJ_TYPE(
    machine_can_type,
    MP_QSTR_CAN,
    MP_TYPE_FLAG_NONE,
    make_new, machine_hw_can_make_new,
    print, machine_hw_can_print,
    locals_dict, (mp_obj_dict_t *)&machine_can_locals_dict
);

but it is not working with my hardware. Maybe I missed something? I'm using VP230 for converting TTL levels of esp32 to CAN BUS levels. According to oscilloscope it should send and receive some data, but the only thing I can see is increasing rx_error_counter and bus_error_count in can.info()

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

No branches or pull requests