Difference between revisions of "Template:2-CH CAN HAT User Manual"

From Waveshare Wiki
Jump to: navigation, search
(Created page with "==Introduction== 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 1...")
 
Line 22: Line 22:
  
 
==Interfaces==
 
==Interfaces==
===CAN接口总线===
+
===CAN bus===
 
{|border=1; style="width:900px;"
 
{|border=1; style="width:900px;"
 
|-style="background:#464646; color:#fff;" align="center"
 
|-style="background:#464646; color:#fff;" align="center"
|'''功能引脚'''||'''树莓派接口'''<br />(BCM)||'''树莓派接口'''<br />(WPI)||'''描述'''
+
|'''PIN'''||'''Raspberry Pi'''<br />(BCM)||'''Raspberry Pi'''<br />(WPI)||'''Description'''
 
|-rowspan=2 align="center"
 
|-rowspan=2 align="center"
|5V|| 5V|| 5V|| 5V电源正
+
|5V|| 5V|| 5V|| 5V Power input
 
|-align="center"
 
|-align="center"
|GND||GND||GND||电源地
+
|GND||GND||GND||Ground
 
|-align="center"
 
|-align="center"
|MISO ||9(MISO) ||13(MISO)||SPI时钟输入
+
|MISO ||9(MISO) ||13(MISO)||SPI clock input
 
|-align="center"
 
|-align="center"
|MOSI||10(MOSI)||12(MOSI) ||SPI数据输入
+
|MOSI||10(MOSI)||12(MOSI) ||SPI data input
 
|-align="center"
 
|-align="center"
|SCK||11(SCLK)||14(SCLK)||SPI数据输出
+
|SCK||11(SCLK)||14(SCLK)||SPI data output
 
|-align="center"
 
|-align="center"
|CS_0||8(CE0)||10(CE0)||CAN_0片选
+
|CS_0||8(CE0)||10(CE0)||CAN_0 chip select
 
|-align="center"
 
|-align="center"
|INT_0||25(默认)/24||6(默认)/5||CAN_0中断输出
+
|INT_0||25(Default)/24||6(Default)/5||CAN_0 interrupt output
 
|-align="center"
 
|-align="center"
|CS_1||7(CE1)||11(CE1)||CAN_1片选
+
|CS_1||7(CE1)||11(CE1)||CAN_1 chip select
 
|-align="center"
 
|-align="center"
|INT_1||23(默认)/22||4(默认)/3||CAN_1中断输出
+
|INT_1||23(Default)/22||4(Default)/3||CAN_1 interrupt output
 
|}
 
|}
  
===硬件说明===
+
===Hardware description===
===CAN总线===
+
===CAN bus===
CAN 模块的功能是处理所有 CAN 总线上的报文接收和发送。报文发送时,首先将报文装载到正确的报文缓冲器和控制寄存器中。通过 SPI 接口设置控制寄存器中的相应位或使用发送使能引脚均可启动发送操作。通过读取相应的寄存器可以检查通讯状态和错误。 会对在 CAN总线上检测到的任何报文进行错误检查,然后与用户定义的滤波器进行匹配,以确定是否将报文移到两个接收缓冲器中的一个。<br />
+
The CAN HAT is used to handle all the packets transmitting/receiving on the CAN bus. Transmit: Save the packets to the correct packet buffer and control registers. Begin to transmit by setting the control bit or transmit enable pin via SPI interface. Users can check the communication status and errors by reading related registers. 会对在 CAN总线上检测到的任何报文进行错误检查,然后与用户定义的滤波器进行匹配,以确定是否将报文移到两个接收缓冲器中的一个。<br />
 
<font color="#FF0000">
 
<font color="#FF0000">
 
由于树莓派本身并不支持CAN总线,因此使用SPI接口的CAN控制器,搭配一个收发器完成CAN功能。<br />
 
由于树莓派本身并不支持CAN总线,因此使用SPI接口的CAN控制器,搭配一个收发器完成CAN功能。<br />

Revision as of 10:05, 29 April 2020

Introduction

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.

Features

  • Standard Raspberry Pi 40PIN GPIO extension header, supports Raspberry Pi series boards
  • Adopts MCP2515 and SN65HVD230 dual chips combined solution, allows 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 the communication more safe, more stable, and better anti-interference
  • Onboard TVS (Transient Voltage Suppressor) diode, 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 development resources and manual

Specifications

  • CAN controller: MCP2515
  • CAN transceiver: SN65HVD230
  • Communication interface: SPI (two channels independent SPI by default, swith to one channel SPI by soldering)
  • Operating voltage: 5V
  • Logic level: 3.3V/5V
  • Dimensions: 65.0 x 56.5 mm

Interfaces

CAN bus

PIN Raspberry Pi
(BCM)
Raspberry Pi
(WPI)
Description
5V 5V 5V 5V Power input
GND GND GND Ground
MISO 9(MISO) 13(MISO) SPI clock input
MOSI 10(MOSI) 12(MOSI) SPI data input
SCK 11(SCLK) 14(SCLK) SPI data output
CS_0 8(CE0) 10(CE0) CAN_0 chip select
INT_0 25(Default)/24 6(Default)/5 CAN_0 interrupt output
CS_1 7(CE1) 11(CE1) CAN_1 chip select
INT_1 23(Default)/22 4(Default)/3 CAN_1 interrupt output

Hardware description

CAN bus

The CAN HAT is used to handle all the packets transmitting/receiving on the CAN bus. Transmit: Save the packets to the correct packet buffer and control registers. Begin to transmit by setting the control bit or transmit enable pin via SPI interface. Users can check the communication status and errors by reading related registers. 会对在 CAN总线上检测到的任何报文进行错误检查,然后与用户定义的滤波器进行匹配,以确定是否将报文移到两个接收缓冲器中的一个。
由于树莓派本身并不支持CAN总线,因此使用SPI接口的CAN控制器,搭配一个收发器完成CAN功能。
Microchip 的 MCP2515 是一款CAN协议控制器,完全支持 CAN V2.0B 技术规范。该器件能发送和接收标准和扩展数据帧以及远程帧。 MCP2515 自带的两个验收屏蔽寄存器和六个验收滤波寄存器可以过滤掉不想要的报文,因此减少了主单片机(MCU)的开销。MCU通过SPI接口与该器件连接,即树莓派通过SPI接口连接芯片,对于树莓派使用该芯片不需要编写驱动,只需要打开设备树中的内核驱动即可使用。
900px
更多详细请参考数据手册;
SN65HVD230 是德州仪器公司生产的 3.3V CAN 收发器,该器件适用于较高通信速率、良好抗干扰 能力和高可靠性 CAN 总线的串行通信。SN65HVD230 具有高速、斜率和等待 3 种不同的工作模式。 其工作模式控制可通过 Rs 控制引脚来实现。CAN 控制器的输出引脚 Tx 接到 SN65HVD230 的数据 输入端 D,可将此 CAN 节点发送的数据传送到 CAN 网络中;而 CAN 控制器的接收引脚 Rx 和 SN65HVD230 的数据输出端 R 相连,用于接收数据。
900px

树莓派使用

在使用树莓派演示该例程时,需注意的是由于树莓派属于3.3V逻辑系统,需改变将2-CH CAN HAT的逻辑电压引脚跳帽到3.3V,如下图所示。
2-CH-CAN-HAT-3V3.jpg

安装必要的函数库

  • 安装BCM2835, 打开树莓派终端,并运行一下指令
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
# 更多的可以参考官网:http://www.airspayce.com/mikem/bcm2835/
  • 安装wiringPi
sudo apt-get install wiringpi
#对于树莓派4B可能需要进行升级:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
# 运行gpio -v会出现2.52版本,如果没有出现说明安装出错
  • 安装Python函数库
#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
  • 在官网上找到对应产品,在产品资料打开下载路径,在wiki中下载示例程序:
  • 得到解压包,解压并将程序复制到树莓派中。

前置工作

将模块插在树莓派上,然后修改开机脚本config.txt

sudo nano /boot/config.txt

在最后一行加入如下:

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=23
dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=25
  • 重启树莓派以应用所有设置:
sudo reboot
  • 待树莓派重启后,查看SPI信息:
dmesg | grep spi

2-CH-CAN-HAT-SPI-Init.png

  • 开启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
  • 更多相关CAN内核指令可以查看:
https://www.kernel.org/doc/Documentation/networking/can.txt
  • 查看ifconfig:
ifconfig

2-CH-CAN-HAT-ifconfig.jpg

开始测试

若手上只有一个2-CH CAN HAT,可以将可通过将模块的CAN0_H与CAN1_H,CAN0_L与CAN1_L相连,如下图所示:
2-CH-CAN-HAT-connect.jpg

  • 打开两个终端窗口:

其中一个终端输入接收CAN0数据指令:

candump can0

另外一个终端输入发送CAN1数据指令:

cansend can1 000##11.22.33.44
  • 演示效果如下:(左边为接收,右边为发送)

2-CH-CAN-HAT-cansend.png
若手上有两个2-CH CAN HAT,可以直接将CAN_H,CAN_L两两相连。效果跟上述一样,需注意匹配好通信速率,识别ID,输出接口序号。

Python例程

  • 浏览目录:
  • 接收端运行receive.py:
sudo python reveive.py
  • 发送端运行send.py:
sudo python send.py

需注意的是这里的发送端是使用CAN1发送,接收端则是CAN0,具体修改可以参照下面的代码分析。

代码分析

【Python例程】

本例程是基于python平台,确保以及安装了python-can库
在发送之前要先创建一个can设备,因为前面只是启用MCP2515内核:

os.system('sudo ip link set can0 type can bitrate 100000')
os.system('sudo ifconfig can0 up')

上面这条则是通过对CAN0的配置初始化并启用,且指定CAN0作为发送/接收接口。如需改成CAN1,则代码如下:

os.system('sudo ip link set can1 type can bitrate 100000')
os.system('sudo ifconfig can1 up')
  • 第一步:连接到CAN总线
can0 = can.interface.Bus(channel = 'can0', bustyp = 'socketcan_ctypes')
  • 需改成CAN1,则代码如下:
can0 = can.interface.Bus(channel = 'can1', bustyp = 'socketcan_ctypes')
  • 第二步:创建信息
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)
  • 第三步:发送信息
can0.send(msg)
  • 需改成CAN1,则代码如下:
can1.send(msg)
  • 最后同样要关闭can设备
os.system('sudo ifconfig can0 down')
  • 需改成CAN1,则代码如下:
os.system('sudo ifconfig can1 down')
  • 接收数据:
msg = can0.recv(10.0)

recv()中定义超时接收时间。
更多请参考:https://python-can.readthedocs.io/en/stable/interfaces/socketcan.html

【WringPi例程】

  • 阻塞接收,树莓派打开终端,运行:
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