Google Chat: zj734465502@gmail.com
+86-0755-88291180
sales01@spotpear.com
dragon_manager@163.com
services01@spotpear.com
manager01@spotpear.com
WhatsApp:13246739196
This is a 2-Channel CAN bus expansion HAT designed for Raspberry Pi, supports CAN FD (CAN with Flexible Data-Rate), the speed is higher than the traditional 1Mbps of CAN2.0, features multi onboard protection circuits, high anti-interference capability, and stable operation. It suits for fields such as automotive devices or industrial automation.
PIN | Raspberry Pi (BCM2835) | Raspberry Pi (WPI) | Description |
5V | 5V | 5V | 5V Power input |
GND | GND | GND | Ground |
MISO_0 | 9 (MISO) | 13 (MISO) | SPI_0 Data output |
MOSI_0 | 10 (MOSI) | 12(MOSI) | SPI_0 Data input |
SCK_0 | 11 (SCK) | 14 (SCK) | SPI_0 Clock input |
CS_0 | 8 (CE0) | 10 (CE0) | CAN_0 Chip select |
INT_0 | 25 | 6 | CAN_0 Interrupt Pin |
MISO_1 | 9/19 (MISO) | 13/24 (MISO) | SPI_1 Data output |
MOSI_1 | 10/20 (MOSI) | 12/28 (MOSI) | SPI_1 Data input |
SCK_1 | 11/21(SCLK) | 14/29 (SCLK) | SPI_1 Clock input |
CS_1 | 7(CE1)/26 | 11(CE1)/25 | CAN_1 Chip select |
INT_1 | 16 | 27 | CAN_1 Interrupt Pin |
Raspberry Pi doesn't support CAN communication. If you want to do CAN communicating with Raspberry Pi, you require an expansion board which extends the CAN function. Here is the 2-CH CAN FD HAT.
The MCP2518FD is a cost-effective and small-footprint CAN FD controller that can be easily added to a microcontroller with an available SPi interface. The MCP2518FD supports both, CAN frames in the Classical format (CAN2.0B) and CAn Flexible Data Rate (CAN FD) as specified in ISO 11898-1:2015. Its arbitration bit rate is up to 1Mbps, data bit rate up to 8Mbps. It supports up to 29 MHz SPi clock speed. All the standard frame, extended frame, and remote frames are receivable and transmittable. 32 intel flexible filter and mask object allows the CP2518FD to filter usable packets to reduce consumption of MCU.
You can connect this module to your board like Raspberry Pu by SPi interface.
The MCP2561/2FD is a high-speed CAN device,fault-tolerant device that serves as the interface between a CAN protocol controller and the physical bus. The MCP2561/2FD device provides differential transmit and receive capability for the CAN protocol controller, and is fully compatible with the ISO-11898-2 and ISO-11898-5 standards.
The Loop Delay Symmetry is guaranteed to support data rates that are up to 5 Mbps for CAN FD (Flexible Data rate). The maximum propagation delay was improved to support longer bus length. Typically, each node in a CAN system must have a device to convert the digital signals generated by a CAN controller to signals suitable for transmission over the bus cabling (differential output).
The working voltage level of Raspberry Pi is 3.3V, therefore we need to set the VIO of 2-CH CAN FD HAT to 3.3V as below:
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.60.tar.gz tar zxvf bcm2835-1.60.tar.gz cd bcm2835-1.60/ sudo ./configure sudo make sudo make check sudo make install
sudo apt-get install wiringpi cd /tmp wget https://project-downloads.drogon.net/wiringpi-latest.deb sudo dpkg -i wiringpi-latest.deb gpio -v
python2
sudo apt-get update sudo apt-get install python-pip sudo apt-get install python-pil sudo apt-get install python-numpy sudo pip install RPi.GPIO sudo pip install spidev sudo pip2 install python-can
python3
sudo apt-get update sudo apt-get install python3-pip sudo apt-get install python3-pil sudo apt-get install python3-numpy sudo pip3 install RPi.GPIO sudo pip3 install spidev sudo pip3 install python-can
The demo codes can be found and downloaded on #Resource part.
Download and unzip it.
Copy demo to the home(~) directory of Raspberry Pi.
cd ~/2-CH-CAN-FD-HAT-Demo/Raspberry_Pi/ sudo chmod -R 777 Linux_Driver/ cd Linux_Driver/ ls
sudo ./install.sh
sudo reboot dmesg | grep spi
【Note1】Mode A is factory default, and mode B CAN be realized by changing the resistance. In mode A, two CAN channels use two sets of independent SPI respectively, while in mode B, two CAN channels share one set of SPI. See the figure below
【Note2】Because the compatible detection method is adopted, there will be additional information during the initialization, which does not affect the normal use and can be ignored
You can set the baud rate, working mode, and buffer size.
sudo ip link set can1 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on sudo ifconfig can0 txqueuelen 65536 sudo ifconfig can1 txqueuelen 65536
The available modes:
The FD type:
For more details of CAN commands, please refer to : https://www.kernel.org/doc/Documentation/networking/can.txt
Check the can bus:
ifconfig
If you have only one 2-CH CAN FD HAT, you can connect CAN0_H to CAN1_H, and CAN0_L to CAN0_L to CAN1_L
Open two terminals, one works are sender and another is receiver.
receiver:
candump can0
sender:
cansend can1 000##11.22.33.44
cd 2-CH CAN FD HAT/Raspberry Pi/python
sudo python receive.py
sudo python send.py
【Note】 The CAN1 is used as sender, and CAN0 is used as receiver.
The demo codes provided is based on python, please check that if you installed the python-can library.
os.system('sudo ip link set can0 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on')
This code is used to initialize CAN0 as receiver/sender. If you want to change it to CNA1, you can use this one:
os.system('sudo ip link set can1 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on')
can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan_ctypes')# socketcan_native
or
can1 = can.interface.Bus(channel = 'can1', bustype = 'socketcan_ctypes')# socketcan_native
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)
can0.send(msg)
or
can1.send(msg)
os.system('sudo ifconfig can0 down')
or
os.system('sudo ifconfig can1 down')
msg = can0.recv(10.0)
The variables of recv() function is the timeout of receving.
For more information, please refer to https://python-can.readthedocs.io/en/stable/interfaces/socketcan.html
The example of Arduino is based on the MCP2518FD project of Pierre Molinaro (Thanks to Pierre Molinaro).
To run the example, you should prepare two Arduino boards and two 2-CH CAN FD HAT. Note that the working level of most of Arduino board is 5V, therefore we should set the VIO to 5V as below:
PIN | Arduino UNO |
5V | 5V |
GND | GND |
MISO_O | D12(MISO) |
MOSI_0 | D11(MOSI) |
SCK_0 | D13(SCK) |
CS_0 | D10 |
INT_0 | D2 |
Connect CAN0_H and CAN0-L of one HAT to another's. Set the baudrate of Serial monitor to 115200 and check the data: