Difference between revisions of "Template:2-CH CAN FD HAT User Manual"
(97 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
===Features=== | ===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=== | ===Specifications=== | ||
+ | *Power input terminal voltage: DC 8~28V | ||
+ | *Logic level: 3.3V/5V | ||
*CAN controller: MCP2518FD | *CAN controller: MCP2518FD | ||
− | *CAN transceiver: MCP2562FD | + | <!--*CAN transceiver: MCP2562FD/SIT1044T--> |
− | |||
− | |||
− | |||
− | |||
− | |||
*Dimensions: 65.0 x 56.5 mm | *Dimensions: 65.0 x 56.5 mm | ||
+ | *Fixed hole through diameter: 3.0mm | ||
− | ==Interfaces== | + | ===Interfaces=== |
− | {|border=1 style="text-align:center; width: 700px" | + | {| border="1" style="text-align:center; width: 700px" |
− | + | |- style="color:white; background:green" | |
− | |-style="color:white; background:green" | + | | <font color=white>PIN</font> |
− | | | + | | <font color=white>Raspberry Pi (BCM2835)</font> |
+ | | <font color=white>Raspberry Pi (WPI)</font> | ||
+ | | <font color=white>Description</font> | ||
|- | |- | ||
− | |5V | + | | 5V |
+ | | 5V | ||
+ | | 5V | ||
+ | | 5V Power input | ||
|- | |- | ||
− | |GND | + | | GND |
+ | | GND | ||
+ | | GND | ||
+ | | Ground | ||
|- | |- | ||
− | |MISO_0 | + | | MISO_0 |
+ | | 9 (MISO) | ||
+ | | 13 (MISO) | ||
+ | | SPI_0 Data output | ||
|- | |- | ||
− | |MOSI_0 | + | | MOSI_0 |
+ | | 10 (MOSI) | ||
+ | | 12(MOSI) | ||
+ | | SPI_0 Data input | ||
|- | |- | ||
− | |SCK_0 | + | | SCK_0 |
+ | | 11 (SCK) | ||
+ | | 14 (SCK) | ||
+ | | SPI_0 Clock input | ||
|- | |- | ||
− | |CS_0 | + | | CS_0 |
+ | | 8(CE0)/7(CE1) | ||
+ | | 10(CE0)/11(CE1) | ||
+ | | CAN_0 Chip select | ||
|- | |- | ||
− | |INT_0 | + | | INT_0 |
+ | | 25/13 | ||
+ | | 6/23 | ||
+ | | CAN_0 Interrupt Pin | ||
|- | |- | ||
− | |MISO_1 | | + | | MISO_1 |
+ | | 19(MISO)/9 | ||
+ | | 24(MISO)/13 | ||
+ | | SPI_1 Data output | ||
|- | |- | ||
− | |MOSI_1 | | + | | MOSI_1 |
+ | | 20(MOSI)/10 | ||
+ | | 28(MOSI)/12 | ||
+ | | SPI_1 Data input | ||
|- | |- | ||
− | |SCK_1 | | + | | SCK_1 |
+ | | 21(SCLK)/11 | ||
+ | | 29(SCLK)/14 | ||
+ | | SPI_1 Clock input | ||
|- | |- | ||
− | |CS_1 | | + | | CS_1 |
+ | | 18(SPI1_CE0)/17/16/26 | ||
+ | | 1/0/27/25 | ||
+ | | CAN_1 Chip select | ||
|- | |- | ||
− | |INT_1 | | + | | INT_1 |
+ | | 24/23/22/16 | ||
+ | | 5/4/3/27 | ||
+ | | CAN_1 Interrupt Pin | ||
|} | |} | ||
− | |||
− | |||
− | |||
+ | [[File:2-CH-CAN-FD-HAT-details-inter.jpg]] | ||
+ | |||
+ | ===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.<br> | ||
+ | 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.<br> | ||
+ | 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.<br> | ||
:[[File:2-CH CAN FD HAT Manual-1.png|500px]] | :[[File:2-CH CAN FD HAT Manual-1.png|500px]] | ||
− | The | + | <!--The MCP2562FD is the second generation of high-speed CAN transceivers produced by Microchip. It is used for the interface between the CAN protocol controller and the physical CAN bus. It supports the higher data rates required by CAN FD and improves the maximum propagation delay to support longer bus lengths. The MCP2562FD meets or exceeds automotive requirements for CAN FD transfer rate (5 Mbps), low quiescent current, electromagnetic compatibility (EMC) and electrostatic discharge (ESD) requirements, ISO-11898-5 physical standard, and AEC-Q100 grade 0 The test standard can connect up to 112 nodes at most, please refer to the datasheet for more details. |
+ | You can connect this module to your board like Raspberry Pi by SPi interface. | ||
+ | :[[File:2-CH CAN FD HAT Manual-2.png|500px]]--> | ||
− | + | ==Working with Raspberry Pi== | |
− | :[[File:2-CH CAN FD HAT | + | === 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:<br /> | |
+ | {{Note|Note: The pinheader on the expansion board is soldered using lead-free surface mount technology. When plugging or unplugging the pinheader, it is recommended to use tools to pry them open. Make sure to apply even and gentle force on both sides to avoid damaging the solder joints. If you attempt to pry them open directly with your hands, the numerous connections between the header and pins make them generally secure when fully plugged in. However, this method can easily lead to the detachment of the PCBA and pinheader due to insufficient soldering.|error}} | ||
+ | [[File:2-CH-CAN-FD-HAT-hardware-connnected.png]]<br /> | ||
− | + | <div class="cautionSec">PS: It is recommended to use "apt" instead of "apt-get" when using the Bullseye branch system; and the Bullseye branch system only supports Python3. </div> | |
+ | When using the Raspberry Pi to demonstrate this routine, 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.<br> | ||
+ | :[[File:2-CH CAN FD HAT Manual-3.png|300px]] | ||
+ | {{RPI open i2c}} | ||
− | == | + | ===Install Libraries=== |
− | + | <!-- | |
− | |||
− | |||
;bcm2835 | ;bcm2835 | ||
− | : Open terminal and run commands below to install bcm2835 library | + | : Open the terminal and run the commands below to install the bcm2835 library. |
<pre> | <pre> | ||
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.60.tar.gz | wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.60.tar.gz | ||
Line 75: | Line 122: | ||
sudo make check | sudo make check | ||
sudo make install | sudo make install | ||
+ | # For more information, please refer to the official website: http://www.airspayce.com/mikem/bcm2835/ | ||
</pre> | </pre> | ||
− | ;Install wiringPi library | + | ; Install wiringPi library: |
<pre> | <pre> | ||
sudo apt-get install wiringpi | sudo apt-get install wiringpi | ||
− | + | #For Raspberry Pi 4B may need to be upgraded: | |
wget https://project-downloads.drogon.net/wiringpi-latest.deb | wget https://project-downloads.drogon.net/wiringpi-latest.deb | ||
sudo dpkg -i wiringpi-latest.deb | sudo dpkg -i wiringpi-latest.deb | ||
gpio -v | gpio -v | ||
+ | # Run gpio -v and version 2.52 will appear. If it does not appear, the installation is wrong | ||
</pre> | </pre> | ||
− | ;Install python library | + | --> |
+ | ;Install python library: | ||
'''python2''' | '''python2''' | ||
<pre> | <pre> | ||
Line 107: | Line 157: | ||
</pre> | </pre> | ||
− | === | + | ===Driver Installation (New Version)=== |
− | + | '''<font color="#FF0000">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.'''</font><br/> | |
+ | You can flexibly adjust the SPI and interrupt pins used by modifying the 0R resistor, '''select any of the following modes.'''<br/> | ||
− | + | ===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: <br/> | ||
+ | CAN_0 uses SPI0-0, interrupt pin is 25, CAN_1 uses SPI1-0, interrupt pin is 24<br/> | ||
− | + | Insert the module into Raspberry Pi, and then modify the config.txt file:<br /> | |
− | |||
− | |||
− | |||
<pre> | <pre> | ||
− | + | sudo nano /boot/config.txt | |
− | sudo | ||
− | |||
− | |||
</pre> | </pre> | ||
− | :[[File:2-CH CAN FD HAT | + | {{Note|The above configuration is based on the '''Raspbian OS''' system. If it is used in the '''Ubuntu''' system, the config.txt path is generally "''/boot/firmware/config.txt'' "|warn}} |
− | + | 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:<br/> | ||
+ | CAN_0 uses SPI0-0, interrupt pin is 25, CAN_1 uses SPI0-1, interrupt pin is 24. As shown below:<br> | ||
+ | [[File:2-CH CAN FD HAT SPI-09.jpg]]<br> | ||
+ | please add in /boot/config.txt:<br/> | ||
+ | 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.<br/> | ||
+ | *''' Note: When stacking is required, the hardware configuration needs to be kept in dual SPI mode ('A' mode, default setting).'''<br/> | ||
+ | *CAN_0 has two groups to choose from: | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! 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: | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! 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.''<br/> | ||
+ | ====Example of use==== | ||
+ | *If you have '''two boards, how to configure them when using 4-way CAN? '''<br/> | ||
+ | Answer: The first board is not modified, 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 and then adds at the end of /boot/config.txt:<br/> | ||
+ | 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: <br/> | ||
+ | |||
+ | Insert the module to Raspberry Pi, and then modify the config.txt file:<br /> | ||
<pre> | <pre> | ||
− | sudo | + | sudo nano /boot/config.txt |
</pre> | </pre> | ||
− | + | {{Note|The above configuration is based on the '''Raspbian OS''' system. If it is used in the '''Ubuntu''' system, the config.txt path is generally "''/boot/firmware/config.txt'' "|warn}} | |
− | + | dtparam=spi=on | |
− | :After installing, reboot your Raspberry Pi and check if the driver is installed properly. | + | 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. | ||
<pre> | <pre> | ||
sudo reboot | sudo reboot | ||
Line 136: | Line 245: | ||
: A work mode should be | : A work mode should be | ||
:[[File:2-CH CAN FD HAT Manual-6.png|800px]] | :[[File:2-CH CAN FD HAT Manual-6.png|800px]] | ||
− | :B work mod | + | : B work mod should be |
:[[File:2-CH CAN FD HAT Manual-7.png|800px]] | :[[File:2-CH CAN FD HAT Manual-7.png|800px]] | ||
− | + | <font color=red>【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.</font> | |
+ | :[[File:2-CH-CAN-FD-HD.jpg]] | ||
+ | <font color=red>【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.</font><br> | ||
+ | Note: If after installing the driver according to the above method, it is prompted that the kernel reports an error, you need to download the latest version of the image from the official Raspberry Pi, or directly update the system kernel: | ||
+ | <pre> | ||
+ | sudo apt update | ||
+ | sudo apt upgrade | ||
+ | uname -a | ||
+ | </pre> | ||
− | === | + | ===Configure CAN=== |
+ | <!-- | ||
You can set the baud rate, working mode, and buffer size. | You can set the baud rate, working mode, and buffer size. | ||
<pre> | <pre> | ||
+ | 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 | 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 can0 txqueuelen 65536 | ||
Line 159: | Line 278: | ||
*[ fd-non-iso { on | off } ] | *[ fd-non-iso { on | off } ] | ||
− | For more details | + | For more details on CAN commands, please refer to: https://www.kernel.org/doc/Documentation/networking/can.txt |
Check the can bus: | Check the can bus: | ||
ifconfig | ifconfig | ||
:[[File:2-CH CAN FD HAT Manual-8.png|600px]] | :[[File:2-CH CAN FD HAT Manual-8.png|600px]] | ||
+ | --> | ||
+ | Set the baud rate, operating mode, whether to enable FD and configure the transmission buffer size: | ||
+ | *Baud rate setting: | ||
+ | <pre> | ||
+ | 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 | ||
+ | </pre> | ||
+ | '''bitrate xxxxxx (bps):arbitration bitrate<br />dbitrate xxxxxx (bps):data bitrate<br />The following is an example of setting the bit rate<br />''' | ||
+ | [[File:2-CH-CAN-FD-HAT-sample-cmd.png]] | ||
+ | |||
+ | '''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.<br / >''' | ||
+ | |||
+ | *Can be configured into the following modes: | ||
+ | [ loopback { on | off } ]<br /> | ||
+ | |||
+ | [ listen-only { on | off } ]<br /> | ||
+ | |||
+ | [ triple-sampling { on | off } ]<br /> | ||
+ | |||
+ | [ one-shot { on | off } ]<br /> | ||
+ | |||
+ | [ berr-reporting { on | off } ]<br /> | ||
− | + | FD is the opening command:<br /> | |
− | |||
− | |||
− | |||
− | + | [ fd { on | off } ]<br /> | |
− | |||
− | + | [ fd-non-iso { on | off } ]<br /> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | '''More related CAN kernel commands can be viewed:<br /> | |
+ | https://www.kernel.org/doc/Documentation/networking/can.txt'''<br /> | ||
+ | * Configuration buffer | ||
<pre> | <pre> | ||
− | + | sudo ifconfig can0 txqueuelen 65536 | |
+ | sudo ifconfig can1 txqueuelen 65536 | ||
</pre> | </pre> | ||
− | + | * View ifconfig: | |
<pre> | <pre> | ||
− | + | ifconfig | |
</pre> | </pre> | ||
− | + | [[File:pre-work5.png|800px]]<br /> | |
+ | *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:<br /> | ||
+ | [[File:test-1.png|800px]]<br /> | ||
+ | *Install can-utils: | ||
+ | sudo apt-get install can-utils | ||
+ | * Open two terminal windows: | ||
+ | One of the terminal inputs receives the CAN0 data command: | ||
<pre> | <pre> | ||
− | can0 | + | candump can0 |
</pre> | </pre> | ||
− | + | Another terminal input sends the CAN1 data command: | |
<pre> | <pre> | ||
− | can1 | + | cansend can1 000#11.22.33.44 |
</pre> | </pre> | ||
− | + | *The demonstration effect is as follows: (the left is receiving, the right is sending) | |
+ | [[File:CAN-FD-test-88.png]] | ||
+ | <!--[[File:2-CH CAN FD HAT Manual-10.png|900px]]<br />--> | ||
+ | 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. <br /> | ||
+ | |||
+ | <!-- | ||
+ | ===Testing=== | ||
+ | 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 | ||
+ | :[[File:2-CH CAN FD HAT Manual-9.png]] | ||
+ | |||
+ | Install can-utils: | ||
+ | sudo apt-get install can-utils | ||
+ | |||
+ | Open two terminals, one works are sender and another is the receiver. | ||
+ | |||
+ | receiver: | ||
+ | candump can0 | ||
+ | |||
+ | sender: | ||
+ | cansend can1 000#111.22.33.44 | ||
+ | :[[File:2-CH CAN FD HAT Manual-10.png]] | ||
+ | |||
+ | If you have two 2-CH CAN FD HATs, you can directly connect CAN_H and CAN_L. The effect is the same as the above, you need to pay attention to match the communication rate, identify the ID, and output 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: | ||
<pre> | <pre> | ||
− | + | 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" | ||
</pre> | </pre> | ||
− | + | *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 | ||
+ | {{Note|It is generally recommended to fix it to CAN10,CAN11 or above.|warn}} | ||
+ | |||
+ | ===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: | ||
<pre> | <pre> | ||
− | + | sudo python receive0.py | |
</pre> | </pre> | ||
− | + | * The sender runs send1.py: | |
<pre> | <pre> | ||
+ | sudo python send1.py | ||
+ | </pre> | ||
+ | '''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:<br /> | ||
+ | <source lang="python">os.system('sudo ip link set can0 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on')</source><br /> | ||
+ | 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:<br /> | ||
+ | <source lang="python">os.system('sudo ip link set can1 up type can bitrate 1000000 dbitrate 8000000 restart-ms 1000 berr-reporting on fd on')</source><br /> | ||
+ | *First step: connect to the CAN bus. | ||
+ | <source lang="python"> | ||
+ | can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan_ctypes')# socketcan_native<br /> | ||
+ | </source><br /> | ||
+ | {{Note|As the python-can version is upgraded to 4.0.0, the corresponding code changes are as follows, otherwise, an error will be reported: | ||
+ | <source lang="python"> | ||
+ | can1 = can.interface.Bus(channel = 'can0', bustype = 'socketcan') | ||
+ | </source > | ||
+ | |warn}} | ||
+ | *If it needs to be changed to CAN1, the code is as follows:<br /> | ||
+ | <source lang="python"> | ||
+ | can1 = can.interface.Bus(channel = 'can1', bustype = 'socketcan_ctypes')# socketcan_native<br /> | ||
+ | </source><br /> | ||
+ | *Step 2: Create a message.<br /> | ||
+ | <source lang="python"> | ||
+ | msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)<br /> | ||
+ | </source><br /> | ||
+ | {{Note|As the python-can version is upgraded to 4.0.0, the corresponding code changes are as follows, otherwise an error will be reported | ||
+ | <source lang="python"> | ||
+ | msg = can.Message(is_extended_id=False, arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7]) | ||
+ | </source > | ||
+ | |warn}} | ||
+ | *Step 3: Send the message. | ||
+ | <source lang="python"> | ||
+ | can0.send(msg)<br /> | ||
+ | </source><br /> | ||
+ | *If it needs to be changed to CAN1, the code is as follows:<br /> | ||
+ | <source lang="python"> | ||
can1.send(msg) | can1.send(msg) | ||
− | </ | + | </source> |
− | + | *Finally, also close the CAN device. | |
− | < | + | <source lang="python"> |
os.system('sudo ifconfig can0 down') | os.system('sudo ifconfig can0 down') | ||
− | </ | + | </source> |
− | + | *To be changed to CAN1, the code is as follows: | |
− | < | + | <source lang="python"> |
os.system('sudo ifconfig can1 down') | os.system('sudo ifconfig can1 down') | ||
− | </ | + | </source> |
− | + | *Receive data: | |
− | < | + | <source lang="python"> |
msg = can0.recv(10.0) | msg = can0.recv(10.0) | ||
− | </ | + | </source> |
− | The | + | The timeout reception time is defined in recv(). <br /> |
+ | ''' For more information, please refer to: https://python-can.readthedocs.io/en/stable/interfaces/socketcan.html<br />''' | ||
− | |||
==Arduino example== | ==Arduino example== | ||
The example of Arduino is based on the MCP2518FD project of Pierre Molinaro (Thanks to Pierre Molinaro). | 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 | + | 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: |
:[[File:2-CH CAN FD HAT Manual-11.png|400px]] | :[[File:2-CH CAN FD HAT Manual-11.png|400px]] | ||
Line 239: | Line 454: | ||
{|border=1 style="text-align:center; width:600px" | {|border=1 style="text-align:center; width:600px" | ||
|+Connect 2-CH CAN FD HAT to Arduino | |+Connect 2-CH CAN FD HAT to Arduino | ||
− | |- style="color:white; background: | + | |- style="color:white; background:green" |
|PIN||Arduino UNO | |PIN||Arduino UNO | ||
|- | |- | ||
Line 257: | Line 472: | ||
|} | |} | ||
− | Connect | + | *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) : |
:[[File:2-CH CAN FD HAT Manual-12.png|700px]] | :[[File:2-CH CAN FD HAT Manual-12.png|700px]] | ||
==FAQ== | ==FAQ== | ||
− | {{FAQ|There are two demo codes named | + | {{FAQ|Can I attach the 2-CH CAN FD HAT with 2-CH CAN HAT? |
− | |The CAN HAT support two modes, in A mode, the two CAN | + | |Attaching with 2-CH CAN HAT is not supported, but 2x 2-CH CAN FD can be attached. |
+ | ||}} | ||
+ | {{FAQ| Every time I turn it on, I find that the order of CAN0 and CAN1 is random, how to fix it? | ||
+ | |You can add the 80-can.rules file under /etc/udev/rules.d/ and add the code: | ||
+ | <source lang="shell"> | ||
+ | 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" | ||
+ | </source> | ||
+ | }} | ||
+ | {{FAQ|What is the maximum baudrate of CAN FD? | ||
+ | |The theoretical baud rate limit of CAN FD is 8Mbps, but in the actual application, the baud rate will be affected by the interference of the communication environment and the communication distance, and other factors, the actual environment is subject to the actual measurement. | ||
+ | ||}} | ||
+ | |||
+ | {{FAQ|There are two demo codes named A mode and B mode, which one should I use? | ||
+ | |The CAN HAT support two modes, in A mode, the two CAN channel use different SPI interfaces, and the two CAN channel use the same SPI in B mode. The hardware is set to A mode by default. If you are the first time receive it and use it, please choose the A mode codes. | ||
+ | }} | ||
+ | {{FAQ| Is the G pin of the CAN interface connected? | ||
+ | |G is the signal ground, also known as the isolation ground. In the industrial environment, CAN is subject to a changeable environment. To ensure stability, the G of the two communication modules needs to be connected. | ||
}} | }} | ||
{{FAQ|Why does CAN initialize failed? | {{FAQ|Why does CAN initialize failed? | ||
− | |Please check if you | + | |Please check if you connected the hardware correctly and restart the devices to test again. |
|}} | |}} | ||
{{FAQ|The speed of CAN communication is less than the MAX value expected? | {{FAQ|The speed of CAN communication is less than the MAX value expected? | ||
− | |The maximum bps is experimental data, the actual speed may be influenced by the quality of cable, software | + | |The maximum bps is experimental data, the actual speed may be influenced by the quality of the cable, software, communication distance, etc. Therefore, the actual maximum speed may be less than it. |
}} | }} | ||
− | {{FAQ|The CAN bus | + | {{FAQ|The CAN bus failed to work? |
− | |If the | + | |If the initialization is successful and the speed is set properly, it still cannot communicate normally. Please check whether the CAN_H of the next two 2-CH CAN FD HATs is connected to another CAN_H and whether CAN_L is connected to another CAN_L. It should be noted that these two lines cannot be reversed. H corresponds to H, and L corresponds to L. If there is no problem, please check whether the CAN port configured by the program is correct. For example, CAN0 is required, but the program code configures CAN1. At the same time, it is also necessary to check whether the hardware is connected correctly and whether there is a short circuit in the CAN interface. As shown in the figure below, the CAN interface and the HDMI interface of the Raspberry Pi are accidentally connected to cause a short circuit, which makes the CAN unable to communicate normally. At this time, it is necessary to purchase a 2-CH The accessories 2×20PIN headers delivered during CAN FD HAT are increased in height. |
[[File:2-CH CAN FD HAT Manual-13.png|400px]] | [[File:2-CH CAN FD HAT Manual-13.png|400px]] | ||
}} | }} | ||
+ | {{FAQ|Can multiple devices be connected to the CAN interface? | ||
+ | |In theory, as long as the CAN IDs are different, multiple CAN devices can be connected for communication. However, the specific number of connections is limited by various factors such as the communication volume and electrical load of the CAN sub-devices, and needs to be tested according to the actual application. Assuming that the sub-devices communicate with a large amount of data and have high real-time requirements, it may not be possible to carry 2 to 3 devices at the same time. | ||
+ | }} | ||
+ | {{FAQ|How much current is used at the "External power supply" connector? | ||
+ | | | ||
+ | 20mA to 50mA depending on voltage. | ||
+ | ||}} |
Latest revision as of 08:26, 20 March 2024
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 routine, 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 I2C Interface
Open a terminal and run the following commands:
sudo raspi-config Choose Interfacing Options -> I2C -> Yes.
Reboot Raspberry Pi:
sudo reboot
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, 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 and then adds at the end of /boot/config.txt:
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.
Note: If after installing the driver according to the above method, it is prompted that the kernel reports an error, you need to download the latest version of the image from the official Raspberry Pi, or directly update the system kernel:
sudo apt update sudo apt upgrade uname -a
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)<br />
- 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
{{{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}}}