System Management code

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Revision:
36:0afc0fc8f86b
Parent:
34:18bcf276d3bf
Child:
38:8efacce315ae
--- a/main.cpp	Wed Jan 07 03:36:43 2015 +0000
+++ b/main.cpp	Thu Jan 22 07:58:51 2015 +0000
@@ -3,18 +3,33 @@
 #include "outDiagnostics.h"
 #include "inCommands.h"
 
+//#define DEBUG_MODE
+
+void thread_watchDog(void const* args) {
+    tempData.wdtThreadId = Thread::gettid();
+#ifdef DEBUG_MODE
+    wdt.kick();
+#else
+    wdt.kick(WDT_TIME);
+#endif
+    while(1) {
+        Thread::signal_wait(0x1F);
+        wdt.kick();
+    }   
+}
+
 int main() {
-    wdt.kick();                         // Kick the watchdog timer
     pc.baud(BAUD);
-    pc.format(8, SerialBase::None, 2);  // 2 Stop bits, reduce bad serial packets
+    pc.format(8, SerialBase::None, 2);      // 2 Stop bits, reduce bad serial packets
+    pc.printf("\033[2J\033[2J\r\n");        // Clear screen
     
     can.mode(FIFO);
+    NVIC_SetPriority(TIMER0_IRQn, 0);
+    NVIC_SetPriority(PWM1_IRQn, 1);
+    NVIC_SetPriority(CAN_IRQn, 2);
+    NVIC_SetPriority(UART0_IRQn, 3);
     NVIC_SetPriority(TIMER3_IRQn, 4);
-    NVIC_SetPriority(UART0_IRQn, 0);
-    NVIC_SetPriority(CAN_IRQn, 3);
-    NVIC_SetPriority(TIMER0_IRQn, 1);
-    NVIC_SetPriority(PWM1_IRQn, 2);
-    
+
     bool normalReset = true;
     // Did a watchdog reset occur since last power cycle?
     if (wdt.checkFlag()) {
@@ -31,28 +46,25 @@
     }
     // Print normal reset string
     if (normalReset) pc.printf("Sys Mgmt Reset\r\n");
-    
-    // Start the 10Hz data thread
-    Thread gather(runTime::thread_gather, 0, osPriorityHigh, 512);
-    
-    // Start the 100Hz data timer (more time critical than thread)
+        
+    // Start the watchdog check-in thread
+    Thread watchdogCheck(thread_watchDog, 0, osPriorityRealtime, 128);
+        
+    // Start the 100Hz data timer (priotity high)
     RtosTimer sample(runTime::thread_sample, osTimerPeriodic);
     sample.start(FAST_LOOP*1000);
     
     // Start the serial, CAN threads
-    Thread serial_out(outDiagnostics::thread_serialOut, 0, osPriorityAboveNormal, 2000);     // Allocate 6kB RAM stack
-    Thread can_out(outDiagnostics::thread_canOut, 0, osPriorityAboveNormal, 512);            // Allocate 256B RAM stack
+    Thread serial_out(outDiagnostics::thread_serialOut, 0, osPriorityBelowNormal);
+    Thread can_out(outDiagnostics::thread_canOut, 0, osPriorityBelowNormal, 512);
     
-    wdt.kick(1);     // Startup complete, normal timeout
-    // Background task
+    // Start the input polling thread
+    Thread getInputs(inCommands::thread_getInputs, 0, osPriorityLow);
+        
+    // Main task
     while(1) {
-        // Service CAN and Xbee logic
-        inCommands::serviceCAN();
-        inCommands::receiveMsgXbee();
-        
-        int ret = inCommands::serviceSerial();
-        if (ret == -1) tempData.parseGoodChar = 'x';
-        if (ret == 1)  tempData.parseGoodChar = 251;
-        wdt.kick();
+        runTime::gather(0);
+        osSignalSet((osThreadId)(tempData.wdtThreadId), 1<<0);      // Signal watchdog thread
+        Thread::wait(GATHER_LOOP*1000);
     }
 }
\ No newline at end of file