Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

main.cpp

Committer:
TickTock
Date:
2013-02-03
Revision:
1:9dcd70c32180
Parent:
0:1596b8644523
Child:
2:71b1999a8ea5

File content as of revision 1:9dcd70c32180:

#include "mbed.h"
#include "CAN.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   
//2:GND 
//3:VCC 
//4:R   
//5:Vref
//6:CANL
//7:CANH
//8:RS

//LPC1768
//1:    VSS
//2:        NC:VIN  (4.5-9V supply)
//3:        NC:VB
//4:        NC:nR
//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:   Spkr-           (optional complimentary output for more volume)
//23:       NC:pwm
//24:       LED
//25:       NC:pwm
//26:       NC:pwm
//27:       NC
//28:   CAN2:Sleep -->  8:CAN2:RS
//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 (only available when connected as USB device)
//40:   VCC3.3

time_t seconds ;
Beep buzzer(p21);

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 (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;

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) {
        file = fopen(LOGFILE, "w");
        logCreated = true;
    }
    else
        file = fopen(LOGFILE, "a");

    if (file == NULL) {
        if (logCreated)
            logCreated = false;
        return;
    }
    else
    {
        fputs(message, 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 recieve1() {
    static CANMessage msg1;
    unsigned short msgTime;
    
    msgTime=getTimeStamp();
    can1.read(msg1);
    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);
    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() {
    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);
    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
    }
}