From: Eugene Crosser Date: Mon, 25 Jan 2016 20:03:11 +0000 (+0300) Subject: code for jitter counter complete but not tried X-Git-Url: http://www.average.org/gitweb/?p=pulsecounter.git;a=commitdiff_plain;h=61da112497ec35ae85641a250b06b1b08e3b55c3 code for jitter counter complete but not tried --- diff --git a/msp430/Hal/Hal.c b/msp430/Hal/Hal.c index 4cae9fa..e351bb6 100644 --- a/msp430/Hal/Hal.c +++ b/msp430/Hal/Hal.c @@ -90,6 +90,7 @@ static void gpioHandler(uint8_t id); static void postEvent(uint8_t handlerId); static Hal_Handler appGpioHandler; +static void (*appJitterHandler)(uint8_t id, uint16_t count); static volatile uint16_t handlerEvents = 0; static uint16_t clockTick = 0; static Hal_Handler handlerTab[NUM_HANDLERS]; @@ -99,15 +100,22 @@ static uint16_t timerPoint[3]; /* -------- INTERNAL FUNCTIONS -------- */ -static void gpioHandler(uint8_t id) { +static uint32_t getCount(uint8_t id) { + DINT(); + uint32_t count = gpioCount[id]; + gpioCount[id] = 0; + EINT(); + return count; +} + +static void setTimer(uint8_t id, uint16_t delay) { uint8_t i; uint16_t now, left; - if (timerActive[id]) - return; timerActive[id] = true; + // enable clock if it was disabled to save power? now = TA1R; - timerPoint[id] = now + ACLK_TICKS_PER_SECOND; // One second ahead + timerPoint[id] = now + delay; left = ACLK_TICKS_PER_SECOND; for (i = 0; i < 3; i++) if (timerActive[i] && (timerPoint[i] - now) < left) { @@ -117,17 +125,39 @@ static void gpioHandler(uint8_t id) { TA1CCTL0 = CCIE; } +static void clearTimer(uint8_t id) { + uint8_t i; + bool keep = false; + + timerActive[id] = false; + for (i = 0; i < 3; i++) + if (timerActive[i]) keep = true; + if (!keep) { + TA1CCTL0 = 0; + // disable clock to save power? + } +} + +static void gpioHandler(uint8_t id) { + if (timerActive[id]) + return; + setTimer(id, ACLK_TICKS_PER_SECOND); // One second ahead +} + 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); + uint32_t count = getCount(i); + uint16_t mask = BIT3 << i; if (count) { - ; // update timer; call jitter handler + setTimer(i, ACLK_TICKS_PER_SECOND); // One second ahead + if (appJitterHandler) (*appJitterHandler)(i, count); } else { - ; // clear timer; call app gpio handler + clearTimer(i); + if (GPIO_LOW(mask) && appGpioHandler) (*appGpioHandler)(i); } } // if all timers are unset, disable ticker. @@ -141,13 +171,14 @@ static void postEvent(uint8_t handlerId) { /* -------- APP-HAL INTERFACE -------- */ -void Hal_gpioEnable(Hal_Handler handler) { +void Hal_gpioEnable(Hal_Handler handler, void (*jhandler)(uint8_t id, uint32_t count)) { uint8_t id; uint16_t mask; for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1) { handlerTab[id] = gpioHandler; appGpioHandler = handler; + appJitterHandler = jhandler; (P1DIR &= ~mask, P1REN |= mask, P1OUT |= mask, P1IES |= mask); Hal_delay(100); (P1IFG &= ~mask, P1IE |= mask); @@ -299,14 +330,6 @@ 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) { diff --git a/msp430/Hal/Hal.h b/msp430/Hal/Hal.h index 0c969c4..a613190 100644 --- a/msp430/Hal/Hal.h +++ b/msp430/Hal/Hal.h @@ -46,7 +46,7 @@ typedef void (*Hal_Handler)(uint8_t id); * BUTTON interrupt enabled * **/ -extern void Hal_gpioEnable(Hal_Handler handler); +extern void Hal_gpioEnable(Hal_Handler handler, void (*jhandler)(uint8_t id, uint32_t count)); /** * --------- Hal_connected --------- * @@ -236,23 +236,6 @@ extern void Hal_redLedToggle(void); 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 diff --git a/msp430/Pulsecounter-Prog.c b/msp430/Pulsecounter-Prog.c index e9280d7..1d31b75 100644 --- a/msp430/Pulsecounter-Prog.c +++ b/msp430/Pulsecounter-Prog.c @@ -2,22 +2,34 @@ #include "Hal.h" static void gpioHandler(uint8_t id); -static void tickHandler(uint16_t clock); +static void jitterHandler(uint8_t id, uint32_t count); static int32_t cold = 0; static int32_t hot = 0; +static int32_t coldJ = 0; +static int32_t hotJ = 0; static bool connected = false; void main() { Hal_init(); - Hal_gpioEnable(gpioHandler); + Hal_gpioEnable(gpioHandler, jitterHandler); Pulsecounter_setDeviceName("PULS-CNTR"); Pulsecounter_start(); Hal_idleLoop(); } -static void gpioHandler(uint8_t id) { +static void blink(uint8_t which, uint8_t count) { uint8_t i; + for (i = 0; i < count; i++) { + if (i) Hal_delay(10); + if (which & 1) Hal_greenLedOn(); + if (which & 2) Hal_redLedOn(); + Hal_delay(10); + if (which & 1) Hal_greenLedOff(); + if (which & 2) Hal_redLedOff(); + } +} +static void gpioHandler(uint8_t id) { switch (id) { case 0: /* Pulsecounter_accept(true); */ @@ -26,57 +38,45 @@ static void gpioHandler(uint8_t id) { Hal_delay(100); Pulsecounter_hotTick_indicate(); } - Hal_greenLedOn(); - Hal_redLedOn(); - Hal_delay(10); - Hal_greenLedOff(); - Hal_redLedOff(); - Hal_tickStart(15000, tickHandler); + blink(3, 2); break; case 1: cold++; if (connected) Pulsecounter_coldTick_indicate(); - Hal_greenLedOn(); - Hal_delay(10); - Hal_greenLedOff(); + blink(1, 2); break; case 2: hot++; if (connected) Pulsecounter_hotTick_indicate(); - Hal_redLedOn(); - Hal_delay(10); - Hal_redLedOff(); + blink(2, 2); break; default: - for (i = 0; i < 5; i++) { - Hal_greenLedOn(); - Hal_redLedOn(); - Hal_delay(10); - Hal_greenLedOff(); - Hal_redLedOff(); - Hal_delay(10); - } + blink(3, 15); } } -static void tickHandler(uint16_t clock) { - uint8_t i; - - Hal_tickStop(); - if (connected) - return; - for (i = 0; i < 3; i++) { - Hal_greenLedOn(); - Hal_delay(50); - Hal_redLedOn(); - Hal_delay(50); - Hal_redLedOff(); - Hal_delay(50); - Hal_greenLedOff(); +static void jitterHandler(uint8_t id, uint32_t count) { + switch (id) { + case 0: + blink(3, count); + break; + case 1: + coldJ = count; + if (connected) + Pulsecounter_coldJitter_indicate(); + blink(1, 1); + break; + case 2: + hotJ = count; + if (connected) + Pulsecounter_hotJitter_indicate(); + blink(2, 1); + break; + default: + blink(3, 13); } - /* Pulsecounter_accept(false); */ } /* -------- SCHEMA CALLBACKS -------- */ @@ -114,9 +114,9 @@ void Pulsecounter_hotTick_fetch(Pulsecounter_hotTick_t* const output) { } void Pulsecounter_coldJitter_fetch(Pulsecounter_coldJitter_t* output) { - *output = Hal_gpioCount(1); + *output = coldJ; } void Pulsecounter_hotJitter_fetch(Pulsecounter_hotJitter_t* output) { - *output = Hal_gpioCount(2); + *output = hotJ; }