Design of Linux can bus driver based on MCP2515

Source: Internet
Author: User

MCP2515 Introduction

MCP2515 is an independent can bus communication controller, is the first microchip company independent can solution upgrade device, its transmission capacity is more than microchip company original can controller (MCP2510) twice times, the highest communication rate can reach 1Mbps. The MCP2515 can receive and transmit standard data frames and extended data frames as well as remote frames, eliminating extraneous messages through two receive screen registers and six receive filter registers, thereby reducing the CPU burden.

MCP2515 main function parameters and electrical characteristics are as follows:
(1) Support can technical specification 2.0a/b, the maximum transmission rate of 1Mbps;
(2) Support standard data frame, extended data frame and remote frame, each frame data domain length can be 0~8 bytes;
(3) contains two receive buffers and three transmit buffers, and can be programmed to set priority;
(4) contains six 29-bit (bit) receive filter registers and two 29-bit (bit) receive screen registers;
(5) High-speed SPI interface, support SPI 0,0 and the model;
(6) One-time mode ensures that the message is transmitted at once;
(7) with programmable clock pulse output pin, can be used as other chip clock signal source;
(8) The frame start (SOF) Signal output function can be used in a determined system (such as time-triggered Can-ttcan) to perform the time slot function, or in the Can bus diagnostics to determine the early bus out-level;
(9) The use of low-power CMOS technology, operating voltage: 2.7v~5.5v, operating current: 5mA (standby state);
(10) Operating temperature range: (I)-40 ℃ to +85℃, (E)-40 ℃ to +125℃.

MCP2515 Socket can driver implementation

The socket can driver is available in the kernel for MCP2515, and we only need to configure the kernel option for the MCP2515 chip to use the MCP2515 can bus using the socket programming method.

Socket can introduction

Socket can is an implementation of the can protocol implemented under Linux. The first way to use can is based on a character device, unlike the socket can uses the Berkeley socket interface and Linux network protocol stack, which allows the can device driver to be called through the network interface. The socket can interface is designed to be as close to TCP/IP protocols as possible, making it easier for programmers who are familiar with network programming to learn and use it.

Registering the MCP2515 driver with the Linux kernel

Adding SPI drivers to the Linux kernel has been described in detail in previous articles, according to the hardware schematic diagram 3, MCP2515 hangs on the SPI0, using interrupt GPF1, so to register the MCP2515 device with the Linux kernel,


Registration complete, execute make menuconfig configure kernel options.

1 [*]networking support->
2 <*>can Bus Subsystem support->
3 <*>raw CAN protocal
4 <*>broadcast Manage CAN protocal
5 CAN Device drivers->
6 <*>platform CAN driver with NetLink support
7 [*]can bit-timing calculation
8 <*>microchip mcp251x SPI CAN Controllers
9
Ten Device drivers->
[*]SPI Support
<*> Samsung s3c2416 Series Type SPI

To compile the kernel, the kernel boot display indicates that the MCP2515 driver was successfully loaded.


Socket can test

Because the latest version of the BusyBox socket can is not supported, so in order to test and use the socket can, we need to compile the socket can tool. Here are two tools, namely Iproute2 and Canutils.

1 Iproute2

(1) Download Iproute2 's latest source code http://www.kernel.org/pub/linux/utils/net/iproute2/. The author download is Iproute2 3.6.0.

(2) Decompression Iproute2-3.6.0.tar.xz, modify the makefile line 33rd.
#CC = gcc
CC = ARM-NONE-LINUX-GNUEABI-GCC

(3) because we only need IPROUT2 IP commands, we modify the 42nd line of the makefile.
#SUBDIRS =lib IP TC Bridge misc Netem genl Man
Subdirs=lib IP

(4) Modify the Execute make command, generate the IP command, copy to the Development Board file system directory.

(5) Use the IP command.
Ifconfig can0 down//close can0 for configuration
./IP Link set can0 up type can bitrate 250000//set Can0 baud rate
./ip-details link Show can0//display can0 information


2 canutils

Canutils is an open source code based on the GNU GPLv2 license, including the Canconfig, Canecho, Cansend, Candump, cansequence five tools for detecting and monitoring the socket can interface.

(1) Download Canutils's latest source code http://www.pengutronix.de/software/socket-can/download/canutils. The author download is canutils 4.0.6.
(2) Because Canutils compilation requires Libsocketcan library support, download Libsocketcan is required. http://www.pengutronix.de/software/libsocketcan/download/the author of the download is Libsocketcan 0.0.9.
(3) Unzip the libsocketcan-0.0.9.tar.bz2. Executes the Configure command. (where--host is the specified cross tool chain,--prefix is the build location for the specified library)
./configure--host=arm-none-linux-gnueabi--prefix=/home/linux/workdir/can/tools/libsocketcan
(4) Execute make compile library;
(5) Execute make install build library. At this point, Libsocketcan compiles.
(6) Extract canutils-4.0.6.tar.bz2, execute configure command. (where--host is the specified cross toolchain,--prefix is the build location for the specified library, Libsocketcan_libs is the library that specifies the canconfig needs to be linked, Ldflags is the path to the specified external library, and Cppflags is the path to the specified external header file)
./configure--host=arm-none-linux-gnueabi--prefix=/home/linux/workdir/can/tools/canutils libsocketcan_LIBS=- Lsocketcan Ldflags=-l/home/linux/workdir/can/tools/socketcan/lib cppflags=-i/home/linux/workdir/can/tools/ Socketcan/include
(7) Modify the Execute make command, generate four directories, and copy to the corresponding directory of the Development Board file system respectively.
(8) Use the Canutils tool. (You can use [command +--HELP] to see the detailed usage of the command, and here are just a few common instructions)

① Configuring the CAN bus baud rate:
Canconfig CanX bitrate + baud rate


② Open/restart/Shut down can bus
Canconfig CanX Start
Canconfig CanX Restart
Canconfig CanX Stop


③ view can bus status
Canecho CanX


④ Sending information
Cansend Canx–-identifier=id + data


⑤ Receive data
         candump canx


⑥ uses a filter to receive ID matching data
        candump canx–-filter=id:mask





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.