Difference between revisions of "2-CH CAN HAT+"

From Waveshare Wiki
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
=Overview=
 
=Overview=
 
==Introduction==
 
==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.
+
2-CH CAN HAT+ is an isolated expansion board for Raspberry Pi, supports dual-channel CAN communication, and features multi-onboard protection circuits, wide voltage input, and so on.
 
==Features==
 
==Features==
 
*Designed for Raspberry Pi, support Raspberry Pi Zero/Zero W/Zero WH/2B/3B/3B+/4B/5.
 
*Designed for Raspberry Pi, support Raspberry Pi Zero/Zero W/Zero WH/2B/3B/3B+/4B/5.
 
*Standard HAT+ design, with onboard EEPROM chip.
 
*Standard HAT+ design, with onboard EEPROM chip.
*Adopts MCP2515 and SI65HVD230(or SN65HVD230) dual chips combined solution, allowing 2-channel CAN communication.
+
*Adopts MCP2515 and SI65HVD230(or SN65HVD230) dual-chip solution, allowing 2-channel CAN communication.
 
*Integrated power isolation, providing stable isolated voltage, requires no extra power supply for the isolated terminal.
 
*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 digital isolator, signal isolation makes communication safer, more stable, and better anti-interference.
Line 34: Line 34:
 
|'''PIN'''||'''RPI PINOUT'''<br />(BCM)||'''RPI PINOUT'''<br />(WPI)||'''Description'''
 
|'''PIN'''||'''RPI PINOUT'''<br />(BCM)||'''RPI PINOUT'''<br />(WPI)||'''Description'''
 
|-rowspan=2 align="center"
 
|-rowspan=2 align="center"
|5V|| 5V|| 5V|| 5V power input
+
|5V||5V||5V||5V power input
 
|-align="center"
 
|-align="center"
 
|GND||GND||GND||Ground
 
|GND||GND||GND||Ground
Line 53: Line 53:
 
|}
 
|}
 
<font color="red">
 
<font color="red">
Note: Bolded pins are the default connection pins
+
Note: Bolded pins are the default connection pins.
 
</font>
 
</font>
  
Line 76: Line 76:
  
 
==Dimensions==
 
==Dimensions==
 +
[[File:2-CH CAN HAT+ dimen.jpg]]
 +
 
=Raspberry Pi Guide=
 
=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:<br />
 
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:<br />
Line 83: Line 85:
  
 
==Install libraries==
 
==Install libraries==
*Install BCM2835, open the Raspberry Pi terminal and run the following commands:   
+
*Install BCM2835, open the Raspberry Pi terminal, and run the following commands:   
 
<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 94: Line 96:
 
# For More: http://www.airspayce.com/mikem/bcm2835/
 
# For More: http://www.airspayce.com/mikem/bcm2835/
 
</pre>
 
</pre>
*Install wiringPi
+
===Install wiringPi===
===wiringPi===
 
 
====32-bit Raspberry Pi System====
 
====32-bit Raspberry Pi System====
 
<pre>
 
<pre>
Line 151: Line 152:
 
</pre>
 
</pre>
 
*Find the corresponding product on the official website, open the download path in the product information, and download the sample demo in the wiki:<br />
 
*Find the corresponding product on the official website, open the download path in the product information, and download the sample demo in the wiki:<br />
*Get the unzipped package, unzip it and copy the demo to the Raspberry Pi.
+
*Get the unzipped package, unzip it, and copy the demo to the Raspberry Pi.
  
 
===Preparation===
 
===Preparation===
 
====Enable SPI Interface====
 
====Enable SPI Interface====
*Open the Raspberry Pi terminal, input the following commands to enter the configure interface: <br />
+
*Open the Raspberry Pi terminal, and input the following commands to enter the configure interface: <br />
 
<pre>
 
<pre>
 
sudo raspi-config
 
sudo raspi-config
Line 179: Line 180:
 
dtoverlay=i2c0  
 
dtoverlay=i2c0  
 
dtoverlay=spi-3cs
 
dtoverlay=spi-3cs
dtoverlay=mcp2515,oscillator=16000000,interrupt=22
+
dtoverlay=mcp2515,spi1-1,oscillator=16000000,interrupt=22
dtoverlay=mcp2515,oscillator=16000000,interrupt=13
+
dtoverlay=mcp2515,spi1-2,oscillator=16000000,interrupt=13
dtoverlay=spi-bcm2835-overlay
 
 
</pre>
 
</pre>
 
*Save and exit, then restart your Pi:
 
*Save and exit, then restart your Pi:
Line 192: Line 192:
 
</pre>
 
</pre>
 
[[File:2-CH CAN HAT+240423 06.png|700px]]
 
[[File:2-CH CAN HAT+240423 06.png|700px]]
*Enable CAN:<br />
+
*Enable CAN:<br />
 
<pre>
 
<pre>
 
sudo ip link set can0 up type can bitrate 1000000
 
sudo ip link set can0 up type can bitrate 1000000
Line 215: Line 215:
  
 
===Test===
 
===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:<br>
+
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:<br>
 
[[File:2-CH CAN HAT+240423_05.png|600px]]
 
[[File:2-CH CAN HAT+240423_05.png|600px]]
 
*Install can-utils:
 
*Install can-utils:
Line 251: Line 251:
 
os.system('sudo ifconfig can0 up')
 
os.system('sudo ifconfig can0 up')
 
</pre>
 
</pre>
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:
+
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:
 
<pre>
 
<pre>
 
os.system('sudo ip link set can1 type can bitrate 100000')
 
os.system('sudo ip link set can1 type can bitrate 100000')
Line 349: Line 349:
 
|||}}
 
|||}}
 
{{FAQ|The CAN communication speed cannot reach the maximum value?
 
{{FAQ|The CAN communication speed cannot reach the maximum value?
|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.
+
|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.
 
|||}}
 
|||}}
 
{{FAQ|Is pin G of the CAN interface connected?
 
{{FAQ|Is pin G of the CAN interface connected?

Latest revision as of 02:54, 30 April 2024

2-CH CAN HAT+
2-CH CAN HAT+.jpg

SPI, CAN, RPi
{{{name2}}}

{{{name3}}}

{{{name4}}}

{{{name5}}}

Overview

Introduction

2-CH CAN HAT+ is an isolated expansion board for Raspberry Pi, supports dual-channel CAN communication, and 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-chip 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. 2-CH-CAN-HAT-PLUS-setting.png

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.
MCP2515-PIN.png

Dimensions

2-CH CAN HAT+ dimen.jpg

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:
2-CH-CAN-HATPLUS-3V3.png
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:
2-CH-CAN-HATPLUS-3V303.png

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

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

Example:
TF-Luna LiDAR Range SensorRas 05.png
TF-Luna LiDAR Range SensorRas 07.png

  • 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, and input the following commands to enter the configure interface:
sudo raspi-config
Select Interfacing Options -> SPI -> Yes to enable SPI interface 

RPI open spi.png
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.

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
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 "

Add these statements at the last line:

dtparam=spi=on
dtoverlay=i2c0 
dtoverlay=spi-3cs
dtoverlay=mcp2515,spi1-1,oscillator=16000000,interrupt=22
dtoverlay=mcp2515,spi1-2,oscillator=16000000,interrupt=13
  • Save and exit, then restart your Pi:
sudo reboot
  • After rebooting the Raspberry Pi, and view SPI information:
dmesg | grep spi1

2-CH CAN HAT+240423 06.png

  • 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

2-CH-CAN-HAT-ifconfig.jpg

  • 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 to CAN1_H, and CAN0_L to CAN1_L of the module as shown in the following figure:
2-CH CAN HAT+240423 05.png

  • Install can-utils:
sudo apt-get install can-utils
  • Open two terminal windows:

One inputs commands to receive CAN0 data:

candump can0

Another one inputs 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.)

2-CH-CAN-HAT-cansend.png
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 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')
Step 1: Connect to CAN Bus
can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes')
As the python-can version is upgraded to 4.0.0, the corresponding code changes are as follows, otherwise an error will be reported.
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)
As the python-can version is upgraded to 4.0.0, the corresponding code changes are as follows, otherwise an error will be reported.
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

 Answer:

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}}}


 Answer:
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.

{{{5}}}


 Answer:
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.

{{{5}}}


 Answer:
You can try to update the kernel version:
sudo apt update
sudo apt upgrade
uname --all


 Answer:
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.


 Answer:
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.


 Answer:
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.

{{{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)