-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCAN_IO.cpp
More file actions
444 lines (379 loc) · 11 KB
/
CAN_IO.cpp
File metadata and controls
444 lines (379 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* CAN_IO.cpp
* Implementation of CAN_IO class.
*/
#include "CAN_IO.h"
#include <SPI.h>
CAN_IO::CAN_IO(byte CS_pin, byte INT_p, int baud, byte freq) : INT_pin(INT_p), controller(CS_pin, INT_p), bus_speed(baud), bus_freq(freq),
tec(0), rec(0), errors(0) {}
/*
* Define global interrupt function
*/
void CAN_ISR()
{
main_CAN->Fetch();
main_CAN->int_counter++;
}
// Make sure to initialize the mainCAN pointer to 0 here.
CAN_IO *main_CAN = 0;
/*
* Setup function for CAN_IO. Arguments are a FilterInfo struct and a pointer to a place to raise error flags.
*/
void CAN_IO::Setup(byte interrupts)
{ // default interrupts are RX0IE | RX1IE | TX1IE | TX2IE | TX0IE.
// SPI setup
SPI.setClockDivider(10);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.begin();
// reset tx tracker
tx_open = 0x07;
// Set as main can
main_CAN = this;
pinMode(INT_pin, INPUT_PULLUP);
// Copy filters and interrupts to internal variables
this->my_interrupts = interrupts;
// init the controller
init_controller(); //private helper function
}
inline void CAN_IO::init_controller() //private helper function
{
// Clear error counters
this->errors = 0;
this->tec = 0;
this->rec = 0;
int baudRate = controller.Init(this->bus_speed, this->bus_freq, 1); //SJW of 1
if (baudRate <= 0)
{ // error
this->errors |= CANERR_SETUP_BAUDFAIL;
if (Serial)
Serial.println(F("Baud ERROR"));
}
// return controller to config mode
if (!controller.Mode(MODE_CONFIG))
{ // error
this->errors |= CANERR_SETUP_MODEFAIL;
if (Serial)
Serial.println(F("Mode ERROR"));
}
// disable interrupts we don't care about
controller.Write(CANINTE, this->my_interrupts);
// config RX masks/filters
write_rx_filter(RXM0SIDH, this->filters.RXM0, this->filters.eidM0);
write_rx_filter(RXF1SIDH, this->filters.RXF1, this->filters.eidM0);
write_rx_filter(RXF2SIDH, this->filters.RXF2, this->filters.eidM0);
write_rx_filter(RXM1SIDH, this->filters.RXM1, this->filters.eidM1);
write_rx_filter(RXF3SIDH, this->filters.RXF3, this->filters.eidM1);
write_rx_filter(RXF4SIDH, this->filters.RXF4, this->filters.eidM1);
write_rx_filter(RXF5SIDH, this->filters.RXF5, this->filters.eidM1);
write_rx_filter(RXF0SIDH, this->filters.RXF0, this->filters.eidM1);
// return controller to normal mode
if (!controller.Mode(MODE_NORMAL))
{ // error
this->errors |= CANERR_SETUP_MODEFAIL;
}
}
bool CAN_IO::Sleep()
{
return controller.Mode(MODE_SLEEP);
}
bool CAN_IO::Wake()
{
controller.BitModify(CANINTF, WAKIF, WAKIF); // Set the WAKEIF bit to request that the controller wake up.
noInterrupts();
delayMicroseconds(5000); //Wait for it to run the start-up timer
interrupts();
return controller.Mode(MODE_NORMAL); // The device wakes up in listen-only mode. Put it back in normal (we may have to clear the TX registers)
}
void CAN_IO::ResetController()
{
this->init_controller(); // Re-initialize the controller.
}
void CAN_IO::Fetch()
{
// read status of CANINTF register
if (!controller.Interrupt())
return; // Do nothing if there is not an interrupt
byte interrupt = controller.GetInterrupt(); // Otherwise get the interrupt from the controller and process it.
byte to_clear = 0;
//Serial.print("FETCH ");
//Serial.println(interrupt,BIN);
if (interrupt == 0)
{
this->errors |= CANERR_EMPTY_INTERRUPT;
}
else
{
this->errors &= ~CANERR_EMPTY_INTERRUPT;
this->last_interrupt = interrupt;
// Note: Not all interrupts may be enabled. We add all the important ones here in case
// you want to use them. Enabling interrupts other than the RXnIF interrupts may cause
// certain microcontrollers *cough* arduino *cough* to freeze if a bus error happens.
// It is recommended that only the RXnIF interrupts be enabled and the rest of the can
// be read by periodically calling FetchErrors().
// Get Messages
if (interrupt & (RX0IF | RX1IF))
{
if (interrupt & RX1IF)
{ // receive buffer 1 full
RXbuffer.enqueue(controller.ReadBuffer(RXB1));
to_clear |= RX1IF;
}
if (interrupt & RX0IF)
{ // receive buffer 0 full
RXbuffer.enqueue(controller.ReadBuffer(RXB0));
to_clear |= RX0IF;
}
if (RXbuffer.is_full())
errors |= CANERR_RXBUFFER_FULL;
else
errors &= ~CANERR_RXBUFFER_FULL;
}
// Handle any other interrupts that might be flagged.
if (interrupt & MERRF)
{ // message error
this->errors |= CANERR_MESSAGE_ERROR;
to_clear |= MERRF;
}
else
this->errors &= (~CANERR_MESSAGE_ERROR);
if (interrupt & WAKIF)
{ // wake-up interrupt
// No Error implemented
to_clear |= WAKIF;
}
if (interrupt & ERRIF)
{ // error interrupt
this->FetchErrors();
to_clear |= ERRIF;
}
if (interrupt & TX2IF)
{ // transmit buffer 2 empty
tx_open |= TXB2;
to_clear |= TX2IF;
}
if (interrupt & TX1IF)
{ // transmit buffer 1 empty
tx_open |= TXB1;
to_clear |= TX1IF;
}
if (interrupt & TX0IF)
{ // transmit buffer 0 empty
tx_open |= TXB0;
to_clear |= TX0IF;
}
}
// clear interrupt
controller.ResetInterrupt(to_clear); // reset all interrupts
}
void CAN_IO::FetchErrors()
{
this->tec = controller.Read(TEC);
this->rec = controller.Read(REC);
byte eflg = controller.Read(EFLG);
if (eflg & 0x01) // If EWARN flag is set
{
if (eflg & 0x20) // if busmode flag is set
this->errors |= CANERR_BUSOFF_MODE;
else
this->errors &= (~CANERR_BUSOFF_MODE);
if (this->tec > 135 || this->rec > 135 || errors & CANERR_BUSOFF_MODE) // If any TX/RX errors have occured, raise this flag.
this->errors |= CANERR_HIGH_ERROR_COUNT;
else
this->errors &= ~CANERR_HIGH_ERROR_COUNT;
// Receive errors
if (eflg & 0x40) // if RX0OVR
this->errors |= CANERR_RX0FULL_OCCURED;
else
this->errors &= ~CANERR_RX0FULL_OCCURED;
if (eflg & 0x80) // if RX1OVR
this->errors |= CANERR_RX1FULL_OCCURED;
else
this->errors &= ~CANERR_RX1FULL_OCCURED;
if (eflg & 0xC0) // if RXnOVR
controller.BitModify(EFLG, 0xC0, 0x00); // Clear RXnOVR bits
}
else
errors &= ~(CANERR_HIGH_ERROR_COUNT & CANERR_BUSOFF_MODE & CANERR_RX0FULL_OCCURED & CANERR_RX1FULL_OCCURED);
}
void CAN_IO::FetchStatus()
{
this->canstat_register = controller.Read(CANSTAT);
}
bool CAN_IO::SendVerified(const Layout &layout, uint8_t buffer)
{
// The TXBANY buffer can be specified to allow the program to choose which buffer to send from.
// The TXnIE interrupt flags should be enabled for this to work properly.
if (buffer == TXBANY)
{
buffer = select_open_buffer();
}
if (buffer == 0x00)
{
return false;
} // Fail
if (!controller.LoadBuffer(buffer, layout.generate_frame(), true))
{
Serial.println(F("LOAD FAILED"));
return false;
}
else
controller.SendBuffer(buffer);
//set a flag in the tx_open bitfield that this buffer is closed.
//It will clear on the first interrupt received after the buffer finishes sending
//For best performance, enable all TXnIE flags.
tx_open &= ~buffer;
return true;
}
bool CAN_IO::SendVerified(const Frame &frame, uint8_t buffer)
{
// The TXBANY buffer can be specified to allow the program to choose which buffer to send from.
// The TXnIE interrupt flags should be enabled for this to work properly.
if (buffer == TXBANY)
{
buffer = select_open_buffer();
}
if (buffer == 0x00)
{
return false;
} // Fail
if (!controller.LoadBuffer(buffer, frame, true))
{
Serial.println(F("LOAD FAILED"));
return false;
}
else
controller.SendBuffer(buffer);
//set a flag in the tx_open bitfield that this buffer is closed.
//It will clear on the first interrupt received after the buffer finishes sending
//For best performance, enable all TXnIE flags.
tx_open &= ~buffer;
return true;
}
bool CAN_IO::Send(const Layout &layout, uint8_t buffer)
{
// The TXBANY buffer can be specified to allow the program to choose which buffer to send from.
// The TXnIE interrupt flags should be enabled for this to work properly.
if (buffer == TXBANY)
{
buffer = select_open_buffer();
}
if (buffer == 0x00)
{
return false;
} // Fail
controller.LoadBuffer(buffer, layout.generate_frame());
controller.SendBuffer(buffer);
//set a flag in the tx_open bitfield that this buffer is closed.
//It will clear on the first interrupt received after the buffer finishes sending
//For best performance, enable all TXnIE flags.
tx_open &= ~buffer;
return true;
}
bool CAN_IO::Send(const Frame &frame, uint8_t buffer)
{
// The TXBANY buffer can be specified to allow the program to choose which buffer to send from.
// The TXnIE interrupt flags should be enabled for this to work properly.
if (buffer == TXBANY)
{
buffer = select_open_buffer();
}
if (buffer == 0x00)
{
return false;
} // Fail
controller.LoadBuffer(buffer, frame);
controller.SendBuffer(buffer);
//set a flag in the tx_open bitfield that this buffer is closed.
//It will clear on the first interrupt received after the buffer finishes sending
//For best performance, enable all TXnIE flags.
tx_open &= ~buffer;
return true;
}
// RX filters for Standard IDs (SID)
// Define two macros for the following function, to improve readability.
void CAN_IO::write_rx_filter(uint8_t address, uint16_t data)
{
// FOR LEGACY, GENERATES SID
write_rx_filter(address, data, false);
}
#define B1_S(value) uint8_t((value >> 3) & 0x00FF) //11111111
#define B2_S(value) uint8_t((value << 5) & 0x00E0) //11100000
#define B1_E(value) uint8_t((value >> (29 - 8)) & 0x00FF)
// For extended case (EID), need to set EID flag
#define B2_E(value) uint8_t((value >> (29 - 16) & B11100000) | B00001000 | (value >> (29-13)) & B111)
#define B3_E(value) uint8_t((value >> (29 - 21)) & 0x00FF)
#define B4_E(value) uint8_t(value & 0x00FF)
void CAN_IO::write_rx_filter(uint8_t address, uint32_t data, bool eid)
{
uint8_t bytes[4] = {}; // initialize to all 0s
if (eid) // Extended ID
{
bytes[0] = B1_E(data);
bytes[1] = B2_E(data);
bytes[2] = B3_E(data);
bytes[3] = B4_E(data);
}
else
{
bytes[0] = B1_S(data);
bytes[1] = B2_S(data);
}
controller.Write(address, bytes, 4);
}
// For writing mask of EIDs
void CAN_IO::write_rx_mask(uint8_t address, uint32_t data, bool eid)
{
uint8_t bytes[4] = {}; // initialize to all 0s
if (eid)
{
bytes[0] = B1_E(data);
bytes[1] = B2_E(data);
bytes[2] = B3_E(data);
bytes[3] = B4_E(data);
// Maks do not have EID flag
bytes[1] = uint8_t(bytes[1] & ~B00001000);
}
else // Same procedure as RX Filter
{
bytes[0] = B1_S(data);
bytes[1] = B2_S(data);
}
controller.Write(address, bytes, 4);
}
inline uint8_t CAN_IO::select_open_buffer()
{
if (this->tx_open & TXB0)
return TXB0;
else if (this->tx_open & TXB1)
return TXB1;
else if (this->tx_open & TXB2)
return TXB2;
else
return 0x00; //Failure
}
bool CAN_IO::ConfigureInterrupts(byte interrupts)
{
if (controller.Mode(MODE_CONFIG))
{
controller.Write(CANINTE, interrupts);
if (controller.Mode(MODE_NORMAL))
{
my_interrupts = interrupts;
return true;
}
else
return false;
}
else
return false;
}
void CAN_IO::setAutoFetch(bool set)
{
if (set)
attachInterrupt(INT_pin, CAN_ISR, LOW);
else
detachInterrupt(INT_pin);
}
#undef first_byte
#undef second_byte