A simple CAN adapter that supports two channels of CAN on the mbed. Configurable speed, monitor mode, statistics and send/receive via the USB serial port to a PC (or terminal program set for 921.6 kbaud)

Dependencies:   CommandProcessor Watchdog mbed

Committer:
WiredHome
Date:
Mon Apr 11 11:42:08 2011 +0000
Revision:
1:6b831d0c058c
Parent:
0:ea85c59ec672
documentation changes only

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:ea85c59ec672 1 /// CANadapter is a simple program that permits monitoring as well as transmitting
WiredHome 0:ea85c59ec672 2 /// on both CAN buses that the mbed supports. It communicates to either the user or
WiredHome 0:ea85c59ec672 3 /// a PC hosted program via the USB Serial port.
WiredHome 0:ea85c59ec672 4 ///
WiredHome 0:ea85c59ec672 5 /// For robustness, there is a small CommandProcessor, which permits setting
WiredHome 0:ea85c59ec672 6 /// CAN interface metrics, reviewing statistics, and sending messages.
WiredHome 0:ea85c59ec672 7 ///
WiredHome 0:ea85c59ec672 8 /// There is also a watchdog, which will keep the system running and recover
WiredHome 0:ea85c59ec672 9 /// if there was a problem.
WiredHome 0:ea85c59ec672 10 ///
WiredHome 0:ea85c59ec672 11 /// @note Copyright &copr; 2011 by Smartware Computing, all rights reserved.
WiredHome 0:ea85c59ec672 12 /// Individuals may use this application for evaluation or non-commercial
WiredHome 0:ea85c59ec672 13 /// purposes. Within this restriction, changes may be made to this application
WiredHome 0:ea85c59ec672 14 /// as long as this copyright notice is retained. The user shall make
WiredHome 0:ea85c59ec672 15 /// clear that their work is a derived work, and not the original.
WiredHome 0:ea85c59ec672 16 /// Users of this application and sources accept this application "as is" and
WiredHome 0:ea85c59ec672 17 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 0:ea85c59ec672 18 /// using this application - whether real or imagined.
WiredHome 0:ea85c59ec672 19 /// @author David Smart, Smartware Computing
WiredHome 0:ea85c59ec672 20 ///
WiredHome 0:ea85c59ec672 21 #include "mbed.h"
WiredHome 0:ea85c59ec672 22 #include "Watchdog.h"
WiredHome 0:ea85c59ec672 23 #include "CommandProcessor.h"
WiredHome 0:ea85c59ec672 24 #include "CANUtilities.h"
WiredHome 0:ea85c59ec672 25 #include "CANQueue.h"
WiredHome 0:ea85c59ec672 26 #include "vs_string.h" // helpers that normalize between compilers for string functions
WiredHome 0:ea85c59ec672 27
WiredHome 0:ea85c59ec672 28 extern "C" void mbed_reset();
WiredHome 0:ea85c59ec672 29
WiredHome 0:ea85c59ec672 30 Serial pc(USBTX, USBRX); ///!< Used as the console for interactively reporting progress
WiredHome 0:ea85c59ec672 31 DigitalOut myled(LED4); /// LED sign of life
WiredHome 0:ea85c59ec672 32
WiredHome 0:ea85c59ec672 33 Watchdog wd;
WiredHome 0:ea85c59ec672 34
WiredHome 0:ea85c59ec672 35 //Ticker ticker; // for automated sending of messages during development
WiredHome 0:ea85c59ec672 36
WiredHome 1:6b831d0c058c 37 // Services for each of the two CAN channels
WiredHome 0:ea85c59ec672 38 CAN can1(p9, p10); // bind CAN1 to the hardware
WiredHome 0:ea85c59ec672 39 CAN can2(p30, p29); // bind CAN2 to the hardware
WiredHome 0:ea85c59ec672 40 Timeout t1; // create a timeout mechanism for can1
WiredHome 0:ea85c59ec672 41 Timeout t2; // create a timeout mechanism for can2
WiredHome 0:ea85c59ec672 42 void c1off(); // extinquish the can1 activity indicator
WiredHome 0:ea85c59ec672 43 void c2off(); // extinquish the can2 activity indicator
WiredHome 0:ea85c59ec672 44
WiredHome 1:6b831d0c058c 45 /// binding the services together for a simpler interface
WiredHome 0:ea85c59ec672 46 struct {
WiredHome 0:ea85c59ec672 47 CAN *can;
WiredHome 0:ea85c59ec672 48 Timeout *flash;
WiredHome 0:ea85c59ec672 49 DigitalOut led;
WiredHome 0:ea85c59ec672 50 void (*off)(void);
WiredHome 0:ea85c59ec672 51 bool active;
WiredHome 0:ea85c59ec672 52 int bitrate;
WiredHome 0:ea85c59ec672 53 uint32_t txCounter;
WiredHome 0:ea85c59ec672 54 uint32_t rxCounter;
WiredHome 0:ea85c59ec672 55 } can[] = {
WiredHome 0:ea85c59ec672 56 {&can1, &t1, LED1, c1off, false, 250000, 0, 0},
WiredHome 0:ea85c59ec672 57 {&can2, &t2, LED2, c2off, false, 250000, 0, 0}
WiredHome 0:ea85c59ec672 58 };
WiredHome 0:ea85c59ec672 59
WiredHome 1:6b831d0c058c 60 // Support up to this many inbound CAN messages from the attached callbacks
WiredHome 0:ea85c59ec672 61 CANQueue inQueue(10);
WiredHome 0:ea85c59ec672 62
WiredHome 1:6b831d0c058c 63 // A method to transmit a CAN message of the derived type
WiredHome 0:ea85c59ec672 64 bool CANTransmitMsg(CANmsg msg);
WiredHome 0:ea85c59ec672 65
WiredHome 1:6b831d0c058c 66
WiredHome 1:6b831d0c058c 67 // The included command processor supports interactive sessions, with these
WiredHome 1:6b831d0c058c 68 // and the built-in commands.
WiredHome 0:ea85c59ec672 69 RUNRESULT_T CANconfig(char *p);
WiredHome 0:ea85c59ec672 70 const CMD_T CANconfigCmd = {"CANconfig", "Configure [channel mode speed], ? for more", CANconfig, visible};
WiredHome 0:ea85c59ec672 71 RUNRESULT_T CANmessage(char *p);
WiredHome 0:ea85c59ec672 72 const CMD_T CANmessageCmd = {"CANmessage", "Shows the CAN message format", CANmessage, visible};
WiredHome 0:ea85c59ec672 73 RUNRESULT_T CANtransmit(char *p);
WiredHome 0:ea85c59ec672 74 const CMD_T CANtransmitCmd = {"t", "transmit a CAN message (see CANmessage format)", CANtransmit, visible};
WiredHome 0:ea85c59ec672 75 RUNRESULT_T CANstatistics(char *p);
WiredHome 0:ea85c59ec672 76 const CMD_T CANstatisticsCmd = {"CANstats", "Shows the CAN statistics", CANstatistics, visible};
WiredHome 0:ea85c59ec672 77 RUNRESULT_T CANreset(char *p);
WiredHome 0:ea85c59ec672 78 const CMD_T CANresetCmd = {"CANreset", "Reset CAN channel [0|1|*]", CANreset, visible};
WiredHome 0:ea85c59ec672 79 RUNRESULT_T Free(char *p);
WiredHome 0:ea85c59ec672 80 const CMD_T FreeCmd = {"Free", "Shows the free memory in bytes", Free, visible};
WiredHome 0:ea85c59ec672 81 RUNRESULT_T Reboot(char *p);
WiredHome 0:ea85c59ec672 82 const CMD_T RebootCmd = {"Reboot", "Causes a near immediate reboot", Reboot, visible};
WiredHome 0:ea85c59ec672 83
WiredHome 1:6b831d0c058c 84 // Implementation of Command Processor accessed commands
WiredHome 1:6b831d0c058c 85
WiredHome 0:ea85c59ec672 86 RUNRESULT_T Reboot(char *p) {
WiredHome 0:ea85c59ec672 87 (void)p;
WiredHome 0:ea85c59ec672 88 pc.printf(" now...\r\n");
WiredHome 0:ea85c59ec672 89 wait(0.5);
WiredHome 0:ea85c59ec672 90 mbed_reset();
WiredHome 0:ea85c59ec672 91 return runok;
WiredHome 0:ea85c59ec672 92 }
WiredHome 0:ea85c59ec672 93
WiredHome 0:ea85c59ec672 94 RUNRESULT_T Free(char *p) {
WiredHome 0:ea85c59ec672 95 (void)p;
WiredHome 0:ea85c59ec672 96 uint32_t max = 100000;
WiredHome 0:ea85c59ec672 97 uint32_t x = max / 2;
WiredHome 0:ea85c59ec672 98 uint32_t min = 0;
WiredHome 0:ea85c59ec672 99
WiredHome 0:ea85c59ec672 100 while (min < max-1) {
WiredHome 0:ea85c59ec672 101 void * p = malloc(x);
WiredHome 0:ea85c59ec672 102 if (p) {
WiredHome 0:ea85c59ec672 103 free(p);
WiredHome 0:ea85c59ec672 104 min = x;
WiredHome 0:ea85c59ec672 105 } else {
WiredHome 0:ea85c59ec672 106 max = x;
WiredHome 0:ea85c59ec672 107 }
WiredHome 0:ea85c59ec672 108 x = (max + min)/2;
WiredHome 0:ea85c59ec672 109 }
WiredHome 0:ea85c59ec672 110 pc.printf("\r\n%u bytes free\r\n", x);
WiredHome 0:ea85c59ec672 111 return runok;
WiredHome 0:ea85c59ec672 112 }
WiredHome 0:ea85c59ec672 113
WiredHome 0:ea85c59ec672 114 RUNRESULT_T CANconfig(char *p) {
WiredHome 0:ea85c59ec672 115 int ch, mode, bitrate;
WiredHome 0:ea85c59ec672 116 char *token;
WiredHome 0:ea85c59ec672 117 char *search = " ,\t";
WiredHome 0:ea85c59ec672 118
WiredHome 0:ea85c59ec672 119 token = strtok(p, search);
WiredHome 0:ea85c59ec672 120 ch = atoi(token);
WiredHome 0:ea85c59ec672 121 token = strtok(NULL, search);
WiredHome 0:ea85c59ec672 122 if (mystrnicmp(token, "monitor", 7) == 0)
WiredHome 0:ea85c59ec672 123 mode = 0;
WiredHome 0:ea85c59ec672 124 else if (mystrnicmp(token, "0", 1) == 0)
WiredHome 0:ea85c59ec672 125 mode = 0;
WiredHome 0:ea85c59ec672 126 else if (mystrnicmp(token, "active", 7) == 0)
WiredHome 0:ea85c59ec672 127 mode = 1;
WiredHome 0:ea85c59ec672 128 else if (mystrnicmp(token, "1", 1) == 0)
WiredHome 0:ea85c59ec672 129 mode = 1;
WiredHome 0:ea85c59ec672 130 else
WiredHome 0:ea85c59ec672 131 mode = -1;
WiredHome 0:ea85c59ec672 132 token = strtok(NULL, search);
WiredHome 0:ea85c59ec672 133 bitrate = atoi(token);
WiredHome 0:ea85c59ec672 134
WiredHome 0:ea85c59ec672 135 if (ch >=1 && ch <= 2 && mode != -1 && bitrate > 1000 && bitrate <= 1000000) {
WiredHome 0:ea85c59ec672 136 can[ch-1].can->monitor(mode);
WiredHome 0:ea85c59ec672 137 can[ch-1].can->frequency(bitrate);
WiredHome 0:ea85c59ec672 138 pc.printf("\r\n");
WiredHome 0:ea85c59ec672 139 } else {
WiredHome 0:ea85c59ec672 140 pc.printf("\r\n CANconfig [channel mode bits/sec]\r\n"
WiredHome 0:ea85c59ec672 141 " channel = 1 or 2\r\n"
WiredHome 0:ea85c59ec672 142 " mode = 0|monitor|1|active\r\n"
WiredHome 0:ea85c59ec672 143 " speed = baud rate (e.g. 10000, 250000, 500000, etc.)\r\n"
WiredHome 0:ea85c59ec672 144 "");
WiredHome 0:ea85c59ec672 145 }
WiredHome 0:ea85c59ec672 146 return runok;
WiredHome 0:ea85c59ec672 147 }
WiredHome 0:ea85c59ec672 148
WiredHome 0:ea85c59ec672 149 RUNRESULT_T CANreset(char *p) {
WiredHome 0:ea85c59ec672 150 if (*p == '1' || *p == '*')
WiredHome 0:ea85c59ec672 151 can[CH1].can->reset();
WiredHome 0:ea85c59ec672 152 if (*p == '2' || *p == '*')
WiredHome 0:ea85c59ec672 153 can[CH2].can->reset();
WiredHome 0:ea85c59ec672 154 pc.printf("\r\n");
WiredHome 0:ea85c59ec672 155 return runok;
WiredHome 0:ea85c59ec672 156 }
WiredHome 0:ea85c59ec672 157
WiredHome 0:ea85c59ec672 158 RUNRESULT_T CANmessage(char *p) {
WiredHome 0:ea85c59ec672 159 pc.printf( "\r\n// CAN Message Format\r\n"
WiredHome 0:ea85c59ec672 160 "//\r\n"
WiredHome 0:ea85c59ec672 161 "// +--- 'r'eceive or 't'ransmit\r\n"
WiredHome 0:ea85c59ec672 162 "// | +--- 'nrm' 11 bit identifier, 'xtd' 29 bit identifier\r\n"
WiredHome 0:ea85c59ec672 163 "// | | +--- channel '1' to '2'\r\n"
WiredHome 0:ea85c59ec672 164 "// | | | +--- identifier in hex\r\n"
WiredHome 0:ea85c59ec672 165 "// | | | | +--- dlc is data length control from 0 to 8\r\n"
WiredHome 0:ea85c59ec672 166 "// | | | | | +--- data bytes 1 to 8\r\n"
WiredHome 0:ea85c59ec672 167 "// | | | | | | [Below not required to send\r\n"
WiredHome 0:ea85c59ec672 168 "// | | | | | | +--- fixed zero\r\n"
WiredHome 0:ea85c59ec672 169 "// | | | | | | | +--- err count\r\n"
WiredHome 0:ea85c59ec672 170 "// | | | | | | | | +--- timestamp\r\n"
WiredHome 0:ea85c59ec672 171 "// | | | | | | | | |\r\n"
WiredHome 0:ea85c59ec672 172 "// _ ___ __ ________ __ _______________________ _ ___ ___________\r\n"
WiredHome 0:ea85c59ec672 173 "// r xtd 02 1CF00400 08 11 22 33 44 55 66 77 88 0 0 1234.567890\r\n"
WiredHome 0:ea85c59ec672 174 "// t xtd 01 18EAFF03 03 EE EE 00 0 0 1235.654321\r\n"
WiredHome 0:ea85c59ec672 175 "// 12345678901234567890123456789012345678901234567890123456789012\r\n");
WiredHome 0:ea85c59ec672 176 return runok;
WiredHome 0:ea85c59ec672 177 }
WiredHome 0:ea85c59ec672 178
WiredHome 0:ea85c59ec672 179 RUNRESULT_T CANtransmit(char *p) {
WiredHome 0:ea85c59ec672 180 if (*p) {
WiredHome 0:ea85c59ec672 181 CANmsg msg(p);
WiredHome 1:6b831d0c058c 182 if (msg.dir == xmt) // silent failure if they try to transmit a receive msg
WiredHome 0:ea85c59ec672 183 CANTransmitMsg(msg);
WiredHome 0:ea85c59ec672 184 pc.printf("\r\n");
WiredHome 0:ea85c59ec672 185 } else {
WiredHome 0:ea85c59ec672 186 pc.printf( "\r\n't'ransmit a CAN message in the message format\r\n");
WiredHome 0:ea85c59ec672 187 }
WiredHome 0:ea85c59ec672 188 return runok;
WiredHome 0:ea85c59ec672 189 }
WiredHome 0:ea85c59ec672 190
WiredHome 0:ea85c59ec672 191 RUNRESULT_T CANstatistics(char *p) {
WiredHome 0:ea85c59ec672 192 pc.printf("\r\n ch mode bitrate rxCount rxErrors txCount txErrors\r\n");
WiredHome 0:ea85c59ec672 193 for (int i=0; i<CANCHANNELS; i++)
WiredHome 0:ea85c59ec672 194 pc.printf(" %2u %7s %8u %7u %8u %7u %8u\r\n",
WiredHome 0:ea85c59ec672 195 i+1,
WiredHome 0:ea85c59ec672 196 can[i].active ? "active" : "monitor",
WiredHome 0:ea85c59ec672 197 can[i].bitrate,
WiredHome 0:ea85c59ec672 198 can[i].rxCounter,
WiredHome 0:ea85c59ec672 199 can[i].can->rderror(),
WiredHome 0:ea85c59ec672 200 can[i].txCounter,
WiredHome 0:ea85c59ec672 201 can[i].can->tderror()
WiredHome 0:ea85c59ec672 202 );
WiredHome 0:ea85c59ec672 203 return runok;
WiredHome 0:ea85c59ec672 204 }
WiredHome 0:ea85c59ec672 205
WiredHome 0:ea85c59ec672 206
WiredHome 0:ea85c59ec672 207
WiredHome 1:6b831d0c058c 208 // Helper functions provided to the command processor as a means of I/O
WiredHome 0:ea85c59ec672 209 int mReadable() {
WiredHome 0:ea85c59ec672 210 return pc.readable();
WiredHome 0:ea85c59ec672 211 }
WiredHome 0:ea85c59ec672 212 int mGetCh() {
WiredHome 0:ea85c59ec672 213 return pc.getc();
WiredHome 0:ea85c59ec672 214 }
WiredHome 0:ea85c59ec672 215 int mPutCh(int a) {
WiredHome 0:ea85c59ec672 216 return pc.putc(a);
WiredHome 0:ea85c59ec672 217 }
WiredHome 0:ea85c59ec672 218 int mPutS(const char * s) {
WiredHome 0:ea85c59ec672 219 return pc.printf("%s\r\n", s);
WiredHome 0:ea85c59ec672 220 }
WiredHome 0:ea85c59ec672 221
WiredHome 1:6b831d0c058c 222
WiredHome 1:6b831d0c058c 223 // LED activity indicators need to extinguish on timeout
WiredHome 0:ea85c59ec672 224 void c1off() {
WiredHome 0:ea85c59ec672 225 can[CH1].led = false;
WiredHome 0:ea85c59ec672 226 }
WiredHome 0:ea85c59ec672 227 void c2off() {
WiredHome 0:ea85c59ec672 228 can[CH2].led = false;
WiredHome 0:ea85c59ec672 229 }
WiredHome 0:ea85c59ec672 230
WiredHome 1:6b831d0c058c 231 // When a CAN message is received, it is placed in queue
WiredHome 0:ea85c59ec672 232 void canreceive(CANCHANNEL_T ch) {
WiredHome 0:ea85c59ec672 233 CANMessage msg;
WiredHome 0:ea85c59ec672 234
WiredHome 0:ea85c59ec672 235 if (can[ch].can->read(msg)) {
WiredHome 0:ea85c59ec672 236 CANmsg _msg(ch, rcv, msg);
WiredHome 0:ea85c59ec672 237
WiredHome 0:ea85c59ec672 238 inQueue.Enqueue(_msg);
WiredHome 0:ea85c59ec672 239 can[ch].rxCounter++;
WiredHome 0:ea85c59ec672 240 can[ch].led = true;
WiredHome 0:ea85c59ec672 241 can[ch].flash->attach(can[ch].off, 0.02);
WiredHome 0:ea85c59ec672 242 }
WiredHome 0:ea85c59ec672 243 }
WiredHome 0:ea85c59ec672 244
WiredHome 1:6b831d0c058c 245 // received bound to each CAN channel
WiredHome 0:ea85c59ec672 246 void can1rcv() {
WiredHome 0:ea85c59ec672 247 canreceive(CH1);
WiredHome 0:ea85c59ec672 248 }
WiredHome 0:ea85c59ec672 249 void can2rcv() {
WiredHome 0:ea85c59ec672 250 canreceive(CH2);
WiredHome 0:ea85c59ec672 251 }
WiredHome 0:ea85c59ec672 252
WiredHome 1:6b831d0c058c 253 // method to transmit a CAN message of type CANmsg
WiredHome 0:ea85c59ec672 254 bool CANTransmitMsg(CANmsg msg) {
WiredHome 0:ea85c59ec672 255 if (msg.dir == xmt) {
WiredHome 0:ea85c59ec672 256 if (can[msg.ch].can->write(CANMessage(msg.id, (char *)&msg.data, msg.len, CANData, msg.format)))
WiredHome 0:ea85c59ec672 257 return true;
WiredHome 0:ea85c59ec672 258 }
WiredHome 0:ea85c59ec672 259 return false;
WiredHome 0:ea85c59ec672 260 }
WiredHome 0:ea85c59ec672 261
WiredHome 1:6b831d0c058c 262 // quick transmitter for testing only
WiredHome 0:ea85c59ec672 263 void cantransmit(int ch) {
WiredHome 0:ea85c59ec672 264 char byte = (char)can[ch].txCounter;
WiredHome 0:ea85c59ec672 265
WiredHome 0:ea85c59ec672 266 if (can[ch].can->write(CANMessage(1337, &byte, 1))) {
WiredHome 0:ea85c59ec672 267 can[ch].txCounter++;
WiredHome 0:ea85c59ec672 268 }
WiredHome 0:ea85c59ec672 269 }
WiredHome 0:ea85c59ec672 270
WiredHome 0:ea85c59ec672 271 void can1send() {
WiredHome 0:ea85c59ec672 272 cantransmit(1);
WiredHome 0:ea85c59ec672 273 }
WiredHome 0:ea85c59ec672 274 void can2send() {
WiredHome 0:ea85c59ec672 275 cantransmit(2);
WiredHome 0:ea85c59ec672 276 }
WiredHome 0:ea85c59ec672 277
WiredHome 0:ea85c59ec672 278
WiredHome 0:ea85c59ec672 279
WiredHome 0:ea85c59ec672 280 int main(int argc, char* argv[]) {
WiredHome 0:ea85c59ec672 281 CMDP_T * cp = GetCommandProcessor();
WiredHome 0:ea85c59ec672 282 RUNRESULT_T cp_state;
WiredHome 0:ea85c59ec672 283
WiredHome 0:ea85c59ec672 284 pc.baud(921600);
WiredHome 0:ea85c59ec672 285 if (wd.WatchdogCausedReset())
WiredHome 0:ea85c59ec672 286 pc.printf("Watchdog caused reset. WD is now rearmed\r\n");
WiredHome 0:ea85c59ec672 287 wd.Configure(2.0); // sets the timeout interval pretty short
WiredHome 0:ea85c59ec672 288
WiredHome 1:6b831d0c058c 289 // Set up the Command Processor interface and commands.
WiredHome 0:ea85c59ec672 290 cp->Init(
WiredHome 0:ea85c59ec672 291 0xFFFF, // Everything is enabled
WiredHome 0:ea85c59ec672 292 TRUE, // Case Insensitive
WiredHome 0:ea85c59ec672 293 TRUE, // Echo on
WiredHome 0:ea85c59ec672 294 50, // Command Buffer length
WiredHome 0:ea85c59ec672 295 mReadable, // User provided API (kbhit())
WiredHome 0:ea85c59ec672 296 mGetCh, // User provided API
WiredHome 0:ea85c59ec672 297 mPutCh, // User provided API
WiredHome 0:ea85c59ec672 298 mPutS); // User provided API
WiredHome 0:ea85c59ec672 299 cp->Add(&CANconfigCmd);
WiredHome 0:ea85c59ec672 300 cp->Add(&CANmessageCmd);
WiredHome 0:ea85c59ec672 301 cp->Add(&CANtransmitCmd);
WiredHome 0:ea85c59ec672 302 cp->Add(&CANstatisticsCmd);
WiredHome 0:ea85c59ec672 303 cp->Add(&CANresetCmd);
WiredHome 0:ea85c59ec672 304 cp->Add(&FreeCmd);
WiredHome 0:ea85c59ec672 305 cp->Add(&RebootCmd);
WiredHome 0:ea85c59ec672 306
WiredHome 0:ea85c59ec672 307 can2.attach(can2rcv);
WiredHome 0:ea85c59ec672 308 can1.attach(can1rcv);
WiredHome 0:ea85c59ec672 309 can[CH1].can->monitor(false); // make them active on the network or tx errors result
WiredHome 0:ea85c59ec672 310 can[CH2].can->monitor(false);
WiredHome 0:ea85c59ec672 311
WiredHome 0:ea85c59ec672 312 // This just sends a message every now and again
WiredHome 0:ea85c59ec672 313 //ticker.attach(&can1send, 1);
WiredHome 0:ea85c59ec672 314
WiredHome 1:6b831d0c058c 315 // Do nothing to waste time in here... (e.g. do not "wait(1.2);")
WiredHome 0:ea85c59ec672 316 do {
WiredHome 0:ea85c59ec672 317 myled = !myled; // activity indicator
WiredHome 0:ea85c59ec672 318
WiredHome 0:ea85c59ec672 319 wd.Service(); // service the dog
WiredHome 0:ea85c59ec672 320 cp_state = cp->Run(); // user interactions on the console interface
WiredHome 0:ea85c59ec672 321
WiredHome 0:ea85c59ec672 322 while (inQueue.QueueCount()) { // If we handle messages badly, could watchdog in here
WiredHome 0:ea85c59ec672 323 CANmsg msg;
WiredHome 0:ea85c59ec672 324
WiredHome 0:ea85c59ec672 325 if (inQueue.Dequeue(&msg)) {
WiredHome 0:ea85c59ec672 326 char buf[100];
WiredHome 0:ea85c59ec672 327 msg.FormatCANMessage(buf, sizeof(buf));
WiredHome 0:ea85c59ec672 328 pc.printf("%s\r\n", buf);
WiredHome 0:ea85c59ec672 329 // To test, just enable the following, which tosses the ball back and forth
WiredHome 0:ea85c59ec672 330 //wait(0.2);
WiredHome 0:ea85c59ec672 331 //msg.dir = xmt; // What we received, we reflect back
WiredHome 0:ea85c59ec672 332 //CANTransmitMsg(msg);
WiredHome 0:ea85c59ec672 333 }
WiredHome 0:ea85c59ec672 334 }
WiredHome 0:ea85c59ec672 335 } while (cp_state == runok);
WiredHome 0:ea85c59ec672 336 cp->End();
WiredHome 0:ea85c59ec672 337 return 0;
WiredHome 0:ea85c59ec672 338 }
WiredHome 0:ea85c59ec672 339
WiredHome 0:ea85c59ec672 340