Google Chat: zj734465502@gmail.com
+86-0755-88291180
sales01@spotpear.com
dragon_manager@163.com
services01@spotpear.com
manager01@spotpear.com
WhatsApp:13246739196
2-CH CAN HAT+ is an isolated expansion board for Raspberry Pi, supports dual-channel CAN communication, and features multi-protection circuits, wide voltage input, and so on.
Note: The bolded are the default connection pins.
NOTE INT_0 is soldered by PIN22 by default, and INT_1 is soldered by PIN13 by default.
If you need to modify the pins, you need to modify the soldering pad of the PCBA board, and modify the corresponding setting of /boot/config.txt (Pi5 is /boot/firmware/config.txt).
For example. if you need to change INT_1 from the default PIN13 to PIN24, you need to solder the 0 ohm at the arrow of PIN13 to PIN24.
CAN module could process packets transmitted/received on the CAN bus. When the packet is sent, the packet is first loaded into the correct packet buffer and control register. Use the SPI interface to set the corresponding bits on the control register or enable the transmit pin for transmitting. Registers could be read to detect communication states and errors. It will first check if there are any errors of packets detected on the CAN bus, then match it with a filter that is defined by the user to determine whether to move the message to one of the two receive buffers.
Since the Raspberry Pi itself does not support the CAN bus, you can use a CAN controller with an SPI interface along with a transceiver for CAN functionality.
Microchip’s MCP2515 is a stand-alone Controller Area Network (CAN) protocol controller that implements the CAN specification, version 2.0B. It is capable of transmitting and receiving both standard and extended data and remote frames. The MCP2515 has two acceptance mask registers and six acceptance filter registers to filter out unwanted packets, thereby reducing the host MCUs overhead. The MCP2515 interfaces with microcontrollers (MCUs) via an industrial standard Serial Peripheral Interface (SPI). Also, the Raspberry Pi connects to the chip through the SPI interface, for the Raspberry Pi to use the chip does not need to write a driver, only needs to open the kernel driver in the device tree.
Refer to the datasheet for more details.
The working voltage level of Raspberry Pi is 3.3V, therefore we need to jump the logical voltage pin of the 2-CH CAN HAT+ to 3.3V as below:
Note: When connecting to the Raspberry Pi 2b/3b/4b/5 boards, please fix it with copper standoffs to avoid the back of the CAN terminal touching the HDMI interface causing a short circuit, and avoid wrong connection or poor contact:
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 # For More: http://www.airspayce.com/mikem/bcm2835/
#Open the Raspberry Pi terminal and run the following command cd sudo apt-get install wiringpi #For Raspberry Pi systems after May 2019 (earlier than that can be executed without), an upgrade may be required: wget https://project-downloads.drogon.net/wiringpi-latest.deb sudo dpkg -i wiringpi-latest.deb gpio -v # Run gpio -v and version 2.52 will appear, if it doesn't it means there was an installation error # Bullseye branch system using the following command: git clone https://github.com/WiringPi/WiringPi cd WiringPi ./build sudo gpio -v # Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error
Copy the resource package to the Raspberry Pi using the command:
wget https://files.waveshare.com/upload/8/8c/WiringPi-master.zip
(optional, you can skip this step if you have used the unzip command) Install the unzip environment:
sudo apt-get install unzip
Go to the file location and execute the unzip command:
unzip WiringPi-master.zip
Go to the file directory (go to the "WiringPi-master" folder):
cd WiringPi-master/
Execute sudo ./build
sudo ./build
(optional, see point 4 for errors) If ./build does not work, execute "chmod +x ./build" and then execute"sudo ./build":
chmod +x ./build
E
#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 pip 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
sudo raspi-config Select Interfacing Options -> SPI -> Yes to enable SPI interface
And then reboot the Raspberry Pi:
sudo reboot
Make sure that the SPI is not occupied by other devices, you can check in /boot/config.txt.
Insert the module into Raspberry Pi, and then modify the boot script config.txt:
sudo nano /boot/config.txt #pi5 is /boot/firmwave/config.txt
Add these statements at the last line:
dtparam=spi=on dtoverlay=i2c0 dtoverlay=spi1-3cs dtoverlay=mcp2515,spi1-1,oscillator=16000000,interrupt=22 dtoverlay=mcp2515,spi1-2,oscillator=16000000,interrupt=13
sudo reboot
dmesg | grep spi1
sudo ip link set can0 up type can bitrate 1000000 sudo ip link set can1 up type can bitrate 1000000 sudo ifconfig can0 txqueuelen 65536 sudo ifconfig can1 txqueuelen 65536
https://www.kernel.org/doc/Documentation/networking/can.txt
ifconfig
sudo ifconfig can0 down sudo ifconfig can1 down
If you only have one 2-CH CAN HAT+, you can connect CAN0_H to CAN1_H, and CAN0_L to CAN1_L of the module as shown in the following figure:
sudo apt-get install can-utils
One inputs commands to receive CAN0 data:
candump can0
Another one inputs commands to send CAN1 data:
cansend can1 000#11.22.33.44
If you have two 2-CH CAN HAT+s, you can directly connect CAN_H and CAN_L between them. The effect is the same as the above, pay attention to match the communication rate, identify the ID, and output the interface serial number.
sudo python reveive.py
sudo python send.py
It should be noted that the sender here is using CAN1 to send, the receiver is CAN0, the specific modifications can refer to the following code analysis.
The demo codes provided are based on Python, please check that you installed the python-can library.
Before you send data, you should create a CAN device first as only the MCP2515 kernel is booted earlier:
os.system('sudo ip link set can0 type can bitrate 100000') os.system('sudo ifconfig can0 up')
The above code initializes the CAN0 configuration, and specify the CAN0 as the receiver/sender. If you want to change it to CAN1, you can use this one:
os.system('sudo ip link set can1 type can bitrate 100000') os.system('sudo ifconfig can1 up')
can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes')
can0 = can.interface.Bus(channel = 'can1', bustyp = 'socketcan')
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)
can0.send(msg)
can1.send(msg)
os.system('sudo ifconfig can0 down')
os.system('sudo ifconfig can1 down')
msg = can0.recv(10.0)
The variable of "recv()" function is the timeout of receiving.
For more information, please refer to: https://python-can.readthedocs.io/en/stable/interfaces/socketcan.html
cd 2-CH_CAN_HAT_Code/wiringPi/receive/ make clean sudo make sudo ./can_receive
cd 2-CH_CAN_HAT_Code/ wiringPi/receive/ make clean sudo make sudo ./can_send
dtparam=spi=on dtoverlay=i2c0 dtoverlay=spi1-3cs dtoverlay=mcp2515,spi1-1,oscillator=16000000,interrupt=22 dtoverlay=mcp2515,spi1-2,oscillator=16000000,interrupt=13 dtoverlay=mcp2515,spi0-0,oscillator=16000000,interrupt=23 dtoverlay=mcp2515,spi0-1,oscillator=16000000,interrupt=25
dtparam=spi=on dtoverlay=i2c0 dtoverlay=spi1-3cs dtoverlay=mcp2515,spi1-1,oscillator=16000000,interrupt=22 dtoverlay=mcp2515,spi1-2,oscillator=16000000,interrupt=13 dtoverlay=mcp251xfd,spi0-0,interrupt=25 dtoverlay=mcp251xfd,spi1-0,interrupt=24
Stacking multiple 2-CH CAN HAT+ expansion boards is supported, interfaces and drivers can be selected via the back resistor; if other HAT are stacked, if the interfaces and drivers do not conflict, theoretically it's stackable,for example, 2-CH CAN HAT, 2-CH CAN FD HAT, and 2-CH RS485 HAT are stackable with 2-CH CAN HAT+.
Yes. CAN bus can be connected to multiple CAN slaves in parallel, but the addresses should not conflict, and the actual data communication volume and interference should be considered, subject to the actual measurement.
Ground pin, common ground can ensure the stability and reliability of the signal, to avoid signal interference and noise. The CAN itself is a differential signal line, if the customer's CAN device does not have a GND pin, it can also be left unconnected.
You can try to update the kernel version, the commands are as follows::
sudo apt update sudo apt upgrade uname --all
If the initialization fails, you can restart the Raspberry Pi or other main control platform development boards to ensure that the connection is correct, refer to the wiki to carefully check whether there is any leakage in the configuration and whether the logic voltage jumper pin is selected correctly.
G is the signal ground, also called isolated ground. In the industrial environment, CAN is subject to changeable surroundings and to ensure stability, the G of the two communication modules need to be connected.
The chip is affected by many factors such as the surrounding environment, communication distance, wires, software, and so on. During high-speed communication, the data baud rate may not reach the nominal maximum rate. Users need to ensure stability and select a suitable communication speed according to actual measurements.
Monday-Friday (9:30-6:30) Saturday (9:30-5:30) -China time
Email: services01@spotpear.com