.
Dependencies: SDHCFileSystem mbed
main.cpp@1:dafb963c3c14, 2012-11-29 (annotated)
- Committer:
- TickTock
- Date:
- Thu Nov 29 12:18:37 2012 +0000
- Revision:
- 1:dafb963c3c14
- Parent:
- 0:1596b8644523
- Child:
- 2:55b2357f0cf9
initial working version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TickTock | 0:1596b8644523 | 1 | #include "mbed.h" |
TickTock | 0:1596b8644523 | 2 | #include "CAN.h" |
TickTock | 0:1596b8644523 | 3 | #include "TextLCD.h" |
TickTock | 1:dafb963c3c14 | 4 | #include "SDHCFileSystem.h" |
TickTock | 0:1596b8644523 | 5 | #include "DebounceIn.h" |
TickTock | 0:1596b8644523 | 6 | #include "beep.h" |
TickTock | 0:1596b8644523 | 7 | |
TickTock | 1:dafb963c3c14 | 8 | #define upLine "\033[1A" |
TickTock | 1:dafb963c3c14 | 9 | #define LOGFILE "/sd/canary.log" |
TickTock | 1:dafb963c3c14 | 10 | |
TickTock | 1:dafb963c3c14 | 11 | //CANcan.cpp |
TickTock | 1:dafb963c3c14 | 12 | |
TickTock | 1:dafb963c3c14 | 13 | //LEAF OBD |
TickTock | 1:dafb963c3c14 | 14 | //1: |
TickTock | 1:dafb963c3c14 | 15 | //2: |
TickTock | 1:dafb963c3c14 | 16 | //3: AVCAN-L White/Blue |
TickTock | 1:dafb963c3c14 | 17 | //4: |
TickTock | 1:dafb963c3c14 | 18 | //5: VSS Brown,White/Brown |
TickTock | 1:dafb963c3c14 | 19 | //6: CARCAN-H Green |
TickTock | 1:dafb963c3c14 | 20 | //7: |
TickTock | 1:dafb963c3c14 | 21 | //8: 12V-SW Orange,White/Orange |
TickTock | 1:dafb963c3c14 | 22 | //9: |
TickTock | 1:dafb963c3c14 | 23 | //10: |
TickTock | 1:dafb963c3c14 | 24 | //11: AVCAN-H Blue |
TickTock | 1:dafb963c3c14 | 25 | //12: EVCAN-L White/Grey |
TickTock | 1:dafb963c3c14 | 26 | //13: EVCAN-H Grey |
TickTock | 1:dafb963c3c14 | 27 | //14: CARCAN-L White/Green |
TickTock | 1:dafb963c3c14 | 28 | //15: |
TickTock | 1:dafb963c3c14 | 29 | //16: 12V-AON Red/Blue,Blue/Red |
TickTock | 1:dafb963c3c14 | 30 | |
TickTock | 0:1596b8644523 | 31 | //VP230 |
TickTock | 1:dafb963c3c14 | 32 | //1:D |
TickTock | 1:dafb963c3c14 | 33 | //2:GND |
TickTock | 1:dafb963c3c14 | 34 | //3:VCC |
TickTock | 1:dafb963c3c14 | 35 | //4:R |
TickTock | 1:dafb963c3c14 | 36 | //5:Vref |
TickTock | 1:dafb963c3c14 | 37 | //6:CANL |
TickTock | 1:dafb963c3c14 | 38 | //7:CANH |
TickTock | 1:dafb963c3c14 | 39 | //8:RS |
TickTock | 0:1596b8644523 | 40 | |
TickTock | 0:1596b8644523 | 41 | //LPC1768 |
TickTock | 0:1596b8644523 | 42 | //1: VSS |
TickTock | 1:dafb963c3c14 | 43 | //2: NC:VIN (4.5-9V supply) |
TickTock | 0:1596b8644523 | 44 | //3: NC:VB |
TickTock | 0:1596b8644523 | 45 | //4: NC:nR |
TickTock | 0:1596b8644523 | 46 | //5: SPI:MOSI --> 6:SDRAM:DI |
TickTock | 0:1596b8644523 | 47 | //6: SPI:MISO --> 2:SDRAM:DO |
TickTock | 0:1596b8644523 | 48 | //7: SPI:SCLK --> 4:SDRAM:SCLK |
TickTock | 0:1596b8644523 | 49 | //8: CS --> 7:SDRAM:CS |
TickTock | 1:dafb963c3c14 | 50 | //9: CAN1:RX --> 4:CAN1:R |
TickTock | 1:dafb963c3c14 | 51 | //10: CAN1:TX --> 1:CAN1:D |
TickTock | 0:1596b8644523 | 52 | //11: RS --> 4:LCD:RS |
TickTock | 0:1596b8644523 | 53 | //12: E --> 6:LCD:E |
TickTock | 0:1596b8644523 | 54 | //13: D4 --> 11:LCD:D4 |
TickTock | 0:1596b8644523 | 55 | //14: D5 --> 12:LCD:D5 |
TickTock | 0:1596b8644523 | 56 | //15: D6 --> 13:LCD:D6 |
TickTock | 0:1596b8644523 | 57 | //16: D7 --> 14:LCD:D7 |
TickTock | 0:1596b8644523 | 58 | //17: CD --> 1:SDRAM:CD |
TickTock | 1:dafb963c3c14 | 59 | //18: MON12V --> 4K to 12V, 1K to VSS (To be implemented) |
TickTock | 0:1596b8644523 | 60 | //19: PB2 |
TickTock | 0:1596b8644523 | 61 | //20: PB1 |
TickTock | 0:1596b8644523 | 62 | //21: Spkr+ |
TickTock | 1:dafb963c3c14 | 63 | //22: Spkr- (optional complimentary output for more volume) |
TickTock | 0:1596b8644523 | 64 | //23: NC:pwm |
TickTock | 1:dafb963c3c14 | 65 | //24: LEDBLU --> 18:LCD:BLU (only used for tri-color displays) |
TickTock | 1:dafb963c3c14 | 66 | //25: LEDGRN --> 17:LCD:GRN (only used for tri-color displays) |
TickTock | 1:dafb963c3c14 | 67 | //26: LEDRED --> 16:LCD:RED |
TickTock | 0:1596b8644523 | 68 | //27: CAN1:Sleep --> 8:CAN1:RS |
TickTock | 0:1596b8644523 | 69 | //28: CAN2:Sleep --> 8:CAN2:RS |
TickTock | 1:dafb963c3c14 | 70 | //29: CAN2:TX --> 1:CAN2:D |
TickTock | 1:dafb963c3c14 | 71 | //30: CAN2:RX --> 4:CAN2:R |
TickTock | 0:1596b8644523 | 72 | //31: NC:USB_D+ |
TickTock | 0:1596b8644523 | 73 | //32: NC:USB_D- |
TickTock | 0:1596b8644523 | 74 | //33: NC:Eth_TD+ |
TickTock | 0:1596b8644523 | 75 | //34: NC:Eth_TD- |
TickTock | 0:1596b8644523 | 76 | //35: NC:Eth_RD+ |
TickTock | 0:1596b8644523 | 77 | //36: NC:Eth_RD- |
TickTock | 0:1596b8644523 | 78 | //37: NC:IF+ |
TickTock | 0:1596b8644523 | 79 | //38: NC:IF- |
TickTock | 1:dafb963c3c14 | 80 | //39: NC:5Vout (only available when connected as USB device) |
TickTock | 0:1596b8644523 | 81 | //40: VCC3.3 |
TickTock | 0:1596b8644523 | 82 | |
TickTock | 1:dafb963c3c14 | 83 | extern bool LogCreated; |
TickTock | 1:dafb963c3c14 | 84 | void Log (char *message); |
TickTock | 1:dafb963c3c14 | 85 | void LogErr (char *message); |
TickTock | 1:dafb963c3c14 | 86 | |
TickTock | 1:dafb963c3c14 | 87 | time_t seconds ; |
TickTock | 0:1596b8644523 | 88 | Beep buzzer(p21); |
TickTock | 1:dafb963c3c14 | 89 | DigitalIn CD(p17); |
TickTock | 1:dafb963c3c14 | 90 | DebounceIn dbPB1( p20 ); |
TickTock | 1:dafb963c3c14 | 91 | ;DigitalIn PB1(p20); |
TickTock | 1:dafb963c3c14 | 92 | SDFileSystem sd(p5, p6, p7, p8, "sd" ); // SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) |
TickTock | 0:1596b8644523 | 93 | TextLCD lcd(p11, p12, p13, p14, p15, p16); // rs, e, d0-d3 (RW to GND) |
TickTock | 0:1596b8644523 | 94 | Ticker ticker; |
TickTock | 1:dafb963c3c14 | 95 | Timer timer; |
TickTock | 0:1596b8644523 | 96 | DigitalOut led1(LED1); |
TickTock | 0:1596b8644523 | 97 | DigitalOut led2(LED2); |
TickTock | 0:1596b8644523 | 98 | DigitalOut led3(LED3); |
TickTock | 0:1596b8644523 | 99 | DigitalOut led4(LED4); |
TickTock | 1:dafb963c3c14 | 100 | CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (rx, tx) and pin 27 (rs) |
TickTock | 1:dafb963c3c14 | 101 | CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (rx, tx) and pin 28 (rs) |
TickTock | 1:dafb963c3c14 | 102 | bool logCreated = false; |
TickTock | 0:1596b8644523 | 103 | char logMsg[64]; |
TickTock | 0:1596b8644523 | 104 | |
TickTock | 0:1596b8644523 | 105 | char counter = 0; |
TickTock | 0:1596b8644523 | 106 | //DigitalOut can1_SleepMode(p11); // Use pin 11 to control the sleep mode of can1 |
TickTock | 0:1596b8644523 | 107 | //DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 |
TickTock | 0:1596b8644523 | 108 | Serial pc(USBTX, USBRX); // tx, rx default settings (9600 8N1) |
TickTock | 0:1596b8644523 | 109 | |
TickTock | 1:dafb963c3c14 | 110 | unsigned short getTimeStamp() {// from Gary's code |
TickTock | 1:dafb963c3c14 | 111 | |
TickTock | 1:dafb963c3c14 | 112 | int msec = timer.read_ms(); // not synchronized with RTC |
TickTock | 1:dafb963c3c14 | 113 | //unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 |
TickTock | 1:dafb963c3c14 | 114 | unsigned long secs = msec / 1000; |
TickTock | 1:dafb963c3c14 | 115 | int isecs = secs % 60; // modulo 60 for 0-59 seconds from RTC |
TickTock | 1:dafb963c3c14 | 116 | return ((isecs << 10) + (msec%1000)); // return the two byte time stamp |
TickTock | 1:dafb963c3c14 | 117 | } |
TickTock | 1:dafb963c3c14 | 118 | |
TickTock | 1:dafb963c3c14 | 119 | void readLog () |
TickTock | 0:1596b8644523 | 120 | { |
TickTock | 0:1596b8644523 | 121 | FILE *file; |
TickTock | 1:dafb963c3c14 | 122 | unsigned char c; |
TickTock | 1:dafb963c3c14 | 123 | int i=0; |
TickTock | 1:dafb963c3c14 | 124 | pc.printf("printing file\n"); |
TickTock | 1:dafb963c3c14 | 125 | file = fopen(LOGFILE, "r"); |
TickTock | 1:dafb963c3c14 | 126 | if (file == NULL) { |
TickTock | 1:dafb963c3c14 | 127 | pc.printf("no file found\n"); |
TickTock | 1:dafb963c3c14 | 128 | } |
TickTock | 1:dafb963c3c14 | 129 | while (!feof(file)) |
TickTock | 1:dafb963c3c14 | 130 | { |
TickTock | 1:dafb963c3c14 | 131 | c=fgetc(file); |
TickTock | 1:dafb963c3c14 | 132 | pc.printf("%02x ",c); |
TickTock | 1:dafb963c3c14 | 133 | if (++i>11) |
TickTock | 1:dafb963c3c14 | 134 | { |
TickTock | 1:dafb963c3c14 | 135 | pc.printf("\n"); |
TickTock | 1:dafb963c3c14 | 136 | i=0; |
TickTock | 1:dafb963c3c14 | 137 | } |
TickTock | 1:dafb963c3c14 | 138 | } |
TickTock | 1:dafb963c3c14 | 139 | pc.printf("\n"); |
TickTock | 1:dafb963c3c14 | 140 | pc.printf("\n"); |
TickTock | 1:dafb963c3c14 | 141 | fclose(file); |
TickTock | 1:dafb963c3c14 | 142 | } |
TickTock | 1:dafb963c3c14 | 143 | |
TickTock | 1:dafb963c3c14 | 144 | void logCan (CANMessage canRXmsg) |
TickTock | 1:dafb963c3c14 | 145 | { |
TickTock | 1:dafb963c3c14 | 146 | FILE *file; |
TickTock | 1:dafb963c3c14 | 147 | unsigned short ts; |
TickTock | 1:dafb963c3c14 | 148 | |
TickTock | 1:dafb963c3c14 | 149 | ts=getTimeStamp(); |
TickTock | 0:1596b8644523 | 150 | |
TickTock | 1:dafb963c3c14 | 151 | if (!logCreated) { |
TickTock | 0:1596b8644523 | 152 | file = fopen(LOGFILE, "w"); |
TickTock | 1:dafb963c3c14 | 153 | logCreated = true; |
TickTock | 0:1596b8644523 | 154 | } |
TickTock | 0:1596b8644523 | 155 | else |
TickTock | 0:1596b8644523 | 156 | file = fopen(LOGFILE, "a"); |
TickTock | 0:1596b8644523 | 157 | |
TickTock | 0:1596b8644523 | 158 | if (file == NULL) { |
TickTock | 1:dafb963c3c14 | 159 | if (logCreated) |
TickTock | 1:dafb963c3c14 | 160 | logCreated = false; |
TickTock | 1:dafb963c3c14 | 161 | return; |
TickTock | 1:dafb963c3c14 | 162 | } |
TickTock | 1:dafb963c3c14 | 163 | else |
TickTock | 1:dafb963c3c14 | 164 | { |
TickTock | 1:dafb963c3c14 | 165 | fprintf(file,"%c%c%c%c%c%c%c%c%c%c%c%c",ts>>8,ts&0xff,canRXmsg.id&0xff,(canRXmsg.id>>8)+(canRXmsg.len<<4),canRXmsg.data[0],canRXmsg.data[1],canRXmsg.data[2],canRXmsg.data[3],canRXmsg.data[4],canRXmsg.data[5],canRXmsg.data[6],canRXmsg.data[7]); |
TickTock | 1:dafb963c3c14 | 166 | fclose(file); |
TickTock | 1:dafb963c3c14 | 167 | } |
TickTock | 1:dafb963c3c14 | 168 | } |
TickTock | 1:dafb963c3c14 | 169 | |
TickTock | 1:dafb963c3c14 | 170 | void Log (char *message) |
TickTock | 1:dafb963c3c14 | 171 | { |
TickTock | 1:dafb963c3c14 | 172 | FILE *file; |
TickTock | 1:dafb963c3c14 | 173 | |
TickTock | 1:dafb963c3c14 | 174 | if (!logCreated) { |
TickTock | 1:dafb963c3c14 | 175 | file = fopen(LOGFILE, "w"); |
TickTock | 1:dafb963c3c14 | 176 | logCreated = true; |
TickTock | 1:dafb963c3c14 | 177 | } |
TickTock | 1:dafb963c3c14 | 178 | else |
TickTock | 1:dafb963c3c14 | 179 | file = fopen(LOGFILE, "a"); |
TickTock | 1:dafb963c3c14 | 180 | |
TickTock | 1:dafb963c3c14 | 181 | if (file == NULL) { |
TickTock | 1:dafb963c3c14 | 182 | if (logCreated) |
TickTock | 1:dafb963c3c14 | 183 | logCreated = false; |
TickTock | 0:1596b8644523 | 184 | return; |
TickTock | 0:1596b8644523 | 185 | } |
TickTock | 0:1596b8644523 | 186 | else |
TickTock | 0:1596b8644523 | 187 | { |
TickTock | 0:1596b8644523 | 188 | fputs(message, file); |
TickTock | 0:1596b8644523 | 189 | fclose(file); |
TickTock | 0:1596b8644523 | 190 | } |
TickTock | 1:dafb963c3c14 | 191 | } |
TickTock | 0:1596b8644523 | 192 | |
TickTock | 1:dafb963c3c14 | 193 | void send1() { |
TickTock | 1:dafb963c3c14 | 194 | static char counter = 0; // use for fake data |
TickTock | 1:dafb963c3c14 | 195 | |
TickTock | 1:dafb963c3c14 | 196 | can1.write(CANMessage(0x350, &counter, 1)); |
TickTock | 1:dafb963c3c14 | 197 | counter++; |
TickTock | 1:dafb963c3c14 | 198 | // test sending 3 quickly |
TickTock | 1:dafb963c3c14 | 199 | //can1.write(CANMessage(0x351, &counter, 1)); |
TickTock | 1:dafb963c3c14 | 200 | //can1.write(CANMessage(0x352, &counter, 1)); |
TickTock | 1:dafb963c3c14 | 201 | |
TickTock | 0:1596b8644523 | 202 | } |
TickTock | 0:1596b8644523 | 203 | |
TickTock | 0:1596b8644523 | 204 | |
TickTock | 0:1596b8644523 | 205 | void recieve1() { |
TickTock | 0:1596b8644523 | 206 | static CANMessage msg1; |
TickTock | 1:dafb963c3c14 | 207 | unsigned short msgTime; |
TickTock | 1:dafb963c3c14 | 208 | |
TickTock | 1:dafb963c3c14 | 209 | msgTime=getTimeStamp(); |
TickTock | 0:1596b8644523 | 210 | can1.read(msg1); |
TickTock | 1:dafb963c3c14 | 211 | pc.printf("%sCan1 Message received: %d %x\n", upLine, msg1.data[0], msgTime); |
TickTock | 0:1596b8644523 | 212 | lcd.printf("Can1 rxd: %d\n", msg1.data[0]); |
TickTock | 1:dafb963c3c14 | 213 | if(logCreated) { |
TickTock | 1:dafb963c3c14 | 214 | if((PB1==0)||(CD==0)){ |
TickTock | 1:dafb963c3c14 | 215 | logCreated=false; |
TickTock | 1:dafb963c3c14 | 216 | pc.printf("Stopping log\n", counter); |
TickTock | 1:dafb963c3c14 | 217 | readLog(); |
TickTock | 1:dafb963c3c14 | 218 | }else{ |
TickTock | 1:dafb963c3c14 | 219 | logCan(msg1); |
TickTock | 1:dafb963c3c14 | 220 | } |
TickTock | 1:dafb963c3c14 | 221 | } |
TickTock | 0:1596b8644523 | 222 | led2 = !led2; |
TickTock | 0:1596b8644523 | 223 | } |
TickTock | 0:1596b8644523 | 224 | void recieve2() { |
TickTock | 0:1596b8644523 | 225 | static CANMessage msg2; |
TickTock | 1:dafb963c3c14 | 226 | unsigned short msgTime; |
TickTock | 1:dafb963c3c14 | 227 | int milisec; |
TickTock | 1:dafb963c3c14 | 228 | |
TickTock | 1:dafb963c3c14 | 229 | msgTime=getTimeStamp(); |
TickTock | 1:dafb963c3c14 | 230 | milisec=timer.read_ms(); |
TickTock | 1:dafb963c3c14 | 231 | |
TickTock | 0:1596b8644523 | 232 | can2.read(msg2); |
TickTock | 1:dafb963c3c14 | 233 | pc.printf("%sCan2 Message received: %d %04x %d \n", upLine, msg2.data[0],msgTime,milisec); |
TickTock | 1:dafb963c3c14 | 234 | ;lcd.printf("Can2 rxd: %d\n", msg2.data[0]); |
TickTock | 1:dafb963c3c14 | 235 | if(logCreated) { |
TickTock | 1:dafb963c3c14 | 236 | if((PB1==0)||(CD==0)){ |
TickTock | 1:dafb963c3c14 | 237 | logCreated=false; |
TickTock | 1:dafb963c3c14 | 238 | pc.printf("Stopping log\n\n", counter); |
TickTock | 1:dafb963c3c14 | 239 | readLog(); |
TickTock | 1:dafb963c3c14 | 240 | }else{ |
TickTock | 1:dafb963c3c14 | 241 | logCan(msg2); |
TickTock | 1:dafb963c3c14 | 242 | } |
TickTock | 1:dafb963c3c14 | 243 | } |
TickTock | 0:1596b8644523 | 244 | led3 = !led3; |
TickTock | 0:1596b8644523 | 245 | } |
TickTock | 0:1596b8644523 | 246 | |
TickTock | 0:1596b8644523 | 247 | int main() { |
TickTock | 0:1596b8644523 | 248 | pc.baud(115200); // change serial interface to pc to 115200, 8N1 |
TickTock | 1:dafb963c3c14 | 249 | can1.frequency(1000000); |
TickTock | 1:dafb963c3c14 | 250 | can2.frequency(1000000); |
TickTock | 1:dafb963c3c14 | 251 | //can1_SleepMode = 0; // Enable TX |
TickTock | 1:dafb963c3c14 | 252 | //can2_SleepMode = 0; // Enable TX |
TickTock | 1:dafb963c3c14 | 253 | //can1_SleepMode = 1; // Turn on Monitor_only Mode |
TickTock | 1:dafb963c3c14 | 254 | //can2_SleepMode = 1; // Turn on Monitor_only Mode |
TickTock | 0:1596b8644523 | 255 | CD.mode(PullUp) ; //SDRAM Chip Detect |
TickTock | 1:dafb963c3c14 | 256 | dbPB1.mode(PullUp) ; //Pushbutton 1 |
TickTock | 1:dafb963c3c14 | 257 | ticker.attach(&send1, 1.1); |
TickTock | 0:1596b8644523 | 258 | can1.attach(&recieve1); |
TickTock | 0:1596b8644523 | 259 | can2.attach(&recieve2); |
TickTock | 1:dafb963c3c14 | 260 | |
TickTock | 1:dafb963c3c14 | 261 | timer.start() ; |
TickTock | 1:dafb963c3c14 | 262 | |
TickTock | 1:dafb963c3c14 | 263 | |
TickTock | 0:1596b8644523 | 264 | if(CD == 1) { |
TickTock | 1:dafb963c3c14 | 265 | pc.printf("Starting Can Log\n\n\n\n"); |
TickTock | 1:dafb963c3c14 | 266 | Log(""); |
TickTock | 0:1596b8644523 | 267 | } |
TickTock | 0:1596b8644523 | 268 | else { |
TickTock | 1:dafb963c3c14 | 269 | pc.printf("No SDRAM Inserted.\n\n\n\n"); |
TickTock | 0:1596b8644523 | 270 | } |
TickTock | 0:1596b8644523 | 271 | } |