.

Dependencies:   SDHCFileSystem mbed

Revision:
8:6872945e8e91
Parent:
7:281df0ba57d0
Child:
9:8396b76e42fc
--- a/main.cpp	Tue Dec 04 04:20:24 2012 +0000
+++ b/main.cpp	Tue Dec 04 19:06:41 2012 +0000
@@ -1,12 +1,10 @@
-#include "mbed.h"
-#include "CAN.h"
-#include "SDHCFileSystem.h"
-
-#define upLine "\033[1A"
-#define maxBufLen 2048
-
 //CANcan.cpp
-
+//A dual canbus monitoring "blackbox" application for the Nissan Leaf
+//Dumps all messages to a file on the SDRAM
+//Todo:
+//  bigger buffer to avoid overflow (or messge filtering - ignore 174?)
+//  
+// Connections:
 //LEAF OBD
 //1:                
 //2:
@@ -28,7 +26,7 @@
 //note 2: pins 12 & 13 next to key
 //note 3: pins 1 & 9 on right side looking into male connector with key on bottom
 
-//VP230
+//VP230{a,b}
 //1:D   
 //2:GND 
 //3:VCC 
@@ -39,8 +37,8 @@
 //8:RS          --> LPC1768:27,28
 
 //LPC1768
-//1:    VSS                             6V
-//2:        NC:VIN  (4.5-9V supply) ---->|---- OBD:16
+//1:    VSS                      6V
+//2:    VIN  (4.5-9V supply) ---->|---- OBD:16
 //3:        NC:VB
 //4:        NC:nR
 //5:    SPI:MOSI   -->  6:SDRAM:DI
@@ -49,22 +47,22 @@
 //8:    CS         -->  7:SDRAM:CS
 //9:    CAN1:RX    -->  4:CAN1:R
 //10:   CAN1:TX    -->  1:CAN1:D
-//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
+//11:       NC:RS         -->  4:LCD:RS
+//12:       NC:E          -->  6:LCD:E
+//13:       NC:D4         -->  11:LCD:D4
+//14:       NC:D5         -->  12:LCD:D5
+//15:       NC:D6         -->  13:LCD:D6
+//16:       NC:D7         -->  14:LCD:D7
 //17:   CD         -->  1:SDRAM:CD
-//18:   MON12V     -->  4K to 12V, 1K to VSS  (To be implemented)
+//18:       NC:MON12V     -->  4K to 12V, 1K to VSS  (To be implemented)
 //19:   PB2
 //20:   PB1
-//21:   Spkr+
-//22:   Spkr-           (optional complimentary output for more volume)
+//21:       NC:Spkr+
+//22:       NC:Spkr-           (optional complimentary output for more volume)
 //23:       NC:pwm
-//24:   LEDBLU     -->  18:LCD:BLU (only used for tri-color displays)
-//25:   LEDGRN     -->  17:LCD:GRN (only used for tri-color displays)
-//26:   LEDRED     -->  16:LCD:RED
+//24:       NC:LEDBLU     -->  18:LCD:BLU (only used for tri-color displays)
+//25:       NC:LEDGRN     -->  17:LCD:GRN (only used for tri-color displays)
+//26:       NC:LEDRED     -->  16:LCD:RED
 //27:   CAN1:Sleep -->  8:CAN1:RS
 //28:   CAN2:Sleep -->  8:CAN2:RS
 //29:   CAN2:TX    -->  1:CAN2:D
@@ -80,6 +78,14 @@
 //39:       NC:5Vout (only available when connected as USB device)
 //40:   VCC3.3
 
+#include "mbed.h"
+#include "CAN.h"
+#include "SDHCFileSystem.h"
+
+#define upLine "\033[1A"
+#define maxBufLen 2048
+#define canTimeout 5
+
 void Log (char *message);
 void LogErr (char *message);
 extern "C" void mbed_reset();
@@ -109,35 +115,41 @@
 
 extern "C" void RTC_IRQHandler() {
     static int i=0;
-    timer.reset() ; // zero ms at the-seconds-tick
-    if (logOpen) {
-        file = fopen(fileName, "ab");
-        if (file == NULL) {
-            logOpen = false;
-            pc.printf("Failed to append log file.\n\n");
-        } else {
-            while (i != bufPointer) {
-                for (int j = 0; j<13; j++){
-                    fprintf(file,"%c",writeBuffer[i][j]);
+    static int secsIdle=0;
+    timer.reset(); // zero ms at the-seconds-tick
+    if(i==bufPointer){ // no canbus messages logges
+        if (++secsIdle>canTimeout) { // canbus idle --> sleep to save power
+            __wfi();
+        }
+    } else {
+        if (logOpen) {
+            file = fopen(fileName, "ab");
+            if (file == NULL) {
+                logOpen = false;
+                pc.printf("Failed to append log file.\n\n");
+            } else {
+                while (i != bufPointer) {
+                    for (int j = 0; j<13; j++){
+                        fprintf(file,"%c",writeBuffer[i][j]);
+                    }
+                    if(++i >= maxBufLen)
+                        i=0;
                 }
-                if(++i >= maxBufLen)
-                    i=0;
+                led3 = !led3;
+                fclose(file);
             }
-            led3 = !led3;
-            fclose(file);
         }
+        i=bufPointer;
+        secsIdle=0;
     }
-
-    LPC_RTC->ILR |= (1<<0); // turn on Low Bit - works
+    LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next
 }
 
 extern "C" void RTC_Init (void) {
     LPC_RTC->ILR=0x00; // set up the RTC interrupts
     //LPC_RTC->CIIR=0x02; // interrupts each minute
     LPC_RTC->CIIR=0x01; // interrupts each second
-    //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)
+    LPC_RTC->CCR = 0x01;  // Clock enable
     NVIC_SetPriority( RTC_IRQn, 10 );
     NVIC_EnableIRQ( RTC_IRQn );
 }
@@ -197,7 +209,7 @@
     }
 }
 
-void send1() {
+/*void send1() {
     static char counter = 0; // use for fake data
     can1.write(CANMessage(0x350, &counter, 1));
     counter++;
@@ -215,7 +227,7 @@
     counter++;
     can2.write(CANMessage(0x352, &counter, 1));
     counter++;
-}
+}*/
 
 void recieve1() {
     CANMessage msg1;
@@ -272,11 +284,11 @@
     while (true) {
         if(CD == 1) {
             int fileNum = 0;
-            sprintf(fileName,"/sd/canary%d.evc",fileNum);
+            sprintf(fileName,"/sd/CANcan%d.alc",fileNum);
             file = fopen(fileName, "r");
             while ((file != NULL)&&(fileNum++<100)) {
                 fclose(file);
-                sprintf(fileName,"/sd/canary%d.evc",fileNum);
+                sprintf(fileName,"/sd/CANcan%d.alc",fileNum);
                 file = fopen(fileName, "r");
             }