.
Dependencies: SDHCFileSystem mbed
main.cpp@8:6872945e8e91, 2012-12-04 (annotated)
- Committer:
- TickTock
- Date:
- Tue Dec 04 19:06:41 2012 +0000
- Revision:
- 8:6872945e8e91
- Parent:
- 7:281df0ba57d0
- Child:
- 9:8396b76e42fc
.
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 | 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 | 8:6872945e8e91 | 88 | |
TickTock | 1:dafb963c3c14 | 89 | void Log (char *message); |
TickTock | 1:dafb963c3c14 | 90 | void LogErr (char *message); |
TickTock | 6:594d9717e609 | 91 | extern "C" void mbed_reset(); |
TickTock | 1:dafb963c3c14 | 92 | |
TickTock | 1:dafb963c3c14 | 93 | time_t seconds ; |
TickTock | 1:dafb963c3c14 | 94 | DigitalIn CD(p17); |
TickTock | 2:55b2357f0cf9 | 95 | DigitalIn PB1( p20 ); |
TickTock | 1:dafb963c3c14 | 96 | SDFileSystem sd(p5, p6, p7, p8, "sd" ); // SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) |
TickTock | 7:281df0ba57d0 | 97 | //Ticker ticker1; |
TickTock | 7:281df0ba57d0 | 98 | //Ticker ticker2; |
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 | 2:55b2357f0cf9 | 112 | int bufPointer = 0; |
TickTock | 0:1596b8644523 | 113 | |
TickTock | 0:1596b8644523 | 114 | Serial pc(USBTX, USBRX); // tx, rx default settings (9600 8N1) |
TickTock | 0:1596b8644523 | 115 | |
TickTock | 2:55b2357f0cf9 | 116 | extern "C" void RTC_IRQHandler() { |
TickTock | 4:fb373a391155 | 117 | static int i=0; |
TickTock | 8:6872945e8e91 | 118 | static int secsIdle=0; |
TickTock | 8:6872945e8e91 | 119 | timer.reset(); // zero ms at the-seconds-tick |
TickTock | 8:6872945e8e91 | 120 | if(i==bufPointer){ // no canbus messages logges |
TickTock | 8:6872945e8e91 | 121 | if (++secsIdle>canTimeout) { // canbus idle --> sleep to save power |
TickTock | 8:6872945e8e91 | 122 | __wfi(); |
TickTock | 8:6872945e8e91 | 123 | } |
TickTock | 8:6872945e8e91 | 124 | } else { |
TickTock | 8:6872945e8e91 | 125 | if (logOpen) { |
TickTock | 8:6872945e8e91 | 126 | file = fopen(fileName, "ab"); |
TickTock | 8:6872945e8e91 | 127 | if (file == NULL) { |
TickTock | 8:6872945e8e91 | 128 | logOpen = false; |
TickTock | 8:6872945e8e91 | 129 | pc.printf("Failed to append log file.\n\n"); |
TickTock | 8:6872945e8e91 | 130 | } else { |
TickTock | 8:6872945e8e91 | 131 | while (i != bufPointer) { |
TickTock | 8:6872945e8e91 | 132 | for (int j = 0; j<13; j++){ |
TickTock | 8:6872945e8e91 | 133 | fprintf(file,"%c",writeBuffer[i][j]); |
TickTock | 8:6872945e8e91 | 134 | } |
TickTock | 8:6872945e8e91 | 135 | if(++i >= maxBufLen) |
TickTock | 8:6872945e8e91 | 136 | i=0; |
TickTock | 3:51134a275609 | 137 | } |
TickTock | 8:6872945e8e91 | 138 | led3 = !led3; |
TickTock | 8:6872945e8e91 | 139 | fclose(file); |
TickTock | 2:55b2357f0cf9 | 140 | } |
TickTock | 2:55b2357f0cf9 | 141 | } |
TickTock | 8:6872945e8e91 | 142 | i=bufPointer; |
TickTock | 8:6872945e8e91 | 143 | secsIdle=0; |
TickTock | 1:dafb963c3c14 | 144 | } |
TickTock | 8:6872945e8e91 | 145 | LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next |
TickTock | 2:55b2357f0cf9 | 146 | } |
TickTock | 2:55b2357f0cf9 | 147 | |
TickTock | 2:55b2357f0cf9 | 148 | extern "C" void RTC_Init (void) { |
TickTock | 2:55b2357f0cf9 | 149 | LPC_RTC->ILR=0x00; // set up the RTC interrupts |
TickTock | 2:55b2357f0cf9 | 150 | //LPC_RTC->CIIR=0x02; // interrupts each minute |
TickTock | 2:55b2357f0cf9 | 151 | LPC_RTC->CIIR=0x01; // interrupts each second |
TickTock | 8:6872945e8e91 | 152 | LPC_RTC->CCR = 0x01; // Clock enable |
TickTock | 3:51134a275609 | 153 | NVIC_SetPriority( RTC_IRQn, 10 ); |
TickTock | 2:55b2357f0cf9 | 154 | NVIC_EnableIRQ( RTC_IRQn ); |
TickTock | 2:55b2357f0cf9 | 155 | } |
TickTock | 2:55b2357f0cf9 | 156 | |
TickTock | 2:55b2357f0cf9 | 157 | unsigned short getTimeStamp() { |
TickTock | 2:55b2357f0cf9 | 158 | int msec = timer.read_ms() ; // read ms from the timer |
TickTock | 2:55b2357f0cf9 | 159 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 2:55b2357f0cf9 | 160 | int isecs = secs%60 ; // modulo 60 for 0-59 seconds from RTC |
TickTock | 2:55b2357f0cf9 | 161 | return ((isecs<<10)+msec) ; // return the two byte time stamp |
TickTock | 1:dafb963c3c14 | 162 | } |
TickTock | 1:dafb963c3c14 | 163 | |
TickTock | 2:55b2357f0cf9 | 164 | void readLog (){ |
TickTock | 2:55b2357f0cf9 | 165 | unsigned char c; |
TickTock | 2:55b2357f0cf9 | 166 | int i=0; |
TickTock | 3:51134a275609 | 167 | char lastMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 168 | char curMsgNum[]={0,0}; |
TickTock | 3:51134a275609 | 169 | char canNum=0; |
TickTock | 2:55b2357f0cf9 | 170 | pc.printf("printing file\n"); |
TickTock | 2:55b2357f0cf9 | 171 | file = fopen(fileName, "r"); |
TickTock | 2:55b2357f0cf9 | 172 | if (file == NULL) { |
TickTock | 2:55b2357f0cf9 | 173 | pc.printf("no file found\n"); |
TickTock | 1:dafb963c3c14 | 174 | } |
TickTock | 2:55b2357f0cf9 | 175 | while (!feof(file)) { |
TickTock | 2:55b2357f0cf9 | 176 | c=fgetc(file); |
TickTock | 2:55b2357f0cf9 | 177 | pc.printf("%02x ",c); |
TickTock | 3:51134a275609 | 178 | if (i==0){ |
TickTock | 3:51134a275609 | 179 | canNum=c; |
TickTock | 3:51134a275609 | 180 | }else if (i==5){ |
TickTock | 3:51134a275609 | 181 | curMsgNum[canNum]=c; |
TickTock | 2:55b2357f0cf9 | 182 | } |
TickTock | 2:55b2357f0cf9 | 183 | if (++i>12) { |
TickTock | 3:51134a275609 | 184 | if (curMsgNum[canNum]>(lastMsgNum[canNum]+1)) { |
TickTock | 2:55b2357f0cf9 | 185 | pc.printf(" ***"); |
TickTock | 2:55b2357f0cf9 | 186 | } |
TickTock | 3:51134a275609 | 187 | lastMsgNum[canNum]=curMsgNum[canNum]; |
TickTock | 2:55b2357f0cf9 | 188 | pc.printf("\n"); |
TickTock | 2:55b2357f0cf9 | 189 | i=0; |
TickTock | 2:55b2357f0cf9 | 190 | } |
TickTock | 2:55b2357f0cf9 | 191 | } |
TickTock | 4:fb373a391155 | 192 | pc.printf("\n\n\n\n"); |
TickTock | 2:55b2357f0cf9 | 193 | fclose(file); |
TickTock | 2:55b2357f0cf9 | 194 | } |
TickTock | 1:dafb963c3c14 | 195 | |
TickTock | 2:55b2357f0cf9 | 196 | void logCan (char mtype, CANMessage canRXmsg) { |
TickTock | 2:55b2357f0cf9 | 197 | unsigned short ts = getTimeStamp(); |
TickTock | 2:55b2357f0cf9 | 198 | writeBuffer[bufPointer][0]=mtype; |
TickTock | 2:55b2357f0cf9 | 199 | writeBuffer[bufPointer][1]=ts>>8; |
TickTock | 2:55b2357f0cf9 | 200 | writeBuffer[bufPointer][2]=ts&0xff; |
TickTock | 2:55b2357f0cf9 | 201 | writeBuffer[bufPointer][3]=canRXmsg.id&0xff; |
TickTock | 2:55b2357f0cf9 | 202 | writeBuffer[bufPointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4); |
TickTock | 2:55b2357f0cf9 | 203 | for (int i = 5; i<13; i++){ |
TickTock | 2:55b2357f0cf9 | 204 | writeBuffer[bufPointer][i]=canRXmsg.data[i-5]; |
TickTock | 0:1596b8644523 | 205 | } |
TickTock | 4:fb373a391155 | 206 | if (++bufPointer >= maxBufLen) { |
TickTock | 4:fb373a391155 | 207 | bufPointer = 0; |
TickTock | 4:fb373a391155 | 208 | led4 = !led4; |
TickTock | 4:fb373a391155 | 209 | } |
TickTock | 1:dafb963c3c14 | 210 | } |
TickTock | 0:1596b8644523 | 211 | |
TickTock | 8:6872945e8e91 | 212 | /*void send1() { |
TickTock | 1:dafb963c3c14 | 213 | static char counter = 0; // use for fake data |
TickTock | 1:dafb963c3c14 | 214 | can1.write(CANMessage(0x350, &counter, 1)); |
TickTock | 1:dafb963c3c14 | 215 | counter++; |
TickTock | 4:fb373a391155 | 216 | can1.write(CANMessage(0x351, &counter, 1)); |
TickTock | 2:55b2357f0cf9 | 217 | counter++; |
TickTock | 4:fb373a391155 | 218 | can1.write(CANMessage(0x352, &counter, 1)); |
TickTock | 4:fb373a391155 | 219 | counter++; |
TickTock | 3:51134a275609 | 220 | } |
TickTock | 3:51134a275609 | 221 | |
TickTock | 3:51134a275609 | 222 | void send2() { |
TickTock | 3:51134a275609 | 223 | static char counter = 0; // use for fake data |
TickTock | 3:51134a275609 | 224 | can2.write(CANMessage(0x351, &counter, 1)); |
TickTock | 3:51134a275609 | 225 | counter++; |
TickTock | 4:fb373a391155 | 226 | can2.write(CANMessage(0x351, &counter, 1)); |
TickTock | 3:51134a275609 | 227 | counter++; |
TickTock | 3:51134a275609 | 228 | can2.write(CANMessage(0x352, &counter, 1)); |
TickTock | 4:fb373a391155 | 229 | counter++; |
TickTock | 8:6872945e8e91 | 230 | }*/ |
TickTock | 0:1596b8644523 | 231 | |
TickTock | 0:1596b8644523 | 232 | void recieve1() { |
TickTock | 2:55b2357f0cf9 | 233 | CANMessage msg1; |
TickTock | 2:55b2357f0cf9 | 234 | unsigned short msgTime=getTimeStamp(); |
TickTock | 3:51134a275609 | 235 | static int lastMsg1 = 0; |
TickTock | 0:1596b8644523 | 236 | can1.read(msg1); |
TickTock | 3:51134a275609 | 237 | if (msg1.id!=0x000) { |
TickTock | 3:51134a275609 | 238 | if (msg1.data[0]>(lastMsg1+1)) { |
TickTock | 3:51134a275609 | 239 | pc.printf("%sCan1 Message %d missed! \n", upLine, lastMsg1+1); |
TickTock | 1:dafb963c3c14 | 240 | } |
TickTock | 3:51134a275609 | 241 | lastMsg1=msg1.data[0]; |
TickTock | 3:51134a275609 | 242 | if(logOpen) { |
TickTock | 4:fb373a391155 | 243 | logCan(1, msg1); |
TickTock | 3:51134a275609 | 244 | } |
TickTock | 3:51134a275609 | 245 | led1 = !led1; |
TickTock | 1:dafb963c3c14 | 246 | } |
TickTock | 0:1596b8644523 | 247 | } |
TickTock | 2:55b2357f0cf9 | 248 | |
TickTock | 0:1596b8644523 | 249 | void recieve2() { |
TickTock | 2:55b2357f0cf9 | 250 | CANMessage msg2; |
TickTock | 2:55b2357f0cf9 | 251 | unsigned short msgTime=getTimeStamp(); |
TickTock | 3:51134a275609 | 252 | static int lastMsg2 = 0; |
TickTock | 0:1596b8644523 | 253 | can2.read(msg2); |
TickTock | 3:51134a275609 | 254 | if (msg2.id!=0x000) { |
TickTock | 3:51134a275609 | 255 | if (msg2.data[0]>(lastMsg2+1)) { |
TickTock | 3:51134a275609 | 256 | pc.printf("%sCan2 Message %d missed! \n", upLine, lastMsg2+1); |
TickTock | 3:51134a275609 | 257 | } |
TickTock | 3:51134a275609 | 258 | lastMsg2=msg2.data[0]; |
TickTock | 3:51134a275609 | 259 | if(logOpen) { |
TickTock | 4:fb373a391155 | 260 | logCan(2, msg2); |
TickTock | 3:51134a275609 | 261 | } |
TickTock | 3:51134a275609 | 262 | led2 = !led2; |
TickTock | 2:55b2357f0cf9 | 263 | } |
TickTock | 0:1596b8644523 | 264 | } |
TickTock | 0:1596b8644523 | 265 | |
TickTock | 0:1596b8644523 | 266 | int main() { |
TickTock | 2:55b2357f0cf9 | 267 | pc.baud(460800); // change serial interface to pc to 115200, 8N1 |
TickTock | 2:55b2357f0cf9 | 268 | CANMessage tsMsg; |
TickTock | 7:281df0ba57d0 | 269 | can1.frequency(500000); |
TickTock | 7:281df0ba57d0 | 270 | can2.frequency(500000); |
TickTock | 7:281df0ba57d0 | 271 | //can1_SleepMode = 0; // Enable TX |
TickTock | 7:281df0ba57d0 | 272 | //can2_SleepMode = 0; // Enable TX |
TickTock | 7:281df0ba57d0 | 273 | can1_SleepMode = 1; // Turn on Monitor_only Mode |
TickTock | 7:281df0ba57d0 | 274 | can2_SleepMode = 1; // Turn on Monitor_only Mode |
TickTock | 0:1596b8644523 | 275 | CD.mode(PullUp) ; //SDRAM Chip Detect |
TickTock | 2:55b2357f0cf9 | 276 | PB1.mode(PullUp) ; //Pushbutton 1 |
TickTock | 0:1596b8644523 | 277 | can1.attach(&recieve1); |
TickTock | 0:1596b8644523 | 278 | can2.attach(&recieve2); |
TickTock | 1:dafb963c3c14 | 279 | |
TickTock | 1:dafb963c3c14 | 280 | timer.start() ; |
TickTock | 2:55b2357f0cf9 | 281 | RTC_Init(); // start the RTC Interrupts that sync the timer |
TickTock | 2:55b2357f0cf9 | 282 | wait(1.1); // give time to sync |
TickTock | 1:dafb963c3c14 | 283 | |
TickTock | 6:594d9717e609 | 284 | while (true) { |
TickTock | 6:594d9717e609 | 285 | if(CD == 1) { |
TickTock | 6:594d9717e609 | 286 | int fileNum = 0; |
TickTock | 8:6872945e8e91 | 287 | sprintf(fileName,"/sd/CANcan%d.alc",fileNum); |
TickTock | 2:55b2357f0cf9 | 288 | file = fopen(fileName, "r"); |
TickTock | 6:594d9717e609 | 289 | while ((file != NULL)&&(fileNum++<100)) { |
TickTock | 6:594d9717e609 | 290 | fclose(file); |
TickTock | 8:6872945e8e91 | 291 | sprintf(fileName,"/sd/CANcan%d.alc",fileNum); |
TickTock | 6:594d9717e609 | 292 | file = fopen(fileName, "r"); |
TickTock | 6:594d9717e609 | 293 | } |
TickTock | 6:594d9717e609 | 294 | |
TickTock | 6:594d9717e609 | 295 | file = fopen(fileName, "wb"); |
TickTock | 6:594d9717e609 | 296 | if(file==NULL){ |
TickTock | 6:594d9717e609 | 297 | pc.printf("\nUnable to open canlog\n\n\n\n"); |
TickTock | 6:594d9717e609 | 298 | mbed_reset(); |
TickTock | 6:594d9717e609 | 299 | } else { |
TickTock | 6:594d9717e609 | 300 | logOpen = true; |
TickTock | 6:594d9717e609 | 301 | pc.printf("\nStarting Can Log %s\n\n\n\n",fileName); |
TickTock | 6:594d9717e609 | 302 | unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 6:594d9717e609 | 303 | tsMsg.id=0xfff; |
TickTock | 6:594d9717e609 | 304 | tsMsg.len=0xf; |
TickTock | 6:594d9717e609 | 305 | tsMsg.data[0]=secs&0xff; |
TickTock | 6:594d9717e609 | 306 | tsMsg.data[1]=(secs>>0)&0xff; |
TickTock | 6:594d9717e609 | 307 | tsMsg.data[2]=(secs>>16)&0xff; |
TickTock | 6:594d9717e609 | 308 | tsMsg.data[3]=secs>>24; |
TickTock | 6:594d9717e609 | 309 | tsMsg.data[4]=0xff; |
TickTock | 6:594d9717e609 | 310 | tsMsg.data[5]=0xff; |
TickTock | 6:594d9717e609 | 311 | tsMsg.data[6]=0xff; |
TickTock | 6:594d9717e609 | 312 | tsMsg.data[7]=0xff; |
TickTock | 6:594d9717e609 | 313 | logCan(0,tsMsg); //write full timestamp |
TickTock | 6:594d9717e609 | 314 | fclose(file); |
TickTock | 6:594d9717e609 | 315 | } |
TickTock | 7:281df0ba57d0 | 316 | //ticker1.attach(&send1, .0052); //start sending messages |
TickTock | 7:281df0ba57d0 | 317 | //ticker2.attach(&send2, .0051); //start sending messages |
TickTock | 6:594d9717e609 | 318 | |
TickTock | 6:594d9717e609 | 319 | while ((PB1==1)&&(CD==1)){ |
TickTock | 6:594d9717e609 | 320 | wait(0.25); // Keep logging until button pushed or SDRAM removed |
TickTock | 6:594d9717e609 | 321 | } |
TickTock | 7:281df0ba57d0 | 322 | //ticker1.detach(); |
TickTock | 7:281df0ba57d0 | 323 | //ticker2.detach(); |
TickTock | 6:594d9717e609 | 324 | logOpen=false; |
TickTock | 6:594d9717e609 | 325 | pc.printf("Stopping log\n\n"); |
TickTock | 6:594d9717e609 | 326 | wait(1); |
TickTock | 6:594d9717e609 | 327 | if (CD==1) |
TickTock | 6:594d9717e609 | 328 | readLog(); |
TickTock | 2:55b2357f0cf9 | 329 | } else { |
TickTock | 6:594d9717e609 | 330 | pc.printf("\nNo SDRAM Inserted.\n\n\n\n"); |
TickTock | 2:55b2357f0cf9 | 331 | } |
TickTock | 6:594d9717e609 | 332 | wait(1); |
TickTock | 0:1596b8644523 | 333 | } |
TickTock | 0:1596b8644523 | 334 | } |