For a more in-depth guide, we've created a detailed tutorial hosted on GitHub. In this tutorial, you'll find step-by-step instructions, code examples, and additional resources to help you better understand and implement the concepts discussed here. Whether you're a beginner or looking to deepen your knowledge, this comprehensive guide covers everything you need.
You can access the GitHub repository with the full code here: https://github.com/System-Electronics/astrial_tutorials/tree/main/09-CAN
IntroductionIn this tutorial, we'll walk you through the process of using two Astrial devices to communicate with each other using the CAN-bus protocol. We'll cover the necessary hardware setup, software installation, and scripts to send and receive messages over CAN-bus.
We will use one Astrial to communicate with Astrial's integrated CAN controller along with CAN FD 3 Click transceiver, and then we'll use Waveshare's CAN Hat.
Note that for this tutorial we used two different carrier boards, but you can use two of the same type.
Hardware setupTo use Astrial's integrated CAN controller you first need to set your CAN FD 3 Click to communicate using 3.3V instead of 5V (if you don't do this you might damage permanently your Astrial). To do this you will need to desolder a 0Ω resistor from the CAN FD 3 Click VIO SEL 5V and solder it on 3V3.
After doing this connect the transceiver to your Astrial using the GPIOs on the carrier as shown in the schematics.
To use the CAN Hat instead you will only need to connect it to the board using the GPIO header.
When both are connected correctly connect the hat CAN port to the FD3 Click serial port.
Install the required package to use the CAN Hat on the Astrial on which you're going to use it with:
pip3 install spidev==3.6
Enable the CAN interfaceTo enable the CAN, you need to select the 'imx8mp-astrial-flexcan2.dtb' device tree file in the U-Boot menu as shown here.
From the CAN hat Astrial to the FD3 Click AstrialTo receive messages on Astrial's CAN controller follow these steps on the CAN hat Astrial:
- Set up CAN: launch this command to establish CAN link with the desired bitrate (we are using 125000 but you can choose other values)
ip link set can0 up type can bitrate 125000
- Make the Astrial listen for messages: to be able to receive messages we are going to use a specific command which prints to terminal every received message over CAN-bus
candump can0
Now let's move on the other Astrial so that we can send messages to be seen on this one:
- Use the default Device Tree Blob (DTB): to enable SPI communication, you need to upload the default Device Tree to your Astrial board if you modified it previously. You can find the DTB in the resources folder of the GitHub repository for this tutorial. If you are not sure how to do this, please refer to our GitHub guide.
- Copy the custom MCP2515 library: we provide in our GitHub guide a custom version of the Adafruit's MCP2515 library in the main folder of this tutorial. Copy the can_lib folder to the Astrial, together with the example scripts.
- Use the Python script to send data: run the python script named can_send.py that will send the message 'hello' every second on the bus.
python3 can_send.py
You will now see the 'hello' messages appearing on the FD3 Click Astrial's terminal.
From the FD3 Click Astrial to the CAN hat AstrialLet's now do the opposite, sending message from the FD3 Click Astrial to the CAN hat Astrial.
On the CAN hat Astrial:
- Create and run the Python script to receive data: create a Python script that implements the code provided in the Code section of this article named can_listen.py that will display on your terminal all the CAN messages it receives on the bus.
python3 can_listen.py
On the FD3 Click Astrial:
- Use the cangen command to send messages: to send messages over CAN-bus there are some terminal commands, for example cangen will start sending random messages at regular intervals on the CAN-bus and you should be able to see them on your other Astrial with the CAN hat.
cangen can0
Comments
Please log in or sign up to comment.