Difference between revisions of "2-CH CAN HAT+"
Line 72: | Line 72: | ||
</font> | </font> | ||
Microchip Technology’s MCP2515 is a stand-alone Controller Area Network (CAN) controller that implements the CAN specification, version 2.0B. It is capable | Microchip Technology’s MCP2515 is a stand-alone Controller Area Network (CAN) 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 masks and six acceptance filters that are used to filter out unwanted messages, thereby reducing the host MCUs overhead. The MCP2515 interfaces with microcontrollers (MCUs) via an industrial standard Serial Peripheral Interface (SPI). | + | of transmitting and receiving both standard and extended data and remote frames. The MCP2515 has two acceptance masks and six acceptance filters that are used to filter out unwanted messages, 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 device tree in the kernel driver can be used.<br> |
[[File:MCP2515-PIN.png|400px]]<br /> | [[File:MCP2515-PIN.png|400px]]<br /> | ||
Revision as of 02:56, 24 April 2024
| ||
Overview
Introduction
2-CH CAN HAT+ is an isolated expansion board for Raspberry Pi, supports dual-channel CAN communication, features multi-onboard protection circuits, wide voltage input, and so on.
Features
- Designed for Raspberry Pi, support Raspberry Pi Zero/Zero W/Zero WH/2B/3B/3B+/4B/5.
- Standard HAT+ design, with onboard EEPROM chip.
- Adopts MCP2515 and SI65HVD230(or SN65HVD230) dual chips combined solution, allowing 2-channel CAN communication.
- Integrated power isolation, providing stable isolated voltage, requires no extra power supply for the isolated terminal.
- Onboard digital isolator, signal isolation makes communication safer, more stable, and better anti-interference.
- Onboard SM24CANB (transient voltage suppressor), provides ESD protection and transient peak voltage protection.
- Onboard voltage translator, select 3.3V/5V operating voltage by jumper.
- Onboard 120Ω terminal resistor, configured by jumper.
- Breakout SPI control pins, for connecting with host control boards like STM32/Arduino.
- Comes with online development resources and user manuals.
Parameters
- Input voltage: 5V, 7~36V
- Logic level: 3.3V/5V
- CAN controller: MCP2515
- CAN receiver: SN65HVD230
- Dimensions: 65.0x56.5mm
- Mounting hole diameter: 3.0mm
Interface Description
PIN | RPI PINOUT (BCM) |
RPI PINOUT (WPI) |
Description |
5V | 5V | 5V | 5V power input |
GND | GND | GND | Ground |
MISO | 19(SPI1 MISO)/9(SPI0 MISO) | 24(SPI1 MISO)/13(SPI0 MISO) | SPI clock input |
MOSI | 20(SPI1 MOSI)/10(SPI0 MOSI) | 28(SPI1 MOSI)/12(SPI0 MOSI) | SPI data input |
SCK | 21(SPI1 SCLK)/11(SPI0 SCLK) | 29(SPI1 SCLK)/14(SPI0 SCLK) | SPI data output |
CS_0 | 17(SPI1 CE1)/8(SPI0 CE0) | 0(SPI1 CE1)/10(SPI0 CE0) | CAN_0 chip selection |
INT_0 | 22/23 | 3/4 | CAN_0 interrupt output |
CS_1 | 16(SPI1 CE2)/18(SPI1 CE0)/7(SPI0 CE1) | 27(SPI1 CE2)/1(SPI1 CE0)/11(SPI0 CE1) | CAN_1 chip selection |
INT_1 | 13/24/25 | 23/5/6 | CAN_1 interrupt output |
Note: Bolded pins 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 bus
CAN module could process packets transmitted/received on the CAN bus. Packets transmit the first store packet to the related buffer and control register.
Use the SPI interface to set the 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 verify it with a filter that is defined by the user. And store the packet in one of the buffers if it has no errors.
Raspberry Pi cannot support SPI bus, so this module uses an SPI interface and on
board a receiver/transmitter for CAN communication.
Microchip Technology’s MCP2515 is a stand-alone Controller Area Network (CAN) 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 masks and six acceptance filters that are used to filter out unwanted messages, 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 device tree in the kernel driver can be used.
Dimensions
Raspberry Pi Guide
The working voltage level of Raspberry Pi is 3.3V, therefore we need to set the logic level of 2-CH CAN HAT+ to 3.3V as below:
600px
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:
600px
Install libraries
- Install BCM2835, open the Raspberry Pi terminal and run the following commands:
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/
- Install wiringPi
wiringPi
32-bit Raspberry Pi System
#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
64-bit Raspberry Pi System
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 "sudo . /build":
chmod +x ./build
- 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 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
- Find the corresponding product on the official website, open the download path in the product information, and download the sample demo in the wiki:
- Get the unzipped package, unzip it and copy the demo to the Raspberry Pi.
Preparation
Enable SPI Interface
- Open the Raspberry Pi terminal, input the following commands to enter the configure interface:
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 another device, you can check in /boot/config.txt.
Modify config.txt
Insert the module into Raspberry Pi, and then modify the config.txt file:
sudo nano /boot/config.txt #pi5 is /boot/firmwave/config.txt
Add these statements at the last line:
dtparam=spi=on dtoverlay=i2c0 dtoverlay=spi-3cs dtoverlay=mcp2515,oscillator=16000000,interrupt=22 dtoverlay=mcp2515,oscillator=16000000,interrupt=13 dtoverlay=spi-bcm2835-overlay
- Save and exit, then restart your Pi:
sudo reboot
- After rebooting the Raspberry Pi, and view SPI information:
dmesg | grep spi1
- Enable CAN:
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
- For more commands about CAN kernel:
https://www.kernel.org/doc/Documentation/networking/can.txt
- Run ifconfig:
ifconfig
- Turn off CAN:
sudo ifconfig can0 down sudo ifconfig can1 down
Test
If you only have one 2-CH CAN HAT+, you can connect CAN0_H and CAN1_H and CAN0_L and CAN1_L of the module as shown in the following figure:
- Install can-utils:
sudo apt-get install can-utils
- Open two terminal windows:
One for inputting commands to receive CAN0 data:
candump can0
Another one is for inputting commands to send CAN1 data:
cansend can1 000#11.22.33.44
- The demonstration effect is as follows: (receiving on the left, sending on the right.)
If you have two 2-CH CAN HATs, 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.
Python Example
- Browse the catalog:
- Run receive.py on the receiving terminal:
sudo python reveive.py
- Run send.py on the sending terminal:
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.
Demo Analysis
【Python Demo】
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:
os.system('sudo ip link set can0 type can bitrate 100000') os.system('sudo ifconfig can0 up')
The above code initializes 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')
Step 1: Connect to CAN Bus
can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes')
can1 = can.interface.Bus(channel = 'can0', bustype = 'socketcan')
- Change to CAN1 as shown below:
can0 = can.interface.Bus(channel = 'can1', bustyp = 'socketcan')
Step 2: Create Message
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)
msg = can.Message(is_extended_id=False, arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7])
Step 3: Send Message
can0.send(msg)
- Change to CAN1 as shown below:
can1.send(msg)
- Finally, close the CAN device:
os.system('sudo ifconfig can0 down')
- Change to CAN1 as shown below:
os.system('sudo ifconfig can1 down')
- Receive data:
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
【WringPi Demo】
- Blocking the reception, the Raspberry Pi opens the terminal and runs:
cd 2-CH_CAN_HAT_Code/wiringPi/receive/ make clean sudo make sudo ./can_receive
- Sending: open the Raspberry Pi and run the following codes:
cd 2-CH_CAN_HAT_Code/ wiringPi/receive/ make clean sudo make sudo ./can_send
Resource
Document
Demo
Related Resource
FAQ
Stacking multiple 2-CH CAN HAT expansion boards is not supported, the interfaces and drivers will conflict; if other HAT are stacked, if the interfaces and drivers do not conflict, theoretically 2-CH CAN HAT, 2-CH CAN FD HAT, and 2-CH RS485 HAT are stackable with 2-CH CAN HAT+.
File:2-CH CAN HAT+ FAQ01.PNG
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
{{{5}}}
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)