System Management code

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
pspatel321
Date:
Wed Feb 11 23:09:57 2015 +0000
Revision:
39:ddf38df9699e
Parent:
38:8efacce315ae
Updated CAN IDs for datalogging.  Changed profile encoding.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pspatel321 30:91af74a299e1 1 #include "IOobjects.h"
pspatel321 30:91af74a299e1 2 #include "runTime.h"
pspatel321 30:91af74a299e1 3 #include "outDiagnostics.h"
pspatel321 34:18bcf276d3bf 4 #include "inCommands.h"
pspatel321 30:91af74a299e1 5
pspatel321 38:8efacce315ae 6 void thread_watchDog(void const* args)
pspatel321 38:8efacce315ae 7 {
pspatel321 36:0afc0fc8f86b 8 tempData.wdtThreadId = Thread::gettid();
pspatel321 36:0afc0fc8f86b 9 wdt.kick(WDT_TIME);
pspatel321 36:0afc0fc8f86b 10 while(1) {
pspatel321 39:ddf38df9699e 11 //Thread::wait(100);
pspatel321 39:ddf38df9699e 12 Thread::signal_wait(0x1F);
pspatel321 36:0afc0fc8f86b 13 wdt.kick();
pspatel321 38:8efacce315ae 14 }
pspatel321 36:0afc0fc8f86b 15 }
pspatel321 38:8efacce315ae 16 // Startup code to initialize interfaces and fetch the profile
pspatel321 38:8efacce315ae 17 void init()
pspatel321 38:8efacce315ae 18 {
pspatel321 38:8efacce315ae 19 char str[100];
pspatel321 38:8efacce315ae 20 sprintf(str, "\033[2J\033[2J\r\n"); // Clear screen
pspatel321 36:0afc0fc8f86b 21
pspatel321 38:8efacce315ae 22 // GENERATE RESET TYPE MESSAGE
pspatel321 38:8efacce315ae 23 bool normalReset = true;
pspatel321 38:8efacce315ae 24 if (wdt.checkFlag()) { // Did a watchdog reset occur?
pspatel321 38:8efacce315ae 25 wdt.clearFlag(); // Clear flag
pspatel321 38:8efacce315ae 26 op->faultCode |= WATCHDOG; // Record fault
pspatel321 38:8efacce315ae 27 sprintf(str, "System Mgmt Watchdog Reset\r\n");
pspatel321 38:8efacce315ae 28 normalReset=false;
pspatel321 38:8efacce315ae 29 }
pspatel321 38:8efacce315ae 30 if (LPC_SC->RSID & (1<<3)) { // Did a brownout reset occur?
pspatel321 38:8efacce315ae 31 LPC_SC->RSID = (1<<3); // Clear flag
pspatel321 38:8efacce315ae 32 op->faultCode |= BROWNOUT; // Record fault
pspatel321 38:8efacce315ae 33 sprintf(str, "System Mgmt Brownout Reset\r\n");
pspatel321 38:8efacce315ae 34 normalReset=false;
pspatel321 38:8efacce315ae 35 }
pspatel321 38:8efacce315ae 36 if (normalReset) sprintf(str, "System Mgmt Reset\r\n"); // Normal reset message
pspatel321 38:8efacce315ae 37
pspatel321 38:8efacce315ae 38 // SET INTERUPT PRIORITIES
pspatel321 36:0afc0fc8f86b 39 NVIC_SetPriority(TIMER0_IRQn, 0);
pspatel321 36:0afc0fc8f86b 40 NVIC_SetPriority(PWM1_IRQn, 1);
pspatel321 36:0afc0fc8f86b 41 NVIC_SetPriority(CAN_IRQn, 2);
pspatel321 36:0afc0fc8f86b 42 NVIC_SetPriority(UART0_IRQn, 3);
pspatel321 34:18bcf276d3bf 43 NVIC_SetPriority(TIMER3_IRQn, 4);
pspatel321 36:0afc0fc8f86b 44
pspatel321 38:8efacce315ae 45 // SET SYSTEM TIME (RTC)
pspatel321 38:8efacce315ae 46 int t = time(NULL); // Get RTC time
pspatel321 38:8efacce315ae 47 if (t == 0 || t == -1) set_time(0); // Seed the timer if it was never setup
pspatel321 38:8efacce315ae 48 tempData.timeSynced = false; // Time unsynced on reset, use AMS as master clock
pspatel321 38:8efacce315ae 49 op->startTime = time(NULL); // Capture the startup time
pspatel321 38:8efacce315ae 50 op->SysTime = time(NULL); // Caputre the system time
pspatel321 38:8efacce315ae 51
pspatel321 38:8efacce315ae 52 // LOAD LAST-USED CONFIGURATION PROFILE
pspatel321 38:8efacce315ae 53 if (!Profile::loadStartUp()) {
pspatel321 38:8efacce315ae 54 strncat(str, "Profile load Failed, using default\r\n", 99);
pspatel321 30:91af74a299e1 55 }
pspatel321 38:8efacce315ae 56 op->profileIndex = Profile::usingProfile();
pspatel321 38:8efacce315ae 57 wdt.kick(); // Kick watchdog timer before the wait
pspatel321 38:8efacce315ae 58 wait(0.5); // Wait so above messages aren't immediately over-written
pspatel321 38:8efacce315ae 59
pspatel321 38:8efacce315ae 60 // ALLOCATE/RESIZE BUFFERS
pspatel321 38:8efacce315ae 61 glvBat.size(param->glvBat_taps);
pspatel321 38:8efacce315ae 62 dcdc.size(param->dcdc_taps);
pspatel321 38:8efacce315ae 63 can.txSize(param->CANtxSize);
pspatel321 38:8efacce315ae 64 can.rxSize(param->CANrxSize);
pspatel321 38:8efacce315ae 65 pc.baud(param->SerialBaud);
pspatel321 38:8efacce315ae 66 pc.txBufferSetSize(param->SerialTxSize);
pspatel321 39:ddf38df9699e 67 //xbeeRelay.baud(param->XbeeBaud);
pspatel321 39:ddf38df9699e 68 //xbeeRelay.txSize(param->XbeeTxSize);
pspatel321 39:ddf38df9699e 69 //xbeeRelay.rxSize(param->XbeeRxSize);
pspatel321 38:8efacce315ae 70
pspatel321 38:8efacce315ae 71 // INITIAL SETUP, LINK PROFILE VARIABLES
pspatel321 38:8efacce315ae 72 glvBat.changeCapacity(param->nominalCapacity);
pspatel321 38:8efacce315ae 73 glvBat.setup(&(param->chargeCurrent), &(param->dischargeCurrent));
pspatel321 38:8efacce315ae 74 dcdc.setup(&(param->dcdcThreshold), &(param->dcdcOverCurrent), &(param->dcdcStartDelay), &(param->dcdcStopDelay));
pspatel321 38:8efacce315ae 75 AMSlatch.setup(&(param->amsStartDelay));
pspatel321 38:8efacce315ae 76 IMDlatch.setup(&(param->imdStartDelay));
pspatel321 38:8efacce315ae 77 AMSlatch.delayStart();
pspatel321 38:8efacce315ae 78 IMDlatch.delayStart();
pspatel321 38:8efacce315ae 79
pspatel321 38:8efacce315ae 80 // SETUP SERIAL PORT
pspatel321 38:8efacce315ae 81 pc.format(8, SerialBase::None, 2); // 2 Stop bits, reduce bad serial packets
pspatel321 38:8efacce315ae 82 str[99] = 0;
pspatel321 38:8efacce315ae 83 pc.printf(str);
pspatel321 38:8efacce315ae 84 }
pspatel321 38:8efacce315ae 85
pspatel321 38:8efacce315ae 86 int main()
pspatel321 38:8efacce315ae 87 {
pspatel321 38:8efacce315ae 88 init();
pspatel321 38:8efacce315ae 89
pspatel321 36:0afc0fc8f86b 90 // Start the watchdog check-in thread
pspatel321 36:0afc0fc8f86b 91 Thread watchdogCheck(thread_watchDog, 0, osPriorityRealtime, 128);
pspatel321 38:8efacce315ae 92
pspatel321 36:0afc0fc8f86b 93 // Start the 100Hz data timer (priotity high)
pspatel321 34:18bcf276d3bf 94 RtosTimer sample(runTime::thread_sample, osTimerPeriodic);
pspatel321 34:18bcf276d3bf 95 sample.start(FAST_LOOP*1000);
pspatel321 38:8efacce315ae 96
pspatel321 30:91af74a299e1 97 // Start the serial, CAN threads
pspatel321 36:0afc0fc8f86b 98 Thread serial_out(outDiagnostics::thread_serialOut, 0, osPriorityBelowNormal);
pspatel321 36:0afc0fc8f86b 99 Thread can_out(outDiagnostics::thread_canOut, 0, osPriorityBelowNormal, 512);
pspatel321 38:8efacce315ae 100
pspatel321 36:0afc0fc8f86b 101 // Start the input polling thread
pspatel321 36:0afc0fc8f86b 102 Thread getInputs(inCommands::thread_getInputs, 0, osPriorityLow);
pspatel321 38:8efacce315ae 103
pspatel321 36:0afc0fc8f86b 104 // Main task
pspatel321 30:91af74a299e1 105 while(1) {
pspatel321 36:0afc0fc8f86b 106 runTime::gather(0);
pspatel321 36:0afc0fc8f86b 107 osSignalSet((osThreadId)(tempData.wdtThreadId), 1<<0); // Signal watchdog thread
pspatel321 36:0afc0fc8f86b 108 Thread::wait(GATHER_LOOP*1000);
pspatel321 30:91af74a299e1 109 }
pspatel321 30:91af74a299e1 110 }