laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 7:35 pm

Last thing I have to mention and ask so I know if this is OK. I've hooked up the RST pin on the arduino shield to the 5V pin on the shield. Also, from the raspberry pi I use the 5V and GND to plug into the 5V and GND spots near the DB9 connector so I can power the shield.

The strange thing over here is that when I hit the reset button on the arduino shield the raspberry pi also resets. Is this supposed to be like this or it is something wrong?

User avatar
DougieLawson
Posts: 30213
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 8:03 pm

Nope, the SPI stuff is loaded by the GPU and doesn't show up in dmesg now.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 8:05 pm

Oh. I see now. It should be fine then since I have the /dev/spi0.1 in it's place. I'm doing a

Code: Select all

apt-get update && apt-get upgrade
right now and I see that several overlays are being updated/added, them including mcp and spi. So after the update finishes I'll give it another try.

The CLK line doesn't have to be level converted, right?

edo1
Posts: 136
Joined: Sun Jun 15, 2014 3:33 pm
Location: Russia

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 8:16 pm

saper_2 wrote:I don't see a mcp2515 being mentioned as registered can interface. Only virtual can (vcan)...
current kernel prints nothing about mcp2515 initialization failure/success.
laalex wrote:Here is a diagram for my circuit. The software is fine. I have everything required plus the vcan is working perfectly.

Do You see anything wrong in the wiring?
sorry, I cannot help with circuit, but I can say that if mcp251x module is loaded and can0 interface don't appear then certainly you have a hardware issue (wrong circuit, bad contacts, broken mcp2515 chip and so on).

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 8:20 pm

@edo1, thanks a lot for your information. It seems that everything is fine software side. It should be a hw issue or a wiring issue somehow. I'll try to test the shield tomorrow with an arduino to see if it's going to work as expected or if it's not working (to know if the MCP2515 is fried or is good).

If someone else could find out or point out something related to the wiring in my circuit, let me know! Thanks a lot for everyone's help so far. I'm still trying to see if I can get it working

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 8:21 pm

Alsom @edo1, should the can0 automatically be dispalyed if the circuit is good? Or to test a restart is required for every time I try a new wire configuration?

edo1
Posts: 136
Joined: Sun Jun 15, 2014 3:33 pm
Location: Russia

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 8:59 pm

laalex wrote:Or to test a restart is required for every time I try a new wire configuration?
you could do

Code: Select all

rmmod mcp251x; modprobe mcp251x
to check without rebooting.

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 9:14 pm

Running the SPI test utility (code below) I always get the following answer (for spidev0.1) having the MCP2515 sparkfun shield wired to my PI:
Response:

Code: Select all

spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF

Test code:

Code: Select all

/*
 * SPI testing utility (using spidev driver)
 *
 * Copyright (c) 2007  MontaVista Software, Inc.
 * Copyright (c) 2007  Anton Vorontsov <avorontsov@ru.mvista.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
 */

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

static void pabort(const char *s)
{
	perror(s);
	abort();
}

static const char *device = "/dev/spidev1.1";
static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay;

static void transfer(int fd)
{
	int ret;
	uint8_t tx[] = {
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
		0xF0, 0x0D,
	};
	uint8_t rx[ARRAY_SIZE(tx)] = {0, };
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)tx,
		.rx_buf = (unsigned long)rx,
		.len = ARRAY_SIZE(tx),
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");

	for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
		if (!(ret % 6))
			puts("");
		printf("%.2X ", rx[ret]);
	}
	puts("");
}

static void print_usage(const char *prog)
{
	printf("Usage: %s [-DsbdlHOLC3]\n", prog);
	puts("  -D --device   device to use (default /dev/spidev1.1)\n"
	     "  -s --speed    max speed (Hz)\n"
	     "  -d --delay    delay (usec)\n"
	     "  -b --bpw      bits per word \n"
	     "  -l --loop     loopback\n"
	     "  -H --cpha     clock phase\n"
	     "  -O --cpol     clock polarity\n"
	     "  -L --lsb      least significant bit first\n"
	     "  -C --cs-high  chip select active high\n"
	     "  -3 --3wire    SI/SO signals shared\n");
	exit(1);
}

static void parse_opts(int argc, char *argv[])
{
	while (1) {
		static const struct option lopts[] = {
			{ "device",  1, 0, 'D' },
			{ "speed",   1, 0, 's' },
			{ "delay",   1, 0, 'd' },
			{ "bpw",     1, 0, 'b' },
			{ "loop",    0, 0, 'l' },
			{ "cpha",    0, 0, 'H' },
			{ "cpol",    0, 0, 'O' },
			{ "lsb",     0, 0, 'L' },
			{ "cs-high", 0, 0, 'C' },
			{ "3wire",   0, 0, '3' },
			{ "no-cs",   0, 0, 'N' },
			{ "ready",   0, 0, 'R' },
			{ NULL, 0, 0, 0 },
		};
		int c;

		c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);

		if (c == -1)
			break;

		switch (c) {
		case 'D':
			device = optarg;
			break;
		case 's':
			speed = atoi(optarg);
			break;
		case 'd':
			delay = atoi(optarg);
			break;
		case 'b':
			bits = atoi(optarg);
			break;
		case 'l':
			mode |= SPI_LOOP;
			break;
		case 'H':
			mode |= SPI_CPHA;
			break;
		case 'O':
			mode |= SPI_CPOL;
			break;
		case 'L':
			mode |= SPI_LSB_FIRST;
			break;
		case 'C':
			mode |= SPI_CS_HIGH;
			break;
		case '3':
			mode |= SPI_3WIRE;
			break;
		case 'N':
			mode |= SPI_NO_CS;
			break;
		case 'R':
			mode |= SPI_READY;
			break;
		default:
			print_usage(argv[0]);
			break;
		}
	}
}

int main(int argc, char *argv[])
{
	int ret = 0;
	int fd;

	parse_opts(argc, argv);

	fd = open(device, O_RDWR);
	if (fd < 0)
		pabort("can't open device");

	/*
	 * spi mode
	 */
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret == -1)
		pabort("can't set spi mode");

	ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
	if (ret == -1)
		pabort("can't get spi mode");

	/*
	 * bits per word
	 */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't set bits per word");

	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't get bits per word");

	/*
	 * max speed hz
	 */
	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't set max speed hz");

	ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
	if (ret == -1)
		pabort("can't get max speed hz");

	printf("spi mode: %d\n", mode);
	printf("bits per word: %d\n", bits);
	printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);

	transfer(fd);

	close(fd);

	return ret;
}
Is this something wrong?

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 9:17 pm

Also, my last question here: if the can bus shield is not attached to any can, should the raspberry PI detect the can0?

I have ordered the can bus module that the creator of this post has been using and I'll wait for it to test. I just need to know the above.

Thanks a lot everyone!

edo1
Posts: 136
Joined: Sun Jun 15, 2014 3:33 pm
Location: Russia

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 9:31 pm

laalex wrote:if the can bus shield is not attached to any can, should the raspberry PI detect the can0?
yes.

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sun May 29, 2016 9:57 pm

Ok. Then it seems that my MCP2515 is fried. It had a point where it was very heated (burnt my finger when touched it) and maybe that's why it doesn't detect it.

I'll check it tomorrow with an arduino to see if it is working or not. If it is working with arduino then I'll program an atmega328p and use serial between the microcontroller and rpy to communicate.

Thank you very much guys!

User avatar
saper_2
Posts: 214
Joined: Sun Aug 03, 2014 10:08 am
Location: PL

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Tue May 31, 2016 8:37 pm

laalex wrote:Ok. Then it seems that my MCP2515 is fried. It had a point where it was very heated (burnt my finger when touched it) and maybe that's why it doesn't detect it.
Yes, you've murdered it :lol: .

Sorry could not resist :lol: :lol:

Code: Select all

  /++++++++++++\
 /              \
|                |
|     R.I.P.     |
|    MCP 2515    |
|   28 May '16   |
|                |
|----------------|
|                |
|________________|

mothl
Posts: 1
Joined: Fri Jun 03, 2016 3:03 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Fri Jun 03, 2016 3:23 pm

Hi,

I think I have the same board as you...
I come up with the same wiring idea as you did, except the voltage supply :oops:
The guy who sold the board told me it can be powerded either with 3.3V or 5V.
Decided to take 5V :evil:

I can't get the board running and now I fear that I have destroyed the gpio ports...
i've got a brand new Pi3 :evil:
Is the MCP2515 really built that way that the SPI pins have the same voltage level as the Vcc?

Is it possible to test the SPI pins of the controller as normal GPIO?
is there any way use SPI1 instead - with the mcp251x module if I really destroyed my controller?

Thx

Pi Flyer
Posts: 33
Joined: Sat Jun 04, 2016 1:57 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sat Jun 04, 2016 2:01 pm

I'm probably missing something here or maybe you all just enjoy the challenge of getting this to work but are you aware there is a CAN board that is nearly 'plug and play' for the Pi?

http://skpang.co.uk/catalog/pican2-canb ... -1475.html

Canberry
Posts: 1
Joined: Sat Jun 04, 2016 2:06 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sat Jun 04, 2016 2:27 pm

Dear Raspberry users,
I am Pierpaolo of Industrialberry project,I work with Raspberry and MCP2515 from years, but I have a problem with the last Raspbian Iso with kernel 4.4.9-v7+.
I can perform the following commands with success:
ip link set can0 up type can bitrate 1000000
ip link set can1 up type can bitrate 1000000


with my board with two can interfaces.
I receive the following error message:

Cannot find device "can0"
Cannot find device "can1"

I have modified the /boot/config.txt with the following overlay

dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=22
dtoverlay=mcp2515-can1-overlay,oscillator=16000000,interrupt=25


I have enabled spi, i2c on raspi-config.
All my boards work Raspberry Pi3, but with 2016-03-18-raspbian-jessie not with
2016-05-10-raspbian-jessie, I don't understand the reason :( . Any suggestion?

Best regards
Pierpaolo

User avatar
saper_2
Posts: 214
Joined: Sun Aug 03, 2014 10:08 am
Location: PL

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sat Jun 04, 2016 3:19 pm

mothl wrote:I think I have the same board as you...
I come up with the same wiring idea as you did, except the voltage supply :oops:
The guy who sold the board told me it can be powerded either with 3.3V or 5V.
Decided to take 5V :evil:
Ugh, this must pains you, brand new Pi3 and fried some gpios already :( ...
mothl wrote:I can't get the board running and now I fear that I have destroyed the gpio ports...
i've got a brand new Pi3 :evil:
Is the MCP2515 really built that way that the SPI pins have the same voltage level as the Vcc?
YES. High logic level voltage is taken from Vcc of MCP2515...
mothl wrote: Is it possible to test the SPI pins of the controller as normal GPIO?
Yes, just don't use any SPI-enabled device, and blacklist SPI module (google how to).
Then using some gpio lib (e.g. wiring-pi came first to my head) to test those GPIOs where is SPI... Don't have around my Pi-GPIO cheatsheet so check yourself which pins those are...
mothl wrote: is there any way use SPI1 instead - with the mcp251x module if I really destroyed my controller?
I think, loading just overlay to switch to ALT location of SPI should do the trick... Or compile your own mcp2515 overlay with changed spi pins.

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sat Jun 04, 2016 4:09 pm

So, @Canberry did you managed to make the mcp2515 work with the 03 version of rpy? Should I do a downgrade?

User avatar
saper_2
Posts: 214
Joined: Sun Aug 03, 2014 10:08 am
Location: PL

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sat Jun 04, 2016 6:40 pm

Hi,

I did some tests (I found my CAN module :D), and using image:

Code: Select all

2016-05-27-raspbian-jessie-lite.img
So, there might be a something off with previous image.

CAN module works out of box. I have at end of /boot/config.txt:

Code: Select all

# Enable audio (loads snd_bcm2835)
dtparam=audio=on
gpu_mem=64
enable_uart=0
dtoverlay=w1-gpio,gpiopin=4
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=12
dtoverlay=spi-bcm2835-overlay
Check if you have: can0 (and can1)
in:

Code: Select all

ls /sys/class/net
[...]

I did a quick test:
  1. Connected CAN module & configured it
  2. Tested if it exists (it exists)
  3. disconnected MISO wire from CAN module to Pi
  4. rebooted Pi
  5. checked if can0 interface still is created (because of loaded modules) - no there is no interface.
So, if there is no comm between CAN module(s) and Pi there will be no interfaces created.
can-test-01.jpg
can-test-01.jpg (60.99 KiB) Viewed 3952 times
- sorry for crappy quality but this forum limit attachments to 64k :cry:

laalex
Posts: 20
Joined: Fri May 27, 2016 7:29 pm

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Sat Jun 04, 2016 7:03 pm

Hey saper_2, can we have a small private chat somehow ( facebook or something with IM?). If so, please let me know by a private message with your contact details. Thanks a lot!

ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Mon Jun 27, 2016 7:16 am

Hi all,

Thanks for this post. It is really great way to get such cheap can module to be interfaced with the PI :)
I have one question. I only have the Raspberry and the can module (same pcb as in the original post). I interfaced it with the PI with the SPI. Now ..

Is there any other way to test if it is working without having the usbtin or any other device, I mean i want to make sure that module is working ?

many thanks in advance
Ahmed

User avatar
saper_2
Posts: 214
Joined: Sun Aug 03, 2014 10:08 am
Location: PL

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Mon Jun 27, 2016 3:40 pm

ahmedawad wrote:Hi all,

Thanks for this post. It is really great way to get such cheap can module to be interfaced with the PI :)
I have one question. I only have the Raspberry and the can module (same pcb as in the original post). I interfaced it with the PI with the SPI. Now ..
I just ask, have you split power supplies for MCP2515 and MCP2551 (TJA1050/etc.)? (you didn't mentioned nothing about it (hacking/modifying/etc.)...)
ahmedawad wrote: Is there any other way to test if it is working without having the usbtin or any other device, I mean i want to make sure that module is working ?
many thanks in advance
Look about 2 post up, I put a terminal screenshot where you can see a some way to check if MCP2115 is recognized by kernel (there is canX device in /sys/class/net ) - this can tell you that mcp2515 is working fine, but for bus itself, without 2nd device you won't know.
To be sure that whole module works fine, connect it to some CANbus system/device and try sniff the bus, if you connect it to some "slave" device (device that need to be "asked" for data) then you need to know supported IDs and bus baudrate...

Right now, I don't have anything else to test the CAN module on something else, I did this just to show that "this is possible" :)

ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Tue Jun 28, 2016 7:19 am

Thanks @saper , yes i actually did the surgery operation like you described before to split the power supplies of both the controller and transceiver. :)

I also checked the CAN module's functionality using the mosi pin as in your screen shot and it is working. Next i will send some frames from a usb to can interface (similar to USBtin) from a PC and perform candump to see whether the can0 interface on the pi will sniff the bus. I am optimistic to get nice results :) .. Thanks again, that was a great job.

ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Tue Jun 28, 2016 9:11 am

I have another issue @saper, now i have a PCAN adapter from PEAK http://www.peak-system.com/PCAN-USB.199.0.html?L=1

I tried to send some frames using their PCAN view software from my pc and I connected only can_l and can_h of the adapter to that of the can module. Running

Code: Select all

cansniffer can0
on the pi but i don't see the sent frames.

I am not sure of the capacitor I soldered, it is 10 uF/6.3volt. does it have to be 15uF/6.3v exactly ? .. when I tested the module using the screen shot you provided. it is recognized but i don't know why it is not receiving the sent frames. any ideas. thanks

User avatar
saper_2
Posts: 214
Joined: Sun Aug 03, 2014 10:08 am
Location: PL

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Tue Jun 28, 2016 9:31 pm

Cap 10..1000uF will be ok :) (with bigger cap it can be good to add a ceramic 100nF cap too ).

If I understand correctly, you did not connected GND in both modules...
This is differential bus, but the levels have to some reference point, which is ground...
e.g. in car you have GND on metal frame so all CAN devices have common GND, but on desk you have to connect together GNDs (desk is from wood/glass, and your PC & RPi have different PSU which ones are separated from AC ~23V0/~110V)... So in reality CAN bus is 3-wire bus: 2 hot wires (signal) + 1 ground (like RS485/422).

ahmedawad
Posts: 42
Joined: Mon Apr 18, 2016 11:44 am
Location: Bremen, DE

Re: [quick-guide] CAN bus on raspberry pi with MCP2515

Wed Jun 29, 2016 6:16 am

Thanks @saper
It is now working :D , I really had totally different problem. I wasn't paying attention to the bitrate. It is 125kbps like in your case but in the Pcan viewer i was selecting other different number when I had both the same, I was able to send/receive frames. Of course it is important for synchronization of all nodes exist on the CAN bus but i didn't pay attention to this at the beginning.

It is also working with and without connecting the ground, I don't understand how but I don't care now after all :lol:

I thank you for your great post and sharing this good idea with us all, besides your quick response :)

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 14 guests