A CAN driver for the ESP32

Last year I ordered an ESP32, a chip that got quiet a lot of attention because it is small, affordable and comes with a free SDK. I worked with the predecessor, the ESP8266 and I think that the ESP32 is way easier to use and the SDK (called ESP-IDF) is much more powerful compared with the ESP8266.

Time to get hands on the ESP32 as I want a CAN/CANopen-Wifi bridge. The first few months nothing happend and I already thought that the chip will never arrive but finally I got mail from China. I started with a simple "Hello world" but soon found that there is no CAN driver included in the SDK. After some research I found that there is a CAN port and that the CAN module on the ESP32 is SJA1000 compatible.I stripped the driver to work out of the box with the latest SDK version. The code is not perfect (some magic numbers etc.) but should be a good starting point for an own driver.

As I do not want to go to jail because you build something with the code and someone dies:

This software is a PROTOTYPE version and is not designed or intended for use in production, especially not for safety-critical applications! The user represents and warrants that it will NOT use or redistribute the Software for such purposes. This prototype is for research purposes only. This software is provided "AS IS," without a warranty of any kind.

 

First we need the API definition for CAN:

 

Configuration

 

Register definition

 

Module Initialization

 

Interrupt Service Routine (for all interrupts)

 

Read a Frame from the FIFO

 

Writing a CAN frame

 

Stop CAN Module

 

An example project can be downloaded here:

GitHub

Download

11 thoughts on “A CAN driver for the ESP32

  1. pallyya

    hi, you had done a very interesting work.
    im interested in developing a can driver for ESP32 and your work is very promising and well coded.
    can i use your code in my academic project and can you send me or post an example.
    thank you in advance.

  2. Pawel Szabaciuk

    "If you need the Code as a example project, post a comment."
    I would like some example if it is not a problem 🙂

  3. Hey guys,

    sorry for the late reply, I just have seen your posts. I will prepare an example project next week, including the latest version of the driver (dynamic baudrate calculation), based on the latest ESP-IDF. Of course you can use the code in your projects but plase not that there is absolutely no warrenty and it comes "AS IS".

    Regards
    Thomas

    • Pawel Szabaciuk

      Thank you very much!

    • pallyya

      thank you very much, i will try as soon as possible.
      very good work

  4. Pawel Szabaciuk

    Hello again.
    Can you tell me if I need config anything to receive CAN2.0B extended frames?
    I've changed already CAN_read_frame function. I'am using MODULE_CAN->MBX_CTRL.FCTRL.FIR.B.FF flag to assembly message using MODULE_CAN->MBX_CTRL.FCTRL.TX_RX.EXT.
    But I don't get any message. I've been testing CAN2.0A standard frames at 500kbit and it is working correctly. But other device use 2.0B extended frames at 250kbit and I don't get any message.
    Can you have any idea what can I check?

    • So you don't receive any interrupts?
      I have not tried extended frames but my assumption is that the the acceptance filter is blocking them.
      I am afraid you have to check the SJA1000 manual and look where the acceptance filter for extended frames is configured. I guess its within the reserved field of ACC.

      • Pawel Szabaciuk

        I receive interrupts only with bus error. But it is normal, in working 500kbit also have some errors after start. I'll try with acceptance filter. Thank you!

        • Pawel Szabaciuk

          Unfortunately, acceptance filter has correct setup (according to SJA1000 documentation).
          Have you any other idea what can be wrong? Maybe config bits for 250kbits? But I think I was reading than you have been trying this speed (esp32.com).

          • There is currently a pending pull request.
            I will try if I can test and merge it tomorrow, If not, I will have no access to a computer for the next weeks, so please test it on your own.

Leave a Reply

Thomas

About Thomas Barth

Thomas Barth, born 1986, is a german teaching fellow and Ph.D. student. He studied electrical engineering in Darmstadt, Frankfurt and Helsinki and worked 7 years in industry automation before he switched to embedded systems and microelectronics. To read more about him, click here.