uhi22 / wifican

Bridge between Wifi and CAN. Minimal variant of the ESP32RET. Using ESP32s3
18 stars 6 forks source link
canbus esp32s3 savvycan

wifican

Bridge between Wifi and CAN.

image image

News

2024-10-17 CAN baud rate limitations

With the commit from September 2024, the CAN baud rate was changed to 105kBaud, with the goal to support DXBUS (More details here: https://github.com/uhi22/invacare-g50-investigations). This breaks the use cases, which require 500kBaud. It is not yet clarified whether setting the baud rate in SavvyCAN actually works. This needs further investigations. There is no clean solution at the moment, because custom baud rates are not supported in SavvyCAN (https://github.com/collin80/SavvyCAN/issues/469 , https://github.com/collin80/SavvyCAN/issues/654, https://savvycan.com/index.php/forum/welcome-mat/207-custom-speeds).

So the conclusion is:

2024-04-24

Wifican used by forum members, and description on the openinverter wiki available.

2023-10-10

Receive path runs much more stable with the own TWAI FIFO driver. Transmit path not yet implemented.

Motivation

SavvyCAN is a great open-source CAN tool, and the ESP32RET is assumingly a pretty good interface between the PC and multiple CANs. However, the try to use ESP32RET to create a simple single-channel-CAN interface using a good old ESP32 dev kit and adding a CAN transceiver, was not successful.

The good old ESP32 board had the following issues:

Even when switching to the ESP32s3, there are stability issues with the original espressif TWAI driver, see doc/crashlog.md .

Out of this situation, the plan is create a minimal single channel device, which is a bridge between Wifi and CAN, and is still compatible to SavvyCAN.

Solution

The better way would be to add fixes to the ESP32RET and the espressif TWAI driver, but for experimentation the following approach was chosen:

So we end-up with the following features for the moment:

Quick Start Guide

The Longer Quick Start Guide

(Collecting the experiences from https://openinverter.org/forum/viewtopic.php?p=69766#p69766 and https://openinverter.org/forum/viewtopic.php?p=69881#p69881 and https://openinverter.org/wiki/Getting_started_with_CAN_bus)

  1. Install the Arduino IDE (e.g. version 2.2.1) and the ESP32s3 board support.
  2. Solder a 3.3V CAN transceiver board to the ESP32s3 board. This needs just four wires: 3.3V, ground, CAN_RX (pin 4), CAN_TX (pin 5). Attention: There are cheap CAN transceiver boards on Aliexpress which do not work, and also wrongly labeled CAN transceiver chips. It makes sense to obtain the transceiver from a reliable source.
  3. Connect a switch, which connects the pin35 either to ground or to pin 47. (See chapter wifi modes below)
  4. Create a new folder C:\Sketchfolder
  5. Open in a browser https://github.com/uhi22/wifican
  6. Green Button "Code", choose "Download zip". This creates a wifican-main.zip in the downloads folder.
  7. Copy this zip into C:\Sketchfolder
  8. Unzip, and arrange the folders in the way that you have the folder C:\Sketchfolder\wifican, and in this is the wifican.ino. This is because the Arduino IDE takes the folder name as name of the sketch, and the main ino file needs to have the same name.
  9. Start the Arduino IDE. In my case it is Arduino IDE 2.2.1.
  10. File -> Open..., navigate to C:\Sketchfolder\wifican\wifican.ino. This should directly show all files, with the wifican.ino as most left, because it is the main file.
  11. Choose the board "ESP32S3 Dev Module"
  12. Rename the wifi_credentials_template.h to wifi_credentials.h, and enter the credentials of your home wifi there.
  13. Make sure that your home wifi is a 2.4GHz wifi. The ESP does not work with 5GHz wifi.
  14. Press the upload button. This takes some minutes to compile, and afterwards it will upload the code to the ESP.
  15. In the Arduino IDE, open the serial monitor. Configure it to 115200 Baud.
  16. Connect pin35 to 3.3V (which are also provided by pin 47). This selects the "home mode".
  17. On the ESP, press shortly the reset button.
  18. In the Arduino IDE, observe the serial monitor. It should show something like this wifiMode1 and WifiMode is 'station'. Connecting to myHomeWifi" and WiFi connected and telnet is listening
  19. Open SavvyCAN, choose menu -> connection -> open connection window.
  20. Choose "Add new device connection", and "Network connection (GVRET)". In the IP address field there should automatically appear the IP of the wifican device. If not, check whether the wifican device and your PC are in the same Wifi network.
  21. The wifican should now be visible in the connection list, with status "Connected".
  22. In the connection window, set the bus speed and check the "Enable bus" checkbox.
  23. Connect the CANH and CANL to your CAN network. The SavvyCAN should show the incoming traffic.
  24. SavvyCAN -> Menu -> SendFrames -> Custom allows you to transmit frames.

The two different wifi modes

The wifican supports two different modes of wifi operation. The mode is selected by the level of pin35 during startup.

The "home mode"

In the source code called WIFI_MODE_STATION. This mode is reached when during the boot the pin35 is high (3.3V). The wifican tries to connect to your home wifi. You configured the name of your wifi and the password in wifi_credentials.h. The wifican will be in your home network, so you can use SavvyCAN to observe the CAN while you are in parallel browsing the internet or sending mails.

The "field mode"

In the source code called WIFI_MODE_ACCESS_POINT. This mode is reached if during boot the pin35 is grounded. The wifican will create its own wifi network, called "wifican" and with the password "topsecret", configurable here: https://github.com/uhi22/wifican/blob/44dbb5f25db3eddefdc1b644b3ff3e60f3bf27ec/wifican.ino#L15. To use this mode, go to the wifi settings of your laptop, and connect to the "wifican". This means, you will loose your internet connection, because you change your wifi connection from your router to the wifican. This mode is intended to use when you are outside, where you do not reach your home wifi network anyway.

Development messages

The wifican privides some debug data as "virtual" CAN messages. For debugging your setup, you could add the data base file (dbc) of the "virtual" debug messages into SavvyCAN. This file is here: https://github.com/uhi22/wifican/blob/m ... opment.dbc This allows you to see how long the wifican is running (wificanAliveCounter is counting up), and how many messages it has seen on CAN (wificanTotalRxMessages)

Results of reverse-engineering

How did a CAN transmission work?

The states and transitions of the ESP32S3 CAN driver are described here: https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/twai.html

How did the CAN reception work (in the old world with espressif TWAI driver)?

...

How was the receive callback path configured?

attachCANInterrupt() --> CAN_COMMON::setGeneralCallback() sets the cbGeneral. canBuses[i]->watchFor() --> setRXFilter() CAN_COMMON::attachCANInterrupt() --> CAN0.setCallback() --> ...tbd...

Where is the espressif TWAI driver stored?

Source code in the espressif IDE:

Header and lib in the arduino IDE:

Is useBinarySerialComm true?

Yes, the SavvyCAN sends 0xE7 as first two bytes. This turns-on the binary mode.