.
Dependencies: SDHCFileSystem mbed
main.cpp@13:544b15b00c2d, 2012-12-16 (annotated)
- Committer:
- TickTock
- Date:
- Sun Dec 16 00:25:57 2012 +0000
- Revision:
- 13:544b15b00c2d
- Parent:
- 12:7cd4e9303165
- Child:
- 14:ed5d2a503374
Programs RTC from pc
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 | 13:544b15b00c2d | 112 | char c; |
TickTock | 12:7cd4e9303165 | 113 | char sTemp[35] ; |
TickTock | 11:d97071bf877b | 114 | volatile int writePointer = 0; |
TickTock | 11:d97071bf877b | 115 | volatile int secsIdle = canTimeout; |
TickTock | 11:d97071bf877b | 116 | volatile bool canIdle = true; |
TickTock | 10:663bff0a6144 | 117 | Serial pc(USBTX, USBRX); |
TickTock | 0:1596b8644523 | 118 | |
TickTock | 2:55b2357f0cf9 | 119 | extern "C" void RTC_IRQHandler() { |
TickTock | 9:8396b76e42fc | 120 | timer.reset(); // zero ms at the-seconds-tic |
TickTock | 10:663bff0a6144 | 121 | canIdle=(++secsIdle>canTimeout); |
TickTock | 8:6872945e8e91 | 122 | LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next |
TickTock | 2:55b2357f0cf9 | 123 | } |
TickTock | 2:55b2357f0cf9 | 124 | |
TickTock | 2:55b2357f0cf9 | 125 | extern "C" void RTC_Init (void) { |
TickTock | 2:55b2357f0cf9 | 126 | LPC_RTC->ILR=0x00; // set up the RTC interrupts |
TickTock | 2:55b2357f0cf9 | 127 | LPC_RTC->CIIR=0x01; // interrupts each second |
TickTock | 8:6872945e8e91 | 128 | LPC_RTC->CCR = 0x01; // Clock enable |
TickTock | 9:8396b76e42fc | 129 | //NVIC_SetPriority( RTC_IRQn, 10 ); |
TickTock | 2:55b2357f0cf9 | 130 | NVIC_EnableIRQ( RTC_IRQn ); |
TickTock | 2:55b2357f0cf9 | 131 | } |
TickTock | 2:55b2357f0cf9 | 132 | |
TickTock | 2:55b2357f0cf9 | 133 | unsigned short getTimeStamp() { |
TickTock | 2:55b2357f0cf9 | 134 | int msec = timer.read_ms() ; // read ms from the timer |
TickTock | 2:55b2357f0cf9 | 135 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 2:55b2357f0cf9 | 136 | int isecs = secs%60 ; // modulo 60 for 0-59 seconds from RTC |
TickTock | 2:55b2357f0cf9 | 137 | return ((isecs<<10)+msec) ; // return the two byte time stamp |
TickTock | 1:dafb963c3c14 | 138 | } |
TickTock | 1:dafb963c3c14 | 139 | |
TickTock | 2:55b2357f0cf9 | 140 | void readLog (){ |
TickTock | 2:55b2357f0cf9 | 141 | unsigned char c; |
TickTock | 2:55b2357f0cf9 | 142 | int i=0; |
TickTock | 3:51134a275609 | 143 | char lastMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 144 | char curMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 145 | char canNum=0; |
TickTock | 2:55b2357f0cf9 | 146 | pc.printf("printing file\n"); |
TickTock | 2:55b2357f0cf9 | 147 | file = fopen(fileName, "r"); |
TickTock | 2:55b2357f0cf9 | 148 | if (file == NULL) { |
TickTock | 2:55b2357f0cf9 | 149 | pc.printf("no file found\n"); |
TickTock | 1:dafb963c3c14 | 150 | } |
TickTock | 2:55b2357f0cf9 | 151 | while (!feof(file)) { |
TickTock | 2:55b2357f0cf9 | 152 | c=fgetc(file); |
TickTock | 2:55b2357f0cf9 | 153 | pc.printf("%02x ",c); |
TickTock | 3:51134a275609 | 154 | if (i==0){ |
TickTock | 3:51134a275609 | 155 | canNum=c; |
TickTock | 3:51134a275609 | 156 | }else if (i==5){ |
TickTock | 3:51134a275609 | 157 | curMsgNum[canNum]=c; |
TickTock | 2:55b2357f0cf9 | 158 | } |
TickTock | 2:55b2357f0cf9 | 159 | if (++i>12) { |
TickTock | 3:51134a275609 | 160 | if (curMsgNum[canNum]>(lastMsgNum[canNum]+1)) { |
TickTock | 2:55b2357f0cf9 | 161 | pc.printf(" ***"); |
TickTock | 2:55b2357f0cf9 | 162 | } |
TickTock | 3:51134a275609 | 163 | lastMsgNum[canNum]=curMsgNum[canNum]; |
TickTock | 2:55b2357f0cf9 | 164 | pc.printf("\n"); |
TickTock | 2:55b2357f0cf9 | 165 | i=0; |
TickTock | 2:55b2357f0cf9 | 166 | } |
TickTock | 2:55b2357f0cf9 | 167 | } |
TickTock | 10:663bff0a6144 | 168 | pc.printf("\n\n"); |
TickTock | 2:55b2357f0cf9 | 169 | fclose(file); |
TickTock | 2:55b2357f0cf9 | 170 | } |
TickTock | 1:dafb963c3c14 | 171 | |
TickTock | 2:55b2357f0cf9 | 172 | void logCan (char mtype, CANMessage canRXmsg) { |
TickTock | 2:55b2357f0cf9 | 173 | unsigned short ts = getTimeStamp(); |
TickTock | 11:d97071bf877b | 174 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 9:8396b76e42fc | 175 | writeBuffer[writePointer][0]=mtype; |
TickTock | 11:d97071bf877b | 176 | writeBuffer[writePointer][1]=((secs%60)<<2)+((ts&0x300)>>8); |
TickTock | 9:8396b76e42fc | 177 | writeBuffer[writePointer][2]=ts&0xff; |
TickTock | 9:8396b76e42fc | 178 | writeBuffer[writePointer][3]=canRXmsg.id&0xff; |
TickTock | 9:8396b76e42fc | 179 | writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); |
TickTock | 2:55b2357f0cf9 | 180 | for (int i = 5; i<13; i++){ |
TickTock | 9:8396b76e42fc | 181 | writeBuffer[writePointer][i]=canRXmsg.data[i-5]; |
TickTock | 0:1596b8644523 | 182 | } |
TickTock | 9:8396b76e42fc | 183 | if (++writePointer >= maxBufLen) { |
TickTock | 9:8396b76e42fc | 184 | writePointer = 0; |
TickTock | 4:fb373a391155 | 185 | led4 = !led4; |
TickTock | 4:fb373a391155 | 186 | } |
TickTock | 1:dafb963c3c14 | 187 | } |
TickTock | 0:1596b8644523 | 188 | |
TickTock | 0:1596b8644523 | 189 | void recieve1() { |
TickTock | 2:55b2357f0cf9 | 190 | CANMessage msg1; |
TickTock | 9:8396b76e42fc | 191 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 192 | can1.read(msg1); |
TickTock | 11:d97071bf877b | 193 | if(logOpen) |
TickTock | 11:d97071bf877b | 194 | logCan(1, msg1); |
TickTock | 11:d97071bf877b | 195 | led1 = !led1; |
TickTock | 0:1596b8644523 | 196 | } |
TickTock | 2:55b2357f0cf9 | 197 | |
TickTock | 0:1596b8644523 | 198 | void recieve2() { |
TickTock | 2:55b2357f0cf9 | 199 | CANMessage msg2; |
TickTock | 9:8396b76e42fc | 200 | secsIdle=0; // reset deadman switch |
TickTock | 0:1596b8644523 | 201 | can2.read(msg2); |
TickTock | 11:d97071bf877b | 202 | if(logOpen) |
TickTock | 11:d97071bf877b | 203 | logCan(2, msg2); |
TickTock | 11:d97071bf877b | 204 | led2 = !led2; |
TickTock | 0:1596b8644523 | 205 | } |
TickTock | 0:1596b8644523 | 206 | |
TickTock | 0:1596b8644523 | 207 | int main() { |
TickTock | 9:8396b76e42fc | 208 | int readPointer=0; |
TickTock | 11:d97071bf877b | 209 | unsigned long secs; |
TickTock | 10:663bff0a6144 | 210 | pc.baud(460800); // change serial interface to pc to 450800, 8N1 |
TickTock | 2:55b2357f0cf9 | 211 | CANMessage tsMsg; |
TickTock | 7:281df0ba57d0 | 212 | can1.frequency(500000); |
TickTock | 7:281df0ba57d0 | 213 | can2.frequency(500000); |
TickTock | 10:663bff0a6144 | 214 | can1_SleepMode = 1; // Monitor_only Mode |
TickTock | 10:663bff0a6144 | 215 | can2_SleepMode = 1; // Monitor_only Mode |
TickTock | 0:1596b8644523 | 216 | CD.mode(PullUp) ; //SDRAM Chip Detect |
TickTock | 2:55b2357f0cf9 | 217 | PB1.mode(PullUp) ; //Pushbutton 1 |
TickTock | 0:1596b8644523 | 218 | can1.attach(&recieve1); |
TickTock | 0:1596b8644523 | 219 | can2.attach(&recieve2); |
TickTock | 1:dafb963c3c14 | 220 | |
TickTock | 1:dafb963c3c14 | 221 | timer.start() ; |
TickTock | 2:55b2357f0cf9 | 222 | RTC_Init(); // start the RTC Interrupts that sync the timer |
TickTock | 2:55b2357f0cf9 | 223 | wait(1.1); // give time to sync |
TickTock | 12:7cd4e9303165 | 224 | |
TickTock | 12:7cd4e9303165 | 225 | //-------------------------- |
TickTock | 12:7cd4e9303165 | 226 | // check for 2012 or later |
TickTock | 12:7cd4e9303165 | 227 | |
TickTock | 12:7cd4e9303165 | 228 | struct tm t; // pointer to a static tm structure |
TickTock | 12:7cd4e9303165 | 229 | |
TickTock | 12:7cd4e9303165 | 230 | seconds = time(NULL); |
TickTock | 12:7cd4e9303165 | 231 | t = *localtime(&seconds) ; |
TickTock | 12:7cd4e9303165 | 232 | |
TickTock | 12:7cd4e9303165 | 233 | // is it a date before 2012 ? |
TickTock | 12:7cd4e9303165 | 234 | if ((t.tm_year + 1900) < 2012 ) { |
TickTock | 12:7cd4e9303165 | 235 | // before 2012, so the RTC probably lost power |
TickTock | 12:7cd4e9303165 | 236 | // So, set a near-recent date in 2012 |
TickTock | 12:7cd4e9303165 | 237 | |
TickTock | 12:7cd4e9303165 | 238 | // enter people-values here |
TickTock | 13:544b15b00c2d | 239 | pc.printf("\nEnter year (yyyy):"); |
TickTock | 13:544b15b00c2d | 240 | pc.scanf("%s", &sTemp); |
TickTock | 13:544b15b00c2d | 241 | t.tm_year = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 242 | |
TickTock | 13:544b15b00c2d | 243 | pc.printf("\nEnter month (mm):"); |
TickTock | 13:544b15b00c2d | 244 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 245 | t.tm_mon = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 246 | |
TickTock | 13:544b15b00c2d | 247 | pc.printf("\nEnter day (dd):"); |
TickTock | 13:544b15b00c2d | 248 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 249 | t.tm_mday = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 250 | |
TickTock | 13:544b15b00c2d | 251 | pc.printf("\nEnter hour (hh):"); |
TickTock | 13:544b15b00c2d | 252 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 253 | t.tm_hour = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 254 | |
TickTock | 13:544b15b00c2d | 255 | pc.printf("\nEnter minute (mm):"); |
TickTock | 13:544b15b00c2d | 256 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 257 | t.tm_min = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 258 | |
TickTock | 13:544b15b00c2d | 259 | pc.printf("\nEnter seconds (ss):"); |
TickTock | 13:544b15b00c2d | 260 | pc.scanf("%2s", &sTemp); |
TickTock | 13:544b15b00c2d | 261 | t.tm_sec = atoi(sTemp); |
TickTock | 13:544b15b00c2d | 262 | |
TickTock | 13:544b15b00c2d | 263 | //t.tm_year = 2012 ; // 28 May 2012 |
TickTock | 13:544b15b00c2d | 264 | //t.tm_mon = 6 ; // 1 to 12 |
TickTock | 13:544b15b00c2d | 265 | //t.tm_mday = 1; |
TickTock | 13:544b15b00c2d | 266 | //t.tm_hour = 12; // 12:59:56 PM (after noon) |
TickTock | 13:544b15b00c2d | 267 | //t.tm_min = 59; |
TickTock | 13:544b15b00c2d | 268 | //t.tm_sec = 56; |
TickTock | 12:7cd4e9303165 | 269 | |
TickTock | 12:7cd4e9303165 | 270 | // adjust for tm structure required values |
TickTock | 12:7cd4e9303165 | 271 | t.tm_year = t.tm_year - 1900; |
TickTock | 12:7cd4e9303165 | 272 | t.tm_mon = t.tm_mon - 1; |
TickTock | 12:7cd4e9303165 | 273 | |
TickTock | 12:7cd4e9303165 | 274 | // set the RTC |
TickTock | 12:7cd4e9303165 | 275 | set_time(mktime(&t)); |
TickTock | 12:7cd4e9303165 | 276 | seconds = time(NULL); |
TickTock | 12:7cd4e9303165 | 277 | |
TickTock | 13:544b15b00c2d | 278 | pc.printf("\n\mRTC set to: " ); |
TickTock | 12:7cd4e9303165 | 279 | strftime(sTemp, 32, "%a %m/%d/%Y %X", localtime(&seconds)); |
TickTock | 12:7cd4e9303165 | 280 | pc.printf("%s\n", sTemp); // DAY MM/DD/YYYY HH:MM:SS |
TickTock | 12:7cd4e9303165 | 281 | } |
TickTock | 1:dafb963c3c14 | 282 | |
TickTock | 6:594d9717e609 | 283 | while (true) { |
TickTock | 6:594d9717e609 | 284 | if(CD == 1) { |
TickTock | 11:d97071bf877b | 285 | if (!logOpen) { // Open new file if one is not already open |
TickTock | 11:d97071bf877b | 286 | int fileNum = 0; |
TickTock | 11:d97071bf877b | 287 | sprintf(fileName,"/sd/cancan%d.alc",fileNum); |
TickTock | 6:594d9717e609 | 288 | file = fopen(fileName, "r"); |
TickTock | 11:d97071bf877b | 289 | while ((file != NULL)&&(fileNum++<100)) { |
TickTock | 11:d97071bf877b | 290 | fclose(file); |
TickTock | 11:d97071bf877b | 291 | sprintf(fileName,"/sd/cancan%d.alc",fileNum); |
TickTock | 11:d97071bf877b | 292 | file = fopen(fileName, "r"); |
TickTock | 11:d97071bf877b | 293 | } |
TickTock | 11:d97071bf877b | 294 | file = fopen(fileName, "wb"); |
TickTock | 11:d97071bf877b | 295 | if(file==NULL){ |
TickTock | 11:d97071bf877b | 296 | pc.printf("\nUnable to open canlog\n\n\n\n"); |
TickTock | 11:d97071bf877b | 297 | mbed_reset(); |
TickTock | 11:d97071bf877b | 298 | } else { |
TickTock | 11:d97071bf877b | 299 | logOpen = true; |
TickTock | 11:d97071bf877b | 300 | readPointer=writePointer; |
TickTock | 11:d97071bf877b | 301 | pc.printf("\nStarting Can Log %s\n\n\n\n",fileName); |
TickTock | 11:d97071bf877b | 302 | secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 11:d97071bf877b | 303 | tsMsg.id=0xfff; |
TickTock | 11:d97071bf877b | 304 | tsMsg.len=0xf; |
TickTock | 11:d97071bf877b | 305 | tsMsg.data[0]=secs&0xff; |
TickTock | 11:d97071bf877b | 306 | tsMsg.data[1]=(secs>>0)&0xff; |
TickTock | 11:d97071bf877b | 307 | tsMsg.data[2]=(secs>>16)&0xff; |
TickTock | 11:d97071bf877b | 308 | tsMsg.data[3]=secs>>24; |
TickTock | 11:d97071bf877b | 309 | tsMsg.data[4]=0xff; |
TickTock | 11:d97071bf877b | 310 | tsMsg.data[5]=0xff; |
TickTock | 11:d97071bf877b | 311 | tsMsg.data[6]=0xff; |
TickTock | 11:d97071bf877b | 312 | tsMsg.data[7]=0xff; |
TickTock | 11:d97071bf877b | 313 | logCan(0,tsMsg); //write full timestamp |
TickTock | 11:d97071bf877b | 314 | fclose(file); |
TickTock | 11:d97071bf877b | 315 | } |
TickTock | 11:d97071bf877b | 316 | } // if (!logOpen) |
TickTock | 9:8396b76e42fc | 317 | do { |
TickTock | 10:663bff0a6144 | 318 | if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/2)||canIdle||(PB1==0)) { |
TickTock | 10:663bff0a6144 | 319 | // Dump buffer if > 1/2 full, canbus has stopped, or PB1 pressed |
TickTock | 9:8396b76e42fc | 320 | if (logOpen) { |
TickTock | 9:8396b76e42fc | 321 | file = fopen(fileName, "ab"); |
TickTock | 9:8396b76e42fc | 322 | if (file == NULL) { |
TickTock | 9:8396b76e42fc | 323 | logOpen = false; |
TickTock | 9:8396b76e42fc | 324 | pc.printf("Failed to append log file.\n\n"); |
TickTock | 9:8396b76e42fc | 325 | } else { |
TickTock | 9:8396b76e42fc | 326 | while (readPointer != writePointer) { |
TickTock | 9:8396b76e42fc | 327 | for (int j = 0; j<13; j++){ |
TickTock | 9:8396b76e42fc | 328 | fprintf(file,"%c",writeBuffer[readPointer][j]); |
TickTock | 9:8396b76e42fc | 329 | } |
TickTock | 9:8396b76e42fc | 330 | if(++readPointer >= maxBufLen) |
TickTock | 9:8396b76e42fc | 331 | readPointer=0; |
TickTock | 9:8396b76e42fc | 332 | } |
TickTock | 9:8396b76e42fc | 333 | led3 = !led3; |
TickTock | 9:8396b76e42fc | 334 | fclose(file); |
TickTock | 9:8396b76e42fc | 335 | } |
TickTock | 10:663bff0a6144 | 336 | } // if (logOpen) |
TickTock | 9:8396b76e42fc | 337 | } |
TickTock | 10:663bff0a6144 | 338 | if (canIdle) { // canbus idle --> sleep to save power |
TickTock | 13:544b15b00c2d | 339 | //LPC_RTC->CIIR=0x00; // block RTC interrupts |
TickTock | 10:663bff0a6144 | 340 | led1=0; |
TickTock | 10:663bff0a6144 | 341 | led2=0; |
TickTock | 10:663bff0a6144 | 342 | led3=0; |
TickTock | 10:663bff0a6144 | 343 | led4=0; |
TickTock | 11:d97071bf877b | 344 | secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 10:663bff0a6144 | 345 | while (secsIdle>canTimeout) |
TickTock | 11:d97071bf877b | 346 | //DeepPowerDown(); |
TickTock | 10:663bff0a6144 | 347 | __wfi(); // freeze CPU and wait for interrupt (from canbus) |
TickTock | 11:d97071bf877b | 348 | if (time(NULL)>(secs+3600)) |
TickTock | 11:d97071bf877b | 349 | logOpen = false; // Start new file if asleep for more than an hour |
TickTock | 13:544b15b00c2d | 350 | //LPC_RTC->CIIR=0x01; // re-enable RTC interrupts |
TickTock | 9:8396b76e42fc | 351 | } |
TickTock | 10:663bff0a6144 | 352 | wait(0.25); // We get >2K messages per second |
TickTock | 11:d97071bf877b | 353 | } while ((PB1==1)&&(CD==1)&&logOpen); // keep going until button or SDram removed |
TickTock | 9:8396b76e42fc | 354 | |
TickTock | 11:d97071bf877b | 355 | if (PB1==0) { |
TickTock | 11:d97071bf877b | 356 | led1=0; |
TickTock | 11:d97071bf877b | 357 | led2=0; |
TickTock | 11:d97071bf877b | 358 | led3=0; |
TickTock | 11:d97071bf877b | 359 | led4=0; |
TickTock | 11:d97071bf877b | 360 | pc.printf("Log stopped\n\n"); |
TickTock | 11:d97071bf877b | 361 | logOpen=false; |
TickTock | 11:d97071bf877b | 362 | wait(5); // wait 5 seconds to give time to remove SDRAM if desired |
TickTock | 11:d97071bf877b | 363 | } |
TickTock | 11:d97071bf877b | 364 | } else { |
TickTock | 11:d97071bf877b | 365 | pc.printf("\nNo SDRAM Inserted.\n\n"); |
TickTock | 6:594d9717e609 | 366 | logOpen=false; |
TickTock | 11:d97071bf877b | 367 | led1=!led1; |
TickTock | 11:d97071bf877b | 368 | led2=led1; |
TickTock | 11:d97071bf877b | 369 | led3=led1; |
TickTock | 11:d97071bf877b | 370 | led4=led1; |
TickTock | 11:d97071bf877b | 371 | wait(5); |
TickTock | 10:663bff0a6144 | 372 | } //if (CD==1) |
TickTock | 9:8396b76e42fc | 373 | } //while (true) |
TickTock | 9:8396b76e42fc | 374 | } |
TickTock | 9:8396b76e42fc | 375 |