• 树莓派can通信


    本文转载自: https://harrisonsand.com/can-on-the-raspberry-pi/

    This post walks through setting up a CAN controller on the Raspberry Pi. My goal is to help demystify the process, and provide simple instructions that a relative beginner should be able to follow. Particularly, including information that I wish was more readily available when I started with the project.

    By the end of this tutorial you should have a working CAN controller connected to the Raspberry Pi via the SPI bus. Using SocketCAN it will be possible to send and receive CAN packets through the Linux IP stack. This means you can analyse CAN packets just as you would with TCP/IP, using standard utilities like tcpdump and Wireshark. Additionally, using utilities like candump/cansend provided by Can-utils, you can remove an extra layer of abstraction, and focus purely on the CAN data layer.

    Getting the Hardware

    You have a fair amount of choices when it comes to hardware. The most discussed and most popular modules use the MCP2515 CAN-SPI chip, so I'll focus on those.

    Some options are:
    -PICAN by SK Pang Electronics - £32.28 Inc VAT (~$46) - Link
    -CAN-Bus Sheld by SparkFun - ~$25 - Link
    -Ebay - less than $2 (search for "mcp2515 module")

    I chose to go with the cheap Ebay module (pictured above), not that the other alternatives weren't great. The SparkFun shield looked very interesting with a microSD slot for data logging, and a hookup for GPS, but since I was learning and didn't want to end up bricking a nice piece of hardware just yet, I went the cheaper route.

    You should be able to follow this guide for the other modules as well, you'll just have to determine the wiring by looking at the schematics for your module of choice.

    I found some schematics for the Ebay module - PDF

    Note, the Ebay module uses the TJA1050 CAN transceiver, while the others use the MCP2551. Not sure if it makes much of a difference, but thought it might be worth noting.

    Wiring it Up

    This should apply to all the modules I mentioned, though the labeling may be a bit different. pinout.xyz has a super cool interactive RPi pinout for reference.

    RPi Pin    RPi Label     CAN Module
    02---------5V------------VCC
    06---------GND-----------GND
    19---------GPIO10--------MOSI (SI)
    21---------GPIO9---------MISO (SO)
    22---------GPIO25--------INT
    23---------GPIO11--------SCK
    24---------GPIO8---------CS
    

    Enabling the SPI Bus and Loading the Kernel Modules

    Edit /boot/config.txt and add the following lines, setting the oscillator parameter to that of the oscillator present on the module. Most of the modules use a 16MHz oscillator for the clock, though the Ebay module uses 8MHz oscillator=8000000. You can determine the frequency by looking at the chip.

    dtparam=spi=on
    dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25 
    dtoverlay=spi-bcm2835-overlay
    

    Reboot.

    If everything is hooked up and working the can0 interface should be visible with ifconfig can.

    pi@raspberrypi:~ $ ifconfig can
    can0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
              NOARP  MTU:16  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:10
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    

    The MCP2515 device tree overlay parameters are described in /boot/overlays/README.

    Name:   mcp2515-can0
    Info:   Configures the MCP2515 CAN controller on spi0.0
    Load:   dtoverlay=mcp2515-can0,<param>=<val>
    Params: oscillator              Clock frequency for the CAN controller (Hz)
    
            spimaxfrequency         Maximum SPI frequence (Hz)
    
            interrupt               GPIO for interrupt signal
    

    Compiling Can-utils

    You may need to install some prerequisites:

    sudo apt-get install git autoconf libtool
    

    Run the following commands to compile can-utils:

    git clone https://github.com/linux-can/can-utils.git
    cd can-utils
    ./autogen.sh
    ./configure
    make
    sudo make install
    

    You may also install via the repository:

    sudo apt-get install can-utils
    

    Bring Up the Interface

    You can bring up the interface with:

    sudo ip link set can0 up type can bitrate 50000
    

    You'll need to select the proper bitrate for the network you're connecting to. Common bitrates are: 10000, 20000, 50000, 100000, 125000, 250000, 500000, 800000, 1000000.

    If you need to change a setting, first bring down the interface:

    sudo ip link set can0 down
    

    You can see a list of available settings by appending help to the command:

    pi@raspberrypi:~ $ ip link set can0 up type can bitrate 50000 help
    Usage: ip link set DEVICE type can
            [ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |
            [ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1
              phase-seg2 PHASE-SEG2 [ sjw SJW ] ]
    
            [ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] |
            [ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1
              dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]
    
            [ loopback { on | off } ]
            [ listen-only { on | off } ]
            [ triple-sampling { on | off } ]
            [ one-shot { on | off } ]
            [ berr-reporting { on | off } ]
            [ fd { on | off } ]
    
            [ restart-ms TIME-MS ]
            [ restart ]
    
            Where: BITRATE  := { 1..1000000 }
                      SAMPLE-POINT  := { 0.000..0.999 }
                      TQ            := { NUMBER }
                      PROP-SEG      := { 1..8 }
                      PHASE-SEG1    := { 1..8 }
                      PHASE-SEG2    := { 1..8 }
                      SJW           := { 1..4 }
                      RESTART-MS    := { 0 | NUMBER }
    

    The loopback setting enables loopback mode on the MCP2515, which can be useful for verifying that the RPi is communicating with the CAN module without having to connect to an actual CAN bus. All messages transmitted in loopback mode will be echoed back over the interface.

    Listen-only can be useful if you're interested in sniffing CAN traffic, and want to lessen the risk of messing something up by ensuring the device doesn't send any packets.

    Send and Receive CAN Messages

    Using cansend/candump it's possible to respectively send and receive CAN messages.

    Send

    cansend can0 001#1122334455667788
    

    And receive:

    candump can0
    

    输出

    On some cars you may not see any traffic through the CAN-bus on the OBD2 port. This is due to the fact that there is a 'gateway' present between the OBD2 port and the various CAN networks. The gateway acts like a firewall between the networks, and it's necessary to send a request for data before it can be received.

  • 相关阅读:
    Luogu P1273 有限电视网【树形Dp/树形背包】
    Luogu P1160队列安排【链表/老文搬家】By cellur925
    Luogu P1970 花匠 【线性Dp】 By cellur925
    Luogu P1541 乌龟棋 【线性dp】
    P2885 [USACO07NOV]电话线Telephone Wire——Chemist
    Luogu P3916 图的遍历 【优雅的dfs】【内有待填坑】By cellur925
    状压dp之二之三 炮兵阵地/玉米田 By cellur925
    Luogu P1991 无线通讯网 【最小生成树】
    浅谈并查集 By cellur925【内含题目食物链、银河英雄传说等】
    Luogu P1134 阶乘问题 【数学/乱搞】 By cellur925
  • 原文地址:https://www.cnblogs.com/chendeqiang/p/12861683.html
Copyright © 2020-2023  润新知