Homepage Wiki Forum Buy

MCP2515 CAN

From GNUBLIN

severity (level) requirement Gnublin family
Gnublin logo advanced.png load drivers, CAN knowledge, Terminal-Tools all


Contents


This article describes the connection of GNUBLINs to a CAN bus. Therefore a MCP2515 device is used, which is accessed via the SPI interface of the Gnublin and drives the MCP2551 CAN-driver.

The data sheet of the MCP2515 is here

The data sheet of the MCP2551 is here

Module can.jpg

In this Picture you can see the CAN-Module and the null-modem cable.



Installation Tools & Driver

First you have to connect to the CAN adapter with Gnublin.

To use the CAN-Interface we use SocketCAN. We built a Debian Package with the can-utils (User Space programs to send and receive CAN messages) and Socketcan.


cd /root/deb
dpkg -i socketcan_full.deb


In that package there are the can-utils, which make it easy to send and receive CAN messages and the needed library libsocketcan.

After that, we start the Gnublin and test the hardware. First we have to add some lines in the file: /etc/network/interfaces

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

Then you have to restart the network

/etc/init.d/networking restart

In the next step we load the mcp251x module:

Hint: All SPI driver like CAN, ENC28J60, etc. have to be unloaded! (e.g. modprobe -r enc28j60 or modprobe -r spidev)

modprobe mcp251x


As output, the following should appear:

mcp251x spi0.1: probed

In the next step we configure and start the CAN device:

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

The first instruction gives no response. The second instruction replies:

mcp251x spi0.1: CNF: 0x03 0xf5 0x01

CAN example session

Now we can use cansend to send data via the CAN bus.

cansend can0 5A1#68.61.6c.6c.6f.21

The syntax for a sample frame is as follows:

cansend can0 Identifier#HEX-Data

The identifier is a three digit hex number and HEX-Data are up to eight two-digit hexadecimal numbers with an optional point after every two digits.

If you want to listen to the CAN bus, you use the comand candump:

candump can0

For information how many packages were sent and received, read the file /proc/net/can/stats:

cat /proc/net/can/stats

Test the transmission with CAN_VIEW

For a qualitative test of the CAN bus adapter a Tiny-Can adapter from MHS-electronics was used. It delivers the software Can_View, which brings a GUI to view the CAN messages.

Can view.jpg

In this picture we see the instructions on the Gnublin (left) and the programm Can-View (right).

candump can be terminated with Ctrl-C.


For advanced

Konfiguration with old/other kernel

If you want to change the setup code manually, in the file arch/arm/mach-lpc313x/ea313x.c add the following lines. In the newest version of the kernel 2.6.33 this is not necessary (if you have an old version just type "git pull" in the kernel directory)

At begin of the file add this line:

#include <linux/can/platform/mcp251x.h>

Then you scroll down to about line 439, where you should then see the following text:

#if defined(CONFIG_ENC28J60_SPI_DEV)

static int __init lpc313x_enc_register(void)
{ 
	struct spi_board_info info =
	{
		.modalias = "enc28j60",
		.max_speed_hz = 1000000,
		.bus_num = 1,
		.irq = IRQ_GPIO_16,//IRQ_GPIO14
		.irq = IRQ_GPIO_14,/*IRQ_GPIO14*/
		.chip_select = 0,
	};

	return spi_register_board_info(&info, 1);
}
arch_initcall(lpc313x_enc_register);

#endif


after #endif add the following text:


static int mcp251x_setup(struct spi_device *spi)
{
	return 0;
}


static int __init mcp251x_lpc313x_register (void)
{

   
    static struct mcp251x_platform_data mcp251x_info = {
         .oscillator_frequency = 16000000,
         .board_specific_setup = &mcp251x_setup,
         .model = CAN_MCP251X_MCP2515,
         .power_enable = NULL,
         .transceiver_enable = NULL,
 };
       
   struct spi_board_info info = {
           
                   .modalias = "mcp251x",
                   .platform_data = &mcp251x_info,
                   .irq = IRQ_GPIO14,
                   .max_speed_hz = 1000000,
                   .chip_select = 1,
 		  .bus_num = 0,
           }; 
 
    return spi_register_board_info(&info, 1);
 }
 arch_initcall(mcp251x_lpc313x_register);

After that, save the file and activate the needed modules in the kernel.


HINT! If you use an 8MHz Oszillator you need to change the line .oscillator_frequency = 16000000, to .oscillator_frequency = 8000000,.


Kernelmodules

After the instruction make gnublin_defconfig the Gnublin development kernel is configured for basic CAN support built directly in the kernel. The driver for the MCP2515 is available as module.

If you have a different version of the kernel, then the following options are listed, which must be activated.

  │ │       -*- Networking support  --->                                                   │ │   
  │ │       	<*>   CAN bus subsystem support  --->                                      │ │   
  │ │       		<*>   Raw CAN Protocol (raw access with CAN-ID filtering)          │ │   
  │ │       		<*>   Broadcast Manager CAN Protocol (with content filtering)      │ │   
  │ │            		 CAN Device Drivers  --->                                  │ │ 
  │ │       				<*> Virtual Local CAN Interface (vcan)             │ │   
  │ │       				<*> Platform CAN drivers with Netlink support      │ │   
  │ │       				[*]   CAN bit-timing calculation                   │ │   
  │ │       				<M>   Microchip MCP251x SPI CAN controllers        │ │    


Then compile the kernel and install the modules as shown in this instructions and copy it to your SD card.

In other languages