I want to connect the rasp with the can bus via mcp2515.
I have installed the latest version raspbian(4.19.57), and there are sth wrong.
I have a sample successfully run on the old version OS(4.4.x), everything is ok, but when I run "sudo apt-get update & sudo apt-get upgrade" or I reinstall the latest version OS, the code can't read the mcp2515's register data.
And I make a simple exam, one raspberry with old version OS img(backup img file), and one with the latest version OS img(both rasp are pi 3B).
old-one(kernal 4.4.x) ---------[0x01,0x02,0x03,0x04]--------> latest-one(kernal 4.19.57) : get the interrupt but can't read the data(which shows [0])
latest-one ----------[0x05,0x06,0x07,0x08]--------> old-one : get the interrupt and read the right data
The old one can read and send the right data , but the latest one can send but can't read the data.
I find the function spidev.xfer2() couldn't get the right data, so what should I do?
following are my code
Code: Select all
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#ECU
import spidev,time
import sys,cmd,shlex,types
import os,threading,signal
import RPi.GPIO as gpio
from mcp2515 import *
from time import ctime
spi = spidev.SpiDev(0,0)
print(spi.max_speed_hz)
def mcp2515_reset():
tmpc = [0xc0]
spi.writebytes(tmpc)
def mcp2515_writeReg(addr, val):
buf = [0x02, addr, val]
spi.writebytes(buf)
def mcp2515_readReg(addr):
buf = [0x03, addr, 0x55]
buf = spi.xfer2(buf)
return int(buf[2])
def mcp2515_init():
gpio.setup(26,gpio.OUT)
mcp2515_reset()
time.sleep(2)
#mcp2515_writeReg(CNF1,0x01)
#mcp2515_writeReg(CNF2,0x9d)
#mcp2515_writeReg(CNF3,0x44)
#CNF1 xx xxxxxx SJW BRP Fosc 8MHz
#CNF2 xx xxx xxx
#CNF3 xxxxx xxx
#NBR = 1/NBT
#TQ = 2*(BRP + 1)/Fosc
#NBT/TQ = 1 + ( CNF2<2:0>+1 ) + ( CNF2<5:3>+1 ) + ( CNF3<2:0>+1 )
#NBR = Fosc / [ 2 * (BRP+1) * ( 1 + ( CNF2<2:0>+1 ) + ( CNF2<5:3>+1 ) + ( CNF3<2:0>+1 ) ) ]
mcp2515_writeReg(CNF1,CAN_125Kbps);
print(CAN_125Kbps)
mcp2515_writeReg(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
print(0x80|PHSEG1_3TQ|PRSEG_1TQ)
mcp2515_writeReg(CNF3,PHSEG2_3TQ);
print(PHSEG2_3TQ)
# 125kbps
mcp2515_writeReg(RXB0SIDH,0x00) #
mcp2515_writeReg(RXB0SIDL,0x00) #
mcp2515_writeReg(RXB0EID8,0x00) #
mcp2515_writeReg(RXB0EID0,0x00) #
mcp2515_writeReg(RXB0CTRL,0x60) # 0 10 0 0 0 0 0
print(mcp2515_readReg(RXB0CTRL))
mcp2515_writeReg(RXB0DLC,DLC_8) #
#
mcp2515_writeReg(RXF0SIDH,0x00) #00000000 #
mcp2515_writeReg(RXF0SIDL,0x00) #001 0 0 0 11 #
mcp2515_writeReg(RXF0EID8,0x00) #11111111 #
mcp2515_writeReg(RXF0EID0,0x00) #11111111 #
mcp2515_writeReg(RXM0SIDH,0xFF) #1111 1111 #
mcp2515_writeReg(RXM0SIDL,0xE3) #111 0 0 0 11 #
mcp2515_writeReg(RXM0EID8,0xFF) #1111 1111 #
mcp2515_writeReg(RXM0EID0,0xFF) #1111 1111 #
mcp2515_writeReg(CANINTF,0x00) #
mcp2515_writeReg(CANINTE,0x01) #
mcp2515_writeReg(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED) #
print("\r\nMCP2515 Initialized.\r\n")
print(mcp2515_readReg(RXB0CTRL))
def mcp2515_write(buf,addr):
if mcp2515_readReg(TXB0CTRL)&0x10:
print("send message error")
print(mcp2515_readReg(TXB0CTRL))
print(mcp2515_readReg(CANINTF))
print(mcp2515_readReg(EFLG))
mcp2515_writeReg(TXB0CTRL,0x00)
for i in range(50):
time.sleep(2)
if not mcp2515_readReg(TXB0CTRL)&0x08:
break
mcp2515_writeReg(TXB0SIDH, addr[0]) #
mcp2515_writeReg(TXB0SIDL, addr[1]) #
mcp2515_writeReg(TXB0EID8, addr[2]) #
mcp2515_writeReg(TXB0EID0, addr[3]) #
N = len(buf)
for j in range(N):
mcp2515_writeReg(TXB0D0+j,buf[j])
mcp2515_writeReg(TXB0DLC,N)
mcp2515_writeReg(TXB0CTRL,0x08)
# 0 0 0 0 1 0 00
def mcp2515_read():
N = 0
buf = []
if mcp2515_readReg(CANINTF) & 0x01:
N = mcp2515_readReg(RXB0DLC)#
for i in range(N):
buf.append(mcp2515_readReg(RXB0D0+i))#
mcp2515_writeReg(CANINTF,0)#
return buf
gpio.setmode(gpio.BCM)
#set the interrupt
gpio.setup(21,gpio.IN)
def getorder_callBack(channel):
print("get the data")
buf2=mcp2515_read()
print("received data: ",buf2)
# recover the interrupt
mcp2515_writeReg(CANINTF,0x00) #
gpio.add_event_detect(21,gpio.FALLING,callback=getorder_callBack)
def quit(signum,frame):
print("You stop me")
sys.exit()
if __name__=='__main__':
try:
mcp2515_init()
while True:
buf1 = [1,2,3,4]
mcp2515_write(buf1,[0x00,0x20,0x00,0x00])
time.sleep(3)
finally:
gpio.cleanup()