X-Git-Url: http://average.org/gitweb/?a=blobdiff_plain;f=msp430%2FHal%2FHal.c;h=dda8c690e6c28bf8d9d22805426c77b55478b0d7;hb=refs%2Fheads%2Fcountjitter;hp=4cae9fab389db0e8d4a7bbc33858400ff0374025;hpb=4e9dda3367ff17c80358426a6b4126704741c0b1;p=pulsecounter.git diff --git a/msp430/Hal/Hal.c b/msp430/Hal/Hal.c index 4cae9fa..dda8c69 100644 --- a/msp430/Hal/Hal.c +++ b/msp430/Hal/Hal.c @@ -26,6 +26,7 @@ #define GPIO_ENABLE(mask) (P1IFG &= ~mask, P1IE |= mask) #define GPIO_DISABLE(mask) (P1IE &= ~mask, P1IFG &= ~mask) #define GPIO_FIRED(mask) (P1IFG & mask) +#define GPIO_ACK(mask) (P1IFG &= ~mask) #define GPIO_LOW(mask) (!(P1IN & mask)) #define GPIO_DEBOUNCE_MSECS 100 @@ -89,25 +90,33 @@ static void gpioHandler(uint8_t id); static void postEvent(uint8_t handlerId); -static Hal_Handler appGpioHandler; +static Hal_Handler appSettleHandler; +static void (*appJitterHandler)(uint8_t id, uint32_t count); static volatile uint16_t handlerEvents = 0; static uint16_t clockTick = 0; static Hal_Handler handlerTab[NUM_HANDLERS]; -static uint32_t gpioCount[3]; +static volatile uint32_t gpioCount[3] = {0}; static bool timerActive[3] = {false, false, false}; 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 +126,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); + if (timerActive[i] && timerPoint[i] == clock) { /* FIXME */ + 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) && appSettleHandler) (*appSettleHandler)(i); } } // if all timers are unset, disable ticker. @@ -141,13 +172,14 @@ static void postEvent(uint8_t handlerId) { /* -------- APP-HAL INTERFACE -------- */ -void Hal_gpioEnable(Hal_Handler handler) { +void Hal_gpioEnable(Hal_Handler shandler, 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; + appSettleHandler = shandler; + appJitterHandler = jhandler; (P1DIR &= ~mask, P1REN |= mask, P1OUT |= mask, P1IES |= mask); Hal_delay(100); (P1IFG &= ~mask, P1IE |= mask); @@ -237,12 +269,15 @@ void Hal_idleLoop(void) { EINT(); uint16_t mask; uint8_t id; + for (id = 0, mask = 0x1; id < NUM_HANDLERS; id++, mask <<= 1) { if ((events & mask) && handlerTab[id]) { - if (id == TICK_HANDLER_ID) - handlerTab[id](TA1R); - else + if (id == TICK_HANDLER_ID) { + uint16_t now = TA1R; + handlerTab[id](now); + } else { handlerTab[id](id); + } } } } @@ -286,7 +321,7 @@ void Hal_redLedToggle(void) { uint16_t Hal_tickStart(uint16_t msecs, void (*handler)(uint16_t clock)) { handlerTab[TICK_HANDLER_ID] = handler; - uint16_t clockTick = (ACLK_TICKS_PER_SECOND * msecs) / 1000; + 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 @@ -299,14 +334,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) { @@ -365,6 +392,7 @@ INTERRUPT void gpioIsr(void) { if (GPIO_FIRED(mask)) { gpioCount[id]++; postEvent(id); + GPIO_ACK(mask); } WAKEUP(); } @@ -393,7 +421,7 @@ INTERRUPT void rxIsr(void) { #pragma vector=TIMER1_A0_VECTOR #endif INTERRUPT void timerIsr(void) { - TA1CCR0 += clockTick; + // TA1CCR0 += clockTick; postEvent(TICK_HANDLER_ID); WAKEUP(); }