Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Revision:
88:45185a5f1c9b
Parent:
87:46ac3f2519d6
Child:
89:2263d349b484
--- a/utility.cpp	Fri Apr 19 02:06:59 2013 +0000
+++ b/utility.cpp	Sat Apr 20 17:08:40 2013 +0000
@@ -21,7 +21,7 @@
     NVIC_EnableIRQ( RTC_IRQn );
 }
 
-void logMsg (char *msg) {
+void printMsg (char *msg) {
     strcpy(displayLog[displayLoc],msg);
     displayLoc=displayLoc>17?0:displayLoc+1;
 }
@@ -137,7 +137,7 @@
                     if( nLost > 0 ) {
                         // We previously lost messages that did not get into the buffer
                         sprintf(sTemp,"-- Lost %d Messages.\n", nLost);
-                        logMsg(sTemp); // write buffer overrun
+                        printMsg(sTemp); // write buffer overrun
                         //spkr.beep(500,0.25);
                         
                         nLost = 0 ;
@@ -146,37 +146,41 @@
                 }
             }
         }
-    }else{ // not debugMode - keep code short
+    } else { // not debugMode - keep code short
         if(logOpen){
-            if(canRXmsg.id>0) {
-                ts=getTimeStamp(); // only use
-                writeBuffer[writePointer][0]=mType;
-                writeBuffer[writePointer][1]=(ts&0xff00)>>8;
-                writeBuffer[writePointer][2]=(ts&0x00ff);
-                writeBuffer[writePointer][3]=canRXmsg.id&0xff;
-                writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
-                for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
-                    writeBuffer[writePointer][i]=canRXmsg.data[i-5];
-                }
-                if (++writePointer >= maxBufLen) {
-                    writePointer = 0;
-                    led3 = !led3;
-                }
-                if (writePointer==readPointer) {
-                    // Just overwrote an entry that hasn't been sent to thumbdrive
-                    sprintf(sTemp,"Write buffer overrun.\n");
-                    logMsg(sTemp); // write buffer overrun
-                    spkr.beep(500,0.25);
-                }
+            int localWritePointer = writePointer++; // create local copy to make logCan reentrant
+            // note that the static variables above do not prevent safe reentry
+            // since they are only used for msgId<0x800 which will never interrupt
+            // another msgId<0x800 (both CANbusses are same priority)
+            if (writePointer >= maxBufLen) {
+                writePointer = 0;
+                led3 = !led3;
+            }
+            if (localWritePointer >= maxBufLen) { //Have to test local, too, just in case interrupted just after increment (got double incremented)
+                localWritePointer = 0;
+            }
+            ts=getTimeStamp(); // only use
+            writeBuffer[localWritePointer][0]=mType;
+            writeBuffer[localWritePointer][1]=(ts&0xff00)>>8;
+            writeBuffer[localWritePointer][2]=(ts&0x00ff);
+            writeBuffer[localWritePointer][3]=canRXmsg.id&0xff;
+            writeBuffer[localWritePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
+            for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[localWritePointer][i]=canRXmsg.data?)
+                writeBuffer[localWritePointer][i]=canRXmsg.data[i-5];
+            }
+            if (writePointer==readPointer) {
+                // Just caught up to read pointer
+                sprintf(sTemp,"Write buffer overrun.\n");
+                printMsg(sTemp); // write buffer overrun
+                spkr.beep(500,0.25);
             }
         }
     }
 
     if(canRXmsg.id<0x800){ // Block FFE and FFF messages
+
         if(indexLastMsg[canRXmsg.id]==0) { //Check if no entry
-            //ii=ii<99?ii+1:0; // Should never wrap - less than 100 different messages ever used
             if(ii<99) {
-                //indexLastMsg[canRXmsg.id]=ii; //Create entry if first message
                 indexLastMsg[canRXmsg.id]=++ii; //Create entry for first MsgID occurance
                 // ii max is 99 here
             } else {
@@ -184,7 +188,7 @@
                 if(ii==99) {
                     ii++; // step to 100 to log only one error
                     sprintf(sTemp,"MsgID buffer overrun.\n");
-                    logMsg(sTemp); // write buffer overrun
+                    printMsg(sTemp); // write buffer overrun
                 }
             }
         }
@@ -213,27 +217,27 @@
                 if(canRXmsg.data[3]==2){//Group 2 = cellpair data
                     bdi=BatDataBaseG2; // index offset for CP data (uses 00 - 1C)
                     sprintf(sTemp,"  Getting cell pair data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==4){//Group 4 = temperature data
                     bdi=BatDataBaseG4; // index offset for Temperature data (uses 20 - 22)
                     sprintf(sTemp,"  Getting temperature data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==1){//Group 1 data
                     bdi=BatDataBaseG1; // index offset for Group 1 data (uses 20 - 22)
                     sprintf(sTemp,"Getting Group 1 data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==3){//Group 3 data
                     bdi=BatDataBaseG3; // index offset for Group 3 data (uses 20 - 22)
                     sprintf(sTemp,"  Getting Group 3 data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else if(canRXmsg.data[3]==5){//Group 5 data
                     bdi=BatDataBaseG5; // index offset for Group 5 data (uses 20 - 22)
                     sprintf(sTemp,"  Getting Group 5 data\n");
-                    logMsg(sTemp);
+                    printMsg(sTemp);
                     
                 }else bdi=0xff; // ignore other messages (for now)
                 lasti=0;
@@ -307,16 +311,16 @@
     logCan(0,tsMsg); // Date-Time
 }
 
-void logErrMsg (char * errMsg) {
+void logEventMsg (char * eventMsg) {
     // log CAN-Do 8-character Pseudo Message
     CANMessage tsMsg;
     tsMsg.id=0xffe; // pseudo Message to CAN-Do log
     tsMsg.len=0xf;
-    int iMsgLen = strlen(errMsg);
+    int iMsgLen = strlen(eventMsg);
     // 8 character message compatible with CAN-Do
     for(int i=0; i<8; i++){
       tsMsg.data[i]=' '; 
-      if( i < iMsgLen ) tsMsg.data[i]=errMsg[i];
+      if( i < iMsgLen ) tsMsg.data[i]=eventMsg[i];
     }
     logCan(0,tsMsg); // FFE Comment Message
 }
@@ -369,52 +373,6 @@
     }
 }
 
-/*void sendCPreq() {
-    char i;
-    char data[8] = {0x02, 0x21, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff};
-    can1.monitor(false); // set to active mode
-    can1SleepMode = 0; // enable TX
-    can1.write(CANMessage(0x79b, data, 8));
-    
-    if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
-    
-    logCan(1,CANMessage(0x79b, data, 8)); // Group 2 Request on EV
-    data[0]=0x30; //change to request next line message
-    data[1]=0x01;
-    data[2]=0x00;
-    for(i=0;i<29;i++){
-        wait_ms(16); //wait 16ms
-        can1.write(CANMessage(0x79b, data, 8));
-    }
-    can1SleepMode = 1; // disable TX
-    can1.monitor(true); // set to snoop mode
-}
-
-void sendTreq() {
-    char i;
-    char data[8] = {0x02, 0x21, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff};
-    can1.monitor(false); // set to active mode
-    can1SleepMode = 0; // enable TX
-    can1.write(CANMessage(0x79b, data, 8));
-    
-    if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
-    
-    logCan(1,CANMessage(0x79b, data, 8)); // Group 4 request on EV
-    data[0]=0x30; //change to request next line message
-    data[1]=0x01;
-    data[2]=0x00;
-    for(i=0;i<3;i++){
-        wait_ms(16); //wait 16ms
-        can1.write(CANMessage(0x79b, data, 8));
-    }
-    can1SleepMode = 1; // disable TX
-    can1.monitor(true); // set to snoop mode
-}
-
-void autoPollISR() {  //This is the ticker ISR for auto-polling
-    pollCP=true;    //Set a flag to do in main loop instead of here
-}                   //since ticker blocks other interrupts*/
-
 void autoPollISR(){
     reqMsgCnt = 0; //reset message counter
     msgReq.attach(&sendReq,0.025);
@@ -496,9 +454,9 @@
     cfile = fopen("/local/config.txt", "r");
     if (cfile==NULL){ // if doesn't exist --> create
         sprintf(sTemp,"No config file found.\n");
-        logMsg(sTemp); // no config file
+        printMsg(sTemp); // no config file
         sprintf(sTemp,"Calibrating touch screen.\n");
-        logMsg(sTemp); // calibrating
+        printMsg(sTemp); // calibrating
         //tt.setcal(5570, 34030, 80, 108, 33700, 5780, 82, 108, 32500);// bypass calibration using my values
         tt.calibrate();   // run touchscreen calibration routine
         // NOTE: calibrates screen 1 first, then screen 0.
@@ -542,10 +500,10 @@
         if(ff<4){//If not latest format, save as latest format
             saveConfig();
             sprintf(sTemp,"Config file format updated.\n");
-            logMsg(sTemp); // config forat updates
+            printMsg(sTemp); // config forat updates
         }
         sprintf(sTemp,"Config file loaded.\n");
-        logMsg(sTemp); // config file loaded
+        printMsg(sTemp); // config file loaded
     }
 }