-
Notifications
You must be signed in to change notification settings - Fork 6
/
mcp_can.cpp
533 lines (444 loc) · 23.4 KB
/
mcp_can.cpp
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include "mcp_can.h"
#include <IFCT.h>
#include <kinetis_flexcan.h>
#undef rtr
CAN_message_t storage_mcp_sim;
/*********************************************************************************************************
** Function name: MCP_CAN
** Descriptions: Constructor
*********************************************************************************************************/
MCP_CAN::MCP_CAN(byte _CS) : nReservedTx(0)
{
}
/*********************************************************************************************************
** Function name: set CS
** Descriptions: init CS pin and set UNSELECTED
*********************************************************************************************************/
void MCP_CAN::init_CS(byte _CS)
{
}
/*********************************************************************************************************
** Function name: begin
** Descriptions: init can and set speed
*********************************************************************************************************/
byte MCP_CAN::begin(uint32_t speedset, const byte clockset)
{
Can0.setBaudRate(speedset);
return CAN_OK;
}
/*********************************************************************************************************
** Function name: sendMsg
** Descriptions: send message
*********************************************************************************************************/
byte MCP_CAN::sendMsg(unsigned long id, byte extended, byte rtrBit, byte len, const byte *buf, bool wait_sent)
{
#define rtr flags.remote
CAN_message_t frame;
( rtrBit ) ? frame.rtr = 1 : frame.rtr = 0;
( extended ) ? frame.ext = 1 : frame.ext = 0;
frame.id = id;
frame.len = len;
memmove(&frame.buf[0],&buf[0],8);
Can0.write(frame);
return CAN_OK;
#undef rtr
}
/*********************************************************************************************************
** Function name: sendMsgBuf
** Descriptions: send buf
*********************************************************************************************************/
byte MCP_CAN::sendMsgBuf(unsigned long id, byte extended, byte rtrBit, byte len, const byte *buf, bool wait_sent)
{
return sendMsg(id,extended,rtrBit,len,buf,wait_sent);
}
/*********************************************************************************************************
** Function name: sendMsgBuf
** Descriptions: send buf
*********************************************************************************************************/
byte MCP_CAN::sendMsgBuf(unsigned long id, byte extended, byte len, const byte *buf, bool wait_sent)
{
return sendMsg(id,extended,0,len,buf,wait_sent);
}
/*********************************************************************************************************
** Function name: checkReceive
** Descriptions: check if got something
*********************************************************************************************************/
byte MCP_CAN::checkReceive(void)
{
if ( Can0.read(storage_mcp_sim) ) {
if ( !filter_masks[0] || !filter_masks[1] ) return CAN_MSGAVAIL; /* not all masks set */
for ( uint8_t i = 0; i < 6; i++ ) {
if ( !filter_filters[i] ) return CAN_MSGAVAIL; /* not all filters set */
if ( (( filter_masks[0] & storage_mcp_sim.id ) == filter_filters[i]) || (( filter_masks[1] & storage_mcp_sim.id ) == filter_filters[i]) ) {
if ( filter_ext_bit[i] == storage_mcp_sim.ext ) return CAN_MSGAVAIL;
return CAN_NOMSG;
}
}
}
return CAN_NOMSG;
}
/*********************************************************************************************************
** Function name: readMsgBuf
** Descriptions: read message buf
*********************************************************************************************************/
byte MCP_CAN::readMsgBuf(byte *len, byte buf[])
{
return readMsgBufID(readRxTxStatus(),&can_id,&ext_flg,&rtr,len,buf);
}
/*********************************************************************************************************
** Function name: readMsgBufID
** Descriptions: read message buf and can bus source ID
*********************************************************************************************************/
byte MCP_CAN::readMsgBufID(unsigned long *ID, byte *len, byte buf[])
{
return readMsgBufID(readRxTxStatus(),ID,&ext_flg,&rtr,len,buf);
}
/*********************************************************************************************************
** Function name: readMsgBufID
** Descriptions: Read message buf and can bus source ID according to status.
** Status has to be read with readRxTxStatus.
*********************************************************************************************************/
byte MCP_CAN::readMsgBufID(byte status, volatile unsigned long *id, volatile byte *extended, volatile byte *rtrBit, volatile byte *len, volatile byte *buf)
{
mcp2515_read_canMsg( MCP_READ_RX0, id, extended, rtrBit, len, buf);
return 0;
}
/*********************************************************************************************************
** Function name: mcp2515_read_canMsg
** Descriptions: read message
*********************************************************************************************************/
#undef rtr
#undef ext
void MCP_CAN::mcp2515_read_canMsg( const byte buffer_load_addr, volatile unsigned long *id, volatile byte *ext, volatile byte *rtrBit, volatile byte *len, volatile byte *buf) /* read can msg */
{
*id = storage_mcp_sim.id;
*ext = storage_mcp_sim.flags.extended;
*len = storage_mcp_sim.len;
*rtrBit = storage_mcp_sim.flags.remote;
for ( uint8_t i = 0; i < 8; i++ ) buf[i] = storage_mcp_sim.buf[i];
}
#define rtr flags.remote
#define ext flags.extended
/*********************************************************************************************************
** Function name: readRxTxStatus
** Descriptions: Read RX and TX interrupt bits. Function uses status reading, but translates.
** result to MCP_CANINTF. With this you can check status e.g. on interrupt sr
** with one single call to save SPI calls. Then use checkClearRxStatus and
** checkClearTxStatus for testing.
*********************************************************************************************************/
byte MCP_CAN::readRxTxStatus(void)
{
return 0;
}
/*********************************************************************************************************
** Function name: checkClearRxStatus
** Descriptions: Return first found rx CANINTF status and clears it from parameter.
** Note that this does not affect to chip CANINTF at all. You can use this
** with one single readRxTxStatus call.
*********************************************************************************************************/
byte MCP_CAN::checkClearRxStatus(byte *status)
{
return 0;
}
/*********************************************************************************************************
** Function name: checkClearTxStatus
** Descriptions: Return specified buffer of first found tx CANINTF status and clears it from parameter.
** Note that this does not affect to chip CANINTF at all. You can use this
** with one single readRxTxStatus call.
*********************************************************************************************************/
byte MCP_CAN::checkClearTxStatus(byte *status, byte iTxBuf)
{
return 0;
}
/*********************************************************************************************************
** Function name: clearBufferTransmitIfFlags
** Descriptions: Clear transmit interrupt flags for specific buffer or for all unreserved buffers.
** If interrupt will be used, it is important to clear all flags, when there is no
** more data to be sent. Otherwise IRQ will newer change state.
*********************************************************************************************************/
void MCP_CAN::clearBufferTransmitIfFlags(byte flags)
{
}
/*********************************************************************************************************
** Function name: checkError
** Descriptions: if something error
*********************************************************************************************************/
byte MCP_CAN::checkError(void)
{
return 0;
}
/*********************************************************************************************************
** Function name: getCanId
** Descriptions: when receive something, you can get the can id!!
*********************************************************************************************************/
unsigned long MCP_CAN::getCanId(void)
{
return can_id;
}
/*********************************************************************************************************
** Function name: isRemoteRequest
** Descriptions: when receive something, you can check if it was a request
*********************************************************************************************************/
byte MCP_CAN::isRemoteRequest(void)
{
#undef rtr
return rtr;
#define rtr flags.remote
}
/*********************************************************************************************************
** Function name: isExtendedFrame
** Descriptions: did we just receive standard 11bit frame or extended 29bit? 0 = std, 1 = ext
*********************************************************************************************************/
byte MCP_CAN::isExtendedFrame(void)
{
return ext_flg;
}
/*********************************************************************************************************
** Function name: sendMsgBuf
** Descriptions: Send message by using buffer read as free from CANINTF status
** Status has to be read with readRxTxStatus and filtered with checkClearTxStatus
*********************************************************************************************************/
byte MCP_CAN::sendMsgBuf(byte status, unsigned long id, byte extended, byte rtrBit, byte len, volatile const byte *buf)
{
CAN_message_t frame;
( rtrBit ) ? frame.rtr = 1 : frame.rtr = 0;
( extended ) ? frame.ext = 1 : frame.ext = 0;
frame.id = id;
frame.len = len;
for ( uint8_t i = 0; i < 8; i++ ) frame.buf[i] = buf[i];
Can0.write(frame);
return CAN_OK;
}
/*********************************************************************************************************
** Function name: trySendMsgBuf
** Descriptions: Try to send message. There is no delays for waiting free buffer.
*********************************************************************************************************/
byte MCP_CAN::trySendMsgBuf(unsigned long id, byte extended, byte rtrBit, byte len, const byte *buf, byte iTxBuf)
{
CAN_message_t frame;
( rtrBit ) ? frame.rtr = 1 : frame.rtr = 0;
( extended ) ? frame.ext = 1 : frame.ext = 0;
frame.id = id;
frame.len = len;
for ( uint8_t i = 0; i < 8; i++ ) frame.buf[i] = buf[i];
Can0.write(frame);
return CAN_OK;
}
/*********************************************************************************************************
** Function name: mcp2515_start_transmit
** Descriptions: Start message transmit on mcp2515
*********************************************************************************************************/
void MCP_CAN::mcp2515_start_transmit(const byte mcp_addr) // start transmit
{
}
/*********************************************************************************************************
** Function name: mcp2515_isTXBufFree
** Descriptions: Test is tx buffer free for transmitting
*********************************************************************************************************/
byte MCP_CAN::mcp2515_isTXBufFree(byte *txbuf_n, byte iBuf) /* get Next free txbuf */
{
return 0;
}
/*********************************************************************************************************
** Function name: mcp2515_getNextFreeTXBuf
** Descriptions: finds next free tx buffer for sending. Return MCP_ALLTXBUSY, if there is none.
*********************************************************************************************************/
byte MCP_CAN::mcp2515_getNextFreeTXBuf(byte *txbuf_n) // get Next free txbuf
{
return 0;
}
/*********************************************************************************************************
** Function name: enableTxInterrupt
** Descriptions: enable interrupt for all tx buffers
*********************************************************************************************************/
void MCP_CAN::enableTxInterrupt(bool enable)
{
}
/*********************************************************************************************************
** Function name: init_Mask
** Descriptions: init canid Masks
*********************************************************************************************************/
byte MCP_CAN::init_Mask(byte num, byte extended, unsigned long ulData)
{
filter_masks[constrain(num,0,1)] = ulData;
mask_ext_bit[constrain(num,0,1)] = extended;
return 1;
}
/*********************************************************************************************************
** Function name: init_Filt
** Descriptions: init canid filters
*********************************************************************************************************/
byte MCP_CAN::init_Filt(byte num, byte extended, unsigned long ulData)
{
filter_filters[constrain(num,0,5)] = ulData;
filter_ext_bit[constrain(num,0,5)] = extended;
return 1;
}
/*********************************************************************************************************
** Function name: mcp2515_read_id
** Descriptions: read can id
*********************************************************************************************************/
void MCP_CAN::mcp2515_read_id(const byte mcp_addr, byte* extended, unsigned long* id)
{
}
/*********************************************************************************************************
** Function name: mcp2515_write_canMsg
** Descriptions: write msg
** Note! There is no check for right address!
*********************************************************************************************************/
void MCP_CAN::mcp2515_write_canMsg(const byte buffer_sidh_addr, unsigned long id, byte extended, byte rtrBit, byte len, volatile const byte *buf)
{
}
/*********************************************************************************************************
** Function name: mcp2515_write_id
** Descriptions: write can id
*********************************************************************************************************/
void MCP_CAN::mcp2515_write_id(const byte mcp_addr, const byte extended, const unsigned long id)
{
}
/*********************************************************************************************************
** Function name: mcp2515_id_to_buf
** Descriptions: configure tbufdata[4] from id and ext
*********************************************************************************************************/
void mcp2515_id_to_buf(const byte extended, const unsigned long id, byte *tbufdata)
{
}
/*********************************************************************************************************
** Function name: mcp2515_init
** Descriptions: init the device
*********************************************************************************************************/
byte MCP_CAN::mcp2515_init(const byte canSpeed, const byte clock)
{
return 0;
}
/*********************************************************************************************************
** Function name: txCtrlReg
** Descriptions: return tx ctrl reg according to tx buffer index.
** According to my tests this is faster and saves memory compared using vector
*********************************************************************************************************/
byte txCtrlReg(byte i)
{
return 0;
}
/*********************************************************************************************************
** Function name: statusToBuffer
** Descriptions: converts CANINTF status to tx buffer index
*********************************************************************************************************/
byte statusToTxBuffer(byte status)
{
return 0;
}
/*********************************************************************************************************
** Function name: statusToBuffer
** Descriptions: converts CANINTF status to tx buffer sidh
*********************************************************************************************************/
byte statusToTxSidh(byte status)
{
return 0;
}
/*********************************************************************************************************
** Function name: txSidhToTxLoad
** Descriptions: return tx load command according to tx buffer sidh register
*********************************************************************************************************/
byte txSidhToRTS(byte sidh) {
return 0;
}
/*********************************************************************************************************
** Function name: txSidhToTxLoad
** Descriptions: return tx load command according to tx buffer sidh register
*********************************************************************************************************/
byte txSidhToTxLoad(byte sidh) {
return 0;
}
/*********************************************************************************************************
** Function name: txIfFlag
** Descriptions: return tx interrupt flag
*********************************************************************************************************/
byte txIfFlag(byte i) {
return 0;
}
/*********************************************************************************************************
** Function name: txStatusPendingFlag
** Descriptions: return buffer tx pending flag on status
*********************************************************************************************************/
byte txStatusPendingFlag(byte i) {
return 0;
}
/*********************************************************************************************************
** Function name: mcp2515_reset
** Descriptions: reset the device
*********************************************************************************************************/
void MCP_CAN::mcp2515_reset(void)
{
}
/*********************************************************************************************************
** Function name: mcp2515_readRegister
** Descriptions: read register
*********************************************************************************************************/
byte MCP_CAN::mcp2515_readRegister(const byte address)
{
return 0;
}
/*********************************************************************************************************
** Function name: mcp2515_readRegisterS
** Descriptions: read registerS
*********************************************************************************************************/
void MCP_CAN::mcp2515_readRegisterS(const byte address, byte values[], const byte n)
{
}
/*********************************************************************************************************
** Function name: mcp2515_setRegister
** Descriptions: set register
*********************************************************************************************************/
void MCP_CAN::mcp2515_setRegister(const byte address, const byte value)
{
}
/*********************************************************************************************************
** Function name: mcp2515_setRegisterS
** Descriptions: set registerS
*********************************************************************************************************/
void MCP_CAN::mcp2515_setRegisterS(const byte address, const byte values[], const byte n)
{
}
/*********************************************************************************************************
** Function name: mcp2515_modifyRegister
** Descriptions: set bit of one register
*********************************************************************************************************/
void MCP_CAN::mcp2515_modifyRegister(const byte address, const byte mask, const byte data)
{
}
/*********************************************************************************************************
** Function name: mcp2515_readStatus
** Descriptions: read mcp2515's Status
*********************************************************************************************************/
byte MCP_CAN::mcp2515_readStatus(void)
{
return 0;
}
/*********************************************************************************************************
** Function name: mcp2515_setCANCTRL_Mode
** Descriptions: set control mode
*********************************************************************************************************/
byte MCP_CAN::mcp2515_setCANCTRL_Mode(const byte newmode)
{
return 0;
}
/*********************************************************************************************************
** Function name: mcp2515_configRate
** Descriptions: set baudrate
*********************************************************************************************************/
byte MCP_CAN::mcp2515_configRate(const byte canSpeed, const byte clock)
{
Can0.setBaudRate(canSpeed);
return 0;
}
/*********************************************************************************************************
** Function name: mcp2515_initCANBuffers
** Descriptions: init canbuffers
*********************************************************************************************************/
void MCP_CAN::mcp2515_initCANBuffers(void)
{
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/