Dual CANbus monitor and instrumentation cluster

Dependencies:   SPI_TFTx2 TFT_fonts TOUCH_TFTx2 beep mbed

Fork of CANary by Tick Tock

Revision:
93:c2402e8cd0e2
Parent:
92:935adef49ea4
Child:
94:c3a14b3975d6
--- a/utility.cpp	Mon Apr 22 02:27:37 2013 +0000
+++ b/utility.cpp	Wed May 01 03:02:59 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 ;
@@ -148,26 +148,30 @@
         }
     }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);
-                }
+            NVIC_DisableIRQ(CAN_IRQn);  // Block interrupts until write pointer assigned
+            int localWritePointer = writePointer++; // create local copy to make logCan reentrant
+            // note that the static variables 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;
+            }
+            NVIC_EnableIRQ(CAN_IRQn);  // Unblock interrupts once local pointer set and global pointer incremented
+            ts=getTimeStamp();
+            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][5]=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);
             }
         }
     }
@@ -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
                 }
             }
         }
@@ -205,35 +209,32 @@
         
         //-------------------
         //Miscellaneous on-recieve operations below
-        if((mType==2)&&(canRXmsg.id==0x358)){ // headlight/turn signal indicator
-            headlights = (canRXmsg.data[1]&0x80)?true:false;
-            
-        }else if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data?  Need to store all responses
+        if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data?  Need to store all responses
             if(canRXmsg.data[0]<0x20){
                 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);
+                    sprintf(sTemp,"  Getting Group 1 data\n");
+                    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,7 +308,7 @@
     logCan(0,tsMsg); // Date-Time
 }
 
-void logErrMsg (char * errMsg) {
+void logEvent (char * errMsg) {
     // log CAN-Do 8-character Pseudo Message
     CANMessage tsMsg;
     tsMsg.id=0xffe; // pseudo Message to CAN-Do log
@@ -496,9 +497,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 +543,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
     }
 }