Repository navigation
Description
I tested the additional spi bus 1 on RPI3 after enabling the overlay in config.txt file
dtoverlay=spi1-3cs
the 3 chip selects spi1.0 / spi1.1 / spi1.2, all works fine with an mcp3008 ADC chip, so there is no problem at all with this bus and its corresponding chip selects.
Now, I wanted to write .dts source that supports the functionality of mcp2515 on this additional SPI bus. Very similar to what a contributor before did @petit_miner to the overlay mcp2515-can1-overlay.dts but i changed every spi0 to spi1, the mcp2515 worked but spi0.1 disappeared and the chipselects of "SPI 1" were not properly functioning, that is if connecting the mcp2515 module to spi1.0 or spi1.1 they both works which is not right.
here is my trial, i didn't change much except using spi1 instead of spi0.
the original dts file is found on your repository rpi-4.4.y , this is what i used to modify and recompile
My modifications are:
`
/*
* Device tree overlay for mcp251x/can2 on spi1.0
*/
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2835", "brcm,bcm2836", "brcm,bcm2708", "brcm,bcm2709";
/* disable spi-dev for spi1.0 */
fragment@0 {
target = <&spi1>;
__overlay__ {
status = "okay";
};
};
fragment@1 {
target = <&spidev0>;
__overlay__ {
status = "disabled";
};
};
/* the interrupt pin of the can-controller */
fragment@2 {
target = <&gpio>;
__overlay__ {
can2_pins: can2_pins {
brcm,pins = <25>; /* it is the default pin and it will be overriden */
brcm,function = <0>; /* input */
};
};
};
/* the clock/oscillator of the can-controller */
fragment@3 {
target-path = "/clocks";
__overlay__ {
/* external oscillator of mcp2515 on SPI1.0 */
can2_osc: can2_osc {
compatible = "fixed-clock";
#clock-cells = <0>;
clock-frequency = <16000000>;
};
};
};
/* the spi config of the can-controller itself binding everything together */
fragment@4 {
target = <&spi1>;
__overlay__ {
/* needed to avoid dtc warning */
#address-cells = <1>;
#size-cells = <0>;
can2: mcp2515@0 {
reg = <0>;
compatible = "microchip,mcp2515";
pinctrl-names = "default";
pinctrl-0 = <&can2_pins>;
spi-max-frequency = <10000000>;
interrupt-parent = <&gpio>;
interrupts = <25 0x2>;
clocks = <&can2_osc>;
};
};
};
__overrides__ {
oscillator = <&can2_osc>,"clock-frequency:0";
spimaxfrequency = <&can2>,"spi-max-frequency:0";
interrupt = <&can2_pins>,"brcm,pins:0",<&can2>,"interrupts:0";
};
};
`
please what i am doing wrong or what needs to be changed in order to have the mcp2515 overlay correctly uses the spi1.0 / spi1.1 / spi1.2 overlays.
Thanks
I am using Raspbian: Linux raspberrypi 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l GNU/Linux
Activity
pelwell commentedon Jan 19, 2017
That's would make sense if you meant "spi0.0", due to
fragment@1-spidev0is the label for the node that createsspidev0.0, and fragment 1 disable it; just delete fragment 1.You will need to disable the spidev associated with spi1.0 - you can do this with an overlay parameter:
Also, fragment 0 is unnecessary because loading the spi1-3cs overlay enables spi1.
In summary:
dtoverlay=spi1-3cstodtoverlay=spi1-3cs,cs0_spidev=off.However, see #1484 - it seems that there are interrupt problems when various Aux peripherals (SPI1, SPI2 and UART1) are used together. You may have to disable (or not enable) UART1 - put
enable_uart=0in config.txt, or just don't addenable_uart=1. Alternatively, if you do need a serial port and you can live without Bluetooth for a the experiments, adddtoverlay=pi3-disable-btto config.txt which tells the Pi3 to use UART0 (ttyAMA0) for console output, just like a Pi2.I'm hoping to look at the Aux interrupt issue today.
ahmedawad1 commentedon Jan 19, 2017
This is incredibly awesome @pelwell . Thank you very much.
I had now SPI0 along with SPI1 working without any issues and i did not disable the UART at all. For the moment i don't need either UART1 or the Blutooth.
I have tested also the mcp2515 Transceiver on the whole chip selects of SPI1 with different overlays and it worked fine.
Just a matter of curiosity, you mentioned SPI2 but actually the SPI 2 bins are assigned to bins which don't exist for the 40 bins RPI3, is this considered for future RPI versions?
is it possible to change the brcm bins in the dts and have SPI 2 assigned to different pins ?
I am grateful to your help and Thank you again !
Regards
Ahmed
pelwell commentedon Jan 19, 2017
SPI2 can be used on a Compute Module on GPIOs 40-45, but sadly the SPI2 function isn't available on GPIOS 0-27.
ahmedawad1 commentedon Jan 19, 2017
Thank you !
karu2003 commentedon Mar 19, 2017
Hi ahmedawad1
Share please the final Result.
I use three SPI SPI0.0 SPI0.1 and SPI1.0 for the MCP2515.
Thank you.
ahmedawad1 commentedon Mar 19, 2017
It is exactly as what what @pelwell mentioned in the summary steps. Follow the dts source code in the first comment, and make the required modifications. Then use a dtc compiler to compile from .dts to dtb or .dtbo according to the Raspbian kernel version that you use. You can see this post on the Raspberry Forum to get to know how to compile it from .dts source file. here
karu2003 commentedon Mar 19, 2017
I make mcp2515-can2-overlay.dts
/*
*/
/dts-v1/;
/plugin/;
/ {
};
on RPi
sudo apt-get install device-tree-compiler
dtc -@ -I dts -O dtb -o mcp2515-can2.dtbo mcp2515-can2-overlay.dts
sudo cp mcp2515-can2.dtbo /boot/overlays
in /boot/config.txt
dtparam=spi=on
dtoverlay=mcp2515-can2-overlay,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay
CAN2 was not created.
sudo /sbin/ip link set can2 up type can bitrate 500000
karu2003 commentedon Mar 19, 2017
need
dtoverlay=spi1-1cs
ahmedawad1 commentedon Mar 19, 2017
your .dts source file looks ok but I think you are missing in the config.txt :
Try it!
karu2003 commentedon Mar 19, 2017
I have only one SPI1.0
CE1 and CE2 use for RTS, CTS
Wong-Jhon commentedon Apr 1, 2017
@karu2003
Maybe there is can0 not can2,
try
ifconfig -afirst ,and see what it is.In my test, CAN0 was created when I connected mcp2515 to spi1 .
karu2003 commentedon Apr 2, 2017
Thanks, it works. CAN0 is better. :)
DavidDavid123 commentedon Nov 26, 2018
Hello , I'm trying to do the same - connect the Canbus to SPI1 - I think I did everything written here - but it doesn't work,
this is what I have in the config.txt:
dtparam=i2c_arm=on
dtparam=spi=on
dtoverlay=spi1-1cs
dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=12
dtoverlay=spi1-bcm2835-overlay
enable_uart=0
dtoverlay=pi3-disable-bt
the connection are:
int -- 32 (GPIO 12)
sck -- 40 (GPIO 21 , SPI1 SCLS)
si -- 38 (GPIO 20 , SPI1 MOSI)
so -- 35 (GPIO 19 , SPI1 MISO)
cs -- 12 (GPIO 18 , SPI1 CS0)
gnd -- 6 (GND)
vcc - 2 (5V)
but I can't see the can0
why?
what am I missing \ doing wrong?
Thanks,
pelwell commentedon Nov 26, 2018
Shouldn't that be:
?
23 remaining items
DavidDavid123 commentedon Nov 26, 2018
it's seem that you did a GRAET job!
Thank you VERY VERY MUCH!!!!!!!!!!
I have try for 3 days all over the internet to find a solution - nothing (stackoverflow,stackexchange,PI forum,electronic forum ,linux forum)
it's seem that you are the only one that really understadn this
Thanks again!
I will let it work all night and tomorrow let you know
pelwell commentedon Nov 26, 2018
Unfortunately there is no easy way to make an overlay work with more than one of the many combinations of SPI interfaces and chip selects, so a new combination often runs into problems like this. The larger number of wires compared to I2C makes a wiring fault more likely, and differences in the protocol (again compared to I2C) make it harder to tell a configuration error from a hardware issue.
AHeinrichPem commentedon Nov 26, 2018
Hello,
maybe its a another topic, but how can I use the mcp2515 in a c application. I cant find a library or driver for it :(
pelwell commentedon Nov 26, 2018
It is the wrong topic - try the Raspberry Pi Forums. But here's a starting point: https://en.wikipedia.org/wiki/SocketCAN
haodongnj commentedon Jun 12, 2019
Hello,
I have tried the same way to use CAN bus with SPI2. I have already use the default mcp2515-can0(with SPI0.0) device tree file to enable CAN bus communication. However SPI2 just doesn't work.
Here is my pin map for another mcp2515:
I tried the test script on SPI2 and got all zeros:
00: 00 00 00 00 00 00 00 00
08: 00 00 00 00 00 00 00 00
10: 00 00 00 00 00 00 00 00
18: 00 00 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00
28: 00 00 00 00 00 00 00 00
30: 00 00 00 00 00 00 00 00
38: 00 00 00 00 00 00 00 00
The result on SPI0 is OK:
00: 00 00 00 00 00 00 00 00
08: b8 8f 9e eb 00 00 00 00
10: 50 00 00 00 00 00 00 00
18: 00 00 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00
28: 00 00 00 00 00 00 00 00
30: 00 00 00 00 00 00 00 00
38: 00 00 00 00 00 00 00 00
This means SPI0 works OK but SPI2 doesn't.
The dmesg log:
From the results above, SPI2 just doesn't work, is there any reason that may lead to this problem?
By the way , here are my configuration:
pelwell commentedon Jun 12, 2019
Fragment 3 looks wrong - you've chosen the 1cs version of the spi2 overlay, but
can1ismcp2515@1, which means it is going to try to use the second CS lines (numbering starts from 0). Try usingcan1: mcp2515@0 {instead.Also note that fragment 0 is redundant - without the
spi2-1csoverlay there would be nospi2label to target, and with it the node is already enabled.haodongnj commentedon Jun 13, 2019
Thank you very much for the correction! I have succeeded read data from can bus(can1) with SPI2!
I have set the spidev to be disabled or off :
dtoverlay=spi2-1cs,cs0_pin=43, cs0_spidev=disabledcan0 and can1 work fine but the message still exists:
dmesg | grep spi[ 4.949327] spi-bcm2835aux 3f2150c0.spi: chipselect 0 already in use
[ 4.949350] spi_master spi2: spi_device register error /soc/spi@7e2150c0/spidev@0
[ 4.949377] spi_master spi2: Failed to create SPI device for /soc/spi@7e2150c0/spidev@0
[ 6.347249] mcp251x spi0.0 can0: MCP2515 successfully initialized.
[ 6.359900] mcp251x spi2.0 can1: MCP2515 successfully initialized.
M-gre commentedon Jan 7, 2020
Hey, so I'm trying to use the SPI1.0 and SPI1.1 for a project with CAN bus.
I have enabled the SPI1 successfully, but when I'm not able to disable spidev on one of the two.
My config.txt :
When I now type
ls /dev/sp*the spidev still shows up on spi1.0Can you tell me why this happens and how to actually disable spidev for both?
pelwell commentedon Jan 7, 2020
The "-cs" suffix indicates how many chip selects to enable, not which chip select to enable. Loading the second overlay overwrites the first and reactivates spidev1.0.
I think what you want is:
By the way,
offalso works, i.e.dtoverlay=spi1-2cs,cs0_spidev=off,cs1_spidev=off.M-gre commentedon Jan 7, 2020
Yes, that managed to fix my issue. Thank you very much for the help!
alyzee-tiger commentedon Mar 26, 2021
Hi there,
i also tried to use the can2 on spi1 with a raspberry pi 3 using an mcp2515.
Here is my pin map : (verified multiple times)
i used the dtbo you provided here and it is in the /boot/overlay folder on my raspberry
The output of the python script looked fine with only 00s
here is my config.txt file :
Tho i dont know what this error is and how to solve it:
ps : it worked once yesterday, i turned it off and today it wasn't working anymore, i just re made all the steps from the start, but nothing helps
pelwell commentedon Mar 26, 2021
See #4183, which suggests it is a wiring/hardware problem.
alyzee-tiger commentedon Mar 26, 2021
ok, i have tried a few more things
i tried to wire it on the can0.0 (and 0.1), works just fine, i have been able to get the
MCP2515 successfully initializedmessage on both. It doesn't seem to come from the module.From there i re did all the steps :
but here when i try the python program, i now have random datas
and when i enable the mcp overlay i get this error :
UPDATE : from my testing a rebooted a few times without changing anything and i got the 110 error message too
do you think i might have a rpi hardware issue ? the mcp2515 looks fine when i connect it via spi0.*
GabyPCgeeK commentedon Sep 21, 2021
I've modified the
mcp251xfd-overlayfor the mcp2515mcp2515-overlay.dts.txt
I've done some testing with 2 mcp2515 but it would be better if more people could test it.
imthernet commentedon Feb 22, 2023
Thanks to all of you guys. I have been working on getting the mcp2515 on spi4 and with the tips in this post finally it works!!