How to configure and use CAN bus

Ansible自动化Rocky Linux 9初始配置实战指南 服务器初始化配置是Linux系统运维的基础环节,涉及时区同步、SELinux策略、firewalld端口管理、用户权限与SSH安全等核心系统设置。其本质是通过声明式自动化工具,实现多节点状态一致性与安全合规性。Ansible凭借无代理架构、YAML可读语法和强幂等性,成为Rocky Linux 9等RHEL系发行版批量部署的首选方案。相比Shell脚本易出错、Puppet学习成本高,Ansible能以最小依赖完成从hostname配置到chronyd时间同步、从firewalld区域放行到SSH密钥注入的全 阅读详情
 

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

[hide]

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

Run

dmesg | fgrep -i mcp

Expected 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


Run

dmesg | fgrep -i can

Expected output:

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

Run:

ls -d /sys/bus/spi/drivers/mcp251x
cat /sys/devices/platform/omap2_mcspi.1/spi1.0/modalias
ls /sys/class/net/

Expect:

/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:

cansend <device> <can_frame>

where the device is the network interface name, typically can0, and a CAN frame is in the format:

<can_id>#{R|data}

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:

candump can0

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:

cat /proc/net/can/version

The CAN bus statistics:

cat /proc/net/can/stats

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 <linux/can/platform/mcp251x.h>

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,
};
<pre>

Then add the logic to initialize the GPIO used as the incoming mcp251x interrupt signal:

<pre>
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:

	{
		.modalias       = "mcp251x",
		.platform_data  = &overo_mcp2515_pdata,
		.irq            = OMAP_GPIO_IRQ(114),
		.max_speed_hz   = 1*1000*1000,
		.bus_num        = 1,
		.mode           = SPI_MODE_0,
		.chip_select    = 0,
	},

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

fgrep 307 /proc/interrupts

with example output being:

307:  60        GPIO  mcp251x

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

and

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

First run

ip link set can0 type can bitrate 125000 triple-sampling on

then run

ifconfig can0 up


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

cat /proc/net/dev

Commands Examples

ip -details link show can0

References

【十万字拆解】从零开始实现GPT-2(完整版) 本文是一篇 14 万字、从零硬核实现 GPT 的保姆级指南。内容全面覆盖四个阶段:①从最基础的 Bigram 语言模型切入,跑通数据处理与训练闭环;②仿照 Transformer 架构,深度研究 Multi-Head Attention、残差网络、LayerNorm 及 Dropout 的底层数学与代码实现;③从零构建BPE 分词器,对齐 GPT-2 规范;④从头搭建 GPT-1/2 工业级预训练流程,包含混合精度、梯度累积等十余项硬件级工程调优,并在 AutoDL 算力平台上从0开始训练!适合所有层次! 阅读详情

相关推荐

(RegionProposal Network)RPN网络结构及详解

RPN(RegionProposal Network)区域生成网络 Faster-RCNN的核心。在这里整理。 1.anchors。 特征可以看做一个尺度51*39的256通道图像,对于该图像的每一个位置,考虑9个可能的候选窗口:三种面积{128,256,512}×{128,256,512}×三种比例{1:1,1:2,2:1}{1:1,1:2,2:1}。这些候选窗口称为anchors...

凹酱的DEEP LEARNING 19万+

嵌入式linux, CAN 驱动有关问题

与can相关的文件有 1. linux3.0.1源码包中的: drivers/net/can/mcp251x.c与Kconfig文件(将mcp251x.c中spi_board_info[] 中的.modalias="mcp2510"设为"mcp2515")是不是此处原因待查! drivers/spi/spi_s3c64xx.c net/can 所有文件 Documen

sean 5584

常微分方程 (ODE) 和 随机微分方程 (SDE)

常微分方程(Ordinary Differential Equations, ODE)和随机微分方程(Stochastic Differential Equations, SDE)是数学中描述系统动态行为的重要工具。它们有一些相似之处,但在处理随机性方面存在显著差异。

weixin_39699362的博客 4384

busybox下 CAN 设置失败

操作与出错信息如下: [root@Loongson-GD:~]# ifconfig can0 down [root@Loongson-GD:~]# ip link set can0 up type can bitrate 250000 ip: either "dev" is duplicate, or "type" is garbage [root@Loongson-GD:~]# ifconfig can0 up [ 1408.592235] sja1000_platform 1fe00c00.can c

sense的专栏 1435

Meta开源Ego-Exo4D数据集:如何用1400小时多模态视频训练你的AI模型

Meta开源的Ego-Exo4D数据集包含1400小时多模态视频,涵盖第一人称和第三人称视角、眼动追踪、IMU传感器等多种模态,是AI模型训练的宝贵资源。本文详细介绍了从数据集下载、预处理到模型训练的全流程,帮助研究者高效利用这一多模态数据集进行AI模型开发。

weixin_29159441的博客 342

SAP-PM设备模块-PM主数据之功能位置

SAP-PM模块学习

TRUIXIANG的博客 5137

softirq原理以及源码分析

<br />  Linux 的softirq机制是与SMP紧密不可分的。为此,整个softirq机制的设计与实现中自始自终都贯彻了一个思想:“谁触发,谁执行”(Who marks,Who runs),也即触发软中断的那个CPU负责执行它所触发的软中断,而且每个CPU都由它自己的软中断触发与控制机制。这个设计思想也使得softirq 机制充分利用了SMP系统的性能和特点。 多个softirq可以并行执行,甚至同一个softirq可以在多个processor上同时执行。 一、softirq的实现     每个s

少帅的天空 7668

2026年【江苏“信息与未来”编程思维】真题及题解(T6:桌面游戏)

2026年【江苏“信息与未来”编程思维】真题及题解(T6:桌面游戏)

王老师青少年编程 534

设置CAN波特率时出现问题

参考方案:http://velep.com/archives/1186.html http://blog.youkuaiyun.com/lizhu_csdn/article/details/51490958   1)can0未启动,启动CAN0接口:                # ifconfig can0 up                ifconfig: SIOCSIFFLAGS:

ShoneX98的博客 5437

Navicat从入门到精通:数据库管理全攻略

本文系统梳理Navicat数据库管理工具的核心应用体系。从基础的安装与多类型数据库连接入手,详解数据库与数据表的创建、修改、删除等操作逻辑;深入数据增删改查的可视化与 SQL 语句双重操作模式,结合Excel/SQL文件的导入导出场景提供实操指南。同时覆盖数据备份恢复、跨库同步等进阶功能,以及视图、存储过程等高级应用。通过对比同类工具突出Navicat的易用性与功能性优势,并针对连接失败、字符乱码等常见问题提供解决方案,帮助读者从零基础掌握数据库全生命周期管理,提升数据操作效率与安全性。

邓邓子的博客 1897

31、航空软件标准DO - 178C/ED - 12C的实践解读

本文深入解读了航空软件标准DO-178C/ED-12C相较于DO-178B的主要变化,涵盖核心文本更新、各章节关键改进及新技术补充文件的影响。重点分析了目标与活动关系、需求层级定义、验证独立性、MC/DC覆盖、停用代码处理等关键变更,并提供了应对策略,包括流程调整、文档更新、工具选择和与认证机构沟通等建议,旨在帮助航空软件开发者确保合规性,提升软件安全性与可靠性。

tech5的博客 245

How_to_configure_and_use_CAN_bus

https://www.ridgerun.com/developer/wiki/index.php/How_to_configure_and_use_CAN_bus Socket CAN utilities The BerliOS Socket CAN

896

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Debugging Embedded Linux

Section #6. Debugging Embedded Linux From a device driver perspective, embedded software developers often face interfaces not commonly found on conventional computers. Figure 1.3 shows a hypothetical...

mounter625的专栏 1161

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

CANN/hcomm HCCL-VM常见问题解答

> This document is used to test the FAQ HTML generation framework. --- ## Module: CANN Package Installation and WSL Environment Configuration --- #### FAQ-C001 **Title:** WSL Environment Configur

gitblog_00189的博客 918

Delta lake第一稿个人翻译和注释(第四章)

亚马逊S3等云对象存储是全球规模最大、最具成本效益的存储系统之一,因此成为存储大型数据仓库和数据湖的理想之选。遗憾的是,它们作为键值存储的实现使得实现ACID事务和高性能变得困难:元数据操作(如列出对象)成本高昂**(解释:对象存储在云上面list操作费用是get操作的10倍,并且抽取类似元数据次数多了,费用很贵;所以整体思想是减少list操作,有个元数据管理文件,然后从文件抽取必要文件然后采取 get操作),一致性保证也有限。

knowcai126的博客 291

juju reactive charm (by quqi99)

sudo snap install charm --classic mkdir -p /bak/work/charms/layers export JUJU_REPOSITORY=/bak/work/charms export LAYER_PATH=$JUJU_REPOSITORY/layers cd $LAYER_PATH charm create simple cd simple sudo ...

技术并艺术着 8718

手撕ARM64启动栈(一):QEMU + TF-A → OP-TEE → U-Boot → Linux 全链路总览

ARM 官方提供的参考固件实现,本文用作 BL1/BL2/BL31:开源 TEE 实现,本仓库用作 BL32,运行在 S-EL1,通过 opteed(SPD)与 BL31 交互,提供 TA 运行环境U-Boot:本仓库用作 BL33/Normal World bootloader,通过 ARM semihosting(,非网络/非fw_cfg)从宿主机文件系统读取内核与 initrd,bootm跳转内核。

Fifty的博客 494

A20_can_BUS 使用

1,kernel_driver /driver/net/sunxi_can/sun7i_can.c a,  /driver/net/sunxi_can/Makefile       #add by jiangdou obj-$(CONFIG_CAN_SUN7I) += sun7i_can.o b:   /driver/net/sunxi_can/Kconfig  #add by

u010402372的专栏 2293

linux内核源码分析之软中断

目录 软中断 软中断执行 软中断注册 软中断触发 软中断守护进程 软中断 软中断是在编译期间静态分配的,不像tasklet能动态分配 软中断机制的核心部分是一个表,包含32个softirq_action类型的数据项,软中断由softirq_action结构表示。 struct softirq_action { void (*action)(struct softirq_action *); }; 最多32个软中断,这是一个定值,数目没法动态改变。 static struct

1408

讯为IMX6UL开发板CAN接口测试学习笔记

问题 原因 移植iproute2的ip命令 验证 IMX6官方手册:i.MX_6_Linux_Reference_Manual.pdf CAN接口相关说明在Chapter 39。 问题 使用ip link set can0 up type can bitrate 125000命令设置并启动CAN接口时报错: ip: either &quot;dev&quot; is duplicate...

China_Style的专栏 2999

local_softirq_pending位何时置位

硬中断执行结束在退出之前,都会在irq_exit中判断如果有pending的软中断就触发软中断: 内核代码通过调用raise_softirq_irqoff来触发相应的软中断,将相应的bit位置位,这样在irq_exit中判断就有pending的软中断,进而触发执行相应的软中断(直接执行软中断或触发软中断线程): raise_softirq_irqoff函数调用__raise_softi...

yiyeguzhou100的专栏 4435

Reading notes of Dynamic Ticks

================================ Most of the contents is from the book, but as it based on 2.6 kernel.   I analysis the a new version 3.1.1. So there are some changes.  ============================

ganggexiongqi的专栏 1797
上一篇: eCAN总线模块的位时间(Bit-Timing)配置
下一篇: 学习HTML 5的10个顶级资源
flymachine
博客等级 码龄23年 253粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值