- How to configure and use CAN bus
- Contents
- References
- Enable the CAN controller kernel driver
- AM35x kernel
- OMAP3x kernel
- Enable socketcan and ip tools
- Setup the networking rules
- Verifying CAN support configuration
- Linux boot
- sysfs entries
- MC2515
- Power on CAN hardware
- Send simple test packet
- Dump received packets
- CAN protocol stack information
- MCP251x interrupt signal
- MCP251x Platform Bus Data
- GPIO MCP251x interrupt verification
- Problems and how to resolve them
- No can0 interface
- bit-timing calculation not available
- bit-timing not yet defined
- MCP251x didn't wake-up
- ip command says link/[280] instead of link/can
- NOHZ: local_softirq_pending 08
- System files and command
- Files
- Commands Examples
- Saved searches
- Use saved searches to filter your results more quickly
- ClearBlade/CANBus-Adapter
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- CAN Bus
- CAN Support in Linux
- SocketCAN Supported Protocols
- SocketCAN Supported Controllers
How to configure and use CAN bus
This article explains how to enable the CAN bus using as examples the AM35x EVM and OMAP35x, but can be applied to other platforms as well. In addition, steps to exchange data with a MCP2515 Bus Monitor board is also documented.
If you are new to CAN, please spend 15 minutes reading the CAN bus protocol technical overview.
Contents
- 1 References
- 2 Enable the CAN controller kernel driver
- 2.1 AM35x kernel
- 2.2 OMAP3x kernel
- 5.1 Linux boot
- 5.2 sysfs entries
- 5.2.1 MC2515
- 10.1 MCP251x Platform Bus Data
- 10.2 GPIO MCP251x interrupt verification
- 11.1 No can0 interface
- 11.2 bit-timing calculation not available
- 11.3 bit-timing not yet defined
- 11.4 MCP251x didn’t wake-up
- 11.5 ip command says link/[280] instead of link/can
- 11.6 NOHZ: local_softirq_pending 08
- 12.1 Files
- 12.2 Commands Examples
References
Enable the CAN controller kernel driver
AM35x kernel
In the case of the AM35x platform the configuration is:
Linux Kernel Configuration Networking support CAN bus subsystem support CAN device drivers Platform CAN drivers with Netlink support CAN bit-timing calculation TI High End CAN controller (HECC)
OMAP3x kernel
In the case of the OMAP3x platform with a MCP2515 chip the configuration is:
Linux Kernel Configuration Networking support CAN bus subsystem support CAN device drivers Platform CAN drivers with Netlink support CAN bit-timing calculation Microchip MCP251x SPI CAN controller
Enable socketcan and ip tools
Working with the CAN bus requires enabling the ‘ip’ tools from iproute2 package (the ‘ip’ tool from busybox won’t work). The socketcan package is optional and provide you with tools to debug your can bus.
These packages are available for selection from the RidgeRun SDK in the configuration menu.
File System Configuration Select target's file system software iproute2-2.6.34 socketcan utils
Setup the networking rules
Edit your /etc/networking/interfaces on the target file system and add the following rules (required to setup the bit-rate of the bus):
auto can0 iface can0 inet manual #pre-up ip link set $IFACE type can bitrate 125000 listen-only off pre-up /sbin/ip link set $IFACE type can bitrate 125000 triple-sampling on up /sbin/ifconfig $IFACE up down /sbin/ifconfig $IFACE down
Verifying CAN support configuration
Linux boot
Look for the following in the Linux boot output
mcp251x spi1.0: setup: speed 750000, sample leading edge, clk normal mcp251x spi1.0: setup mode 0, 8 bits/w, 1000000 Hz max --> 0 mcp251x spi1.0: CANSTAT 0x80 CANCTRL 0x07 mcp251x spi1.0: probed
PM: Adding info for No Bus:can0 CAN device driver interface can: controller area network core (rev 20090105 abi 8)
sysfs entries
Verify the CAN host driver is registered correctly (meaning properly added to kernel arch/arm/mach-*/board-*.c file).
MC2515
ls -d /sys/bus/spi/drivers/mcp251x cat /sys/devices/platform/omap2_mcspi.1/spi1.0/modalias ls /sys/class/net/
/sys/bus/spi/drivers/mcp251x mcp2515 can0 eth0 lo
Power on CAN hardware
ip link set can0 type can bitrate 125000 triple-sampling on ifconfig can0 up
Send simple test packet
The socket-CAN tools include the cansend utility:
where the device is the network interface name, typically can0, and a CAN frame is in the format:
with the can_id having 3 (SFF) or 8 (EFF) hex chars. and data in the format of zero to eight 8-bit hex-values that can optionally be separated by a period (‘.’) or use R for remote transmission request.
To send a CAN data frame message, with a can_id arbitration field value of 0x5A1 and a data field value 0x1122334455667788:
cansend can0 5A1#11.22.33.44.55.66.77.88
If you are using a CAN bus monitor, like the MCP2515 bus monitor, You will see (packet sent 3 times):
Dump received packets
The socket-CAN tools include the candump utility, which dumps all messages being exchanged on the CAN bus. To run candump, just specify the CAN interface:
If a device on the CAN bus sends a packet with ID 0x456 and data 0x122345, the output would be
# candump can0 can0 456 [3] 12 23 45
The MCP2515 bus monitor allows you to send CAN bus packet. You can also telnet into the target hardware and use cansend to put a packet on the bus that can be monitiored with candump.
cansend can0 5A1#11.22.33.44.55.66.77.88
creates the following cumulative candump output
/ # candump can0 can0 456 [3] 12 23 45 can0 5A1 [8] 11 22 33 44 55 66 77 88
CAN protocol stack information
The CAN protocol implementation version:
MCP251x interrupt signal
MCP251x Platform Bus Data
In the architecture specific board file, such as arch/arm/mach-omap2/board-overo.c, first define the platform data:
#include static int overo_mcp2515_setup(struct spi_device *sdev) < printk(KERN_DEBUG "overo_mcp2515_setup: Entry\n"); return 0; >static int overo_mcp2515_transceiver_enable(int enable) < printk(KERN_DEBUG "overo_mcp2515_transceiver_enable: Entry %d\n", enable); return 0; >static struct mcp251x_platform_data overo_mcp2515_pdata = < .oscillator_frequency = 32*1000*1000, .board_specific_setup = overo_mcp2515_setup, .model = CAN_MCP251X_MCP2515, .power_enable = overo_mcp2515_transceiver_enable, >;
Then add the logic to initialize the GPIO used as the incoming mcp251x interrupt signal:
static void __init overo_mcp251x_init(void) < printk(KERN_DEBUG "overo_mcp251x_init: Entry\n"); if ((gpio_request(OVERO_GPIO_CAN_INT, "MCP251x CAN INT") == 0) && (gpio_direction_input(OVERO_GPIO_CAN_INT) == 0)) < gpio_export(OVERO_GPIO_CAN_INT, 0); set_irq_type(OMAP_GPIO_IRQ(OVERO_GPIO_CAN_INT), IRQ_TYPE_EDGE_FALLING); >else < printk(KERN_ERR "could not obtain gpio for MCP251x CAN bus interrupt\n"); return; >>
Add the SPI information to the SPI board info array:
Finally call the GPIO interrupt initialization function right before registering SPI board info:
overo_mcp251x_init(); spi_register_board_info(overo_spi_board_info, ARRAY_SIZE(overo_spi_board_info));
GPIO MCP251x interrupt verification
You can use debugfs to monitor the GPIO used for the MCP251x interrupt signal.
Configure the kernel to enable debugfs:
Symbol: DEBUG_FS [=y] Prompt: Debug Filesystem Defined at lib/Kconfig.debug:77 Depends on: SYSFS Location: -> Kernel configuration -> Kernel hacking
Boot the target hardware and mount debugfs:
mount -t debugfs none /sys/kernel/debug
Check the current value and configuration for the GPIO of interest (for example GPIO 147):
fgrep 147 /sys/kernel/debug/gpio
with example output being:
gpio-147 (MCP251x CAN INT) in hi irq-274 edge-falling
Which indicates GPIO 147 is configure as an input, currently has a high logic level, is mapped to IRQ 307, and causes an interrupt on the falling edge.
You can see if any interrupts have occurred using (for example GPIO 147 being mapped to IRQ 307):
with example output being:
which indicates 60 interrupts have been occurred.
With OMAP3, you also need to verify the pad where the signal leaves the chip is configured properly. For the OMAP3 in the CBB package (used on Gumstix Overo Water board) GPIO147 uses the uart2_rx pad.
cat /sys/kernel/debug/omap_mux/uart2_rx
with example output being:
name: uart2_rx.gpio_147 (0x4800217a/0x14a = 0x4104), b ad25, t NA mode: OMAP_PIN_INPUT | OMAP_PIN_OFF_WAKEUPENABLE | OMAP_MUX_MODE4 signals: uart2_rx | mcbsp3_fsx | gpt8_pwm_evt | NA | gpio_147 | NA | NA | safe_mode
Problems and how to resolve them
The titles of the following sections are text that gets displayed when a problem occurs. Each section describes how to resolve the issue.
No can0 interface
If you have problems with network interface can0 not being created, enable
kernel -> Device Drivers -> Generic Driver Options -> Driver Core verbose debug messages
kernel -> Networking support -> CAN bus subsystem support -> CAN Device Drivers -> CAN devices debugging messages
bit-timing calculation not available
Enable CAN bit-timing calculation in the Linux kernel.
kernel -> Networking support -> CAN bus subsystem support -> CAN Device Drivers -> CAN bit-timing calculation
bit-timing not yet defined
ip link set can0 type can bitrate 125000 triple-sampling on
MCP251x didn't wake-up
If the response to ifconfig can0 up is
mcp251x spi1.0: MCP251x didn't wake-up mcp251x spi1.0: CNF: 0x03 0xf5 0x01
Then there is a problem with the interrupt signal from the MCP251x chip back to the driver. Check the arch/arm/mach-*/board-*.c file for your hardware along with any jumpers on your hardware design.
ip command says link/[280] instead of link/can
You need to enable and build iproute2 to get an up-to-date ip command that supports the can bus.
NOHZ: local_softirq_pending 08
You are using an older version of the mcp2515 driver. The driver should call netif_rx() only from interrupt context. Update to a newer version of the driver and the warning will go away.
System files and command
Files
Commands Examples
ip -details link show can0
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
CANBus-MQTT Adapter for connecting your car to the Internet of Things
ClearBlade/CANBus-Adapter
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
CAN Bus Adapter allows an automotive's CAN (Controller Area Network) Bus to publish MQTT messages.
sudo apt-get install libssl-dev libcurl4-openssl-dev libjansson-dev gcc autoconf libtool
git clone https://github.com/eclipse/paho.mqtt.c.git cd paho.mqtt.c make clean make sudo make install
git clone https://github.com/ClearBlade/ClearBlade-C-SDK cd ClearBlade-C-SDK make clean make sudo make install
#ifndef _CONSTANTS_H #define _CONSTANTS_H char *SYSTEM_KEY = ; // ex. "94aaa4890bc0afe1fbd282b1ac8e01" char *SYSTEM_SECRET = ; // ex. "9AAEC4890BC68AB6B9BBEDF6B21F" char *USER_EMAIL = ; // ex. "test@clearblade.com" char *USER_PASSWORD = ; // ex. "password" . #endif
cd /path/to/canbus-adapter/adapters/ make clean make
git clone https://github.com/linux-can/can-utils cd can-utils ./autogen.sh ./configure make sudo make install
sudo modprobe can sudo modprobe can_raw sudo modprobe vcan sudo ip link add dev vcan0 type vcan sudo ip link set up vcan0 sudo ip link show vcan0
sudo ifconfig vcan0 up cansend vcan0 123#1122334455667788
ClearBlade init successful: NByVFwfSCjVf4zF8XSMTG_osxg3B0BA_Tw-CkqU89jsqSlS_eosLT3pPbc4w1Z0KvTQCWjeSvLnGgxoqQw== Successful connection to MQTT broker Subscribe succeeded vcan0 at index 0 Received data from CAN bus: "3DUfw� Send MQTT message: ID: 291, Data: "3DUfw�
About
CANBus-MQTT Adapter for connecting your car to the Internet of Things
CAN Bus
The CAN bus is an ISO standard bus originally developed for vehicles. It manages the Chassis Electrical System Control and is responsible for critical activities like engine electrical, and skid control. This system is also used to provide vehicle diagnostic information for maintenance. A multi-star configuration seems typical of this bus with a primary bus line that branches into sub bus lines at its extremities then attaches to multiple device nodes. Differential voltage is applied over twisted pair at 1.5 to 2.5V and 2.5 to 3.5V for noise resistant signaling. Bit rates up to 1 Mbit/s are possible at network lengths below 40 m. Decreasing the bit rate allows longer network distances (e.g., 500 m at 125 kbit/s). (Jeremiah J. Flerchinger Source) Controllers supporting CAN FD, an enhanced CAN version with frames up to 64 byte and bit rates up to 4 Mbit/s, will be available in the second half of 2014. A can4linux version supportig CAN FD on a IFI CAN is ready to be used.
Although developed as car communication network CAN is used in many other areas, industrial, medical, maritime laboratory and more. Most often with a CAN based higher layer protocol like CANopen on top of it.
Additional information can be found at:
CAN Support in Linux
CAN is supported by Linux device drivers. Mainly two types exist. Character device based drivers and network socket based drivers. The Linux kernel supports CAN with the SocketCAN framework.
One of the character based drivers is can4linux.
SocketCAN Supported Protocols
- RAW: send & receive raw CAN frames
- BCM: Broadcast manager, offload repetitive work to the Linux kernel
- ISOTP .
- SAE J1939
SocketCAN Supported Controllers
- Microchip MCP251x
- Atmel AT91 SoCs
- ESD 331 CAN Cards
- NXP (Philips) SJA1000
- Freescale MPC52xx SoCs
- Bosch CC770
- Intel AN82527
- TIs SoCs
- Serial/network devices utilizing ASCII protocol (slcan driver)