Design of Linux can bus driver based on MCP2515 (I.)

Source: Internet
Author: User
Tags bz2

Time: 2016-12-12 huaqing Vision

1. Preface

The CAN (Controller area Network) bus, which is a control LAN bus, is an effective serial communication network that supports distributed control or real-time control. It is widely used in industrial field control, intelligent buildings, medical devices, transportation and sensors due to its high performance, high reliability, unique design and suitable price, and has been recognized as one of the most promising fieldbus. The CAN bus specification has been developed by ISO as an international standard ISO11898, and has been supported by many semiconductor device manufacturers.

This paper uses the Huaqing Vision FS2416 platform. FS2416 uses socket network device drivers and character device drivers to provide MCP2515 drivers to the Linux kernel, this article details the MCP2515-based Linux can bus driver that is designed using the socket method.

2.fs2416 Introduction


Figure 1 FS2416 Development Board

FS2416 uses Samsung's ARM926EJ core CPU s3c2416, both in terms of performance and cost, s3c2416 is stronger than 2440, and is the most perfect replacement for 2440.

As a 32/16 bit RISC instruction set, low cost, low power, high performance microprocessor. S3C2416 uses a 65nm manufacturing process to reduce costs, power consumption and improve performance, its use of the ARM926EJ core, the integration of 2D graphics acceleration, the addition of a low-power mode, support for internal rom/ram boot, support Movinand start-up and low-power audio codec. In addition to other ARM9 chips, its peripherals have been upgraded, with more resources.


Figure 2 FS2416 Board level resource Introduction

3.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℃.

4. Hardware Design

The hardware connection of the MCP2515 and s3c2416 is shown in Figure 3. If the hardware schematic diagram shows that the MCP2515 chip is connected to the s3c2416 chip SPI0, interrupted on GPF1, MCP2515 output connection sn65hvd230 can bus transceiver, sn65hvd230 is Texas Instruments produced by the 3.3V can transceiver. In order to save power and reduce the circuit volume, the logic level of the MCP2515 can bus controller is lvttl,sn65hvd230 with its supporting transceiver.


Figure 3 MCP2515 Hardware Connection diagram

5.mcp2515 Socket can driver implementation

The FS2416 platform uses the Linux2.6.39 kernel, which provides the socket can driver for MCP2515 in the kernel, and we only need to configure the kernel option for the MCP2515 chip to use the MCP2515 can bus using the socket programming method.

5.1Socket 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.

5.2 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, the registration code 4 shows.


Figure 4 MCP2515 SPI Registration code

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 5 display indicates that the MCP2515 driver was successfully loaded.


Figure 5 Kernel Loading tips

6.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.

6.1iproute2

(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


Figure 6 Introduction to Iproute usage

6.2canutils

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


Figure 7 Setting the can bus baud rate using the Canutils tool

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


Figure 8 Setting the CAN bus status

③ view can bus status
Canecho CanX


Figure 9 Viewing the CAN bus status

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


Figure Ten can bus send data

⑤ receiving data
Candump CanX


Figure one can bus to receive data

⑥ using filters to receive ID matching data
Candump Canx–-filter=id:mask


Figure A can bus uses a filter to receive matching data

7. Summary

At this point, the use of the socket MCP2515 can bus driver design is finished, the user can use socket sockets, referring to Canutils source design their own applications.

The next article will detail how the SPI subsystem can be used to design the MCP2515 method of the can bus-driven character device.

Design of Linux can bus driver based on MCP2515 (I.)

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.