最近调试一个sja1000的can驱动,发现到了2.6.36,linux把can总线封装成了网络接口。内核文档里给出了这么修改的原因。

1. Overview / What is Socket CAN

--------------------------------

The socketcan package is an implementation of CAN protocols (Controller Area Network) for Linux.  CAN is a networking technology which has widespread use in automation, embedded devices, and automotive fields.  While there have been other CAN implementations for Linux based on character devices, Socket CAN uses the Berkeley socket API, the Linux network stack and implements the CAN device drivers as network interfaces.  The CAN socket API has been designed as similar as possible to the TCP/IP protocols to allow programmers, familiar with network programming, to easily learn how to use CAN sockets.

2. Motivation / Why using the socket API

----------------------------------------

There have been CAN implementations for Linux before Socket CAN so the question arises, why we have started another project.  Most existing implementations come as a device driver for some CAN hardware, they are based on character devices and provide comparatively little functionality.  Usually, there is only a hardware-specific device driver which provides a character device interface to send and receive raw CAN frames, directly to/from the controller hardware. Queueing of frames and higher-level transport protocols like ISO-TP have to be implemented in user space applications.  Also, most character-device implementations support only one single process to open the device at a time, similar to a serial interface.  Exchanging the CAN controller requires employment of another device driver and often the need for adaption of large parts of the application to the new driver's API.

Socket CAN was designed to overcome all of these limitations.  A new protocol family has been implemented which provides a socket interface to user space applications and which builds upon the Linux network layer, so to use all of the provided queueing functionality.  A device driver for CAN controller hardware registers itself with the Linux network layer as a network device, so that CAN frames from the controller can be passed up to the network layer and on to the CAN protocol family module and also vice-versa.  Also, the protocol family module provides an API for transport protocol modules to register, so that any number of transport protocols can be loaded or unloaded dynamically.  In fact, the can core module alone does not provide any protocol and cannot be used without loading at least one additional protocol module.  Multiple sockets can be opened at the same time, on different or the same protocol module and they can listen/send frames on different or the same CAN IDs.  Several sockets listening on the same interface for frames with the same CAN ID are all passed the same received matching CAN frames.  An application wishing to communicate using a specific transport protocol, e.g. ISO-TP, just selects that protocol when opening the socket, and then can read and write application data byte streams, without having to deal with CAN-IDs, frames, etc.

Similar functionality visible from user-space could be provided by a character device, too, but this would lead to a technically inelegant solution for a couple of reasons:

* Intricate usage.  Instead of passing a protocol argument to socket(2) and using bind(2) to select a CAN interface and CAN ID, an application would have to do all these operations using ioctl(2)s.

* Code duplication.  A character device cannot make use of the Linux network queueing code, so all that code would have to be duplicated

for CAN networking.

* Abstraction.  In most existing character-device implementations, the hardware-specific device driver for a CAN controller directly

provides the character device for the application to work with.

This is at least very unusual in Unix systems for both, char and

block devices.  For example you don't have a character device for a certain UART of a serial interface, a certain sound chip in your computer, a SCSI or IDE controller providing access to your hard

disk or tape streamer device.  Instead, you have abstraction layers which provide a unified character or block device interface to the application on the one hand, and a interface for hardware-specific device drivers on the other hand.  These abstractions are provided

by subsystems like the tty layer, the audio subsystem or the SCSI

and IDE subsystems for the devices mentioned above.

The easiest way to implement a CAN device driver is as a character device without such a (complete) abstraction layer, as is done by most existing drivers.  The right way, however, would be to add such a

layer with all the functionality like registering for certain CAN

IDs, supporting several open file descriptors and (de)multiplexing

CAN frames between them, (sophisticated) queueing of CAN frames, and providing an API for device drivers to register with.  However, then

it would be no more difficult, or may be even easier, to use the networking framework provided by the Linux kernel, and this is what Socket CAN does.

The use of the networking framework of the Linux kernel is just the natural and most appropriate way to implement CAN for Linux.

好吧,我是干活的,最喜欢内核自带的驱动。plx_pci.c是plx905x扩展几个sja1000的驱动。我这里是fpga做的pci-localbus桥,扩展2片sja1000。简直是专门为我准备的嘛,很快就改吧好了驱动,ifocnfig -a 也能看到can节点了。

但是如何使用 Socket CAN API真犯愁啊。参照http://archive.cnblogs.com/a/1916143/,交叉编译了can-utils 4.0.6的几个重要工具。busybox的文件系统还要移植ip命令。

1、

首先配置can0

ip link set can0 type can tq 125 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1

这时dmesg可以看到sja1000_fpga_pci 0000:07:04.0: setting BTR0=0x01 BTR1=0x1c

周立功的usbcan-2a测试模块里,波特率250kbs时就是BTR0=0x01 BTR1=0x1c

2、

ip -details link show can0 查看一下

can0: mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10

link/can

can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0

bitrate 500000 sample-point 0.875

tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1

sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1

clock 16000000

3、接收测试,接收周立功测试软件发送的帧:

# ./candump can0

interface = can0, family = 29, type = 3, proto = 1

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

<0x00000002> [8] 70 01 02 03 04 05 06 07

4、发送测试

./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

interface = can0, family = 29, type = 3, proto = 1

周立功测试软件上能看到接收的帧

5、重启

使用内核文档说的ip link set can0 type can restart-ms 100 会报

RTNETLINK answers: Device or resource busy

使用ifconfig can0 down ;ip link set can0 up type can

即可

并没有掌握sja1000波特率的配置。摸索出4种常见波特率:

250kbps:

ip link set can0 type can tq 125 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1

125kbps:

ip link set can0 type can tq 250 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1

500kbps:

ip link set can0 type can tq 75 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1

1000kbps:

ip link set can0 up type can bitrate 2000000

常见用法:

ip -details link show can0

ifconfig can0 down ;ip link set can0 up type can

./candump can0

./cansend   can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

linux测试接口配置,linux can 总线socket接口测试使用相关推荐

  1. Linux系统接口ioc,Linux下SCSI API研究及应用

    Linux下SCSI API研究及应用 Linux SCSI体系结构及API数据结构的操作原理和相关的系统调用,运用SCSI API实现了有关的数据存储. 关键词: SCSI通用驱动器  SCSI接口 ...

  2. linux 测试串口命令,Linux 下测试串口的命令microcom

    昨天应为要测试主板上的串口,查了一下,可以使用microcom 这条命令进行测试. 命令使用方法很简单: Usage: microcom [-d DELAY] [-t TIMEOUT] [-s SPE ...

  3. 删除linux 软raid0,配置Linux软RAID0

    配置Linux软RAID0 [[email protected] ~]# mdadm -C -v /dev/md0 -l 0 -n 2 /dev/sdb /dev/sdc [[email protec ...

  4. linux环境安全配置,Linux系统安全配置方案

    Linux系统安全配置方案 [日期:2006-11-28] 来源: 作者: [字体:大 中 小] 由于Linux的开源,使得Linux强大功能的背后,总是有些不尽人意的地方,为了使Linux达到一个最 ...

  5. linux socketCAN通信,linux can 总线socket接口测试使用

    最近调试一个sja1000的can驱动,发现到了2.6.36,linux把can总线封装成了网络接口.内核文档里给出了这么修改的原因. 1. Overview / What is Socket CAN ...

  6. linux can 总线socket接口测试使用

    该篇文章由徐老师编写,我因为需要重新修改该socket驱动到一个PMC CAN卡,故重新把资料粘贴整理了一下,上传到这里方面以后自己查阅. 原文链接如下:http://blog.chinaunix.n ...

  7. curl linux 测试接口_Linux测试端口的连通性的四种方法

    目录 方法一.telnet telnet为用户提供了在本地计算机上完成远程主机工作的能力,因此可以通过telnet来测试端口的连通性,具体用法格式: telnet ip port 说明: ip:是测试 ...

  8. Linux BOND接口配置

    如下使用ip命令创建一个bond接口不指定参数: / # ip link add bond1 type bond 指定bond接口的mode: / # ip link add bond2 type b ...

  9. linux dns已经配置,linux DNS 配置

    在局域网内部通过构建DNS服务器,可以使用户使用域名访问局域网中的每一台计算机.在互联网中,通过DNS服务器,可以使全世界的网络用户使用域名访问各种类型的主机,如WEB服务器.邮件服务器等 而我们使用 ...

最新文章

  1. PhpStorm升级后调用某些类提示phpstorm Unhandled exceptions
  2. E0070 不允许使用不完整的类型
  3. Listview中使用线程实现无限加载更多项目的功能
  4. 轻量级ORM《sqlcommon》第一个版本发布了
  5. beyond company30天到期
  6. 如何在参考文献中优雅地引用URL?How do you get nicely formatted URLs in the bibliography?
  7. SQL 实战教程(八)
  8. EasyRecovery帮你一招找回网易云下载的单曲
  9. 985研究生被小公司领导留了3年,【全栈最全Java框架总结
  10. leetcode第1282题
  11. SwiftUI 高级用法之ForEach如何使用 enumerated() UserDefaults.standard(教程含源码)
  12. 树莓派搭建博客/网站教程
  13. html透明颜色值,css中透明色的颜色值(rgb/rgba)
  14. 断点恢复执行时的设置
  15. 不用u盘如何在小米电视上安装电视直播软件
  16. MSRN(多尺度超分辨率重建)
  17. 【图像配准】多图配准/不同特征提取算法/匹配器比较测试
  18. python 网页视频下载工具 you-get 安装及使用
  19. java暗黑再临-战神之怒_暗黑再临战神之怒
  20. multisim14晶振在哪里_晶体谐振器在multisim中怎么找到

热门文章

  1. 一网打尽!每个程序猿都该了解的黑客技术大汇总
  2. 6 个步骤,搞定 AI 车牌识别器!(附详细分析)
  3. Wi-Fi 6 爆发的 2019
  4. 微信小程序 3 岁:日活超 3 亿,交易额超 8000 亿
  5. 为什么要进入 5G 时代?
  6. 仅用 10 天设计的 JavaScript,凭什么成为程序员最受欢迎的编程语言?
  7. 快应用不会取代 App,未来将赋能 IoT!
  8. Google Cloud大规模宕机;中国正式进入 5G 商用元年!苹果发布SwiftUI |开发者周刊...
  9. C 主导、C++与 C# 为辅,揭秘 Windows 10 源代码!
  10. 字节跳动(今日头条),战斗力为何如此凶猛?| 畅言