.
Dependencies: SDHCFileSystem mbed
main.cpp@22:605c2f33b340, 2013-01-27 (annotated)
- Committer:
- TickTock
- Date:
- Sun Jan 27 20:14:11 2013 +0000
- Revision:
- 22:605c2f33b340
- Parent:
- 21:a45a999d6da3
- Child:
- 23:7e6a9684f647
delete files older than 14 days; don't long msgId=0x000; reversed canbus 0,1 (EV&CAR was reversed in log); delay after SD write before shutting down to avoid corruption
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 | 22:605c2f33b340 | 114 | volatile int secsIdle = 0; |
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 | 22:605c2f33b340 | 174 | if(canRXmsg.id>0) { |
TickTock | 22:605c2f33b340 | 175 | writeBuffer[writePointer][0]=mtype; |
TickTock | 22:605c2f33b340 | 176 | writeBuffer[writePointer][1]=((secs%60)<<2)+((ts&0x300)>>8); |
TickTock | 22:605c2f33b340 | 177 | writeBuffer[writePointer][2]=ts&0xff; |
TickTock | 22:605c2f33b340 | 178 | writeBuffer[writePointer][3]=canRXmsg.id&0xff; |
TickTock | 22:605c2f33b340 | 179 | writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); |
TickTock | 22:605c2f33b340 | 180 | for (int i = 5; i<13; i++){ |
TickTock | 22:605c2f33b340 | 181 | writeBuffer[writePointer][i]=canRXmsg.data[i-5]; |
TickTock | 22:605c2f33b340 | 182 | } |
TickTock | 22:605c2f33b340 | 183 | if (++writePointer >= maxBufLen) { |
TickTock | 22:605c2f33b340 | 184 | writePointer = 0; |
TickTock | 22:605c2f33b340 | 185 | led4 = !led4; |
TickTock | 22:605c2f33b340 | 186 | } |
TickTock | 4:fb373a391155 | 187 | } |
TickTock | 1:dafb963c3c14 | 188 | } |
TickTock | 0:1596b8644523 | 189 | |
TickTock | 18:a4cf0547bfc2 | 190 | void logTS () { |
TickTock | 18:a4cf0547bfc2 | 191 | CANMessage tsMsg; |
TickTock | 18:a4cf0547bfc2 | 192 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 18:a4cf0547bfc2 | 193 | tsMsg.id=0xfff; |
TickTock | 18:a4cf0547bfc2 | 194 | tsMsg.len=0xf; |
TickTock | 18:a4cf0547bfc2 | 195 | tsMsg.data[0]=secs&0xff; |
TickTock | 18:a4cf0547bfc2 | 196 | tsMsg.data[1]=(secs>>8)&0xff; |
TickTock | 18:a4cf0547bfc2 | 197 | tsMsg.data[2]=(secs>>16)&0xff; |
TickTock | 18:a4cf0547bfc2 | 198 | tsMsg.data[3]=secs>>24; |
TickTock | 18:a4cf0547bfc2 | 199 | tsMsg.data[4]=0xff; |
TickTock | 18:a4cf0547bfc2 | 200 | tsMsg.data[5]=0xff; |
TickTock | 18:a4cf0547bfc2 | 201 | tsMsg.data[6]=0xff; |
TickTock | 18:a4cf0547bfc2 | 202 | tsMsg.data[7]=0xff; |
TickTock | 18:a4cf0547bfc2 | 203 | logCan(0,tsMsg); |
TickTock | 18:a4cf0547bfc2 | 204 | } |
TickTock | 0:1596b8644523 | 205 | void recieve1() { |
TickTock | 2:55b2357f0cf9 | 206 | CANMessage msg1; |
TickTock | 9:8396b76e42fc | 207 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 208 | can1.read(msg1); |
TickTock | 11:d97071bf877b | 209 | if(logOpen) |
TickTock | 22:605c2f33b340 | 210 | logCan(2, msg1); |
TickTock | 11:d97071bf877b | 211 | led1 = !led1; |
TickTock | 0:1596b8644523 | 212 | } |
TickTock | 2:55b2357f0cf9 | 213 | |
TickTock | 0:1596b8644523 | 214 | void recieve2() { |
TickTock | 2:55b2357f0cf9 | 215 | CANMessage msg2; |
TickTock | 9:8396b76e42fc | 216 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 217 | can2.read(msg2); |
TickTock | 11:d97071bf877b | 218 | if(logOpen) |
TickTock | 22:605c2f33b340 | 219 | logCan(1, msg2); |
TickTock | 11:d97071bf877b | 220 | led2 = !led2; |
TickTock | 0:1596b8644523 | 221 | } |
TickTock | 0:1596b8644523 | 222 | |
TickTock | 0:1596b8644523 | 223 | int main() { |
TickTock | 9:8396b76e42fc | 224 | int readPointer=0; |
TickTock | 21:a45a999d6da3 | 225 | int fmon; |
TickTock | 21:a45a999d6da3 | 226 | int fday; |
TickTock | 21:a45a999d6da3 | 227 | int ftime; |
TickTock | 21:a45a999d6da3 | 228 | char sTemp[35]; |
TickTock | 11:d97071bf877b | 229 | unsigned long secs; |
TickTock | 22:605c2f33b340 | 230 | bool bit = false; |
TickTock | 10:663bff0a6144 | 231 | pc.baud(460800); // change serial interface to pc to 450800, 8N1 |
TickTock | 7:281df0ba57d0 | 232 | can1.frequency(500000); |
TickTock | 7:281df0ba57d0 | 233 | can2.frequency(500000); |
TickTock | 10:663bff0a6144 | 234 | can1_SleepMode = 1; // Monitor_only Mode |
TickTock | 10:663bff0a6144 | 235 | can2_SleepMode = 1; // Monitor_only Mode |
TickTock | 0:1596b8644523 | 236 | CD.mode(PullUp) ; //SDRAM Chip Detect |
TickTock | 2:55b2357f0cf9 | 237 | PB1.mode(PullUp) ; //Pushbutton 1 |
TickTock | 0:1596b8644523 | 238 | can1.attach(&recieve1); |
TickTock | 0:1596b8644523 | 239 | can2.attach(&recieve2); |
TickTock | 1:dafb963c3c14 | 240 | |
TickTock | 1:dafb963c3c14 | 241 | timer.start() ; |
TickTock | 2:55b2357f0cf9 | 242 | RTC_Init(); // start the RTC Interrupts that sync the timer |
TickTock | 12:7cd4e9303165 | 243 | |
TickTock | 12:7cd4e9303165 | 244 | struct tm t; // pointer to a static tm structure |
TickTock | 12:7cd4e9303165 | 245 | |
TickTock | 12:7cd4e9303165 | 246 | seconds = time(NULL); |
TickTock | 12:7cd4e9303165 | 247 | t = *localtime(&seconds) ; |
TickTock | 14:ed5d2a503374 | 248 | strftime(sTemp, 32, "%a %m/%d/%Y %X", &t); |
TickTock | 14:ed5d2a503374 | 249 | pc.printf("\nCurrent time set to: %s\n", sTemp); // DAY MM/DD/YYYY HH:MM:SS |
TickTock | 14:ed5d2a503374 | 250 | wait(1.1); // give time to sync |
TickTock | 14:ed5d2a503374 | 251 | |
TickTock | 14:ed5d2a503374 | 252 | if (PB1==0) { //set time if pb pressed |
TickTock | 12:7cd4e9303165 | 253 | |
TickTock | 13:544b15b00c2d | 254 | pc.printf("\nEnter year (yyyy):"); |
TickTock | 13:544b15b00c2d | 255 | pc.scanf("%s", &sTemp); |
TickTock | 13:544b15b00c2d | 256 | t.tm_year = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 257 | |
TickTock | 13:544b15b00c2d | 258 | pc.printf("\nEnter month (mm):"); |
TickTock | 13:544b15b00c2d | 259 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 260 | t.tm_mon = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 261 | |
TickTock | 13:544b15b00c2d | 262 | pc.printf("\nEnter day (dd):"); |
TickTock | 13:544b15b00c2d | 263 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 264 | t.tm_mday = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 265 | |
TickTock | 13:544b15b00c2d | 266 | pc.printf("\nEnter hour (hh):"); |
TickTock | 13:544b15b00c2d | 267 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 268 | t.tm_hour = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 269 | |
TickTock | 13:544b15b00c2d | 270 | pc.printf("\nEnter minute (mm):"); |
TickTock | 13:544b15b00c2d | 271 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 272 | t.tm_min = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 273 | |
TickTock | 13:544b15b00c2d | 274 | pc.printf("\nEnter seconds (ss):"); |
TickTock | 13:544b15b00c2d | 275 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 276 | t.tm_sec = atoi(sTemp); |
TickTock | 12:7cd4e9303165 | 277 | |
TickTock | 12:7cd4e9303165 | 278 | // adjust for tm structure required values |
TickTock | 12:7cd4e9303165 | 279 | t.tm_year = t.tm_year - 1900; |
TickTock | 12:7cd4e9303165 | 280 | t.tm_mon = t.tm_mon - 1; |
TickTock | 12:7cd4e9303165 | 281 | |
TickTock | 12:7cd4e9303165 | 282 | // set the RTC |
TickTock | 12:7cd4e9303165 | 283 | set_time(mktime(&t)); |
TickTock | 12:7cd4e9303165 | 284 | seconds = time(NULL); |
TickTock | 12:7cd4e9303165 | 285 | |
TickTock | 14:ed5d2a503374 | 286 | pc.printf("\nRTC set to: " ); |
TickTock | 12:7cd4e9303165 | 287 | strftime(sTemp, 32, "%a %m/%d/%Y %X", localtime(&seconds)); |
TickTock | 12:7cd4e9303165 | 288 | pc.printf("%s\n", sTemp); // DAY MM/DD/YYYY HH:MM:SS |
TickTock | 12:7cd4e9303165 | 289 | } |
TickTock | 21:a45a999d6da3 | 290 | |
TickTock | 6:594d9717e609 | 291 | while (true) { |
TickTock | 6:594d9717e609 | 292 | if(CD == 1) { |
TickTock | 11:d97071bf877b | 293 | if (!logOpen) { // Open new file if one is not already open |
TickTock | 22:605c2f33b340 | 294 | seconds = time(NULL); |
TickTock | 21:a45a999d6da3 | 295 | t = *localtime(&seconds) ; |
TickTock | 21:a45a999d6da3 | 296 | strftime(fileName, 32, "/sd/%m%d%H%M.alc", &t); //mmddhhmm.alc |
TickTock | 21:a45a999d6da3 | 297 | |
TickTock | 15:3ff21ce810da | 298 | pc.printf("Using file %s\n",fileName); |
TickTock | 21:a45a999d6da3 | 299 | file = fopen(fileName, "ab"); |
TickTock | 15:3ff21ce810da | 300 | |
TickTock | 11:d97071bf877b | 301 | if(file==NULL){ |
TickTock | 11:d97071bf877b | 302 | pc.printf("\nUnable to open canlog\n\n\n\n"); |
TickTock | 11:d97071bf877b | 303 | mbed_reset(); |
TickTock | 11:d97071bf877b | 304 | } else { |
TickTock | 11:d97071bf877b | 305 | logOpen = true; |
TickTock | 11:d97071bf877b | 306 | readPointer=writePointer; |
TickTock | 15:3ff21ce810da | 307 | pc.printf("\nStarting Can Log %s\n",fileName); |
TickTock | 18:a4cf0547bfc2 | 308 | logTS(); |
TickTock | 21:a45a999d6da3 | 309 | fclose(file); |
TickTock | 21:a45a999d6da3 | 310 | file = fopen("/sd/loglog.txt", "a"); |
TickTock | 21:a45a999d6da3 | 311 | fprintf(file,"%s\r\n",fileName); |
TickTock | 11:d97071bf877b | 312 | fclose(file); |
TickTock | 11:d97071bf877b | 313 | } |
TickTock | 11:d97071bf877b | 314 | } // if (!logOpen) |
TickTock | 9:8396b76e42fc | 315 | do { |
TickTock | 10:663bff0a6144 | 316 | if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/2)||canIdle||(PB1==0)) { |
TickTock | 10:663bff0a6144 | 317 | // Dump buffer if > 1/2 full, canbus has stopped, or PB1 pressed |
TickTock | 9:8396b76e42fc | 318 | if (logOpen) { |
TickTock | 9:8396b76e42fc | 319 | file = fopen(fileName, "ab"); |
TickTock | 9:8396b76e42fc | 320 | if (file == NULL) { |
TickTock | 9:8396b76e42fc | 321 | logOpen = false; |
TickTock | 9:8396b76e42fc | 322 | pc.printf("Failed to append log file.\n\n"); |
TickTock | 9:8396b76e42fc | 323 | } else { |
TickTock | 9:8396b76e42fc | 324 | while (readPointer != writePointer) { |
TickTock | 9:8396b76e42fc | 325 | for (int j = 0; j<13; j++){ |
TickTock | 9:8396b76e42fc | 326 | fprintf(file,"%c",writeBuffer[readPointer][j]); |
TickTock | 9:8396b76e42fc | 327 | } |
TickTock | 9:8396b76e42fc | 328 | if(++readPointer >= maxBufLen) |
TickTock | 9:8396b76e42fc | 329 | readPointer=0; |
TickTock | 9:8396b76e42fc | 330 | } |
TickTock | 9:8396b76e42fc | 331 | led3 = !led3; |
TickTock | 9:8396b76e42fc | 332 | fclose(file); |
TickTock | 9:8396b76e42fc | 333 | } |
TickTock | 10:663bff0a6144 | 334 | } // if (logOpen) |
TickTock | 17:f0209f149b3e | 335 | } // if > 1/2 full, canbus has stopped, or PB1 pressed |
TickTock | 10:663bff0a6144 | 336 | if (canIdle) { // canbus idle --> sleep to save power |
TickTock | 21:a45a999d6da3 | 337 | // First take advantage of the idle time to clear some room |
TickTock | 22:605c2f33b340 | 338 | |
TickTock | 22:605c2f33b340 | 339 | bit = false; |
TickTock | 21:a45a999d6da3 | 340 | rfile = fopen("/sd/loglog.txt", "r"); |
TickTock | 21:a45a999d6da3 | 341 | file = fopen("/sd/loglog.new", "w"); |
TickTock | 21:a45a999d6da3 | 342 | while (!feof(rfile)) { |
TickTock | 21:a45a999d6da3 | 343 | fscanf(rfile,"/sd/%2d%2d%4d.alc\r\n",&fmon,&fday,&ftime); |
TickTock | 22:605c2f33b340 | 344 | //if ((fmon<t.tm_mon)&&(fday<=t.tm_mday)){ // Delete all files more than 1 month old |
TickTock | 22:605c2f33b340 | 345 | if ((fmon < 12) || (t.tm_mon > 1)){ |
TickTock | 22:605c2f33b340 | 346 | fday = fday + fmon*31; //crude - february will store 3 extra days of data |
TickTock | 22:605c2f33b340 | 347 | } |
TickTock | 22:605c2f33b340 | 348 | if ((fday+14)<(t.tm_mday+t.tm_mon*31)){ // Delete all files more than ~14 days old |
TickTock | 22:605c2f33b340 | 349 | bit=true; |
TickTock | 21:a45a999d6da3 | 350 | sprintf(sTemp,"/sd/%02d%02d%04d.alc",fmon,fday,ftime); |
TickTock | 22:605c2f33b340 | 351 | if ((remove(sTemp)==NULL)) { |
TickTock | 21:a45a999d6da3 | 352 | pc.printf("Removed file %s\n",sTemp); |
TickTock | 22:605c2f33b340 | 353 | } |
TickTock | 21:a45a999d6da3 | 354 | }else{ |
TickTock | 21:a45a999d6da3 | 355 | fprintf(file,"/sd/%02d%02d%04d.alc\r\n",fmon,fday,ftime); |
TickTock | 21:a45a999d6da3 | 356 | } |
TickTock | 21:a45a999d6da3 | 357 | } |
TickTock | 21:a45a999d6da3 | 358 | fclose (file); |
TickTock | 21:a45a999d6da3 | 359 | fclose (rfile); |
TickTock | 22:605c2f33b340 | 360 | if (bit) { |
TickTock | 22:605c2f33b340 | 361 | remove ("/sd/loglog.txt"); |
TickTock | 22:605c2f33b340 | 362 | //rename not working so do it the hard way |
TickTock | 22:605c2f33b340 | 363 | //rename ("/sd/loglog.new","/sd/loglog.txt"); |
TickTock | 22:605c2f33b340 | 364 | rfile = fopen("/sd/loglog.new", "r"); |
TickTock | 22:605c2f33b340 | 365 | file = fopen("/sd/loglog.txt", "w"); |
TickTock | 22:605c2f33b340 | 366 | while (!feof(rfile)) { |
TickTock | 22:605c2f33b340 | 367 | fscanf(rfile,"%s\r\n",&sTemp); |
TickTock | 22:605c2f33b340 | 368 | fprintf(file,"%s\r\n",sTemp); |
TickTock | 22:605c2f33b340 | 369 | } |
TickTock | 22:605c2f33b340 | 370 | fclose (file); |
TickTock | 22:605c2f33b340 | 371 | fclose (rfile); |
TickTock | 21:a45a999d6da3 | 372 | } |
TickTock | 22:605c2f33b340 | 373 | remove ("/sd/loglog.new"); |
TickTock | 22:605c2f33b340 | 374 | wait(5); // wait a few seconds to ensure SDRAM is done |
TickTock | 21:a45a999d6da3 | 375 | |
TickTock | 17:f0209f149b3e | 376 | pc.printf("Putting uC to sleep.\n"); |
TickTock | 13:544b15b00c2d | 377 | //LPC_RTC->CIIR=0x00; // block RTC interrupts |
TickTock | 10:663bff0a6144 | 378 | led1=0; |
TickTock | 10:663bff0a6144 | 379 | led2=0; |
TickTock | 10:663bff0a6144 | 380 | led3=0; |
TickTock | 10:663bff0a6144 | 381 | led4=0; |
TickTock | 11:d97071bf877b | 382 | secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 22:605c2f33b340 | 383 | while (secsIdle>canTimeout) { |
TickTock | 11:d97071bf877b | 384 | //DeepPowerDown(); |
TickTock | 10:663bff0a6144 | 385 | __wfi(); // freeze CPU and wait for interrupt (from canbus) |
TickTock | 22:605c2f33b340 | 386 | } |
TickTock | 22:605c2f33b340 | 387 | canIdle=false; |
TickTock | 22:605c2f33b340 | 388 | pc.printf("Waking uC.\n"); |
TickTock | 21:a45a999d6da3 | 389 | if (time(NULL)>(secs+1800)) { |
TickTock | 21:a45a999d6da3 | 390 | logOpen = false; // Start new file if asleep for more than 30 minutes |
TickTock | 18:a4cf0547bfc2 | 391 | } else { // insert timestamp on each wake |
TickTock | 18:a4cf0547bfc2 | 392 | logTS(); |
TickTock | 18:a4cf0547bfc2 | 393 | } |
TickTock | 13:544b15b00c2d | 394 | //LPC_RTC->CIIR=0x01; // re-enable RTC interrupts |
TickTock | 9:8396b76e42fc | 395 | } |
TickTock | 17:f0209f149b3e | 396 | wait(0.2); // We get >2K messages per second |
TickTock | 11:d97071bf877b | 397 | } while ((PB1==1)&&(CD==1)&&logOpen); // keep going until button or SDram removed |
TickTock | 9:8396b76e42fc | 398 | |
TickTock | 11:d97071bf877b | 399 | if (PB1==0) { |
TickTock | 11:d97071bf877b | 400 | led1=0; |
TickTock | 11:d97071bf877b | 401 | led2=0; |
TickTock | 11:d97071bf877b | 402 | led3=0; |
TickTock | 11:d97071bf877b | 403 | led4=0; |
TickTock | 11:d97071bf877b | 404 | pc.printf("Log stopped\n\n"); |
TickTock | 11:d97071bf877b | 405 | logOpen=false; |
TickTock | 11:d97071bf877b | 406 | wait(5); // wait 5 seconds to give time to remove SDRAM if desired |
TickTock | 17:f0209f149b3e | 407 | if (PB1==0) |
TickTock | 17:f0209f149b3e | 408 | readLog(); // dump file if PB still pressed |
TickTock | 11:d97071bf877b | 409 | } |
TickTock | 11:d97071bf877b | 410 | } else { |
TickTock | 11:d97071bf877b | 411 | pc.printf("\nNo SDRAM Inserted.\n\n"); |
TickTock | 6:594d9717e609 | 412 | logOpen=false; |
TickTock | 11:d97071bf877b | 413 | led1=!led1; |
TickTock | 11:d97071bf877b | 414 | led2=led1; |
TickTock | 11:d97071bf877b | 415 | led3=led1; |
TickTock | 11:d97071bf877b | 416 | led4=led1; |
TickTock | 11:d97071bf877b | 417 | wait(5); |
TickTock | 10:663bff0a6144 | 418 | } //if (CD==1) |
TickTock | 9:8396b76e42fc | 419 | } //while (true) |
TickTock | 9:8396b76e42fc | 420 | } |
TickTock | 9:8396b76e42fc | 421 |