.
Dependencies: SDHCFileSystem mbed
main.cpp@11:d97071bf877b, 2012-12-09 (annotated)
- Committer:
- TickTock
- Date:
- Sun Dec 09 22:40:03 2012 +0000
- Revision:
- 11:d97071bf877b
- Parent:
- 10:663bff0a6144
- Child:
- 12:7cd4e9303165
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TickTock | 1:dafb963c3c14 | 1 | //CANcan.cpp |
TickTock | 8:6872945e8e91 | 2 | //A dual canbus monitoring "blackbox" application for the Nissan Leaf |
TickTock | 8:6872945e8e91 | 3 | //Dumps all messages to a file on the SDRAM |
TickTock | 8:6872945e8e91 | 4 | //Todo: |
TickTock | 8:6872945e8e91 | 5 | // bigger buffer to avoid overflow (or messge filtering - ignore 174?) |
TickTock | 8:6872945e8e91 | 6 | // |
TickTock | 8:6872945e8e91 | 7 | // Connections: |
TickTock | 1:dafb963c3c14 | 8 | //LEAF OBD |
TickTock | 7:281df0ba57d0 | 9 | //1: |
TickTock | 1:dafb963c3c14 | 10 | //2: |
TickTock | 1:dafb963c3c14 | 11 | //3: AVCAN-L White/Blue |
TickTock | 7:281df0ba57d0 | 12 | //4: |
TickTock | 1:dafb963c3c14 | 13 | //5: VSS Brown,White/Brown |
TickTock | 7:281df0ba57d0 | 14 | //6: CARCAN-H Green --> VP230b:7 |
TickTock | 1:dafb963c3c14 | 15 | //7: |
TickTock | 1:dafb963c3c14 | 16 | //8: 12V-SW Orange,White/Orange |
TickTock | 1:dafb963c3c14 | 17 | //9: |
TickTock | 1:dafb963c3c14 | 18 | //10: |
TickTock | 1:dafb963c3c14 | 19 | //11: AVCAN-H Blue |
TickTock | 7:281df0ba57d0 | 20 | //12: EVCAN-L White/Grey --> VP230a:6 |
TickTock | 7:281df0ba57d0 | 21 | //13: EVCAN-H Grey --> VP230a:7 |
TickTock | 7:281df0ba57d0 | 22 | //14: CARCAN-L White/Green --> VP230b:6 |
TickTock | 7:281df0ba57d0 | 23 | //15: 6V |
TickTock | 7:281df0ba57d0 | 24 | //16: 12V-AON Red/Blue,Blue/Red ----|<---- LPC1768:2 |
TickTock | 7:281df0ba57d0 | 25 | //note 1: pins 4 & 5 longer |
TickTock | 7:281df0ba57d0 | 26 | //note 2: pins 12 & 13 next to key |
TickTock | 7:281df0ba57d0 | 27 | //note 3: pins 1 & 9 on right side looking into male connector with key on bottom |
TickTock | 1:dafb963c3c14 | 28 | |
TickTock | 8:6872945e8e91 | 29 | //VP230{a,b} |
TickTock | 1:dafb963c3c14 | 30 | //1:D |
TickTock | 1:dafb963c3c14 | 31 | //2:GND |
TickTock | 1:dafb963c3c14 | 32 | //3:VCC |
TickTock | 1:dafb963c3c14 | 33 | //4:R |
TickTock | 1:dafb963c3c14 | 34 | //5:Vref |
TickTock | 7:281df0ba57d0 | 35 | //6:CANL --> OBD:12,14 |
TickTock | 7:281df0ba57d0 | 36 | //7:CANH --> OBD:13,6 |
TickTock | 7:281df0ba57d0 | 37 | //8:RS --> LPC1768:27,28 |
TickTock | 0:1596b8644523 | 38 | |
TickTock | 0:1596b8644523 | 39 | //LPC1768 |
TickTock | 8:6872945e8e91 | 40 | //1: VSS 6V |
TickTock | 8:6872945e8e91 | 41 | //2: VIN (4.5-9V supply) ---->|---- OBD:16 |
TickTock | 0:1596b8644523 | 42 | //3: NC:VB |
TickTock | 0:1596b8644523 | 43 | //4: NC:nR |
TickTock | 0:1596b8644523 | 44 | //5: SPI:MOSI --> 6:SDRAM:DI |
TickTock | 0:1596b8644523 | 45 | //6: SPI:MISO --> 2:SDRAM:DO |
TickTock | 0:1596b8644523 | 46 | //7: SPI:SCLK --> 4:SDRAM:SCLK |
TickTock | 0:1596b8644523 | 47 | //8: CS --> 7:SDRAM:CS |
TickTock | 1:dafb963c3c14 | 48 | //9: CAN1:RX --> 4:CAN1:R |
TickTock | 1:dafb963c3c14 | 49 | //10: CAN1:TX --> 1:CAN1:D |
TickTock | 8:6872945e8e91 | 50 | //11: NC:RS --> 4:LCD:RS |
TickTock | 8:6872945e8e91 | 51 | //12: NC:E --> 6:LCD:E |
TickTock | 8:6872945e8e91 | 52 | //13: NC:D4 --> 11:LCD:D4 |
TickTock | 8:6872945e8e91 | 53 | //14: NC:D5 --> 12:LCD:D5 |
TickTock | 8:6872945e8e91 | 54 | //15: NC:D6 --> 13:LCD:D6 |
TickTock | 8:6872945e8e91 | 55 | //16: NC:D7 --> 14:LCD:D7 |
TickTock | 0:1596b8644523 | 56 | //17: CD --> 1:SDRAM:CD |
TickTock | 8:6872945e8e91 | 57 | //18: NC:MON12V --> 4K to 12V, 1K to VSS (To be implemented) |
TickTock | 0:1596b8644523 | 58 | //19: PB2 |
TickTock | 0:1596b8644523 | 59 | //20: PB1 |
TickTock | 8:6872945e8e91 | 60 | //21: NC:Spkr+ |
TickTock | 8:6872945e8e91 | 61 | //22: NC:Spkr- (optional complimentary output for more volume) |
TickTock | 0:1596b8644523 | 62 | //23: NC:pwm |
TickTock | 8:6872945e8e91 | 63 | //24: NC:LEDBLU --> 18:LCD:BLU (only used for tri-color displays) |
TickTock | 8:6872945e8e91 | 64 | //25: NC:LEDGRN --> 17:LCD:GRN (only used for tri-color displays) |
TickTock | 8:6872945e8e91 | 65 | //26: NC:LEDRED --> 16:LCD:RED |
TickTock | 0:1596b8644523 | 66 | //27: CAN1:Sleep --> 8:CAN1:RS |
TickTock | 0:1596b8644523 | 67 | //28: CAN2:Sleep --> 8:CAN2:RS |
TickTock | 1:dafb963c3c14 | 68 | //29: CAN2:TX --> 1:CAN2:D |
TickTock | 1:dafb963c3c14 | 69 | //30: CAN2:RX --> 4:CAN2:R |
TickTock | 0:1596b8644523 | 70 | //31: NC:USB_D+ |
TickTock | 0:1596b8644523 | 71 | //32: NC:USB_D- |
TickTock | 0:1596b8644523 | 72 | //33: NC:Eth_TD+ |
TickTock | 0:1596b8644523 | 73 | //34: NC:Eth_TD- |
TickTock | 0:1596b8644523 | 74 | //35: NC:Eth_RD+ |
TickTock | 0:1596b8644523 | 75 | //36: NC:Eth_RD- |
TickTock | 0:1596b8644523 | 76 | //37: NC:IF+ |
TickTock | 0:1596b8644523 | 77 | //38: NC:IF- |
TickTock | 1:dafb963c3c14 | 78 | //39: NC:5Vout (only available when connected as USB device) |
TickTock | 0:1596b8644523 | 79 | //40: VCC3.3 |
TickTock | 0:1596b8644523 | 80 | |
TickTock | 8:6872945e8e91 | 81 | #include "mbed.h" |
TickTock | 8:6872945e8e91 | 82 | #include "CAN.h" |
TickTock | 8:6872945e8e91 | 83 | #include "SDHCFileSystem.h" |
TickTock | 11:d97071bf877b | 84 | #include "PowerControl/PowerControl.h" |
TickTock | 11:d97071bf877b | 85 | #include "PowerControl/EthernetPowerControl.h" |
TickTock | 8:6872945e8e91 | 86 | |
TickTock | 8:6872945e8e91 | 87 | #define upLine "\033[1A" |
TickTock | 8:6872945e8e91 | 88 | #define maxBufLen 2048 |
TickTock | 8:6872945e8e91 | 89 | #define canTimeout 5 |
TickTock | 8:6872945e8e91 | 90 | |
TickTock | 1:dafb963c3c14 | 91 | void Log (char *message); |
TickTock | 1:dafb963c3c14 | 92 | void LogErr (char *message); |
TickTock | 6:594d9717e609 | 93 | extern "C" void mbed_reset(); |
TickTock | 1:dafb963c3c14 | 94 | |
TickTock | 1:dafb963c3c14 | 95 | time_t seconds ; |
TickTock | 9:8396b76e42fc | 96 | DigitalIn CD(p17); |
TickTock | 9:8396b76e42fc | 97 | DigitalIn PB1( p20 ); |
TickTock | 1:dafb963c3c14 | 98 | SDFileSystem sd(p5, p6, p7, p8, "sd" ); // SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) |
TickTock | 1:dafb963c3c14 | 99 | Timer timer; |
TickTock | 0:1596b8644523 | 100 | DigitalOut led1(LED1); |
TickTock | 0:1596b8644523 | 101 | DigitalOut led2(LED2); |
TickTock | 0:1596b8644523 | 102 | DigitalOut led3(LED3); |
TickTock | 0:1596b8644523 | 103 | DigitalOut led4(LED4); |
TickTock | 1:dafb963c3c14 | 104 | CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (rx, tx) and pin 27 (rs) |
TickTock | 2:55b2357f0cf9 | 105 | DigitalOut can1_SleepMode(p27); // Use pin 27 to control the sleep mode of can1 |
TickTock | 1:dafb963c3c14 | 106 | CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (rx, tx) and pin 28 (rs) |
TickTock | 2:55b2357f0cf9 | 107 | DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 |
TickTock | 3:51134a275609 | 108 | bool logOpen = false; |
TickTock | 2:55b2357f0cf9 | 109 | FILE *file; |
TickTock | 2:55b2357f0cf9 | 110 | char fileName[32] = "" ; |
TickTock | 4:fb373a391155 | 111 | char writeBuffer[maxBufLen][13]; |
TickTock | 11:d97071bf877b | 112 | volatile int writePointer = 0; |
TickTock | 11:d97071bf877b | 113 | volatile int secsIdle = canTimeout; |
TickTock | 11:d97071bf877b | 114 | volatile bool canIdle = true; |
TickTock | 10:663bff0a6144 | 115 | Serial pc(USBTX, USBRX); |
TickTock | 0:1596b8644523 | 116 | |
TickTock | 2:55b2357f0cf9 | 117 | extern "C" void RTC_IRQHandler() { |
TickTock | 9:8396b76e42fc | 118 | timer.reset(); // zero ms at the-seconds-tic |
TickTock | 10:663bff0a6144 | 119 | canIdle=(++secsIdle>canTimeout); |
TickTock | 8:6872945e8e91 | 120 | LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next |
TickTock | 2:55b2357f0cf9 | 121 | } |
TickTock | 2:55b2357f0cf9 | 122 | |
TickTock | 2:55b2357f0cf9 | 123 | extern "C" void RTC_Init (void) { |
TickTock | 2:55b2357f0cf9 | 124 | LPC_RTC->ILR=0x00; // set up the RTC interrupts |
TickTock | 2:55b2357f0cf9 | 125 | LPC_RTC->CIIR=0x01; // interrupts each second |
TickTock | 8:6872945e8e91 | 126 | LPC_RTC->CCR = 0x01; // Clock enable |
TickTock | 9:8396b76e42fc | 127 | //NVIC_SetPriority( RTC_IRQn, 10 ); |
TickTock | 2:55b2357f0cf9 | 128 | NVIC_EnableIRQ( RTC_IRQn ); |
TickTock | 2:55b2357f0cf9 | 129 | } |
TickTock | 2:55b2357f0cf9 | 130 | |
TickTock | 2:55b2357f0cf9 | 131 | unsigned short getTimeStamp() { |
TickTock | 2:55b2357f0cf9 | 132 | int msec = timer.read_ms() ; // read ms from the timer |
TickTock | 2:55b2357f0cf9 | 133 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 2:55b2357f0cf9 | 134 | int isecs = secs%60 ; // modulo 60 for 0-59 seconds from RTC |
TickTock | 2:55b2357f0cf9 | 135 | return ((isecs<<10)+msec) ; // return the two byte time stamp |
TickTock | 1:dafb963c3c14 | 136 | } |
TickTock | 1:dafb963c3c14 | 137 | |
TickTock | 2:55b2357f0cf9 | 138 | void readLog (){ |
TickTock | 2:55b2357f0cf9 | 139 | unsigned char c; |
TickTock | 2:55b2357f0cf9 | 140 | int i=0; |
TickTock | 3:51134a275609 | 141 | char lastMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 142 | char curMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 143 | char canNum=0; |
TickTock | 2:55b2357f0cf9 | 144 | pc.printf("printing file\n"); |
TickTock | 2:55b2357f0cf9 | 145 | file = fopen(fileName, "r"); |
TickTock | 2:55b2357f0cf9 | 146 | if (file == NULL) { |
TickTock | 2:55b2357f0cf9 | 147 | pc.printf("no file found\n"); |
TickTock | 1:dafb963c3c14 | 148 | } |
TickTock | 2:55b2357f0cf9 | 149 | while (!feof(file)) { |
TickTock | 2:55b2357f0cf9 | 150 | c=fgetc(file); |
TickTock | 2:55b2357f0cf9 | 151 | pc.printf("%02x ",c); |
TickTock | 3:51134a275609 | 152 | if (i==0){ |
TickTock | 3:51134a275609 | 153 | canNum=c; |
TickTock | 3:51134a275609 | 154 | }else if (i==5){ |
TickTock | 3:51134a275609 | 155 | curMsgNum[canNum]=c; |
TickTock | 2:55b2357f0cf9 | 156 | } |
TickTock | 2:55b2357f0cf9 | 157 | if (++i>12) { |
TickTock | 3:51134a275609 | 158 | if (curMsgNum[canNum]>(lastMsgNum[canNum]+1)) { |
TickTock | 2:55b2357f0cf9 | 159 | pc.printf(" ***"); |
TickTock | 2:55b2357f0cf9 | 160 | } |
TickTock | 3:51134a275609 | 161 | lastMsgNum[canNum]=curMsgNum[canNum]; |
TickTock | 2:55b2357f0cf9 | 162 | pc.printf("\n"); |
TickTock | 2:55b2357f0cf9 | 163 | i=0; |
TickTock | 2:55b2357f0cf9 | 164 | } |
TickTock | 2:55b2357f0cf9 | 165 | } |
TickTock | 10:663bff0a6144 | 166 | pc.printf("\n\n"); |
TickTock | 2:55b2357f0cf9 | 167 | fclose(file); |
TickTock | 2:55b2357f0cf9 | 168 | } |
TickTock | 1:dafb963c3c14 | 169 | |
TickTock | 2:55b2357f0cf9 | 170 | void logCan (char mtype, CANMessage canRXmsg) { |
TickTock | 2:55b2357f0cf9 | 171 | unsigned short ts = getTimeStamp(); |
TickTock | 11:d97071bf877b | 172 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 9:8396b76e42fc | 173 | writeBuffer[writePointer][0]=mtype; |
TickTock | 11:d97071bf877b | 174 | writeBuffer[writePointer][1]=((secs%60)<<2)+((ts&0x300)>>8); |
TickTock | 9:8396b76e42fc | 175 | writeBuffer[writePointer][2]=ts&0xff; |
TickTock | 9:8396b76e42fc | 176 | writeBuffer[writePointer][3]=canRXmsg.id&0xff; |
TickTock | 9:8396b76e42fc | 177 | writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); |
TickTock | 2:55b2357f0cf9 | 178 | for (int i = 5; i<13; i++){ |
TickTock | 9:8396b76e42fc | 179 | writeBuffer[writePointer][i]=canRXmsg.data[i-5]; |
TickTock | 0:1596b8644523 | 180 | } |
TickTock | 9:8396b76e42fc | 181 | if (++writePointer >= maxBufLen) { |
TickTock | 9:8396b76e42fc | 182 | writePointer = 0; |
TickTock | 4:fb373a391155 | 183 | led4 = !led4; |
TickTock | 4:fb373a391155 | 184 | } |
TickTock | 1:dafb963c3c14 | 185 | } |
TickTock | 0:1596b8644523 | 186 | |
TickTock | 0:1596b8644523 | 187 | void recieve1() { |
TickTock | 2:55b2357f0cf9 | 188 | CANMessage msg1; |
TickTock | 9:8396b76e42fc | 189 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 190 | can1.read(msg1); |
TickTock | 11:d97071bf877b | 191 | if(logOpen) |
TickTock | 11:d97071bf877b | 192 | logCan(1, msg1); |
TickTock | 11:d97071bf877b | 193 | led1 = !led1; |
TickTock | 0:1596b8644523 | 194 | } |
TickTock | 2:55b2357f0cf9 | 195 | |
TickTock | 0:1596b8644523 | 196 | void recieve2() { |
TickTock | 2:55b2357f0cf9 | 197 | CANMessage msg2; |
TickTock | 9:8396b76e42fc | 198 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 199 | can2.read(msg2); |
TickTock | 11:d97071bf877b | 200 | if(logOpen) |
TickTock | 11:d97071bf877b | 201 | logCan(2, msg2); |
TickTock | 11:d97071bf877b | 202 | led2 = !led2; |
TickTock | 0:1596b8644523 | 203 | } |
TickTock | 0:1596b8644523 | 204 | |
TickTock | 0:1596b8644523 | 205 | int main() { |
TickTock | 9:8396b76e42fc | 206 | int readPointer=0; |
TickTock | 11:d97071bf877b | 207 | unsigned long secs; |
TickTock | 10:663bff0a6144 | 208 | pc.baud(460800); // change serial interface to pc to 450800, 8N1 |
TickTock | 2:55b2357f0cf9 | 209 | CANMessage tsMsg; |
TickTock | 7:281df0ba57d0 | 210 | can1.frequency(500000); |
TickTock | 7:281df0ba57d0 | 211 | can2.frequency(500000); |
TickTock | 10:663bff0a6144 | 212 | can1_SleepMode = 1; // Monitor_only Mode |
TickTock | 10:663bff0a6144 | 213 | can2_SleepMode = 1; // Monitor_only Mode |
TickTock | 0:1596b8644523 | 214 | CD.mode(PullUp) ; //SDRAM Chip Detect |
TickTock | 2:55b2357f0cf9 | 215 | PB1.mode(PullUp) ; //Pushbutton 1 |
TickTock | 0:1596b8644523 | 216 | can1.attach(&recieve1); |
TickTock | 0:1596b8644523 | 217 | can2.attach(&recieve2); |
TickTock | 1:dafb963c3c14 | 218 | |
TickTock | 1:dafb963c3c14 | 219 | timer.start() ; |
TickTock | 2:55b2357f0cf9 | 220 | RTC_Init(); // start the RTC Interrupts that sync the timer |
TickTock | 2:55b2357f0cf9 | 221 | wait(1.1); // give time to sync |
TickTock | 1:dafb963c3c14 | 222 | |
TickTock | 6:594d9717e609 | 223 | while (true) { |
TickTock | 6:594d9717e609 | 224 | if(CD == 1) { |
TickTock | 11:d97071bf877b | 225 | if (!logOpen) { // Open new file if one is not already open |
TickTock | 11:d97071bf877b | 226 | int fileNum = 0; |
TickTock | 11:d97071bf877b | 227 | sprintf(fileName,"/sd/cancan%d.alc",fileNum); |
TickTock | 6:594d9717e609 | 228 | file = fopen(fileName, "r"); |
TickTock | 11:d97071bf877b | 229 | while ((file != NULL)&&(fileNum++<100)) { |
TickTock | 11:d97071bf877b | 230 | fclose(file); |
TickTock | 11:d97071bf877b | 231 | sprintf(fileName,"/sd/cancan%d.alc",fileNum); |
TickTock | 11:d97071bf877b | 232 | file = fopen(fileName, "r"); |
TickTock | 11:d97071bf877b | 233 | } |
TickTock | 11:d97071bf877b | 234 | file = fopen(fileName, "wb"); |
TickTock | 11:d97071bf877b | 235 | if(file==NULL){ |
TickTock | 11:d97071bf877b | 236 | pc.printf("\nUnable to open canlog\n\n\n\n"); |
TickTock | 11:d97071bf877b | 237 | mbed_reset(); |
TickTock | 11:d97071bf877b | 238 | } else { |
TickTock | 11:d97071bf877b | 239 | logOpen = true; |
TickTock | 11:d97071bf877b | 240 | readPointer=writePointer; |
TickTock | 11:d97071bf877b | 241 | pc.printf("\nStarting Can Log %s\n\n\n\n",fileName); |
TickTock | 11:d97071bf877b | 242 | secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 11:d97071bf877b | 243 | tsMsg.id=0xfff; |
TickTock | 11:d97071bf877b | 244 | tsMsg.len=0xf; |
TickTock | 11:d97071bf877b | 245 | tsMsg.data[0]=secs&0xff; |
TickTock | 11:d97071bf877b | 246 | tsMsg.data[1]=(secs>>0)&0xff; |
TickTock | 11:d97071bf877b | 247 | tsMsg.data[2]=(secs>>16)&0xff; |
TickTock | 11:d97071bf877b | 248 | tsMsg.data[3]=secs>>24; |
TickTock | 11:d97071bf877b | 249 | tsMsg.data[4]=0xff; |
TickTock | 11:d97071bf877b | 250 | tsMsg.data[5]=0xff; |
TickTock | 11:d97071bf877b | 251 | tsMsg.data[6]=0xff; |
TickTock | 11:d97071bf877b | 252 | tsMsg.data[7]=0xff; |
TickTock | 11:d97071bf877b | 253 | logCan(0,tsMsg); //write full timestamp |
TickTock | 11:d97071bf877b | 254 | fclose(file); |
TickTock | 11:d97071bf877b | 255 | } |
TickTock | 11:d97071bf877b | 256 | } // if (!logOpen) |
TickTock | 9:8396b76e42fc | 257 | do { |
TickTock | 10:663bff0a6144 | 258 | if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/2)||canIdle||(PB1==0)) { |
TickTock | 10:663bff0a6144 | 259 | // Dump buffer if > 1/2 full, canbus has stopped, or PB1 pressed |
TickTock | 9:8396b76e42fc | 260 | if (logOpen) { |
TickTock | 9:8396b76e42fc | 261 | file = fopen(fileName, "ab"); |
TickTock | 9:8396b76e42fc | 262 | if (file == NULL) { |
TickTock | 9:8396b76e42fc | 263 | logOpen = false; |
TickTock | 9:8396b76e42fc | 264 | pc.printf("Failed to append log file.\n\n"); |
TickTock | 9:8396b76e42fc | 265 | } else { |
TickTock | 9:8396b76e42fc | 266 | while (readPointer != writePointer) { |
TickTock | 9:8396b76e42fc | 267 | for (int j = 0; j<13; j++){ |
TickTock | 9:8396b76e42fc | 268 | fprintf(file,"%c",writeBuffer[readPointer][j]); |
TickTock | 9:8396b76e42fc | 269 | } |
TickTock | 9:8396b76e42fc | 270 | if(++readPointer >= maxBufLen) |
TickTock | 9:8396b76e42fc | 271 | readPointer=0; |
TickTock | 9:8396b76e42fc | 272 | } |
TickTock | 9:8396b76e42fc | 273 | led3 = !led3; |
TickTock | 9:8396b76e42fc | 274 | fclose(file); |
TickTock | 9:8396b76e42fc | 275 | } |
TickTock | 10:663bff0a6144 | 276 | } // if (logOpen) |
TickTock | 9:8396b76e42fc | 277 | } |
TickTock | 10:663bff0a6144 | 278 | if (canIdle) { // canbus idle --> sleep to save power |
TickTock | 9:8396b76e42fc | 279 | LPC_RTC->CIIR=0x00; // block RTC interrupts |
TickTock | 10:663bff0a6144 | 280 | led1=0; |
TickTock | 10:663bff0a6144 | 281 | led2=0; |
TickTock | 10:663bff0a6144 | 282 | led3=0; |
TickTock | 10:663bff0a6144 | 283 | led4=0; |
TickTock | 11:d97071bf877b | 284 | secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 10:663bff0a6144 | 285 | while (secsIdle>canTimeout) |
TickTock | 11:d97071bf877b | 286 | //DeepPowerDown(); |
TickTock | 10:663bff0a6144 | 287 | __wfi(); // freeze CPU and wait for interrupt (from canbus) |
TickTock | 11:d97071bf877b | 288 | if (time(NULL)>(secs+3600)) |
TickTock | 11:d97071bf877b | 289 | logOpen = false; // Start new file if asleep for more than an hour |
TickTock | 10:663bff0a6144 | 290 | LPC_RTC->CIIR=0x01; // re-enable RTC interrupts |
TickTock | 9:8396b76e42fc | 291 | } |
TickTock | 10:663bff0a6144 | 292 | wait(0.25); // We get >2K messages per second |
TickTock | 11:d97071bf877b | 293 | } while ((PB1==1)&&(CD==1)&&logOpen); // keep going until button or SDram removed |
TickTock | 9:8396b76e42fc | 294 | |
TickTock | 11:d97071bf877b | 295 | if (PB1==0) { |
TickTock | 11:d97071bf877b | 296 | led1=0; |
TickTock | 11:d97071bf877b | 297 | led2=0; |
TickTock | 11:d97071bf877b | 298 | led3=0; |
TickTock | 11:d97071bf877b | 299 | led4=0; |
TickTock | 11:d97071bf877b | 300 | pc.printf("Log stopped\n\n"); |
TickTock | 11:d97071bf877b | 301 | logOpen=false; |
TickTock | 11:d97071bf877b | 302 | wait(5); // wait 5 seconds to give time to remove SDRAM if desired |
TickTock | 11:d97071bf877b | 303 | } |
TickTock | 11:d97071bf877b | 304 | } else { |
TickTock | 11:d97071bf877b | 305 | pc.printf("\nNo SDRAM Inserted.\n\n"); |
TickTock | 6:594d9717e609 | 306 | logOpen=false; |
TickTock | 11:d97071bf877b | 307 | led1=!led1; |
TickTock | 11:d97071bf877b | 308 | led2=led1; |
TickTock | 11:d97071bf877b | 309 | led3=led1; |
TickTock | 11:d97071bf877b | 310 | led4=led1; |
TickTock | 11:d97071bf877b | 311 | wait(5); |
TickTock | 10:663bff0a6144 | 312 | } //if (CD==1) |
TickTock | 9:8396b76e42fc | 313 | } //while (true) |
TickTock | 9:8396b76e42fc | 314 | } |
TickTock | 9:8396b76e42fc | 315 |