.
Dependencies: SDHCFileSystem mbed
main.cpp@21:a45a999d6da3, 2012-12-24 (annotated)
- Committer:
- TickTock
- Date:
- Mon Dec 24 05:24:25 2012 +0000
- Revision:
- 21:a45a999d6da3
- Parent:
- 20:153800633e28
- Child:
- 22:605c2f33b340
Changed log filename to date mmddhhmm.alc.; Now delete all files > 1 month old when going to sleep
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 | 15:3ff21ce810da | 5 | // Get timestamp on files |
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 | 8:6872945e8e91 | 84 | |
TickTock | 8:6872945e8e91 | 85 | #define upLine "\033[1A" |
TickTock | 8:6872945e8e91 | 86 | #define maxBufLen 2048 |
TickTock | 8:6872945e8e91 | 87 | #define canTimeout 5 |
TickTock | 19:610a71236b6e | 88 | #define maxFileNum 30 |
TickTock | 8:6872945e8e91 | 89 | |
TickTock | 1:dafb963c3c14 | 90 | void Log (char *message); |
TickTock | 1:dafb963c3c14 | 91 | void LogErr (char *message); |
TickTock | 6:594d9717e609 | 92 | extern "C" void mbed_reset(); |
TickTock | 1:dafb963c3c14 | 93 | |
TickTock | 1:dafb963c3c14 | 94 | time_t seconds ; |
TickTock | 9:8396b76e42fc | 95 | DigitalIn CD(p17); |
TickTock | 9:8396b76e42fc | 96 | DigitalIn PB1( p20 ); |
TickTock | 1:dafb963c3c14 | 97 | SDFileSystem sd(p5, p6, p7, p8, "sd" ); // SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) |
TickTock | 1:dafb963c3c14 | 98 | Timer timer; |
TickTock | 0:1596b8644523 | 99 | DigitalOut led1(LED1); |
TickTock | 0:1596b8644523 | 100 | DigitalOut led2(LED2); |
TickTock | 0:1596b8644523 | 101 | DigitalOut led3(LED3); |
TickTock | 0:1596b8644523 | 102 | DigitalOut led4(LED4); |
TickTock | 1:dafb963c3c14 | 103 | CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (rx, tx) and pin 27 (rs) |
TickTock | 2:55b2357f0cf9 | 104 | DigitalOut can1_SleepMode(p27); // Use pin 27 to control the sleep mode of can1 |
TickTock | 1:dafb963c3c14 | 105 | CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (rx, tx) and pin 28 (rs) |
TickTock | 2:55b2357f0cf9 | 106 | DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 |
TickTock | 3:51134a275609 | 107 | bool logOpen = false; |
TickTock | 21:a45a999d6da3 | 108 | FILE *rfile; |
TickTock | 2:55b2357f0cf9 | 109 | FILE *file; |
TickTock | 17:f0209f149b3e | 110 | char fileName[35] = "" ; |
TickTock | 4:fb373a391155 | 111 | char writeBuffer[maxBufLen][13]; |
TickTock | 13:544b15b00c2d | 112 | char c; |
TickTock | 11:d97071bf877b | 113 | volatile int writePointer = 0; |
TickTock | 11:d97071bf877b | 114 | volatile int secsIdle = canTimeout; |
TickTock | 19:610a71236b6e | 115 | volatile bool canIdle = false; |
TickTock | 10:663bff0a6144 | 116 | Serial pc(USBTX, USBRX); |
TickTock | 0:1596b8644523 | 117 | |
TickTock | 2:55b2357f0cf9 | 118 | extern "C" void RTC_IRQHandler() { |
TickTock | 9:8396b76e42fc | 119 | timer.reset(); // zero ms at the-seconds-tic |
TickTock | 10:663bff0a6144 | 120 | canIdle=(++secsIdle>canTimeout); |
TickTock | 8:6872945e8e91 | 121 | LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next |
TickTock | 2:55b2357f0cf9 | 122 | } |
TickTock | 2:55b2357f0cf9 | 123 | |
TickTock | 2:55b2357f0cf9 | 124 | extern "C" void RTC_Init (void) { |
TickTock | 2:55b2357f0cf9 | 125 | LPC_RTC->ILR=0x00; // set up the RTC interrupts |
TickTock | 2:55b2357f0cf9 | 126 | LPC_RTC->CIIR=0x01; // interrupts each second |
TickTock | 8:6872945e8e91 | 127 | LPC_RTC->CCR = 0x01; // Clock enable |
TickTock | 9:8396b76e42fc | 128 | //NVIC_SetPriority( RTC_IRQn, 10 ); |
TickTock | 2:55b2357f0cf9 | 129 | NVIC_EnableIRQ( RTC_IRQn ); |
TickTock | 2:55b2357f0cf9 | 130 | } |
TickTock | 2:55b2357f0cf9 | 131 | |
TickTock | 2:55b2357f0cf9 | 132 | unsigned short getTimeStamp() { |
TickTock | 2:55b2357f0cf9 | 133 | int msec = timer.read_ms() ; // read ms from the timer |
TickTock | 2:55b2357f0cf9 | 134 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 2:55b2357f0cf9 | 135 | int isecs = secs%60 ; // modulo 60 for 0-59 seconds from RTC |
TickTock | 2:55b2357f0cf9 | 136 | return ((isecs<<10)+msec) ; // return the two byte time stamp |
TickTock | 1:dafb963c3c14 | 137 | } |
TickTock | 1:dafb963c3c14 | 138 | |
TickTock | 2:55b2357f0cf9 | 139 | void readLog (){ |
TickTock | 2:55b2357f0cf9 | 140 | unsigned char c; |
TickTock | 2:55b2357f0cf9 | 141 | int i=0; |
TickTock | 3:51134a275609 | 142 | char lastMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 143 | char curMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 144 | char canNum=0; |
TickTock | 2:55b2357f0cf9 | 145 | pc.printf("printing file\n"); |
TickTock | 2:55b2357f0cf9 | 146 | file = fopen(fileName, "r"); |
TickTock | 2:55b2357f0cf9 | 147 | if (file == NULL) { |
TickTock | 2:55b2357f0cf9 | 148 | pc.printf("no file found\n"); |
TickTock | 1:dafb963c3c14 | 149 | } |
TickTock | 2:55b2357f0cf9 | 150 | while (!feof(file)) { |
TickTock | 2:55b2357f0cf9 | 151 | c=fgetc(file); |
TickTock | 2:55b2357f0cf9 | 152 | pc.printf("%02x ",c); |
TickTock | 3:51134a275609 | 153 | if (i==0){ |
TickTock | 3:51134a275609 | 154 | canNum=c; |
TickTock | 3:51134a275609 | 155 | }else if (i==5){ |
TickTock | 3:51134a275609 | 156 | curMsgNum[canNum]=c; |
TickTock | 2:55b2357f0cf9 | 157 | } |
TickTock | 2:55b2357f0cf9 | 158 | if (++i>12) { |
TickTock | 3:51134a275609 | 159 | if (curMsgNum[canNum]>(lastMsgNum[canNum]+1)) { |
TickTock | 2:55b2357f0cf9 | 160 | pc.printf(" ***"); |
TickTock | 2:55b2357f0cf9 | 161 | } |
TickTock | 3:51134a275609 | 162 | lastMsgNum[canNum]=curMsgNum[canNum]; |
TickTock | 2:55b2357f0cf9 | 163 | pc.printf("\n"); |
TickTock | 2:55b2357f0cf9 | 164 | i=0; |
TickTock | 2:55b2357f0cf9 | 165 | } |
TickTock | 2:55b2357f0cf9 | 166 | } |
TickTock | 10:663bff0a6144 | 167 | pc.printf("\n\n"); |
TickTock | 2:55b2357f0cf9 | 168 | fclose(file); |
TickTock | 2:55b2357f0cf9 | 169 | } |
TickTock | 1:dafb963c3c14 | 170 | |
TickTock | 2:55b2357f0cf9 | 171 | void logCan (char mtype, CANMessage canRXmsg) { |
TickTock | 2:55b2357f0cf9 | 172 | unsigned short ts = getTimeStamp(); |
TickTock | 11:d97071bf877b | 173 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 9:8396b76e42fc | 174 | writeBuffer[writePointer][0]=mtype; |
TickTock | 11:d97071bf877b | 175 | writeBuffer[writePointer][1]=((secs%60)<<2)+((ts&0x300)>>8); |
TickTock | 9:8396b76e42fc | 176 | writeBuffer[writePointer][2]=ts&0xff; |
TickTock | 9:8396b76e42fc | 177 | writeBuffer[writePointer][3]=canRXmsg.id&0xff; |
TickTock | 9:8396b76e42fc | 178 | writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); |
TickTock | 2:55b2357f0cf9 | 179 | for (int i = 5; i<13; i++){ |
TickTock | 9:8396b76e42fc | 180 | writeBuffer[writePointer][i]=canRXmsg.data[i-5]; |
TickTock | 0:1596b8644523 | 181 | } |
TickTock | 9:8396b76e42fc | 182 | if (++writePointer >= maxBufLen) { |
TickTock | 9:8396b76e42fc | 183 | writePointer = 0; |
TickTock | 4:fb373a391155 | 184 | led4 = !led4; |
TickTock | 4:fb373a391155 | 185 | } |
TickTock | 1:dafb963c3c14 | 186 | } |
TickTock | 0:1596b8644523 | 187 | |
TickTock | 18:a4cf0547bfc2 | 188 | void logTS () { |
TickTock | 18:a4cf0547bfc2 | 189 | CANMessage tsMsg; |
TickTock | 18:a4cf0547bfc2 | 190 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 18:a4cf0547bfc2 | 191 | tsMsg.id=0xfff; |
TickTock | 18:a4cf0547bfc2 | 192 | tsMsg.len=0xf; |
TickTock | 18:a4cf0547bfc2 | 193 | tsMsg.data[0]=secs&0xff; |
TickTock | 18:a4cf0547bfc2 | 194 | tsMsg.data[1]=(secs>>8)&0xff; |
TickTock | 18:a4cf0547bfc2 | 195 | tsMsg.data[2]=(secs>>16)&0xff; |
TickTock | 18:a4cf0547bfc2 | 196 | tsMsg.data[3]=secs>>24; |
TickTock | 18:a4cf0547bfc2 | 197 | tsMsg.data[4]=0xff; |
TickTock | 18:a4cf0547bfc2 | 198 | tsMsg.data[5]=0xff; |
TickTock | 18:a4cf0547bfc2 | 199 | tsMsg.data[6]=0xff; |
TickTock | 18:a4cf0547bfc2 | 200 | tsMsg.data[7]=0xff; |
TickTock | 18:a4cf0547bfc2 | 201 | logCan(0,tsMsg); |
TickTock | 18:a4cf0547bfc2 | 202 | } |
TickTock | 0:1596b8644523 | 203 | void recieve1() { |
TickTock | 2:55b2357f0cf9 | 204 | CANMessage msg1; |
TickTock | 9:8396b76e42fc | 205 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 206 | can1.read(msg1); |
TickTock | 11:d97071bf877b | 207 | if(logOpen) |
TickTock | 11:d97071bf877b | 208 | logCan(1, msg1); |
TickTock | 11:d97071bf877b | 209 | led1 = !led1; |
TickTock | 0:1596b8644523 | 210 | } |
TickTock | 2:55b2357f0cf9 | 211 | |
TickTock | 0:1596b8644523 | 212 | void recieve2() { |
TickTock | 2:55b2357f0cf9 | 213 | CANMessage msg2; |
TickTock | 9:8396b76e42fc | 214 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 215 | can2.read(msg2); |
TickTock | 11:d97071bf877b | 216 | if(logOpen) |
TickTock | 11:d97071bf877b | 217 | logCan(2, msg2); |
TickTock | 11:d97071bf877b | 218 | led2 = !led2; |
TickTock | 0:1596b8644523 | 219 | } |
TickTock | 0:1596b8644523 | 220 | |
TickTock | 0:1596b8644523 | 221 | int main() { |
TickTock | 9:8396b76e42fc | 222 | int readPointer=0; |
TickTock | 21:a45a999d6da3 | 223 | int fmon; |
TickTock | 21:a45a999d6da3 | 224 | int fday; |
TickTock | 21:a45a999d6da3 | 225 | int ftime; |
TickTock | 21:a45a999d6da3 | 226 | char sTemp[35]; |
TickTock | 11:d97071bf877b | 227 | unsigned long secs; |
TickTock | 10:663bff0a6144 | 228 | pc.baud(460800); // change serial interface to pc to 450800, 8N1 |
TickTock | 7:281df0ba57d0 | 229 | can1.frequency(500000); |
TickTock | 7:281df0ba57d0 | 230 | can2.frequency(500000); |
TickTock | 10:663bff0a6144 | 231 | can1_SleepMode = 1; // Monitor_only Mode |
TickTock | 10:663bff0a6144 | 232 | can2_SleepMode = 1; // Monitor_only Mode |
TickTock | 0:1596b8644523 | 233 | CD.mode(PullUp) ; //SDRAM Chip Detect |
TickTock | 2:55b2357f0cf9 | 234 | PB1.mode(PullUp) ; //Pushbutton 1 |
TickTock | 0:1596b8644523 | 235 | can1.attach(&recieve1); |
TickTock | 0:1596b8644523 | 236 | can2.attach(&recieve2); |
TickTock | 1:dafb963c3c14 | 237 | |
TickTock | 1:dafb963c3c14 | 238 | timer.start() ; |
TickTock | 2:55b2357f0cf9 | 239 | RTC_Init(); // start the RTC Interrupts that sync the timer |
TickTock | 12:7cd4e9303165 | 240 | |
TickTock | 12:7cd4e9303165 | 241 | struct tm t; // pointer to a static tm structure |
TickTock | 12:7cd4e9303165 | 242 | |
TickTock | 12:7cd4e9303165 | 243 | seconds = time(NULL); |
TickTock | 12:7cd4e9303165 | 244 | t = *localtime(&seconds) ; |
TickTock | 14:ed5d2a503374 | 245 | strftime(sTemp, 32, "%a %m/%d/%Y %X", &t); |
TickTock | 14:ed5d2a503374 | 246 | pc.printf("\nCurrent time set to: %s\n", sTemp); // DAY MM/DD/YYYY HH:MM:SS |
TickTock | 14:ed5d2a503374 | 247 | wait(1.1); // give time to sync |
TickTock | 14:ed5d2a503374 | 248 | |
TickTock | 14:ed5d2a503374 | 249 | if (PB1==0) { //set time if pb pressed |
TickTock | 12:7cd4e9303165 | 250 | |
TickTock | 13:544b15b00c2d | 251 | pc.printf("\nEnter year (yyyy):"); |
TickTock | 13:544b15b00c2d | 252 | pc.scanf("%s", &sTemp); |
TickTock | 13:544b15b00c2d | 253 | t.tm_year = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 254 | |
TickTock | 13:544b15b00c2d | 255 | pc.printf("\nEnter month (mm):"); |
TickTock | 13:544b15b00c2d | 256 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 257 | t.tm_mon = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 258 | |
TickTock | 13:544b15b00c2d | 259 | pc.printf("\nEnter day (dd):"); |
TickTock | 13:544b15b00c2d | 260 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 261 | t.tm_mday = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 262 | |
TickTock | 13:544b15b00c2d | 263 | pc.printf("\nEnter hour (hh):"); |
TickTock | 13:544b15b00c2d | 264 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 265 | t.tm_hour = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 266 | |
TickTock | 13:544b15b00c2d | 267 | pc.printf("\nEnter minute (mm):"); |
TickTock | 13:544b15b00c2d | 268 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 269 | t.tm_min = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 270 | |
TickTock | 13:544b15b00c2d | 271 | pc.printf("\nEnter seconds (ss):"); |
TickTock | 13:544b15b00c2d | 272 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 273 | t.tm_sec = atoi(sTemp); |
TickTock | 12:7cd4e9303165 | 274 | |
TickTock | 12:7cd4e9303165 | 275 | // adjust for tm structure required values |
TickTock | 12:7cd4e9303165 | 276 | t.tm_year = t.tm_year - 1900; |
TickTock | 12:7cd4e9303165 | 277 | t.tm_mon = t.tm_mon - 1; |
TickTock | 12:7cd4e9303165 | 278 | |
TickTock | 12:7cd4e9303165 | 279 | // set the RTC |
TickTock | 12:7cd4e9303165 | 280 | set_time(mktime(&t)); |
TickTock | 12:7cd4e9303165 | 281 | seconds = time(NULL); |
TickTock | 12:7cd4e9303165 | 282 | |
TickTock | 14:ed5d2a503374 | 283 | pc.printf("\nRTC set to: " ); |
TickTock | 12:7cd4e9303165 | 284 | strftime(sTemp, 32, "%a %m/%d/%Y %X", localtime(&seconds)); |
TickTock | 12:7cd4e9303165 | 285 | pc.printf("%s\n", sTemp); // DAY MM/DD/YYYY HH:MM:SS |
TickTock | 12:7cd4e9303165 | 286 | } |
TickTock | 21:a45a999d6da3 | 287 | /*if (t.tm_year < 10) {//for test - delete |
TickTock | 21:a45a999d6da3 | 288 | t.tm_year = 2012-1900;//delete |
TickTock | 21:a45a999d6da3 | 289 | set_time(mktime(&t));//delete |
TickTock | 21:a45a999d6da3 | 290 | seconds = time(NULL);//delete |
TickTock | 21:a45a999d6da3 | 291 | } |
TickTock | 21:a45a999d6da3 | 292 | t.tm_mon=(t.tm_mon+1)%12; //for test - delete |
TickTock | 21:a45a999d6da3 | 293 | t.tm_mday=(t.tm_mday+1)%30; //for test - delete |
TickTock | 21:a45a999d6da3 | 294 | set_time(mktime(&t));//delete |
TickTock | 21:a45a999d6da3 | 295 | seconds = time(NULL);//delete*/ |
TickTock | 21:a45a999d6da3 | 296 | |
TickTock | 6:594d9717e609 | 297 | while (true) { |
TickTock | 6:594d9717e609 | 298 | if(CD == 1) { |
TickTock | 11:d97071bf877b | 299 | if (!logOpen) { // Open new file if one is not already open |
TickTock | 21:a45a999d6da3 | 300 | t = *localtime(&seconds) ; |
TickTock | 21:a45a999d6da3 | 301 | strftime(fileName, 32, "/sd/%m%d%H%M.alc", &t); //mmddhhmm.alc |
TickTock | 21:a45a999d6da3 | 302 | |
TickTock | 15:3ff21ce810da | 303 | pc.printf("Using file %s\n",fileName); |
TickTock | 21:a45a999d6da3 | 304 | file = fopen(fileName, "ab"); |
TickTock | 15:3ff21ce810da | 305 | |
TickTock | 11:d97071bf877b | 306 | if(file==NULL){ |
TickTock | 11:d97071bf877b | 307 | pc.printf("\nUnable to open canlog\n\n\n\n"); |
TickTock | 11:d97071bf877b | 308 | mbed_reset(); |
TickTock | 11:d97071bf877b | 309 | } else { |
TickTock | 11:d97071bf877b | 310 | logOpen = true; |
TickTock | 11:d97071bf877b | 311 | readPointer=writePointer; |
TickTock | 15:3ff21ce810da | 312 | pc.printf("\nStarting Can Log %s\n",fileName); |
TickTock | 18:a4cf0547bfc2 | 313 | logTS(); |
TickTock | 21:a45a999d6da3 | 314 | fclose(file); |
TickTock | 21:a45a999d6da3 | 315 | file = fopen("/sd/loglog.txt", "a"); |
TickTock | 21:a45a999d6da3 | 316 | fprintf(file,"%s\r\n",fileName); |
TickTock | 11:d97071bf877b | 317 | fclose(file); |
TickTock | 11:d97071bf877b | 318 | } |
TickTock | 11:d97071bf877b | 319 | } // if (!logOpen) |
TickTock | 9:8396b76e42fc | 320 | do { |
TickTock | 10:663bff0a6144 | 321 | if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/2)||canIdle||(PB1==0)) { |
TickTock | 10:663bff0a6144 | 322 | // Dump buffer if > 1/2 full, canbus has stopped, or PB1 pressed |
TickTock | 9:8396b76e42fc | 323 | if (logOpen) { |
TickTock | 9:8396b76e42fc | 324 | file = fopen(fileName, "ab"); |
TickTock | 9:8396b76e42fc | 325 | if (file == NULL) { |
TickTock | 9:8396b76e42fc | 326 | logOpen = false; |
TickTock | 9:8396b76e42fc | 327 | pc.printf("Failed to append log file.\n\n"); |
TickTock | 9:8396b76e42fc | 328 | } else { |
TickTock | 9:8396b76e42fc | 329 | while (readPointer != writePointer) { |
TickTock | 9:8396b76e42fc | 330 | for (int j = 0; j<13; j++){ |
TickTock | 9:8396b76e42fc | 331 | fprintf(file,"%c",writeBuffer[readPointer][j]); |
TickTock | 9:8396b76e42fc | 332 | } |
TickTock | 9:8396b76e42fc | 333 | if(++readPointer >= maxBufLen) |
TickTock | 9:8396b76e42fc | 334 | readPointer=0; |
TickTock | 9:8396b76e42fc | 335 | } |
TickTock | 9:8396b76e42fc | 336 | led3 = !led3; |
TickTock | 9:8396b76e42fc | 337 | fclose(file); |
TickTock | 9:8396b76e42fc | 338 | } |
TickTock | 10:663bff0a6144 | 339 | } // if (logOpen) |
TickTock | 17:f0209f149b3e | 340 | } // if > 1/2 full, canbus has stopped, or PB1 pressed |
TickTock | 10:663bff0a6144 | 341 | if (canIdle) { // canbus idle --> sleep to save power |
TickTock | 21:a45a999d6da3 | 342 | // First take advantage of the idle time to clear some room |
TickTock | 21:a45a999d6da3 | 343 | // Delete all files more than 1 month old |
TickTock | 21:a45a999d6da3 | 344 | rfile = fopen("/sd/loglog.txt", "r"); |
TickTock | 21:a45a999d6da3 | 345 | file = fopen("/sd/loglog.new", "w"); |
TickTock | 21:a45a999d6da3 | 346 | while (!feof(rfile)) { |
TickTock | 21:a45a999d6da3 | 347 | fscanf(rfile,"/sd/%2d%2d%4d.alc\r\n",&fmon,&fday,&ftime); |
TickTock | 21:a45a999d6da3 | 348 | if ((fmon<t.tm_mon)&&(fday<=t.tm_mday)){ |
TickTock | 21:a45a999d6da3 | 349 | sprintf(sTemp,"/sd/%02d%02d%04d.alc",fmon,fday,ftime); |
TickTock | 21:a45a999d6da3 | 350 | if ((remove(sTemp)==NULL)) |
TickTock | 21:a45a999d6da3 | 351 | pc.printf("Removed file %s\n",sTemp); |
TickTock | 21:a45a999d6da3 | 352 | }else{ |
TickTock | 21:a45a999d6da3 | 353 | fprintf(file,"/sd/%02d%02d%04d.alc\r\n",fmon,fday,ftime); |
TickTock | 21:a45a999d6da3 | 354 | } |
TickTock | 21:a45a999d6da3 | 355 | } |
TickTock | 21:a45a999d6da3 | 356 | |
TickTock | 21:a45a999d6da3 | 357 | fclose (file); |
TickTock | 21:a45a999d6da3 | 358 | fclose (rfile); |
TickTock | 21:a45a999d6da3 | 359 | remove ("/sd/loglog.txt"); |
TickTock | 21:a45a999d6da3 | 360 | //rename not working so do it the hard way |
TickTock | 21:a45a999d6da3 | 361 | //rename ("/sd/loglog.new","/sd/loglog.txt"); |
TickTock | 21:a45a999d6da3 | 362 | rfile = fopen("/sd/loglog.new", "r"); |
TickTock | 21:a45a999d6da3 | 363 | file = fopen("/sd/loglog.txt", "w"); |
TickTock | 21:a45a999d6da3 | 364 | while (!feof(rfile)) { |
TickTock | 21:a45a999d6da3 | 365 | fscanf(rfile,"%s\r\n",&sTemp); |
TickTock | 21:a45a999d6da3 | 366 | fprintf(file,"%s\r\n",sTemp); |
TickTock | 21:a45a999d6da3 | 367 | } |
TickTock | 21:a45a999d6da3 | 368 | fclose (file); |
TickTock | 21:a45a999d6da3 | 369 | fclose (rfile); |
TickTock | 21:a45a999d6da3 | 370 | remove ("/sd/loglog.new"); |
TickTock | 21:a45a999d6da3 | 371 | |
TickTock | 17:f0209f149b3e | 372 | pc.printf("Putting uC to sleep.\n"); |
TickTock | 13:544b15b00c2d | 373 | //LPC_RTC->CIIR=0x00; // block RTC interrupts |
TickTock | 10:663bff0a6144 | 374 | led1=0; |
TickTock | 10:663bff0a6144 | 375 | led2=0; |
TickTock | 10:663bff0a6144 | 376 | led3=0; |
TickTock | 10:663bff0a6144 | 377 | led4=0; |
TickTock | 11:d97071bf877b | 378 | secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 10:663bff0a6144 | 379 | while (secsIdle>canTimeout) |
TickTock | 11:d97071bf877b | 380 | //DeepPowerDown(); |
TickTock | 10:663bff0a6144 | 381 | __wfi(); // freeze CPU and wait for interrupt (from canbus) |
TickTock | 21:a45a999d6da3 | 382 | if (time(NULL)>(secs+1800)) { |
TickTock | 21:a45a999d6da3 | 383 | logOpen = false; // Start new file if asleep for more than 30 minutes |
TickTock | 18:a4cf0547bfc2 | 384 | } else { // insert timestamp on each wake |
TickTock | 18:a4cf0547bfc2 | 385 | logTS(); |
TickTock | 18:a4cf0547bfc2 | 386 | } |
TickTock | 13:544b15b00c2d | 387 | //LPC_RTC->CIIR=0x01; // re-enable RTC interrupts |
TickTock | 9:8396b76e42fc | 388 | } |
TickTock | 17:f0209f149b3e | 389 | wait(0.2); // We get >2K messages per second |
TickTock | 11:d97071bf877b | 390 | } while ((PB1==1)&&(CD==1)&&logOpen); // keep going until button or SDram removed |
TickTock | 9:8396b76e42fc | 391 | |
TickTock | 11:d97071bf877b | 392 | if (PB1==0) { |
TickTock | 11:d97071bf877b | 393 | led1=0; |
TickTock | 11:d97071bf877b | 394 | led2=0; |
TickTock | 11:d97071bf877b | 395 | led3=0; |
TickTock | 11:d97071bf877b | 396 | led4=0; |
TickTock | 11:d97071bf877b | 397 | pc.printf("Log stopped\n\n"); |
TickTock | 11:d97071bf877b | 398 | logOpen=false; |
TickTock | 11:d97071bf877b | 399 | wait(5); // wait 5 seconds to give time to remove SDRAM if desired |
TickTock | 17:f0209f149b3e | 400 | if (PB1==0) |
TickTock | 17:f0209f149b3e | 401 | readLog(); // dump file if PB still pressed |
TickTock | 11:d97071bf877b | 402 | } |
TickTock | 11:d97071bf877b | 403 | } else { |
TickTock | 11:d97071bf877b | 404 | pc.printf("\nNo SDRAM Inserted.\n\n"); |
TickTock | 6:594d9717e609 | 405 | logOpen=false; |
TickTock | 11:d97071bf877b | 406 | led1=!led1; |
TickTock | 11:d97071bf877b | 407 | led2=led1; |
TickTock | 11:d97071bf877b | 408 | led3=led1; |
TickTock | 11:d97071bf877b | 409 | led4=led1; |
TickTock | 11:d97071bf877b | 410 | wait(5); |
TickTock | 10:663bff0a6144 | 411 | } //if (CD==1) |
TickTock | 9:8396b76e42fc | 412 | } //while (true) |
TickTock | 9:8396b76e42fc | 413 | } |
TickTock | 9:8396b76e42fc | 414 |