RZ-G/Boards/CAN

From eLinux.org
Jump to: navigation, search

This page provides information on CAN bus support for the RZ-G Starter Kit boards.

For general information regarding CAN bus operation refer to Wikipedia article.

Hardware

Cable

Typical pinout of DB9 CAN connector

Commonly used CAN bus connector is DB-9 female (socket) for cables and DB-9 male (plug) for devices. In some cases power can be provided via the connector.

General rule is that both ends of the CAN bus should be terminated with matching resistors. Typical value is 120 Ohm.

RZ/G1M Starter Kit board
Pins of CAN connector on Renesas RZ/G1M Starter Kit board

Use a cable with XHP-3 and DB-9 connectors. Mating terminals are SXH-001T-P0.6. You may need a crimper tool.

Minimum number of wires is three:

SKRZG1M JP6 Pins Signal DB-9 pins
1 CAN_H* 7
2 CAN_L* 2
3 GND 6

[*] CAN_H and CAN_L signals form a differential pair. These wires should be twisted.

See example (picture) of XNP-3 to DB9 cable:

G1M can cable example.png


Break-out box

CAN break-out box from National Instruments

If you need to connect multiple devices to the bus it it recommended to use a break-out box as shown on the picture

Termination should only be enabled on the break-out box and at the end of long cables.

RZ/G1M Starter Kit board

JP10 should be left open if a break-out box is used.


OBD-II

In order to connect to OBD-II interface use a cable from PEAK Systems [1].

RZ/G1M Starter Kit board

CAN termination resistor must be switched off (JP10 left open) when connecting to OBD-II.

Software

Yocto build

To build a Yocto image with CAN bus support for the RZ/G1M Starter Kit board, follow this procedure carefully:

  1. Do all steps specified on RZ-G/Boards/Yocto and stop before executing bitbake core-image-weston or bitbake core-image-x11
  2. Edit $WORK/build/conf/local.conf with your favorite text editor, and add at the end:
    IMAGE_INSTALL_append = "can-utils iproute2" 
    
  3. Complete all steps on RZ-G/Boards/Yocto.

Configuration

CAN interface can be configured with ip tool from iproute2 package configured in Yocto.

  1. Login into command prompt on the board. Refer to RZ-G/Boards/Yocto#Running Yocto image for steps necessary for starting the board.
  2. Check that can0 interface is present. Run:
    ip link show can0
    

    The output should be:

    2: can0: <NOARP,ECHO> mtu 16 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 10
        link/can
    
  3. Configure the interface with:
    ip link set can0 type can bitrate 500000
    
  4. Bring CAN interface UP with:
    ip link set can0 up
    
  5. You can then dump all messages coming from the bus with candump utility from can-utils package:
    candump can0
    

    When valid CAN messages are received, candump will print the following output:

      can0  2C4   [4]  F8 D4 D6 D1
      can0  2C5   [2]  37 73
      can0  610   [3]  27 B4 45
    
  6. To send one CAN message use cansend command:
    cansend can0 610#000000
    

    Multiple messages can be sent with cangen command:

    cangen: generate CAN frames
    
    Usage: cangen [options] <CAN interface>
    Options: -g <ms>       (gap in milli seconds - default: 200 ms)
             -e            (generate extended frame mode (EFF) CAN frames)
             -f            (generate CAN FD CAN frames)
             -R            (send RTR frame)
             -m            (mix -e -f -R frames)
             -I <mode>     (CAN ID generation mode - see below)
             -L <mode>     (CAN data length code (dlc) generation mode - see below)
             -D <mode>     (CAN data (payload) generation mode - see below)
             -p <timeout>  (poll on -ENOBUFS to write frames with <timeout> ms)
             -n <count>    (terminate after <count> CAN frames - default infinite)
             -i            (ignore -ENOBUFS return values on write() syscalls)
             -x            (disable local loopback of generated CAN frames)
             -v            (increment verbose level for printing sent CAN frames)
    
    Generation modes:
    'r'        => random values (default)
    'i'        => increment values
    <hexvalue> => fix value using <hexvalue>
    
    When incrementing the CAN data the data length code minimum is set to 1.
    CAN IDs and data content are given and expected in hexadecimal values.
    
    Examples:
    cangen can0 -g 4 -I 42A -L 1 -D i -v -v   (fixed CAN ID and length, inc. data)
    cangen can0 -e -L i -v -v -v              (generate EFF frames, incr. length)
    cangen can0 -D 11223344DEADBEEF -L 8      (fixed CAN data payload and length)
    cangen can0 -g 0 -i -x                    (full load test ignoring -ENOBUFS)
    cangen can0 -g 0 -p 10 -x                 (full load test with polling, 10ms timeout)
    cangen can0                               (my favorite default :)
    
  7. When CAN bus is no longer needed or for reconfiguration, bring CAN interface DOWN with:
    ip link set can0 down