Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Revision:
17:c9ce210f6654
Parent:
13:fbd9b3f5a07c
diff -r a72ffe7d879d -r c9ce210f6654 IMD/IMD.cpp
--- a/IMD/IMD.cpp	Fri Oct 24 22:38:20 2014 +0000
+++ b/IMD/IMD.cpp	Sat Oct 25 03:28:55 2014 +0000
@@ -6,10 +6,9 @@
 const uint32_t PCLK = 24000000;     // Timer counting clock = 24Mhz
 const uint32_t TIMEOUT_TICKS = PCLK*ZERO_HZ_TIMEOUT;    // Zeroing timeout in clock ticks = seconds * PCLK
 const float EXTRA = 0.01;           // Margin on the IMD PWM limits
-const float ULIM = 50000000;        // Max value produced by IMD
 
 // Interrupt function, must be static context
-void TIM0_IRQ() {
+void tim0_IRQ() {
     // Capture event was found
     if (LPC_TIM0->IR & 1<<4) instance->edgeIRQ();
     
@@ -25,11 +24,11 @@
     widthTicks = 0;                 // Zero low, so that duty = 0/1 = 0%
     periodTicks = 1;
     
-    LPC_SC->PCONP |= (1<<2);        // Power on timer1
-    LPC_SC->PCLKSEL0 &= ~(3<<4);    // Clock at 24MHz
+    LPC_SC->PCONP |= (1<<1);        // Power on timer0
+    LPC_SC->PCLKSEL0 &= ~(3<<2);    // Clock at 24MHz
     
-    *(p1_26.pinsel_addr) |= 3 << p1_26.selmode_num;     // Set pin as capture pin
-    *(p1_26.pinmode_addr) |= (3 << p1_26.selmode_num);  // Pull down
+    *(p1_26.pinsel_addr)  |= 3 << p1_26.selmode_num;     // Set pin as capture pin
+    *(p1_26.pinmode_addr) |= 3 << p1_26.selmode_num;     // Pull down
     
     LPC_TIM0->TCR=2;       // Stop counter and hold at 0, for configuration, set to 1 to start operation
     LPC_TIM0->IR=0x3F;     // Clear any interrupt flags
@@ -37,9 +36,9 @@
     LPC_TIM0->PR=0;        // No prescale value, clock at full pclk=24Mhz
     LPC_TIM0->EMR=0;       // Do not use external match pins
     
-    NVIC_SetVector(TIMER1_IRQn, (uint32_t)&TIM0_IRQ);   // Point to the edge interrupt handler
-    NVIC_SetPrioriry(TIMER1_IRQn, 0);                   // Highest Priority
-    NVIC_EnableIRQ(TIMER1_IRQn);                        // Enable IRQ
+    NVIC_SetVector(TIMER0_IRQn, (uint32_t)&tim0_IRQ);   // Point to the interrupt handler
+    NVIC_SetPriority(TIMER0_IRQn, 0);                   // Highest Priority
+    NVIC_EnableIRQ(TIMER0_IRQn);                        // Enable IRQ
     LPC_TIM0->CCR = RISING;          // Generate interrupt on capture, capture on rising edge to start
     LPC_TIM0->MCR = 1;               // Interrupt on Match0 to establish the zero speed timeout
     LPC_TIM0->MR0 = LPC_TIM0->TC+TIMEOUT_TICKS;
@@ -47,7 +46,7 @@
 }
 
 void IMD::edgeIRQ() {
-    enum EdgeT type = LPC_TIM0->CCR;
+    uint32_t type = LPC_TIM0->CCR;
     uint32_t capTime = LPC_TIM0->CR0;
     LPC_TIM0->MR0 = capTime+TIMEOUT_TICKS;        // Set the next zeroing timeout
     
@@ -66,12 +65,12 @@
     }
     
     // Switch interrupt types to capture the next edge
-    if (type == RISING)  LPC_TIM0->CCR = FALIING;
+    if (type == RISING)  LPC_TIM0->CCR = FALLING;
     if (type == FALLING) LPC_TIM0->CCR = RISING;
 }
 
 void IMD::zeroIRQ() {
-    enum EdgeT type = LPC_TIM0->CCR;
+    uint32_t type = LPC_TIM0->CCR;
     periodTicks = 1;
     first = true;
     
@@ -94,23 +93,23 @@
 
 char IMD::status() {
     float freq = frequency();
-    if (freq == 0)                      return 0;   // IMD off
-    else if (05 < freq && freq <= 15)   return 1;   // 10Hz normal mode
-    else if (15 < freq && freq <= 25)   return 2;   // 20Hz undervoltage mode
-    else if (25 < freq && freq <= 35)   return 3;   // 30Hz speed start mode
-    else if (45 < freq && freq <= 55)   return 4;   // 40Hz IMD error
-    else if (55 < freq && freq <= 65)   return 5;   // 50Hz Ground error
-    else return -1;                                 // Invalid
+    if (freq == 0)                      return OFF;         // IMD off
+    else if (05 < freq && freq <= 15)   return NORMAL;      // 10Hz normal mode
+    else if (15 < freq && freq <= 25)   return UNDERVOLT;   // 20Hz undervoltage mode
+    else if (25 < freq && freq <= 35)   return SPEEDSTART;  // 30Hz speed start mode
+    else if (45 < freq && freq <= 55)   return ERROR;       // 40Hz IMD error
+    else if (55 < freq && freq <= 65)   return GROUNDERR;   // 50Hz Ground error
+    else return INVALID;                                    // Invalid
 }
 
 float IMD::resistance() {
-    char status = status();
-    float duty = duty();
+    char stat = status();
+    float dut = duty();
     
     // In normal or undervoltage mode, where Rf is available
-    if (status == 1 || status == 2) {
-        if (0.05-EXTRA <= duty && duty >= 0.95+EXTRA) {
-            float rf = (0.9*1200e3/(duty-0.05)) - 1200e3;
+    if (stat == 1 || stat == 2) {
+        if (0.05-EXTRA <= dut && dut >= 0.95+EXTRA) {
+            float rf = (0.9*1200e3/(dut-0.05)) - 1200e3;
             if (rf < 0) rf = 0;
             if (rf > 50e6) rf = 50e6;
             return rf;
@@ -118,10 +117,10 @@
         else return -1;
     }
     // In speed start, where only good/bad estimate is available
-    if (status == 3) {
-        if (0.05-EXTRA <= duty && duty >= 0.10+EXTRA)       return 50e6;        // Good
-        else if (0.90-EXTRA <= duty && duty >= 0.95+EXTRA)  return 0;           // Bad
+    if (stat == 3) {
+        if (0.05-EXTRA <= dut && dut >= 0.10+EXTRA)       return 50e6;        // Good
+        else if (0.90-EXTRA <= dut && dut >= 0.95+EXTRA)  return 0;           // Bad
         else return -1;
     }
-    return NaN;
+    return NAN;     // Measurement not available in this mode
 }
\ No newline at end of file