source: rtos_arduino/trunk/examples/NCESCan/rca_app.cpp@ 171

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

初期化待ちを追加

File size: 3.2 KB
Line 
1#include "rca.h"
2
3// demo: CAN-BUS Shield, receive data
4#include <SPI.h>
5#include "mcp_can.h"
6#include <stdio.h>
7#define uint8_t unsigned char
8
9const int SPI_CS_PIN = 9;
10#define CAN_INT_PIN 3
11MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
12
13extern void MCP2515_ISR();
14
15/*
16 * CAN Controller : MCP2515
17 * TXB : MCP_TXB0:0, MCP_RXB1:1,MCP_RXB2:2
18 * RXB : MCP_RXB0:0 : Mask MCP_RXM0:0 : Filer MCP_RXF0:0, MCP_RXF1:1
19 * MCP_RXB1:1 : Mask MCP_RXM2:1 : Filer MCP_RXF2:2, MCP_RXF3:3, MCP_RXF4:4, MCP_RXF5:5
20 * Interrupt mask
21 * MCP_TX0IF
22 * MCP_TX1IF
23 * MCP_TX2IF
24 * MCP_RX0IF
25 * MCP_RX1IF
26 */
27bool can_init_done = false;
28
29void setup()
30{
31 Serial.begin(115200);
32 pinMode(13, OUTPUT);
33
34 /*
35 * CAN Initialize
36 */
37 if(CAN.begin(CAN_500KBPS) ==CAN_OK) {
38 Serial.print("can init ok!!\r\n");
39 }
40 else {
41 while(1){
42 Serial.print("Can init fail!!\r\n");
43 digitalWrite(13, HIGH);
44 delay(500);
45 digitalWrite(13, LOW);
46 delay(500);
47 };
48 }
49
50 /*
51 * CAN Recive mabx Init
52 */
53
54 /*
55 * RXB0
56 */
57 CAN.init_Mask(MCP_RXM0, 0x0f);
58 CAN.init_Filter(MCP_RXF0, 0x01);
59 CAN.init_Filter(MCP_RXF1, 0x02);
60 CAN.startReceive(MCP_RXB0);
61
62 /*
63 * RXB1
64 */
65 CAN.init_Mask(MCP_RXM1, 0x00);
66 CAN.init_Filter(MCP_RXF2, 0x03);
67 CAN.init_Filter(MCP_RXF3, 0x04);
68 CAN.init_Filter(MCP_RXF4, 0x05);
69 CAN.init_Filter(MCP_RXF5, 0x06);
70 CAN.startReceive(MCP_RXB1);
71
72 CAN.enableInterrupt(MCP_RX0IF|MCP_RX1IF);
73 CAN.attachInterrupt(CAN_INT_PIN, MCP2515_ISR);
74
75 can_init_done = true;
76}
77
78uint8_t Flag_Recv = 0;
79
80void MCP2515_ISR()
81{
82 Flag_Recv = 1;
83}
84
85void loop()
86{
87 uint8_t len = 0;
88 uint8_t buf[8];
89 uint8_t i;
90 uint32_t id;
91
92 if(!Flag_Recv) {
93 delay(1);
94 return;
95 }
96
97 Flag_Recv = 0;
98
99 for(i = 0; i < 2; i++){
100 if(CAN.checkReceive(i) == CAN_NOMSG) {
101 continue;
102 }
103 CAN.readMsg(i, &id, &len, buf); // read data, len: data length, buf: data buf
104 Serial.print("CAN_BUS GET DATA MBX");
105 Serial.println(i);
106 Serial.print("ID = ");Serial.print(id);
107 Serial.print(", DLC = ");Serial.print(len);
108 Serial.print(", data = ");
109 for(int i = 0; i<len; i++){ // print the data
110 Serial.print(buf[i]);Serial.print("\t");
111 }
112 Serial.println();
113 }
114}
115
116void task1_setup()
117{
118 while(can_init_done == false){delay(10);}
119}
120
121unsigned char stmp[8][8] = {
122 {1, 2, 3, 4, 5, 6, 7, 8},
123 {2, 3, 4, 5, 6, 7, 8, 1},
124 {3, 4, 5, 6, 7, 8, 1, 2},
125 {1, 2, 3, 4, 5, 6, 7, 8},
126 {1, 2, 3, 4, 5, 6, 7, 8},
127 {1, 2, 3, 4, 5, 6, 7, 8},
128 {1, 2, 3, 4, 5, 6, 7, 8},
129 {0, 1, 2, 3, 4, 5, 6, 7}
130};
131
132unsigned char stmp1[8] = {0, 1, 2, 3, 4, 5, 6, 7};
133
134void task1_loop()
135{
136 int i, e;
137
138 for(e = 0; e < 3; e++){
139 Serial.print("Send can message from mbx ");
140 Serial.println(e);
141 for(i = 1; i <= 8; i++){
142 while(CAN_SENDWAIT == CAN.checkSend(e)){delay(10);};
143 /* mailbox, ID, DLC, DATA */
144 CAN.sendMsg(e, i, i, stmp[i-1]);
145 }
146 }
147
148 delay(2000);
149}
Note: See TracBrowser for help on using the repository browser.