source: rtos_arduino/trunk/arduino_lib/libraries/NcesCan/mcp_can.cpp@ 260

Last change on this file since 260 was 260, checked in by ertl-honda, 8 years ago

マクロ名を更新.
実行モデルを変更.

File size: 31.2 KB
Line 
1/*
2 mcp_can.cpp
3 2012 Copyright (c) Seeed Technology Inc. All right reserved.
4
5 Author:Loovee
6 Contributor: Cory J. Fowler
7 2014-1-16
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-
21 1301 USA
22*/
23#include "mcp_can.h"
24
25#ifdef TOPPERS_WITH_ARDUINO
26#include "r2ca.h"
27#define ENTER_CRITICAL wai_sem(SPI_SEM);
28#define LEAVE_CRITICAL sig_sem(SPI_SEM);
29#else /* !TOPPERS_WITH_ARDUINO */
30#define WAIT_TIMEOUT
31#define ENTER_CRITICAL
32#define LEAVE_CRITICAL
33#endif /* TOPPERS_WITH_ARDUINO */
34
35#define spi_readwrite SPI.transfer
36#define spi_read() spi_readwrite(0x00)
37
38#define CHECK_RXBID(rxbid) do { \
39 if (!((rxbid == 0) || (rxbid == 1))) { \
40 return CAN_FAIL; \
41 } \
42} while(false)
43
44#define CHECK_LEN(len) do { \
45 if (len > 8) { \
46 return CAN_FAIL; \
47 } \
48} while(false)
49
50#define CHECK_TXBID(txbid) do { \
51 if (!((txbid == 0) || (txbid == 1) || (txbid == 2))) { \
52 return CAN_FAIL; \
53 } \
54} while(false)
55
56/*********************************************************************************************************
57** Function name: mcp2515_reset
58** Descriptions: reset the device
59*********************************************************************************************************/
60void MCP_CAN::mcp2515_reset(void)
61{
62 MCP2515_SELECT();
63 spi_readwrite(MCP_RESET);
64 MCP2515_UNSELECT();
65 delay(10);
66}
67
68/*********************************************************************************************************
69** Function name: mcp2515_readRegister
70** Descriptions: read register
71*********************************************************************************************************/
72INT8U MCP_CAN::mcp2515_readRegister(const INT8U address)
73{
74 INT8U ret;
75
76 MCP2515_SELECT();
77 spi_readwrite(MCP_READ);
78 spi_readwrite(address);
79 ret = spi_read();
80 MCP2515_UNSELECT();
81
82 return ret;
83}
84
85/*********************************************************************************************************
86** Function name: mcp2515_readRegisterS
87** Descriptions: read registerS
88*********************************************************************************************************/
89void MCP_CAN::mcp2515_readRegisterS(const INT8U address, INT8U values[], const INT8U n)
90{
91 INT8U i;
92 MCP2515_SELECT();
93 spi_readwrite(MCP_READ);
94 spi_readwrite(address);
95 // mcp2515 has auto-increment of address-pointer
96 for (i=0; i<n && i<CAN_MAX_CHAR_IN_MESSAGE; i++) {
97 values[i] = spi_read();
98 }
99 MCP2515_UNSELECT();
100}
101
102/*********************************************************************************************************
103** Function name: mcp2515_setRegister
104** Descriptions: set register
105*********************************************************************************************************/
106void MCP_CAN::mcp2515_setRegister(const INT8U address, const INT8U value)
107{
108 MCP2515_SELECT();
109 spi_readwrite(MCP_WRITE);
110 spi_readwrite(address);
111 spi_readwrite(value);
112 MCP2515_UNSELECT();
113}
114
115/*********************************************************************************************************
116** Function name: mcp2515_setRegisterS
117** Descriptions: set registerS
118*********************************************************************************************************/
119void MCP_CAN::mcp2515_setRegisterS(const INT8U address, const INT8U values[], const INT8U n)
120{
121 INT8U i;
122 MCP2515_SELECT();
123 spi_readwrite(MCP_WRITE);
124 spi_readwrite(address);
125
126 for (i=0; i<n; i++)
127 {
128 spi_readwrite(values[i]);
129 }
130 MCP2515_UNSELECT();
131}
132
133/*********************************************************************************************************
134** Function name: mcp2515_modifyRegister
135** Descriptions: set bit of one register
136*********************************************************************************************************/
137void MCP_CAN::mcp2515_modifyRegister(const INT8U address, const INT8U mask, const INT8U data)
138{
139 MCP2515_SELECT();
140 spi_readwrite(MCP_BITMOD);
141 spi_readwrite(address);
142 spi_readwrite(mask);
143 spi_readwrite(data);
144 MCP2515_UNSELECT();
145}
146
147/*********************************************************************************************************
148** Function name: mcp2515_readStatus
149** Descriptions: read mcp2515's Status
150*********************************************************************************************************/
151INT8U MCP_CAN::mcp2515_readStatus(void)
152{
153 INT8U i;
154 MCP2515_SELECT();
155 spi_readwrite(MCP_READ_STATUS);
156 i = spi_read();
157 MCP2515_UNSELECT();
158
159 return i;
160}
161
162/*********************************************************************************************************
163** Function name: mcp2515_setCANCTRL_Mode
164** Descriptions: set control mode
165*********************************************************************************************************/
166INT8U MCP_CAN::mcp2515_setCANCTRL_Mode(const INT8U newmode)
167{
168 INT8U i;
169
170 mcp2515_modifyRegister(MCP_CANCTRL, MODE_MASK, newmode);
171
172 i = mcp2515_readRegister(MCP_CANCTRL);
173 i &= MODE_MASK;
174
175 if ( i == newmode )
176 {
177 return MCP2515_OK;
178 }
179
180 return MCP2515_FAIL;
181
182}
183
184/*********************************************************************************************************
185** Function name: mcp2515_configRate
186** Descriptions: set boadrate
187*********************************************************************************************************/
188INT8U MCP_CAN::mcp2515_configRate(const INT8U canSpeed)
189{
190 INT8U set, cfg1, cfg2, cfg3;
191 set = 1;
192 switch (canSpeed)
193 {
194 case (CAN_5KBPS):
195 cfg1 = MCP_16MHz_5kBPS_CFG1;
196 cfg2 = MCP_16MHz_5kBPS_CFG2;
197 cfg3 = MCP_16MHz_5kBPS_CFG3;
198 break;
199
200 case (CAN_10KBPS):
201 cfg1 = MCP_16MHz_10kBPS_CFG1;
202 cfg2 = MCP_16MHz_10kBPS_CFG2;
203 cfg3 = MCP_16MHz_10kBPS_CFG3;
204 break;
205
206 case (CAN_20KBPS):
207 cfg1 = MCP_16MHz_20kBPS_CFG1;
208 cfg2 = MCP_16MHz_20kBPS_CFG2;
209 cfg3 = MCP_16MHz_20kBPS_CFG3;
210 break;
211
212 case (CAN_31K25BPS):
213 cfg1 = MCP_16MHz_31k25BPS_CFG1;
214 cfg2 = MCP_16MHz_31k25BPS_CFG2;
215 cfg3 = MCP_16MHz_31k25BPS_CFG3;
216 break;
217
218 case (CAN_33KBPS):
219 cfg1 = MCP_16MHz_33kBPS_CFG1;
220 cfg2 = MCP_16MHz_33kBPS_CFG2;
221 cfg3 = MCP_16MHz_33kBPS_CFG3;
222 break;
223
224 case (CAN_40KBPS):
225 cfg1 = MCP_16MHz_40kBPS_CFG1;
226 cfg2 = MCP_16MHz_40kBPS_CFG2;
227 cfg3 = MCP_16MHz_40kBPS_CFG3;
228 break;
229
230 case (CAN_50KBPS):
231 cfg1 = MCP_16MHz_50kBPS_CFG1;
232 cfg2 = MCP_16MHz_50kBPS_CFG2;
233 cfg3 = MCP_16MHz_50kBPS_CFG3;
234 break;
235
236 case (CAN_80KBPS):
237 cfg1 = MCP_16MHz_80kBPS_CFG1;
238 cfg2 = MCP_16MHz_80kBPS_CFG2;
239 cfg3 = MCP_16MHz_80kBPS_CFG3;
240 break;
241
242 case (CAN_83K3BPS):
243 cfg1 = MCP_16MHz_83k3BPS_CFG1;
244 cfg2 = MCP_16MHz_83k3BPS_CFG2;
245 cfg3 = MCP_16MHz_83k3BPS_CFG3;
246 break;
247
248 case (CAN_95KBPS):
249 cfg1 = MCP_16MHz_95kBPS_CFG1;
250 cfg2 = MCP_16MHz_95kBPS_CFG2;
251 cfg3 = MCP_16MHz_95kBPS_CFG3;
252 break;
253
254 case (CAN_100KBPS): /* 100KBPS */
255 cfg1 = MCP_16MHz_100kBPS_CFG1;
256 cfg2 = MCP_16MHz_100kBPS_CFG2;
257 cfg3 = MCP_16MHz_100kBPS_CFG3;
258 break;
259
260 case (CAN_125KBPS):
261 cfg1 = MCP_16MHz_125kBPS_CFG1;
262 cfg2 = MCP_16MHz_125kBPS_CFG2;
263 cfg3 = MCP_16MHz_125kBPS_CFG3;
264 break;
265
266 case (CAN_200KBPS):
267 cfg1 = MCP_16MHz_200kBPS_CFG1;
268 cfg2 = MCP_16MHz_200kBPS_CFG2;
269 cfg3 = MCP_16MHz_200kBPS_CFG3;
270 break;
271
272 case (CAN_250KBPS):
273 cfg1 = MCP_16MHz_250kBPS_CFG1;
274 cfg2 = MCP_16MHz_250kBPS_CFG2;
275 cfg3 = MCP_16MHz_250kBPS_CFG3;
276 break;
277
278 case (CAN_500KBPS):
279 cfg1 = MCP_16MHz_500kBPS_CFG1;
280 cfg2 = MCP_16MHz_500kBPS_CFG2;
281 cfg3 = MCP_16MHz_500kBPS_CFG3;
282 break;
283
284 case (CAN_1000KBPS):
285 cfg1 = MCP_16MHz_1000kBPS_CFG1;
286 cfg2 = MCP_16MHz_1000kBPS_CFG2;
287 cfg3 = MCP_16MHz_1000kBPS_CFG3;
288 break;
289
290 default:
291 set = 0;
292 break;
293 }
294
295 if (set) {
296 mcp2515_setRegister(MCP_CNF1, cfg1);
297 mcp2515_setRegister(MCP_CNF2, cfg2);
298 mcp2515_setRegister(MCP_CNF3, cfg3);
299 return MCP2515_OK;
300 }
301 else {
302 return MCP2515_FAIL;
303 }
304}
305
306/*********************************************************************************************************
307** Function name: mcp2515_initCANBuffers
308** Descriptions: init canbuffers
309*********************************************************************************************************/
310void MCP_CAN::mcp2515_initCANBuffers(void)
311{
312 INT8U i, a1, a2, a3;
313
314 INT8U std = 0;
315 INT8U ext = 1;
316 INT32U ulMask = 0xffffffff, ulFilt = 0x00;
317
318
319 mcp2515_write_id(MCP_RXM0SIDH, ext, ulMask); /*Set both masks to 0xff */
320 mcp2515_write_id(MCP_RXM1SIDH, ext, ulMask); /*Mask register ignores ext bit */
321
322 /* Set all filters to 0 */
323 mcp2515_write_id(MCP_RXF0SIDH, std, ulFilt); /* RXB0: standard */
324 mcp2515_write_id(MCP_RXF1SIDH, std, ulFilt); /* RXB1: standard */
325 mcp2515_write_id(MCP_RXF2SIDH, std, ulFilt); /* RXB2: standard */
326 mcp2515_write_id(MCP_RXF3SIDH, std, ulFilt); /* RXB3: standard */
327 mcp2515_write_id(MCP_RXF4SIDH, std, ulFilt);
328 mcp2515_write_id(MCP_RXF5SIDH, std, ulFilt);
329
330 /* Clear, deactivate the three */
331 /* transmit buffers */
332 /* TXBnCTRL -> TXBnD7 */
333 a1 = MCP_TXB0CTRL;
334 a2 = MCP_TXB1CTRL;
335 a3 = MCP_TXB2CTRL;
336 for (i = 0; i < 14; i++) { /* in-buffer loop */
337 mcp2515_setRegister(a1, 0);
338 mcp2515_setRegister(a2, 0);
339 mcp2515_setRegister(a3, 0);
340 a1++;
341 a2++;
342 a3++;
343 }
344 mcp2515_setRegister(MCP_RXB0CTRL, 0x00);
345 mcp2515_setRegister(MCP_RXB1CTRL, 0xff);
346}
347
348/*********************************************************************************************************
349** Function name: mcp2515_init
350** Descriptions: init the device
351*********************************************************************************************************/
352INT8U MCP_CAN::mcp2515_init(const INT8U canSpeed) /* mcp2515init */
353{
354
355 INT8U res;
356
357 mcp2515_reset();
358
359 res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
360 if(res > 0)
361 {
362#if DEBUG_MODE
363 Serial.print("Enter setting mode fall\r\n");
364#endif
365 return res;
366 }
367#if DEBUG_MODE
368 Serial.print("Enter setting mode success \r\n");
369#endif
370
371 if(mcp2515_configRate(canSpeed))
372 {
373#if DEBUG_MODE
374 Serial.print("set rate fall!!\r\n");
375#endif
376 return res;
377 }
378#if DEBUG_MODE
379 Serial.print("set rate success!!\r\n");
380#endif
381
382 if ( res == MCP2515_OK ) {
383
384 /* init canbuffers */
385 mcp2515_initCANBuffers();
386
387 /* interrupt mode */
388 mcp2515_setRegister(MCP_CANINTE, 0x00);
389
390 /* enter normal mode */
391 res = mcp2515_setCANCTRL_Mode(MODE_NORMAL);
392 if(res)
393 {
394#if DEBUG_MODE
395 Serial.print("Enter Normal Mode Fall!!\r\n");
396#endif
397 return res;
398 }
399
400
401#if DEBUG_MODE
402 Serial.print("Enter Normal Mode Success!!\r\n");
403#endif
404
405 }
406 return res;
407
408}
409
410/*********************************************************************************************************
411** Function name: mcp2515_write_id
412** Descriptions: write can id
413*********************************************************************************************************/
414void MCP_CAN::mcp2515_write_id( const INT8U mcp_addr, const INT8U ext, const INT32U id )
415{
416 uint16_t canid;
417 INT8U tbufdata[4];
418
419 canid = (uint16_t)(id & 0x0FFFF);
420
421 if ( ext == 1)
422 {
423 tbufdata[MCP_EID0] = (INT8U) (canid & 0xFF);
424 tbufdata[MCP_EID8] = (INT8U) (canid >> 8);
425 canid = (uint16_t)(id >> 16);
426 tbufdata[MCP_SIDL] = (INT8U) (canid & 0x03);
427 tbufdata[MCP_SIDL] += (INT8U) ((canid & 0x1C) << 3);
428 tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M;
429 tbufdata[MCP_SIDH] = (INT8U) (canid >> 5 );
430 }
431 else
432 {
433 tbufdata[MCP_SIDH] = (INT8U) (canid >> 3 );
434 tbufdata[MCP_SIDL] = (INT8U) ((canid & 0x07 ) << 5);
435 tbufdata[MCP_EID0] = 0;
436 tbufdata[MCP_EID8] = 0;
437 }
438 mcp2515_setRegisterS( mcp_addr, tbufdata, 4 );
439}
440
441/*********************************************************************************************************
442** Function name: mcp2515_read_id
443** Descriptions: read can id
444*********************************************************************************************************/
445void MCP_CAN::mcp2515_read_id( const INT8U mcp_addr, INT8U* ext, INT32U* id )
446{
447 INT8U tbufdata[4];
448
449 *ext = 0;
450 *id = 0;
451
452 mcp2515_readRegisterS( mcp_addr, tbufdata, 4 );
453
454 *id = (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5);
455
456 if ( (tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M )
457 {
458 /* extended id */
459 *id = (*id<<2) + (tbufdata[MCP_SIDL] & 0x03);
460 *id = (*id<<8) + tbufdata[MCP_EID8];
461 *id = (*id<<8) + tbufdata[MCP_EID0];
462 *ext = 1;
463 }
464}
465
466/*********************************************************************************************************
467** Function name: mcp2515_write_canMsg
468** Descriptions: write msg
469*********************************************************************************************************/
470void MCP_CAN::mcp2515_write_canMsg( INT8U txbid, INT32U id, INT8U len, INT8U *pData, INT8U ext, INT8U rtr )
471{
472 INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL };
473 INT8U mcp_addr = ctrlregs[txbid] + 1;
474
475 mcp2515_setRegisterS(mcp_addr+5, pData, len ); /* write data bytes */
476 if ( rtr == 1) /* if RTR set bit in byte */
477 {
478 len |= MCP_RTR_MASK;
479 }
480 mcp2515_setRegister((mcp_addr+4), len ); /* write the RTR and DLC */
481 mcp2515_write_id(mcp_addr, ext, id ); /* write CAN id */
482
483}
484
485/*********************************************************************************************************
486** Function name: mcp2515_read_canMsg
487** Descriptions: read message
488*********************************************************************************************************/
489void MCP_CAN::mcp2515_read_canMsg( INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf, INT8U *p_ext, INT8U *p_rtr) /* read can msg */
490{
491 INT8U mcp_addr = (rxbid == 0)? MCP_RXBUF_0 : MCP_RXBUF_1;
492 INT8U ctrl;
493
494 mcp2515_read_id( mcp_addr, p_ext, p_id );
495
496 ctrl = mcp2515_readRegister( mcp_addr-1 );
497 *p_len = mcp2515_readRegister( mcp_addr+4 );
498
499 if ((ctrl & 0x08)) {
500 *p_rtr = 1;
501 }
502 else {
503 *p_rtr = 0;
504 }
505
506 *p_len &= MCP_DLC_MASK;
507 mcp2515_readRegisterS( mcp_addr+5, p_buf, *p_len );
508}
509
510/*********************************************************************************************************
511** Function name: sendMsg
512** Descriptions: send message
513*********************************************************************************************************/
514void MCP_CAN::mcp2515_start_transmit(INT8U txbid) /* start transmit */
515{
516 INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL };
517 INT8U mcp_addr = ctrlregs[txbid];
518
519 mcp2515_modifyRegister( mcp_addr, MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M );
520}
521
522/*********************************************************************************************************
523** Function name: sendMsg
524** Descriptions: send message
525*********************************************************************************************************/
526INT8U MCP_CAN::mcp2515_getNextFreeTXBuf(INT8U *txbuf_n) /* get Next free txbuf */
527{
528 INT8U res, i, ctrlval;
529 INT8U ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL };
530
531 res = MCP_ALLTXBUSY;
532 *txbuf_n = 0x00;
533
534 /* check all 3 TX-Buffers */
535 for (i=0; i<MCP_N_TXBUFFERS; i++) {
536 ctrlval = mcp2515_readRegister( ctrlregs[i] );
537 if ( (ctrlval & MCP_TXB_TXREQ_M) == 0 ) {
538 *txbuf_n = ctrlregs[i]+1; /* return SIDH-address of Buffe */
539 /* r */
540 res = MCP2515_OK;
541 return res; /* ! function exit */
542 }
543 }
544 return res;
545}
546
547/*********************************************************************************************************
548** Function name: set CS
549** Descriptions: init CS pin and set UNSELECTED
550*********************************************************************************************************/
551MCP_CAN::MCP_CAN(INT8U _CS)
552{
553 SPICS = _CS;
554 pinMode(SPICS, OUTPUT);
555 MCP2515_UNSELECT();
556}
557
558/*********************************************************************************************************
559** Function name: init
560** Descriptions: init can and set speed
561*********************************************************************************************************/
562INT8U MCP_CAN::begin(INT8U speedset)
563{
564 INT8U res;
565
566 ENTER_CRITICAL
567 SPI.begin();
568 res = mcp2515_init(speedset);
569 LEAVE_CRITICAL
570
571 if (res == MCP2515_OK) return CAN_OK;
572 else return CAN_FAILINIT;
573}
574
575/*********************************************************************************************************
576** Function name: enableInterrupt
577** Descriptions: Enable Interrupt
578*********************************************************************************************************/
579INT8U MCP_CAN::enableInterrupt(INT8U mask)
580{
581 ENTER_CRITICAL
582 mcp2515_modifyRegister(MCP_CANINTE, mask, mask);
583 LEAVE_CRITICAL
584
585 return CAN_OK;
586}
587
588/*********************************************************************************************************
589** Function name: disableInterrupt
590** Descriptions: Disable Interrupt
591*********************************************************************************************************/
592INT8U MCP_CAN::disableInterrupt(INT8U mask)
593{
594 ENTER_CRITICAL
595 mcp2515_modifyRegister(MCP_CANINTE, mask, !mask);
596 LEAVE_CRITICAL
597
598 return CAN_OK;
599}
600
601/*********************************************************************************************************
602** Function name: attachInterrupt
603** Descriptions: Attach Interrupt
604*********************************************************************************************************/
605INT8U MCP_CAN::attachInterrupt(uint32_t ulPin, voidFuncPtr callback)
606{
607 ::attachInterrupt( ulPin, callback, FALLING ) ;
608 return CAN_OK;
609}
610
611/*********************************************************************************************************
612** Function name: init_Mask
613** Descriptions: init canid Masks
614*********************************************************************************************************/
615INT8U MCP_CAN::init_Mask(INT8U num, INT32U ulData, INT8U ext)
616{
617 INT8U res = MCP2515_OK;
618
619 ENTER_CRITICAL;
620#if DEBUG_MODE
621 Serial.print("Begin to set Mask!!\r\n");
622#endif
623 res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
624 if(res > 0){
625#if DEBUG_MODE
626 Serial.print("Enter setting mode fall\r\n");
627#endif
628 LEAVE_CRITICAL;
629 return res;
630 }
631
632 if (num == 0){
633 mcp2515_write_id(MCP_RXM0SIDH, ext, ulData);
634 }
635 else if(num == 1){
636 mcp2515_write_id(MCP_RXM1SIDH, ext, ulData);
637 }
638 else res = MCP2515_FAIL;
639
640 res = mcp2515_setCANCTRL_Mode(MODE_NORMAL);
641 if(res > 0){
642#if DEBUG_MODE
643 Serial.print("Enter normal mode fall\r\n");
644#endif
645 LEAVE_CRITICAL;
646 return res;
647 }
648#if DEBUG_MODE
649 Serial.print("set Mask success!!\r\n");
650#endif
651 LEAVE_CRITICAL;
652 return res;
653}
654
655/*********************************************************************************************************
656** Function name: init_Filter
657** Descriptions: init canid filters
658*********************************************************************************************************/
659INT8U MCP_CAN::init_Filter(INT8U num, INT32U ulData, INT8U ext)
660{
661 INT8U res = MCP2515_OK;
662
663 ENTER_CRITICAL;
664#if DEBUG_MODE
665 Serial.print("Begin to set Filter!!\r\n");
666#endif
667 res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
668 if(res > 0)
669 {
670#if DEBUG_MODE
671 Serial.print("Enter setting mode fall\r\n");
672#endif
673 LEAVE_CRITICAL;
674 return res;
675 }
676
677 switch( num )
678 {
679 case 0:
680 mcp2515_write_id(MCP_RXF0SIDH, ext, ulData);
681 break;
682
683 case 1:
684 mcp2515_write_id(MCP_RXF1SIDH, ext, ulData);
685 break;
686
687 case 2:
688 mcp2515_write_id(MCP_RXF2SIDH, ext, ulData);
689 break;
690
691 case 3:
692 mcp2515_write_id(MCP_RXF3SIDH, ext, ulData);
693 break;
694
695 case 4:
696 mcp2515_write_id(MCP_RXF4SIDH, ext, ulData);
697 break;
698
699 case 5:
700 mcp2515_write_id(MCP_RXF5SIDH, ext, ulData);
701 break;
702
703 default:
704 res = MCP2515_FAIL;
705 }
706
707 res = mcp2515_setCANCTRL_Mode(MODE_NORMAL);
708 if(res > 0)
709 {
710#if DEBUG_MODE
711 Serial.print("Enter normal mode fall\r\nSet filter fail!!\r\n");
712#else
713 delay(10);
714#endif
715 LEAVE_CRITICAL;
716 return res;
717 }
718#if DEBUG_MODE
719 Serial.print("set Filter success!!\r\n");
720#else
721 delay(10);
722#endif
723 LEAVE_CRITICAL;
724 return res;
725}
726
727/*********************************************************************************************************
728** Function name: startReceive
729** Descriptions: start Receive
730*********************************************************************************************************/
731INT8U MCP_CAN::startReceive(INT8U rxbid)
732{
733 CHECK_RXBID(rxbid);
734
735 ENTER_CRITICAL;
736#if (DEBUG_RXANY==1)
737 /* enable both receive-buffers */
738 /* to receive any message */
739 /* and enable rollover */
740 if (rxbid == 0) {
741 mcp2515_modifyRegister(MCP_RXB0CTRL,
742 MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
743 MCP_RXB_RX_ANY);
744 }else{
745 mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
746 MCP_RXB_RX_ANY);
747 }
748#else
749 /* enable both receive-buffers */
750 /* to receive messages */
751 /* with std. and ext. identifie */
752 /* rs */
753 /* and enable rollover */
754 if (rxbid == 0) {
755 mcp2515_modifyRegister(MCP_RXB0CTRL,
756 MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
757 MCP_RXB_RX_STDEXT);
758 }else{
759 mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
760 MCP_RXB_RX_STDEXT);
761 }
762#endif
763 LEAVE_CRITICAL;
764
765 return CAN_OK;
766}
767
768/*********************************************************************************************************
769** Function name: sendMsg
770** Descriptions: send message
771*********************************************************************************************************/
772INT8U MCP_CAN::sendMsg(INT8U txbid, INT32U id, INT8U len, INT8U *p_buf, INT8U ext, INT8U rtr)
773{
774 INT8U intf_tbl[MCP_N_TXBUFFERS] = { MCP_TX0IF, MCP_TX1IF, MCP_TX2IF };
775 INT8U intf_bit = intf_tbl[txbid];
776
777 CHECK_LEN(len);
778 CHECK_TXBID(txbid);
779
780 ENTER_CRITICAL;
781 mcp2515_write_canMsg(txbid, id, len, p_buf, ext, rtr);
782 mcp2515_modifyRegister(MCP_CANINTF, intf_bit, 0);
783 mcp2515_start_transmit( txbid );
784 LEAVE_CRITICAL;
785 return CAN_OK;
786}
787
788/*********************************************************************************************************
789** Function name: sendMsg
790** Descriptions: send buf
791*********************************************************************************************************/
792INT8U MCP_CAN::sendMsg(INT8U txbid, INT32U id, INT8U len, INT8U *p_buf, INT8U ext)
793{
794 return sendMsg(txbid, id, len, p_buf, ext, 0);
795}
796
797/*********************************************************************************************************
798** Function name: sendMsg
799** Descriptions: send buf
800*********************************************************************************************************/
801INT8U MCP_CAN::sendMsg(INT8U txbid, INT32U id, INT8U len, INT8U *p_buf)
802{
803 return sendMsg(txbid, id, len, p_buf, 0, 0);
804}
805
806
807/*********************************************************************************************************
808** Function name: readMsg
809** Descriptions: read message
810*********************************************************************************************************/
811INT8U MCP_CAN::readMsg(INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf, INT8U *p_ext, INT8U *p_rtr)
812{
813 INT8U stat, res;
814 CHECK_RXBID(rxbid);
815 INT8U stat_bit = (rxbid == 0)? MCP_STAT_RX0IF : MCP_STAT_RX1IF;
816 INT8U intf_bit = (rxbid == 0)? MCP_RX0IF : MCP_RX1IF;
817
818 ENTER_CRITICAL;
819 stat = mcp2515_readStatus();
820
821 if ( stat & stat_bit )
822 {
823 mcp2515_read_canMsg(rxbid, p_id, p_len, p_buf, p_ext, p_rtr);
824 mcp2515_modifyRegister(MCP_CANINTF, intf_bit, 0);
825 res = CAN_OK;
826 }
827 else {
828 res = CAN_NOMSG;
829 }
830 LEAVE_CRITICAL;
831 return res;
832}
833
834/*********************************************************************************************************
835** Function name: readMsgBuf
836** Descriptions: read message
837*********************************************************************************************************/
838INT8U MCP_CAN::readMsg(INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf, INT8U *p_ext)
839{
840 INT8U rtr;
841
842 return readMsg(rxbid, p_id, p_len, p_buf, p_ext, &rtr);
843}
844
845/*********************************************************************************************************
846** Function name: readMsgBuf
847** Descriptions: read message
848*********************************************************************************************************/
849INT8U MCP_CAN::readMsg(INT8U rxbid, INT32U *p_id, INT8U *p_len, INT8U *p_buf)
850{
851 INT8U ext;
852 INT8U rtr;
853
854 return readMsg(rxbid, p_id, p_len, p_buf, &ext, &rtr);
855}
856
857/*********************************************************************************************************
858** Function name: readMsgBuf
859** Descriptions: read message
860*********************************************************************************************************/
861INT8U MCP_CAN::readMsg(INT8U rxbid, INT8U *p_len, INT8U *p_buf)
862{
863 INT32U id;
864 INT8U ext;
865 INT8U rtr;
866
867 return readMsg(rxbid, &id, p_len, p_buf, &ext, &rtr);
868}
869
870/*********************************************************************************************************
871** Function name: checkReceive
872** Descriptions: check if got something
873*********************************************************************************************************/
874INT8U MCP_CAN::checkReceive(INT8U rxbid)
875{
876 INT8U stat, res = CAN_NOMSG;
877 CHECK_RXBID(rxbid);
878
879 ENTER_CRITICAL;
880 stat = mcp2515_readStatus(); /* RXnIF in Bit 1 and 0 */
881 if (rxbid == 0) {
882 if ( (stat & MCP_STAT_RX0IF) == MCP_STAT_RX0IF) {
883 res = CAN_MSGAVAIL;
884 }
885 }
886 else if (rxbid == 1) {
887 if ( (stat & MCP_STAT_RX1IF) == MCP_STAT_RX1IF ) {
888 res = CAN_MSGAVAIL;
889 }
890 }
891 LEAVE_CRITICAL;
892
893 return res;
894}
895
896/*********************************************************************************************************
897** Function name: checkSend
898** Descriptions: check if snt msg
899*********************************************************************************************************/
900INT8U MCP_CAN::checkSend(INT8U txbid)
901{
902 INT8U stat, res = CAN_NOSENDWAIT;
903 INT8U txbx_ctrl[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL };
904
905 CHECK_TXBID(txbid);
906
907 ENTER_CRITICAL;
908 stat = mcp2515_readRegister(txbx_ctrl[txbid]);
909
910 if ( (stat & MCP_TXB_TXREQ_M) == MCP_TXB_TXREQ_M) {
911 res = CAN_SENDWAIT;
912 }
913 LEAVE_CRITICAL;
914
915 return res;
916}
917
918/*********************************************************************************************************
919** Function name: checkError
920** Descriptions: if something error
921*********************************************************************************************************/
922INT8U MCP_CAN::checkError(void)
923{
924 INT8U res = CAN_OK;
925
926 ENTER_CRITICAL;
927 INT8U eflg = mcp2515_readRegister(MCP_EFLG);
928
929 if ( eflg & MCP_EFLG_ERRORMASK )
930 {
931 res = CAN_CTRLERROR;
932 }
933 LEAVE_CRITICAL;
934
935 return res;
936}
937
938/*********************************************************************************************************
939 END FILE
940*********************************************************************************************************/
Note: See TracBrowser for help on using the repository browser.