System Management code
Dependencies: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
main.cpp@36:0afc0fc8f86b, 2015-01-22 (annotated)
- Committer:
- pspatel321
- Date:
- Thu Jan 22 07:58:51 2015 +0000
- Revision:
- 36:0afc0fc8f86b
- Parent:
- 34:18bcf276d3bf
- Child:
- 38:8efacce315ae
Tested in car with other systems. Most features are good to go. Except xbees need work. The DC-DC protection features were giving problems due to spurious current measurements. They have been edited to reduce glitchy errors.
Who changed what in which revision?
User | Revision | Line number | New 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 | 36:0afc0fc8f86b | 6 | //#define DEBUG_MODE |
pspatel321 | 36:0afc0fc8f86b | 7 | |
pspatel321 | 36:0afc0fc8f86b | 8 | void thread_watchDog(void const* args) { |
pspatel321 | 36:0afc0fc8f86b | 9 | tempData.wdtThreadId = Thread::gettid(); |
pspatel321 | 36:0afc0fc8f86b | 10 | #ifdef DEBUG_MODE |
pspatel321 | 36:0afc0fc8f86b | 11 | wdt.kick(); |
pspatel321 | 36:0afc0fc8f86b | 12 | #else |
pspatel321 | 36:0afc0fc8f86b | 13 | wdt.kick(WDT_TIME); |
pspatel321 | 36:0afc0fc8f86b | 14 | #endif |
pspatel321 | 36:0afc0fc8f86b | 15 | while(1) { |
pspatel321 | 36:0afc0fc8f86b | 16 | Thread::signal_wait(0x1F); |
pspatel321 | 36:0afc0fc8f86b | 17 | wdt.kick(); |
pspatel321 | 36:0afc0fc8f86b | 18 | } |
pspatel321 | 36:0afc0fc8f86b | 19 | } |
pspatel321 | 36:0afc0fc8f86b | 20 | |
pspatel321 | 30:91af74a299e1 | 21 | int main() { |
pspatel321 | 33:6bc82b6b62e5 | 22 | pc.baud(BAUD); |
pspatel321 | 36:0afc0fc8f86b | 23 | pc.format(8, SerialBase::None, 2); // 2 Stop bits, reduce bad serial packets |
pspatel321 | 36:0afc0fc8f86b | 24 | pc.printf("\033[2J\033[2J\r\n"); // Clear screen |
pspatel321 | 30:91af74a299e1 | 25 | |
pspatel321 | 33:6bc82b6b62e5 | 26 | can.mode(FIFO); |
pspatel321 | 36:0afc0fc8f86b | 27 | NVIC_SetPriority(TIMER0_IRQn, 0); |
pspatel321 | 36:0afc0fc8f86b | 28 | NVIC_SetPriority(PWM1_IRQn, 1); |
pspatel321 | 36:0afc0fc8f86b | 29 | NVIC_SetPriority(CAN_IRQn, 2); |
pspatel321 | 36:0afc0fc8f86b | 30 | NVIC_SetPriority(UART0_IRQn, 3); |
pspatel321 | 34:18bcf276d3bf | 31 | NVIC_SetPriority(TIMER3_IRQn, 4); |
pspatel321 | 36:0afc0fc8f86b | 32 | |
pspatel321 | 33:6bc82b6b62e5 | 33 | bool normalReset = true; |
pspatel321 | 30:91af74a299e1 | 34 | // Did a watchdog reset occur since last power cycle? |
pspatel321 | 30:91af74a299e1 | 35 | if (wdt.checkFlag()) { |
pspatel321 | 33:6bc82b6b62e5 | 36 | wdt.clearFlag(); // Clear flag |
pspatel321 | 30:91af74a299e1 | 37 | data.watchdogReset = true; |
pspatel321 | 33:6bc82b6b62e5 | 38 | pc.printf("Sys Mgmt Watchdog Reset\r\n"); |
pspatel321 | 33:6bc82b6b62e5 | 39 | normalReset=false; |
pspatel321 | 30:91af74a299e1 | 40 | } |
pspatel321 | 33:6bc82b6b62e5 | 41 | // Did a brownout reset occur since last power cycle? |
pspatel321 | 33:6bc82b6b62e5 | 42 | if (LPC_SC->RSID & (1<<3)) { |
pspatel321 | 33:6bc82b6b62e5 | 43 | LPC_SC->RSID = (1<<3); // Clear flag |
pspatel321 | 33:6bc82b6b62e5 | 44 | pc.printf("Sys Mgmt Brownout Reset\r\n"); |
pspatel321 | 33:6bc82b6b62e5 | 45 | normalReset=false; |
pspatel321 | 33:6bc82b6b62e5 | 46 | } |
pspatel321 | 33:6bc82b6b62e5 | 47 | // Print normal reset string |
pspatel321 | 33:6bc82b6b62e5 | 48 | if (normalReset) pc.printf("Sys Mgmt Reset\r\n"); |
pspatel321 | 36:0afc0fc8f86b | 49 | |
pspatel321 | 36:0afc0fc8f86b | 50 | // Start the watchdog check-in thread |
pspatel321 | 36:0afc0fc8f86b | 51 | Thread watchdogCheck(thread_watchDog, 0, osPriorityRealtime, 128); |
pspatel321 | 36:0afc0fc8f86b | 52 | |
pspatel321 | 36:0afc0fc8f86b | 53 | // Start the 100Hz data timer (priotity high) |
pspatel321 | 34:18bcf276d3bf | 54 | RtosTimer sample(runTime::thread_sample, osTimerPeriodic); |
pspatel321 | 34:18bcf276d3bf | 55 | sample.start(FAST_LOOP*1000); |
pspatel321 | 33:6bc82b6b62e5 | 56 | |
pspatel321 | 30:91af74a299e1 | 57 | // Start the serial, CAN threads |
pspatel321 | 36:0afc0fc8f86b | 58 | Thread serial_out(outDiagnostics::thread_serialOut, 0, osPriorityBelowNormal); |
pspatel321 | 36:0afc0fc8f86b | 59 | Thread can_out(outDiagnostics::thread_canOut, 0, osPriorityBelowNormal, 512); |
pspatel321 | 33:6bc82b6b62e5 | 60 | |
pspatel321 | 36:0afc0fc8f86b | 61 | // Start the input polling thread |
pspatel321 | 36:0afc0fc8f86b | 62 | Thread getInputs(inCommands::thread_getInputs, 0, osPriorityLow); |
pspatel321 | 36:0afc0fc8f86b | 63 | |
pspatel321 | 36:0afc0fc8f86b | 64 | // Main task |
pspatel321 | 30:91af74a299e1 | 65 | while(1) { |
pspatel321 | 36:0afc0fc8f86b | 66 | runTime::gather(0); |
pspatel321 | 36:0afc0fc8f86b | 67 | osSignalSet((osThreadId)(tempData.wdtThreadId), 1<<0); // Signal watchdog thread |
pspatel321 | 36:0afc0fc8f86b | 68 | Thread::wait(GATHER_LOOP*1000); |
pspatel321 | 30:91af74a299e1 | 69 | } |
pspatel321 | 30:91af74a299e1 | 70 | } |