EmbDev.net

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


von fuesel (Guest)


Attached files:

Rate this post
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.
1
#include <SPI.h>
2
3
#define CS SS
4
#define mcp_Reset 0xC0
5
#define mcp_WriteRegister 0x02
6
#define mcp_ReadRegister 0x03
7
8
9
#define mcp_CNF3 0x28
10
#define mcp_CNF2 0x29
11
#define mcp_CNF1 0x2A
12
#define mcp_BFPCTRL 0x0C
13
#define mcp_TXRTSCTRL 0x0D
14
#define mcp_RXB0CTRL 0x60
15
#define mcp_RXB1CTRL 0x70
16
#define mcp_CANCTRL 0x0F
17
#define mcp_CANSTAT 0x0E
18
#define mcp_CANINTE 0x2B
19
#define mcp_CANINTF 0x2C
20
#define mcp_RXSTATUS 0xB0
21
#define mcp_STATUS 0xA0
22
#define mcp_READRXB0SIDH 0x90
23
#define mcp_READRXB1SIDH 0x94
24
#define mcp_EFLG 0x2D
25
#define mcp_RXERRC 0x1D
26
#define mcp_MODE_NORMAL 0
27
#define mcp_MODE_LISTENONLY 3
28
#define mcp_MODE_CONFIG 4
29
30
31
#define set_CNF3 0x02 //0x02
32
#define set_CNF2 0x90 //0x90
33
#define set_CNF1 0x01
34
#define set_CANINTE 0x03
35
36
#define set_BFPCTRL 0x00
37
#define set_TXRTSCTRL 0x00
38
#define set_RXB0CTRL 0x60
39
#define set_RXB1CTRL 0x60
40
#define set_CANCTRL 0x00
41
#define set_CANCTRL_LSO 0x60
42
#define set_CANCTRL_CONFIG 0x80
43
44
int rxstat, msg_id, msg_length, msg_RTR, msg_data[8];
45
46
void setup() {
47
//  pinMode(2, INPUT);
48
  Serial.begin(115200);
49
  delay(5000);
50
  pinMode(CS, OUTPUT);
51
  digitalWrite(CS, HIGH);
52
  SPI.begin();
53
  SPI.setFrequency(4000);
54
  SPI.setCPOL(1);
55
  SPI.setCPHA(1);
56
  //  spirw(mcp_Reset); // reset the MCP2515
57
//  delayMicroseconds(10);
58
59
  while((ReadRegister(mcp_CANSTAT)>>5) != mcp_MODE_CONFIG) {
60
    Serial.print("Not in configuration mode");
61
    WriteRegister(mcp_CANCTRL, set_CANCTRL_CONFIG);
62
    delayMicroseconds(10);
63
  }
64
65
  digitalWrite(CS, LOW);
66
  SPI.transfer(mcp_WriteRegister);  // write to register command
67
  SPI.transfer(mcp_CNF3); // start at CNF3
68
  SPI.transfer(set_CNF3); // settings for CNFx:
69
  SPI.transfer(set_CNF2);
70
  SPI.transfer(set_CNF1);
71
  SPI.transfer(set_CANINTE);  // settings for CANINTE
72
  digitalWrite(CS, HIGH);
73
74
  // Serial.println(ReadRegister(mcp_CNF2), BIN);
75
76
  WriteRegister(mcp_BFPCTRL, set_BFPCTRL);
77
  
78
  WriteRegister(mcp_TXRTSCTRL, set_TXRTSCTRL);
79
80
  WriteRegister(mcp_RXB0CTRL, set_RXB0CTRL);
81
82
  WriteRegister(mcp_RXB1CTRL, set_RXB1CTRL);
83
  
84
  WriteRegister(mcp_CANCTRL, set_CANCTRL_LSO);
85
  
86
  while((ReadRegister(mcp_CANSTAT)>>5) != mcp_MODE_LISTENONLY) {
87
    Serial.print("Not in listen only mode");
88
    WriteRegister(mcp_CANCTRL, set_CANCTRL_LSO);
89
    delayMicroseconds(10);
90
  }
91
  Serial.print("CAN init ok");
92
}
93
94
void loop() {
95
  rxstat = ReadWrite(mcp_RXSTATUS);
96
97
  if(bitRead(rxstat, 6)) { // Message in RXB0
98
    digitalWrite(CS, LOW);
99
    SPI.transfer(mcp_READRXB0SIDH);
100
    msg_id = SPI.transfer(0x00) << 3;
101
    msg_id |= SPI.transfer(0x00) >> 5;
102
    SPI.transfer(0x00);
103
    SPI.transfer(0x00);
104
    msg_length = SPI.transfer(0x00) & 0x0F;
105
    msg_RTR = bitRead(rxstat, 3);
106
    for(int i = 0; i < msg_length; i++) {
107
      msg_data[i] = SPI.transfer(0x00);
108
    }
109
    digitalWrite(CS, HIGH);
110
    PrintData();
111
  }
112
  if(bitRead(rxstat, 7)) { // Message in RXB1
113
    digitalWrite(CS, LOW);
114
    SPI.transfer(mcp_READRXB1SIDH);
115
    msg_id = SPI.transfer(0x00) << 3;
116
    msg_id |= SPI.transfer(0x00) >> 5;
117
    SPI.transfer(0x00);
118
    SPI.transfer(0x00);
119
    msg_length = SPI.transfer(0x00) & 0x0F;
120
    msg_RTR = bitRead(rxstat, 3);
121
    for(int i = 0; i < msg_length; i++) {
122
      msg_data[i] = SPI.transfer(0x00);
123
    }
124
    digitalWrite(CS, HIGH);
125
    PrintData();
126
  }
127
  
128
}
129
130
int Read() {
131
  digitalWrite(CS, LOW);
132
  int _datain = SPI.transfer(0x00);
133
  digitalWrite(CS, HIGH);
134
  return _datain;
135
}
136
137
int ReadWrite(int _dataout) {
138
  digitalWrite(CS, LOW);
139
  SPI.transfer(_dataout);
140
  int _datain = SPI.transfer(0x00);
141
  digitalWrite(CS, HIGH);
142
  return _datain;
143
}
144
145
void WriteRegister(int _address, int _dataout) {
146
  digitalWrite(CS, LOW);
147
  SPI.transfer(mcp_WriteRegister);
148
  SPI.transfer(_address);
149
  SPI.transfer(_dataout);
150
  digitalWrite(CS, HIGH);
151
}
152
153
int ReadRegister(int _address) {
154
  digitalWrite(CS, LOW);
155
  SPI.transfer(mcp_ReadRegister);
156
  SPI.transfer(_address);
157
  int _datain = SPI.transfer(0x00);
158
  digitalWrite(CS, HIGH);
159
  return _datain;
160
}
161
162
void PrintData() {
163
  Serial.print("ID: ");
164
  Serial.print(msg_id);
165
  Serial.print(" ");
166
  Serial.print("Data: ");
167
  for(int i = 0; i < msg_length; i++) {
168
    Serial.print(msg_data[i]);
169
    Serial.print(" ");
170
  }
171
  Serial.print("\r\n");
172
}

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.