EmbDev.net

Forum: µC & Digital Electronics RFduino and SparkFun CAN bus shield (MCP2515)


Author: fuesel (Guest)
Posted on:
Attached files:

Rate this post
0 useful
not useful
Hi everyone

I am having real trouble with my RFduino/MCP2515 combination. I 
originally tried the code with libraries on an Arduino Leonardo and it 
worked after some tries. I tried to use the exact same code with my 
RFduino, but it failed. I could see the data sent over SPI with my logic 
analyser to the MCP2515 and it responding to the RFduino. I tested by 
writing registers, then read it back and there is no problem at all. But 
I cannot receive any CAN messages, in fact the flags in the RX status 
are never set. I now try to use it in listen-only mode for debugging.
What bothers me is that I had a look on the SPI data exchange with my 
logic analyser and it is slightly different: I send the same data, but 
get back a lot of chatter with my Leonardo, but not with the RFduino. 
(See attached pictures) Else I also used the same configuration data. 
Just to be sure I recalculated the CNF3,2,1 data with the Microchip 
Timing Calculator (and tried other settings). The only thing which is 
different is the 3.3V supply from the RFduino vs the 5V from the 
Leonardo. But the MCP2515 is rated down to 2.8V!

So in short:
MCP2515 works in terms of reading and writing registers and changing 
mode
CAN reception works on my bike, but only with Leonardo.

Data
http://ww1.microchip.com/downloads/en/DeviceDoc/21801d.pdf
https://www.sparkfun.com/products/10039

If anybody has an idea what to try, I would be really grateful.

The code attached only depends on the SPI library, I wrote it to 
eliminate any problems coming from the libraries I used.
#include <SPI.h>

#define CS SS
#define mcp_Reset 0xC0
#define mcp_WriteRegister 0x02
#define mcp_ReadRegister 0x03


#define mcp_CNF3 0x28
#define mcp_CNF2 0x29
#define mcp_CNF1 0x2A
#define mcp_BFPCTRL 0x0C
#define mcp_TXRTSCTRL 0x0D
#define mcp_RXB0CTRL 0x60
#define mcp_RXB1CTRL 0x70
#define mcp_CANCTRL 0x0F
#define mcp_CANSTAT 0x0E
#define mcp_CANINTE 0x2B
#define mcp_CANINTF 0x2C
#define mcp_RXSTATUS 0xB0
#define mcp_STATUS 0xA0
#define mcp_READRXB0SIDH 0x90
#define mcp_READRXB1SIDH 0x94
#define mcp_EFLG 0x2D
#define mcp_RXERRC 0x1D
#define mcp_MODE_NORMAL 0
#define mcp_MODE_LISTENONLY 3
#define mcp_MODE_CONFIG 4


#define set_CNF3 0x02 //0x02
#define set_CNF2 0x90 //0x90
#define set_CNF1 0x01
#define set_CANINTE 0x03

#define set_BFPCTRL 0x00
#define set_TXRTSCTRL 0x00
#define set_RXB0CTRL 0x60
#define set_RXB1CTRL 0x60
#define set_CANCTRL 0x00
#define set_CANCTRL_LSO 0x60
#define set_CANCTRL_CONFIG 0x80

int rxstat, msg_id, msg_length, msg_RTR, msg_data[8];

void setup() {
//  pinMode(2, INPUT);
  Serial.begin(115200);
  delay(5000);
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
  SPI.begin();
  SPI.setFrequency(4000);
  SPI.setCPOL(1);
  SPI.setCPHA(1);
  //  spirw(mcp_Reset); // reset the MCP2515
//  delayMicroseconds(10);

  while((ReadRegister(mcp_CANSTAT)>>5) != mcp_MODE_CONFIG) {
    Serial.print("Not in configuration mode");
    WriteRegister(mcp_CANCTRL, set_CANCTRL_CONFIG);
    delayMicroseconds(10);
  }

  digitalWrite(CS, LOW);
  SPI.transfer(mcp_WriteRegister);  // write to register command
  SPI.transfer(mcp_CNF3); // start at CNF3
  SPI.transfer(set_CNF3); // settings for CNFx:
  SPI.transfer(set_CNF2);
  SPI.transfer(set_CNF1);
  SPI.transfer(set_CANINTE);  // settings for CANINTE
  digitalWrite(CS, HIGH);

  // Serial.println(ReadRegister(mcp_CNF2), BIN);

  WriteRegister(mcp_BFPCTRL, set_BFPCTRL);
  
  WriteRegister(mcp_TXRTSCTRL, set_TXRTSCTRL);

  WriteRegister(mcp_RXB0CTRL, set_RXB0CTRL);

  WriteRegister(mcp_RXB1CTRL, set_RXB1CTRL);
  
  WriteRegister(mcp_CANCTRL, set_CANCTRL_LSO);
  
  while((ReadRegister(mcp_CANSTAT)>>5) != mcp_MODE_LISTENONLY) {
    Serial.print("Not in listen only mode");
    WriteRegister(mcp_CANCTRL, set_CANCTRL_LSO);
    delayMicroseconds(10);
  }
  Serial.print("CAN init ok");
}

void loop() {
  rxstat = ReadWrite(mcp_RXSTATUS);

  if(bitRead(rxstat, 6)) { // Message in RXB0
    digitalWrite(CS, LOW);
    SPI.transfer(mcp_READRXB0SIDH);
    msg_id = SPI.transfer(0x00) << 3;
    msg_id |= SPI.transfer(0x00) >> 5;
    SPI.transfer(0x00);
    SPI.transfer(0x00);
    msg_length = SPI.transfer(0x00) & 0x0F;
    msg_RTR = bitRead(rxstat, 3);
    for(int i = 0; i < msg_length; i++) {
      msg_data[i] = SPI.transfer(0x00);
    }
    digitalWrite(CS, HIGH);
    PrintData();
  }
  if(bitRead(rxstat, 7)) { // Message in RXB1
    digitalWrite(CS, LOW);
    SPI.transfer(mcp_READRXB1SIDH);
    msg_id = SPI.transfer(0x00) << 3;
    msg_id |= SPI.transfer(0x00) >> 5;
    SPI.transfer(0x00);
    SPI.transfer(0x00);
    msg_length = SPI.transfer(0x00) & 0x0F;
    msg_RTR = bitRead(rxstat, 3);
    for(int i = 0; i < msg_length; i++) {
      msg_data[i] = SPI.transfer(0x00);
    }
    digitalWrite(CS, HIGH);
    PrintData();
  }
  
}

int Read() {
  digitalWrite(CS, LOW);
  int _datain = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  return _datain;
}

int ReadWrite(int _dataout) {
  digitalWrite(CS, LOW);
  SPI.transfer(_dataout);
  int _datain = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  return _datain;
}

void WriteRegister(int _address, int _dataout) {
  digitalWrite(CS, LOW);
  SPI.transfer(mcp_WriteRegister);
  SPI.transfer(_address);
  SPI.transfer(_dataout);
  digitalWrite(CS, HIGH);
}

int ReadRegister(int _address) {
  digitalWrite(CS, LOW);
  SPI.transfer(mcp_ReadRegister);
  SPI.transfer(_address);
  int _datain = SPI.transfer(0x00);
  digitalWrite(CS, HIGH);
  return _datain;
}

void PrintData() {
  Serial.print("ID: ");
  Serial.print(msg_id);
  Serial.print(" ");
  Serial.print("Data: ");
  for(int i = 0; i < msg_length; i++) {
    Serial.print(msg_data[i]);
    Serial.print(" ");
  }
  Serial.print("\r\n");
}

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.