2-CH CAN FD HAT
| ||
Introduction
Release Notes
Around April 2022 we will launch the 'Rev2.1' version (this version and subsequent versions have a version number under the name of the back panel).
New version features:
- DC circuit is optimized, the new version can supply power to the Raspberry Pi and work stably from the DC interface.
- Add stack configuration resistors on the back, and by modifying the resistors, you can achieve up to 5-channel CAN interface expansion (three expansion boards).
- The default configuration supports the official driver, and the driver installation operation is simpler; at the same time, the configuration resistor is reserved to support the old version driver (the old version uses a third-party driver), which is fully compatible with the old version.
Only the driver installation method is different when using the new version. For details, please refer to the 'Driver Installation' chapter.
- The new version only supports systems with kernel versions 5.4.77 and later.
Product Description
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.
More |
Features
- Based on Raspberry Pi design, suitable for Raspberry Pi Zero/Zero W/Zero WH/2B/3B/3B+/4B.
- Support external wide voltage of 8~28V to supply power to Raspberry Pi and 2-CH CAN FD HAT at the same time or directly supply power to 2-CH CAN FD HAT from Raspberry Pi.
- Support CAN FD, the data baud rate breaks through the 1Mbps limit of traditional CAN2.0.
- Reserved control interface for easy control by other controllers whose logic voltage is 3.3V/5V.
- CAN bus adopts electrical isolation, the isolation voltage can reach 5 kV.
- Multiple protection devices onboard, with short-circuit protection, electrostatic protection, effective suppression of lightning, and ESD, each channel provides 500W lightning surge protection.
- The interface has a 120Ω terminal resistance, which can be switched by jumper caps.
- The working mode of dual-channel CAN can be configured, that is, two channels share a set of SPI or two channels use two independent SPIs respectively.
- Provide complete supporting information manuals and demos.
Specifications
- Power input terminal voltage: DC 8~28V
- Logic level: 3.3V/5V
- CAN controller: MCP2518FD
- Dimensions: 65.0 x 56.5 mm
- Fixed hole through diameter: 3.0mm
Interfaces
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)/7(CE1) | 10(CE0)/11(CE1) | CAN_0 Chip select |
INT_0 | 25/13 | 6/23 | CAN_0 Interrupt Pin |
MISO_1 | 19(MISO)/9 | 24(MISO)/13 | SPI_1 Data output |
MOSI_1 | 20(MOSI)/10 | 28(MOSI)/12 | SPI_1 Data input |
SCK_1 | 21(SCLK)/11 | 29(SCLK)/14 | SPI_1 Clock input |
CS_1 | 18(SPI1_CE0)/17/16/26 | 1/0/27/25 | CAN_1 Chip select |
INT_1 | 24/23/22/16 | 5/4/3/27 | CAN_1 Interrupt Pin |
Hardware description
CAN
The function of the CAN module is to handle the reception and transmission of all messages on the CAN bus. When a message is sent, the message is first loaded into the correct message buffer and control registers. A transmit operation can be initiated by setting the corresponding bit in the control register through the SPI interface or by using the transmit enable pin. Communication status and errors can be checked by reading the corresponding registers. Any message detected on the CAN bus is checked for errors and then matched against a user-defined filter to determine whether to move the message to one of two receive buffers.
Since the Raspberry Pi itself does not support the CAN bus, the CAN controller with the SPI interface is used with a transceiver to complete the CAN function.
The MCP2518FD is a CAN FD (Flexible Data Rate) controller produced by Microchip, which fully supports CAN framing in the Classic (CAN2.0) and CAN Flexible Data Rate (CAN FD) formats. The arbitration bit rate is as high as 1Mbps, the data baud rate also breaks through the 1Mbps limit of traditional CAN2.0, and the SPI clock speed is as high as 20MHz, which conforms to the ISO11898-1:2015 standard. The device can transmit and receive standard and extended data frames as well as remote frames. The MCP2518FD comes with 32 flexible filters and shielding objects that can filter out unwanted packets, thus reducing the overhead of the main microcontroller (MCU). The MCU is connected to the device through the SPI interface, that is, the Raspberry Pi is connected to the chip through the SPI interface. For the Raspberry Pi to use the chip, the device can be driven through the prepared device tree file. For more details, please refer to the data sheet.
Working with Raspberry Pi
Hardware Connection
When connecting to the Raspberry Pi, a female header must be added, and then the pins pass through the bottom plate. The effect is as follows:
When using the Raspberry Pi to demonstrate this demo, it should be noted that since the Raspberry Pi belongs to a 3.3V logic system, it is necessary to change the jumper cap of the logic voltage pin of the 2-CH CAN FD HAT to 3.3V, as shown in the figure below.
Enable SPI interface
- Open the terminal, and use the command to enter the configuration page.
sudo raspi-config Choose Interfacing Options -> SPI -> Yes to enable the SPI interface.
sudo reboot
Please make sure that the SPI interface was not used by other devices, you can check in the /boot/config.txt.
Install Libraries
- Install python library
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
Driver Installation (New Version)
Note: Around April 2022 we released the 'Rev2.1' version (with the version number under the name on the back panel), this chapter is only applicable to 'Rev2.1' and For future versions, if you are using an older version, please read the 'Driver Installation (Legacy)' chapter.
You can flexibly adjust the SPI and interrupt pins used by modifying the 0R resistor, select any of the following modes.
Dual SPI Mode
'A' mode (default mode), use SPI0-0 and SPI1-0 to control two CAN channels, hardware does not need any modification, namely:
CAN_0 uses SPI0-0, interrupt pin is 25, CAN_1 uses SPI1-0, interrupt pin is 24
Insert the module into Raspberry Pi, and then modify the config.txt file:
sudo nano /boot/config.txt
Add the following commands at the last line:
dtparam=spi=on dtoverlay=spi1-3cs dtoverlay=mcp251xfd,spi0-0,interrupt=25 dtoverlay=mcp251xfd,spi1-0,interrupt=24
After the addition is complete, restart and enter the 'Configure CAN' chapter.
Single SPI Mode
'B' mode, use SPI0-0 and SPI0-1 to control two CAN outputs, just move the four 0R resistors on the front of the module to the 'B' mode position, namely:
CAN_0 uses SPI0-0, interrupt pin is 25, CAN_1 uses SPI0-1, interrupt pin is 24. As shown below:
please add in /boot/config.txt:
dtoverlay=spi1-3cs dtoverlay=mcp251xfd,spi0-0,interrupt=25 dtoverlay=mcp251xfd,spi0-1,interrupt=24
After the addition is complete, restart and enter the 'Configure CAN' chapter.
Stack Mode
- Principle: Modify the 0R resistor in the CAN_x PIN SELECTION (x is 0 or 1) area on the back to change the CE and INT pins used by the CAN controller.
- Note: When stacking is required, the hardware configuration needs to be kept in dual SPI mode ('A' mode, default setting).
- CAN_0 has two groups to choose from:
CE_0 | INT_0 | config settings | description |
---|---|---|---|
CE0 | D25 | dtoverlay=mcp251xfd,spi0-0,interrupt=25 | CAN_1 uses SPI0-0, the interrupt pin is 25 |
CE1 | D13 | dtoverlay=mcp251xfd,spi0-1,interrupt=13 | CAN_1 uses SPI0-1, interrupt pin 13 |
- CAN_1 has three groups to choose from:
CE_1" | INT_1 | config settings | description |
---|---|---|---|
SPI1_CE0 | D24 | dtoverlay=mcp251xfd,spi1-0,interrupt=24 | CAN_1 uses SPI1-0, the interrupt pin is 24 |
SPI1_CE1 | D23 | dtoverlay=mcp251xfd,spi1-1,interrupt=23 | CAN_1 uses SPI1-1, the interrupt pin is 23 |
SPI1_CE2 | D22 | dtoverlay=mcp251xfd,spi1-2,interrupt=22 | CAN_1 uses SPI1-2, the interrupt pin is 22 |
A careful friend may see that there is still a set of configurations that have not been written. This is reserved for compatibility with old versions, and new users can ignore it.
Example of use
- If you have two boards, how to configure them when using 4-way CAN?
Answer: The first board is not modified (as shown the left figure below), the CAN_0 configuration area on the back of the second board selects CE1 and D13, and the CAN_1 configuration area selects SPI1_CE1 and D23 as shown the right figure below):
Adds at the end of /boot/config.txt:
dtparam=spi=on dtoverlay=spi1-3cs dtoverlay=mcp251xfd,spi0-0,interrupt=25 dtoverlay=mcp251xfd,spi0-1,interrupt=13 dtoverlay=mcp251xfd,spi1-0,interrupt=24 dtoverlay=mcp251xfd,spi1-1,interrupt=23
After the addition is complete, restart and enter the 'Configure CAN' chapter.
Compatibility Mode
- The new version has adjusted some default settings in order to be compatible with the standard SPI, so some modifications need to be made to be compatible with the old version
- Compatible with A mode: CE_1" resistor select to D26; INT_1 resistor select to D16
- Compatible with B mode: CE_1" can not be modified, it has no effect; INT_1 resistor is selected to D16
- For driver installation, please refer to the "Driver Installation (Old Version) Chapter"
Driver Installation (Legacy)
'A' mode (default mode), use SPI0-0 and SPI1-0 to control two CAN channels, hardware does not need any modification, namely:
Insert the module to Raspberry Pi, and then modify the config.txt file:
sudo nano /boot/config.txt
dtparam=spi=on dtoverlay=waveshare-can-fd-hat-mode-a
After the addition is complete, restart and enter the 'Configure CAN' chapter.
- Reboot and check
- After installing, reboot your Raspberry Pi and check if the driver is installed properly.
sudo reboot dmesg | grep spi
【Note1】Mode A is the 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.
Configure CAN
Set the baud rate, operating mode, whether to enable FD and configure the transmission buffer size:
- Baud rate setting:
sudo ip link set can0 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on sudo ip link set can1 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on
bitrate xxxxxx (bps):arbitration bitrate
dbitrate xxxxxx (bps):data bitrate
The following is an example of setting the bit rate
If the CAN FD frame data sent by other devices cannot be received, you can try to add sample-point .8 dsample-point .8 at the end of the command. Note: the sampling point setting (.8) needs to be the same as that of the sending terminal.
- Can be configured into the following modes:
[ loopback { on | off } ]
[ listen-only { on | off } ]
[ triple-sampling { on | off } ]
[ one-shot { on | off } ]
[ berr-reporting { on | off } ]
FD is the opening command:
[ fd { on | off } ]
[ fd-non-iso { on | off } ]
More related CAN kernel commands can be viewed:
https://www.kernel.org/doc/Documentation/networking/can.txt
- Configuration buffer
sudo ifconfig can0 txqueuelen 65536 sudo ifconfig can1 txqueuelen 65536
- View ifconfig:
ifconfig
- start testing:
If you only have one 2-CH CAN FD HAT, you can connect CAN0_H to CAN1_H, CAN0_L to CAN1_L of the module, as shown in the figure below:
- Install can-utils:
sudo apt-get install can-utils
- Open two terminal windows:
One of the terminal inputs receives the CAN0 data command:
candump can0
Another terminal input sends the CAN1 data command:
cansend can1 000#11.22.33.44
- The demonstration effect is as follows: (the left is receiving, the right is sending)
If you have two 2-CH CAN FD HATs, you can directly connect CAN_H and CAN_L two by two. The effect is the same as above, but pay attention to matching the communication rate, identifying the ID, and outputting the serial number of the interface.
Fixed Device Number
If it is found that the system device number of the CAN interface and the name of the board interface do not match, you can use the following methods to fix the device number:
- Create a rules file in the /etc/udev/rules.d directory:
sudo nano /etc/udev/rules.d/80-can.rules
- Add the following to the file:
ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi0.0/net/can?", NAME="can0" ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi0.1/net/can?", NAME="can1" ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi1.0/net/can?", NAME="can1"
- If it is used in stacking, you can add corresponding commands according to your own needs, you only need to modify DEVPATH and NAME, for example, name spi1-1 can3:
ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi1.1/net/can?", NAME="can3"
- For More information please refer to: https://wiki.archlinux.org/title/Udev#top-page
Python examples
- Download and decompress the demos and drivers (downloaded can be skipped):
cd ~ wget https://files.waveshare.com/upload/4/46/2-CH-CAN-FD-HAT-Demo.7z 7z x 2-CH-CAN-FD-HAT-Demo.7z -o./2-CH-CAN-FD-HAT-Demo
- Enter the program directory:
cd 2-CH-CAN-FD-HAT-Demo/Raspberry_Pi/Python/
- The receiver runs receive0.py:
sudo python receive0.py
- The sender runs send1.py:
sudo python send1.py
It should be noted that the sender here is sent by CAN1, and the receiver is CAN0. For specific modifications, please refer to the code analysis below.
Code Analysis
This demo is based on the Python platform, make sure that the python-can library is installed and a can device must be created before sending because the MCP2518FD kernel is only enabled in the front:
os.system('sudo ip link set can0 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on')
The above one is initialized and enabled through the configuration of CAN0, and CAN0 is designated as the transmit/receive interface. If you need to change to CAN1, the code is as follows:
os.system('sudo ip link set can1 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on')
- First step: connect to the CAN bus.
can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan_ctypes')# socketcan_native<br />
can1 = can.interface.Bus(channel = 'can0', bustype = 'socketcan')
- If it needs to be changed to CAN1, the code is as follows:
can1 = can.interface.Bus(channel = 'can1', bustype = 'socketcan_ctypes')# socketcan_native<br />
- Step 2: Create a message.
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)<br />
msg = can.Message(is_extended_id=False, arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7])
- Step 3: Send the message.
can0.send(msg)
- If it needs to be changed to CAN1, the code is as follows:
can1.send(msg)
- Finally, also close the CAN device.
os.system('sudo ifconfig can0 down')
- To be changed to CAN1, the code is as follows:
os.system('sudo ifconfig can1 down')
- Receive data:
msg = can0.recv(10.0)
The timeout reception time is defined in recv().
For more information, please refer to: https://python-can.readthedocs.io/en/stable/interfaces/socketcan.html
Arduino example
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 Arduino boards is 5V, therefore we should set the VIO to 5V as below:
Connection
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 the H and L of CAN_0 of the two 2-CH CAN FD HATs, set the baud rate to 115200, open the two serial monitors, and you can see the following results (the number of transmissions on the left, and the received data on the right) :
FAQ
Question: After installing the Raspbian OS image version released on 2024-10-28, or after upgrading to kernel version 6.6.51, does enabling CAN FD result in a segmentation fault error?
It seems this is also a bug in the new version of Raspbian OS (see: https://github.com/raspberrypi/linux/issues/6407). Please try executing the commands, or downgrade to an older image version.
sudo rpi-update pulls/6466
{{{5}}}
ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi0.0/net/can?", NAME="can0" ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi0.1/net/can?", NAME="can1" ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi1.0/net/can?", NAME="can1"
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
20mA to 50mA depending on voltage.
{{{5}}}
Resources
Datasheet
Support
Technical Support
If you need technical support or have any feedback/review, please click the Submit Now button to submit a ticket, Our support team will check and reply to you within 1 to 2 working days. Please be patient as we make every effort to help you to resolve the issue.
Working Time: 9 AM - 6 PM GMT+8 (Monday to Friday)