Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "IOobjects.h"
00002 #include "runTime.h"
00003 #include "outDiagnostics.h"
00004 #include "inCommands.h"
00005 
00006 void thread_watchDog(void const* args)
00007 {
00008     tempData.wdtThreadId = Thread::gettid();
00009     wdt.kick(WDT_TIME);
00010     while(1) {
00011         //Thread::wait(100);
00012         Thread::signal_wait(0x1F);
00013         wdt.kick();
00014     }
00015 }
00016 // Startup code to initialize interfaces and fetch the profile
00017 void init()
00018 {
00019     char str[100];
00020     sprintf(str, "\033[2J\033[2J\r\n");     // Clear screen
00021 
00022     // GENERATE RESET TYPE MESSAGE
00023     bool normalReset = true;
00024     if (wdt.checkFlag()) {                  // Did a watchdog reset occur?
00025         wdt.clearFlag();                    // Clear flag
00026         op->faultCode |= WATCHDOG;          // Record fault
00027         sprintf(str, "System Mgmt Watchdog Reset\r\n");
00028         normalReset=false;
00029     }
00030     if (LPC_SC->RSID & (1<<3)) {            // Did a brownout reset occur?
00031         LPC_SC->RSID = (1<<3);              // Clear flag
00032         op->faultCode |= BROWNOUT;          // Record fault
00033         sprintf(str, "System Mgmt Brownout Reset\r\n");
00034         normalReset=false;
00035     }
00036     if (normalReset) sprintf(str, "System Mgmt Reset\r\n"); // Normal reset message
00037 
00038     // SET INTERUPT PRIORITIES
00039     NVIC_SetPriority(TIMER0_IRQn, 0);
00040     NVIC_SetPriority(PWM1_IRQn, 1);
00041     NVIC_SetPriority(CAN_IRQn, 2);
00042     NVIC_SetPriority(UART0_IRQn, 3);
00043     NVIC_SetPriority(TIMER3_IRQn, 4);
00044 
00045     // SET SYSTEM TIME (RTC)
00046     int t = time(NULL);                     // Get RTC time
00047     if (t == 0 || t == -1) set_time(0);     // Seed the timer if it was never setup
00048     tempData.timeSynced = false;            // Time unsynced on reset, use AMS as master clock
00049     op->startTime = time(NULL);             // Capture the startup time
00050     op->SysTime = time(NULL);               // Caputre the system time
00051     
00052     // LOAD LAST-USED CONFIGURATION PROFILE
00053     if (!Profile::loadStartUp()) {
00054         strncat(str, "Profile load Failed, using default\r\n", 99);   
00055     }
00056     op->profileIndex = Profile::usingProfile();
00057     wdt.kick();                                     // Kick watchdog timer before the wait
00058     wait(0.5);                                      // Wait so above messages aren't immediately over-written
00059     
00060     // ALLOCATE/RESIZE BUFFERS
00061     glvBat.size(param->glvBat_taps);
00062     dcdc.size(param->dcdc_taps);
00063     can.txSize(param->CANtxSize);
00064     can.rxSize(param->CANrxSize);
00065     pc.baud(param->SerialBaud);
00066     pc.txBufferSetSize(param->SerialTxSize);
00067     //xbeeRelay.baud(param->XbeeBaud);
00068     //xbeeRelay.txSize(param->XbeeTxSize);
00069     //xbeeRelay.rxSize(param->XbeeRxSize);
00070     
00071     // INITIAL SETUP, LINK PROFILE VARIABLES
00072     glvBat.changeCapacity(param->nominalCapacity);
00073     glvBat.setup(&(param->chargeCurrent), &(param->dischargeCurrent));
00074     dcdc.setup(&(param->dcdcThreshold), &(param->dcdcOverCurrent), &(param->dcdcStartDelay), &(param->dcdcStopDelay));
00075     AMSlatch.setup(&(param->amsStartDelay));
00076     IMDlatch.setup(&(param->imdStartDelay));
00077     AMSlatch.delayStart();
00078     IMDlatch.delayStart();
00079     
00080     // SETUP SERIAL PORT
00081     pc.format(8, SerialBase::None, 2);      // 2 Stop bits, reduce bad serial packets
00082     str[99] = 0;
00083     pc.printf(str);
00084 }
00085 
00086 int main()
00087 {
00088     init();
00089     
00090     // Start the watchdog check-in thread
00091     Thread watchdogCheck(thread_watchDog, 0, osPriorityRealtime, 128);
00092 
00093     // Start the 100Hz data timer (priotity high)
00094     RtosTimer sample(runTime::thread_sample, osTimerPeriodic);
00095     sample.start(FAST_LOOP*1000);
00096 
00097     // Start the serial, CAN threads
00098     Thread serial_out(outDiagnostics::thread_serialOut, 0, osPriorityBelowNormal);
00099     Thread can_out(outDiagnostics::thread_canOut, 0, osPriorityBelowNormal, 512);
00100 
00101     // Start the input polling thread
00102     Thread getInputs(inCommands::thread_getInputs, 0, osPriorityLow);
00103 
00104     // Main task
00105     while(1) {
00106         runTime::gather(0);
00107         osSignalSet((osThreadId)(tempData.wdtThreadId), 1<<0);      // Signal watchdog thread
00108         Thread::wait(GATHER_LOOP*1000);
00109     }
00110 }