Dual CANbus monitor and instrumentation cluster supporting ILI9341 display controller
Dependencies: SPI_TFTx2_ILI9341 TOUCH_TFTx2_ILI9341 TFT_fonts mbed
Fork of CANary by
Diff: main.cpp
- Revision:
- 1:9dcd70c32180
- Parent:
- 0:1596b8644523
- Child:
- 2:71b1999a8ea5
--- a/main.cpp Mon Nov 19 04:44:11 2012 +0000 +++ b/main.cpp Sun Feb 03 18:16:53 2013 +0000 @@ -1,157 +1,310 @@ #include "mbed.h" #include "CAN.h" -#include "TextLCD.h" -#include "SDFileSystem.h" -#include "DebounceIn.h" #include "log.h" #include "beep.h" +#include "MSCFileSystem.h" +#include "SPI_TFTx2.h" +#include "Arial12x12.h" +#include "Arial28x28.h" +#include "TOUCH_TFTx2.h" +#define upLine "\033[1A" + +//CANary.cpp + +//LEAF OBD +//1: +//2: +//3: AVCAN-L White/Blue +//4: +//5: VSS Brown,White/Brown +//6: CARCAN-H Green +//7: +//8: 12V-SW Orange,White/Orange +//9: +//10: +//11: AVCAN-H Blue +//12: EVCAN-L White/Grey +//13: EVCAN-H Grey +//14: CARCAN-L White/Green +//15: +//16: 12V-AON Red/Blue,Blue/Red //VP230 -//1:D 8:RS -//2:GND 7:CANH -//3:VCC 6:CANL -//4:R 5:Vref +//1:D +//2:GND +//3:VCC +//4:R +//5:Vref +//6:CANL +//7:CANH +//8:RS //LPC1768 //1: VSS -//2: NC:VIN +//2: NC:VIN (4.5-9V supply) //3: NC:VB //4: NC:nR -//5: SPI:MOSI --> 6:SDRAM:DI -//6: SPI:MISO --> 2:SDRAM:DO -//7: SPI:SCLK --> 4:SDRAM:SCLK -//8: CS --> 7:SDRAM:CS -//9: CAN1:TX --> 1:CAN1:D -//10: CAN1:RX --> 4:CAN1:R -//11: RS --> 4:LCD:RS -//12: E --> 6:LCD:E -//13: D4 --> 11:LCD:D4 -//14: D5 --> 12:LCD:D5 -//15: D6 --> 13:LCD:D6 -//16: D7 --> 14:LCD:D7 -//17: CD --> 1:SDRAM:CD -//18: NC:Ain -//19: PB2 -//20: PB1 +//5: SPI:CS0 +//6: SPI:CS1 +//7: SPI:Reset +//8: CAN1:Sleep --> 8:CAN1:RS +//9: CAN1:RX --> 4:CAN1:R +//10: CAN1:TX --> 1:CAN1:D +//11: SPI:MOSI +//12: SPI:MISO +//13: SPI:SCLK +//14: NC:Ain +//15: MON12V --> 4K to 12V, 1K to VSS (To be implemented) +//16: TOUCH_X+ +//17: TOUCH_X- +//18: NC:Aout +//19: TOUCH_Y+ +//20: TOUCH_Y- //21: Spkr+ -//22: NC:pwm +//22: Spkr- (optional complimentary output for more volume) //23: NC:pwm -//24: NC:pwm +//24: LED //25: NC:pwm //26: NC:pwm -//27: CAN1:Sleep --> 8:CAN1:RS +//27: NC //28: CAN2:Sleep --> 8:CAN2:RS -//29: CAN2:RX --> 4:CAN2:R -//30: CAN2:TX --> 1:CAN2:D -//31: NC:USB_D+ -//32: NC:USB_D- +//29: CAN2:TX --> 1:CAN2:D +//30: CAN2:RX --> 4:CAN2:R +//31: USB_D+ +//32: USB_D- //33: NC:Eth_TD+ //34: NC:Eth_TD- //35: NC:Eth_RD+ //36: NC:Eth_RD- //37: NC:IF+ //38: NC:IF- -//39: NC:5Vout +//39: NC:5Vout (only available when connected as USB device) //40: VCC3.3 +time_t seconds ; Beep buzzer(p21); -DigitalIn CD( p17 ); -DigitalIn PB1( p20 ); -SDFileSystem sd(p5, p6, p7, p8, "sd"); // SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) -TextLCD lcd(p11, p12, p13, p14, p15, p16); // rs, e, d0-d3 (RW to GND) + Ticker ticker; +Timer timer; DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); -CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (tx, rx) -CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (tx, rx) -bool LogCreated = false; +CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (rx, tx) and pin 27 (rs) +DigitalOut can1_SleepMode(p8); // Use pin 8 to control the sleep mode of can1 +CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (rx, tx) and pin 28 (rs) +DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 +bool logCreated = false; char logMsg[64]; char counter = 0; -//DigitalOut can1_SleepMode(p11); // Use pin 11 to control the sleep mode of can1 -//DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 -Serial pc(USBTX, USBRX); // tx, rx default settings (9600 8N1) + +TOUCH_TFTx2 tt(p16, p17, p19, p20, p11, p12, p13, p6, p7, p5, "TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs0, cs1, reset + +unsigned short getTimeStamp() {// from Gary's code + + //----------- + // read ms from the timer + int msec = timer.read_ms() ; + + // quickly, read Date and Time (to seconds) from the RTC + unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900 + + //----------- + if ( msec > 999 ) msec = 999 ; + int isecs = secs % 60 ; // modulo 60 for 0-59 seconds from RTC + + return ( ( isecs << 10 ) + msec ) ; // return the two byte time stamp +} + +extern "C" void RTC_IRQHandler() +{ + + timer.reset() ; // zero ms at the-seconds-tick + +} + +extern "C" void RTC_Init (void) +{ + + // set up the RTC interrupts + LPC_RTC->ILR=0x00; + + //LPC_RTC->CIIR=0x02; // interrupts each minute - verified + LPC_RTC->CIIR=0x01; // interrupts each second - verified + + //LPC_RTC->CCR = 0x11; // use for interrupts every minute ???? + //LPC_RTC->CCR = 0x00; // Stop the RTC (apparently) + LPC_RTC->CCR = 0x01; // Start RTC (apparently use for interrupt every second) + + // NVIC_SetPriority( RTC_IRQn, LOW_PR ); + NVIC_EnableIRQ( RTC_IRQn ); +} + +void readLog () +{ + FILE *rfile; + unsigned char c; + int i=0; + printf("printing file\n"); + rfile = fopen(LOGFILE, "r"); + if (rfile == NULL) { + printf("no file found\n"); + } + while (!feof(rfile)) + { + c=fgetc(rfile); + printf("%02x ",c); + if (++i>11) + { + printf("\n"); + i=0; + } + } + fclose(rfile); +} + +void logCan (CANMessage canRXmsg) +{ + FILE *file; + + unsigned short ts; + + ts=getTimeStamp(); + + if (!logCreated) { + file = fopen(LOGFILE, "w"); + logCreated = true; + } + else + file = fopen(LOGFILE, "a"); + + if (file == NULL) { + if (logCreated) + logCreated = false; + return; + } + else + { + 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]); + fclose(file); + } +} void Log (char *message) { FILE *file; - if (!LogCreated) { + if (!logCreated) { file = fopen(LOGFILE, "w"); - LogCreated = true; + logCreated = true; } else file = fopen(LOGFILE, "a"); if (file == NULL) { - if (LogCreated) - LogCreated = false; + if (logCreated) + logCreated = false; return; } else { fputs(message, file); - //fprintf(file, message); fclose(file); } +} - if (file) - fclose(file); +void send1() { + static char counter = 0; // use for fake data + + can1.write(CANMessage(0x350, &counter, 1)); + counter++; + // test sending 3 quickly + //can1.write(CANMessage(0x351, &counter, 1)); + //can1.write(CANMessage(0x352, &counter, 1)); + } -void send() { - if(can1.write(CANMessage(0x350, &counter, 1))) { - counter++; - pc.printf("Message %d sent\n", counter); - lcd.printf("sent: %d \n", counter); - if(LogCreated) { - if((PB1==0)||(CD==0)){ - LogCreated=false; - pc.printf("Stopping log\n", counter); - }else{ - sprintf(logMsg, "sent: %d \n", counter); - Log(logMsg); - pc.printf("Message logged\n"); - buzzer.beep(800,0.025); } - } - } - led1 = !led1; -} - void recieve1() { static CANMessage msg1; + unsigned short msgTime; + + msgTime=getTimeStamp(); can1.read(msg1); - pc.printf("Can1 Message received: %d\n", msg1.data[0]); - lcd.printf("Can1 rxd: %d\n", msg1.data[0]); + printf("%sCan1 Message received: %d %x\n", upLine, msg1.data[0], msgTime); + printf("Can1 rxd: %d\n", msg1.data[0]); + if(logCreated) { + logCan(msg1); + } led2 = !led2; } void recieve2() { static CANMessage msg2; + unsigned short msgTime; + + msgTime=getTimeStamp(); can2.read(msg2); - pc.printf("Can2 Message received: %d\n", msg2.data[0]); - lcd.printf("Can2 rxd: %d\n", msg2.data[0]); + printf("%sCan2 Message received: %d %x\n", upLine, msg2.data[0],msgTime); + printf("Can2 rxd: %d\n", msg2.data[0]); + if(logCreated) { + logCan(msg2); + } + led3 = !led3; } int main() { - pc.baud(115200); // change serial interface to pc to 115200, 8N1 - can1.frequency(100000); - can2.frequency(100000); - //can1_SleepMode = 0; // Turn off Sleep Mode - //can2_SleepMode = 0; // Turn off Sleep Mode - CD.mode(PullUp) ; //SDRAM Chip Detect - PB1.mode(PullUp) ; //Pushbutton 1 - ticker.attach(&send, 1); + can1.frequency(1000000); + can2.frequency(1000000); + //can1_SleepMode = 0; // Enable TX + //can2_SleepMode = 0; // Enable TX + can1_SleepMode = 1; // Turn on Monitor_only Mode + can2_SleepMode = 1; // Turn on Monitor_only Mode + ticker.attach(&send1, 0.25); can1.attach(&recieve1); can2.attach(&recieve2); - if(CD == 1) { - pc.printf("Starting Can Log\n"); - Log("Starting Can Log\n"); - } - else { - pc.printf("No SDRAM Inserted.\n"); + unsigned int dsel = 1; // select right display + tt.set_display(2); // select both displays + tt.background(Black); // set background to black + tt.foreground(White); // set chars to white + tt.cls(); // clear the screen + tt.set_font((unsigned char*) Arial12x12); // select the font + tt.set_orientation(1); + tt.set_display(dsel); // select display + + tt.calibrate(); // calibrate the touch + tt.locate(0,0); + tt.claim(stdout); // send stdout to the TFT display + + struct tm t; // pointer to a static tm structure + + seconds = time(NULL); + t = *localtime(&seconds) ; + + // is it a date before 2012 ? + if ((t.tm_year + 1900) < 2012 ) { + // before 2012, so the RTC probably lost power + // So, set a near-recent date in 2012 + + // enter people-values here + t.tm_year = 2012 ; // 28 May 2012 + t.tm_mon = 6 ; // 1 to 12 + t.tm_mday = 1; + t.tm_hour = 12; // 12:59:56 PM (after noon) + t.tm_min = 59; + t.tm_sec = 56; + + // adjust for tm structure required values + t.tm_year = t.tm_year - 1900; + t.tm_mon = t.tm_mon - 1; + + // set the RTC + set_time(mktime(&t)); + seconds = time(NULL); + + // printf("Set RTC to:\n" ); + // strftime(sTemp, 32, "%a %m/%d/%Y %X", localtime(&seconds)); + // printf("%s\n", sTemp); // DAY MM/DD/YYYY HH:MM:SS } } \ No newline at end of file