]> www.average.org Git - pulsecounter.git/commitdiff
wip on jitter counter
authorEugene Crosser <crosser@average.org>
Sun, 24 Jan 2016 22:03:18 +0000 (01:03 +0300)
committerEugene Crosser <crosser@average.org>
Sun, 24 Jan 2016 22:03:18 +0000 (01:03 +0300)
msp430/Hal/Hal.c
msp430/Hal/Hal.h
msp430/Pulsecounter-Prog.c
msp430/Pulsecounter.ems

index f7b1213946d59b37d13ba888f2ce5094736d427c..4cae9fab389db0e8d4a7bbc33858400ff0374025 100644 (file)
@@ -93,7 +93,51 @@ static Hal_Handler appGpioHandler;
 static volatile uint16_t handlerEvents = 0;
 static uint16_t clockTick = 0;
 static Hal_Handler handlerTab[NUM_HANDLERS];
+static uint32_t gpioCount[3];
+static bool timerActive[3] = {false, false, false};
+static uint16_t timerPoint[3];
 
+/* -------- INTERNAL FUNCTIONS -------- */
+
+static void gpioHandler(uint8_t id) {
+    uint8_t i;
+    uint16_t now, left;
+
+    if (timerActive[id])
+        return;
+    timerActive[id] = true;
+    now = TA1R;
+    timerPoint[id] = now + ACLK_TICKS_PER_SECOND; // One second ahead
+    left = ACLK_TICKS_PER_SECOND;
+    for (i = 0; i < 3; i++)
+        if (timerActive[i] && (timerPoint[i] - now) < left) {
+            left = timerPoint[i] - now;
+        }
+    TA1CCR0 = now + left;
+    TA1CCTL0 = CCIE;
+}
+
+static void tickHandler(uint16_t clock) {
+    uint8_t i;
+
+    for (i = 0; i < 3; i++)
+        if (timerActive[i] && timerPoint[i] == clock) {
+            uint32_t count = Hal_gpioCount(i);
+
+            if (count) {
+                ; // update timer; call jitter handler
+            } else {
+                ; // clear timer; call app gpio handler
+            }
+        }
+    // if all timers are unset, disable ticker.
+}
+
+static void postEvent(uint8_t handlerId) {
+    uint8_t key = Em_Hal_lock();
+    handlerEvents |= 1 << handlerId;
+    Em_Hal_unlock(key);
+}
 
 /* -------- APP-HAL INTERFACE -------- */
 
@@ -108,6 +152,7 @@ void Hal_gpioEnable(Hal_Handler handler) {
         Hal_delay(100);
         (P1IFG &= ~mask, P1IE |= mask);
     }
+    handlerTab[TICK_HANDLER_ID] = tickHandler;
 }
 
 void Hal_connected(void) {
@@ -194,12 +239,15 @@ void Hal_idleLoop(void) {
             uint8_t id;
             for (id = 0, mask = 0x1; id < NUM_HANDLERS; id++, mask <<= 1) {
                 if ((events & mask) && handlerTab[id]) {
-                    handlerTab[id](id);
+                    if (id == TICK_HANDLER_ID)
+                        handlerTab[id](TA1R);
+                    else
+                        handlerTab[id](id);
                 }
             }
         }
         else {          // await more events
-            SLEEP();
+            SLEEP();    // this also enables interrupts
         }
     }
 }
@@ -236,11 +284,13 @@ void Hal_redLedToggle(void) {
     RED_LED_TOGGLE();
 }
 
-void Hal_tickStart(uint16_t msecs, Hal_Handler handler) {
+uint16_t Hal_tickStart(uint16_t msecs, void (*handler)(uint16_t clock)) {
     handlerTab[TICK_HANDLER_ID] = handler;
-    clockTick = (ACLK_TICKS_PER_SECOND * msecs) / 1000;
-    TA1CCR0 = TA1R + clockTick;                 // Set the CCR0 interrupt for msecs from now.
+    uint16_t clockTick = (ACLK_TICKS_PER_SECOND * msecs) / 1000;
+    uint16_t then = TA1R + clockTick;
+    TA1CCR0 = then;               // Set the CCR0 interrupt for msecs from now.
     TA1CCTL0 = CCIE;                            // Enable the CCR0 interrupt
+    return then;
 }
 
 void Hal_tickStop(void) {
@@ -249,6 +299,14 @@ void Hal_tickStop(void) {
     TA1CCTL0 = 0;
 }
 
+uint32_t Hal_gpioCount(uint8_t id) {
+    DINT();
+    uint32_t count = gpioCount[id];
+    gpioCount[id] = 0;
+    EINT();
+    return count;
+}
+
 /* -------- SRT-HAL INTERFACE -------- */
 
 uint8_t Em_Hal_lock(void) {
@@ -291,24 +349,6 @@ void Em_Hal_watchOn(void) {
     UART_WATCH_ENABLE();
 }
 
-
-/* -------- INTERNAL FUNCTIONS -------- */
-
-static void gpioHandler(uint8_t id) {
-    uint16_t mask = BIT3 << id;
-
-    Hal_delay(GPIO_DEBOUNCE_MSECS);
-    if (GPIO_LOW(mask) && appGpioHandler)
-        appGpioHandler(id);
-    GPIO_ENABLE(mask);
-}
-
-static void postEvent(uint8_t handlerId) {
-    uint8_t key = Em_Hal_lock();
-    handlerEvents |= 1 << handlerId;
-    Em_Hal_unlock(key);
-}
-
 /* -------- INTERRUPT SERVICE ROUTINES -------- */
 
 #ifdef __GNUC__
@@ -323,8 +363,8 @@ INTERRUPT void gpioIsr(void) {
 
     for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1)
         if (GPIO_FIRED(mask)) {
+            gpioCount[id]++;
             postEvent(id);
-            GPIO_DISABLE(mask);
         }
     WAKEUP();
 }
index 1ceecb55f63c98aff226bfc1f0ee5813076a489a..0c969c4c19f96b1682535dd5322b1491aef5fe1c 100644 (file)
@@ -227,15 +227,32 @@ extern void Hal_redLedToggle(void);
  *   tickHandler - the address of the user's tick handler that will be called
  *
  * Returns:
- *   None
+ *   Future clock when handler will be called
  *
  * Side Effects:
  *   tickhandler called by the idle loop
  *
  **/
-extern void Hal_tickStart(uint16_t msecs, Hal_Handler Handler);
+extern uint16_t Hal_tickStart(uint16_t msecs, void (*handler)(uint16_t clock));
 extern void Hal_tickStop(void);
 
+/**
+ * --------- Hal_gpioCount ---------
+ *
+ * Returns the number of interrups encounted on gpio `id`
+ *
+ * Inputs:
+ *   id if the gpio (0-2 for gpio 3-5)
+ *
+ * Returns:
+ *   Counted interrupts
+ *
+ * Side Effects:
+ *   Resets the accumulator (counting restarts from zero).
+ *
+ **/
+extern uint32_t Hal_gpioCount(uint8_t id);
+
 #ifdef __cplusplus
 }
 #endif
index 11c704b70f34c05eca71ac6ade732c7d2a8a7e4e..e9280d7e100cb78c84d1d65615286c535be8036c 100644 (file)
@@ -2,7 +2,7 @@
 #include "Hal.h"
 
 static void gpioHandler(uint8_t id);
-static void tickHandler(void);
+static void tickHandler(uint16_t clock);
 static int32_t cold = 0;
 static int32_t hot  = 0;
 static bool connected = false;
@@ -61,7 +61,7 @@ static void gpioHandler(uint8_t id) {
     }
 }
 
-static void tickHandler(void) {
+static void tickHandler(uint16_t clock) {
     uint8_t i;
 
     Hal_tickStop();
@@ -112,3 +112,11 @@ void Pulsecounter_coldTick_fetch(Pulsecounter_coldTick_t* const output) {
 void Pulsecounter_hotTick_fetch(Pulsecounter_hotTick_t* const output) {
     *output = hot;
 }
+
+void Pulsecounter_coldJitter_fetch(Pulsecounter_coldJitter_t* output) {
+    *output = Hal_gpioCount(1);
+}
+
+void Pulsecounter_hotJitter_fetch(Pulsecounter_hotJitter_t* output) {
+    *output = Hal_gpioCount(2);
+}
index 925f2ada065202bdf1a7ea42dad194c1dbedae10..4217d3414061610cdb054b7f9e957dddad4c9ec8 100644 (file)
@@ -11,4 +11,12 @@ schema Pulsecounter {
         indicator
     };
 
+    int32 coldJitter {
+        indicator
+    };
+
+    int32 hotJitter {
+        indicator
+    };
+
 };