X-Git-Url: http://www.average.org/gitweb/?p=pulsecounter.git;a=blobdiff_plain;f=Hal%2FHal.c;h=0cdcc297a85b19169fad377c4cf5a2cb2575ed75;hp=b4944e2c79090a97dcba17ea049e04377d85a231;hb=693bc78d94d8321baf1744ab7f4c2f695a1b7377;hpb=4868ec0934cb78bf2349296ec33401cf9ce4a953 diff --git a/Hal/Hal.c b/Hal/Hal.c index b4944e2..0cdcc29 100644 --- a/Hal/Hal.c +++ b/Hal/Hal.c @@ -22,12 +22,12 @@ #define RED_LED_READ() (P1OUT & BIT0) #define RED_LED_TOGGLE() (P1OUT ^= BIT0) -#define BUTTON_CONFIG() (P1DIR &= ~BIT3, P1REN |= BIT3, P1OUT |= BIT3, P1IES |= BIT3); -#define BUTTON_ENABLE() (P1IFG &= ~BIT3, P1IE |= BIT3) -#define BUTTON_DISABLE() (P1IE &= ~BIT3, P1IFG &= ~BIT3) -#define BUTTON_FIRED() (P1IFG & BIT3) -#define BUTTON_PRESSED() (!(P1IN & BIT3)) -#define BUTTON_DEBOUNCE_MSECS 100 +#define GPIO_CONFIG(mask) (P1DIR &= ~mask, P1REN |= mask, P1OUT |= mask, P1IES |= mask); +#define GPIO_ENABLE(mask) (P1IFG &= ~mask, P1IE |= mask) +#define GPIO_DISABLE(mask) (P1IE &= ~mask, P1IFG &= ~mask) +#define GPIO_FIRED(mask) (P1IFG & mask) +#define GPIO_LOW(mask) (!(P1IN & mask)) +#define GPIO_DEBOUNCE_MSECS 100 #define DEBUG1_CONFIG() (P2DIR |= BIT3) #define DEBUG1_ON() (P2OUT |= BIT3) @@ -64,7 +64,7 @@ #define EAP_TX_INT_ENABLE() (IE2 |= UCA0TXIE) #define MCLK_TICKS_PER_MS 1000L -#define ACLK_TICKS_PER_SECOND 12000L +#define ACLK_TICKS_PER_SECOND 1500L /* was 12000L with divider /1 */ #define UART_WATCHDOG_PERIOD (ACLK_TICKS_PER_SECOND * 250) / 1000 #define UART_WATCH_DISABLE() (TA1CCTL1 = 0) // Turn off CCR1 Interrupt @@ -88,16 +88,16 @@ #define NUM_HANDLERS 5 -#define BUTTON_HANDLER_ID 0 +#define EVENT3_HANDLER_ID 0 #define EVENT4_HANDLER_ID 1 #define EVENT5_HANDLER_ID 2 #define TICK_HANDLER_ID 3 #define DISPATCH_HANDLER_ID 4 -static void buttonHandler(uint8_t id); +static void gpioHandler(uint8_t id); static void postEvent(uint8_t handlerId); -static void (*appButtonHandler)(void); +static Hal_Handler appGpioHandler; static volatile uint16_t handlerEvents = 0; static uint16_t clockTick = 0; static Hal_Handler handlerTab[NUM_HANDLERS]; @@ -105,12 +105,17 @@ static Hal_Handler handlerTab[NUM_HANDLERS]; /* -------- APP-HAL INTERFACE -------- */ -void Hal_buttonEnable(void (*handler)(void)) { - handlerTab[BUTTON_HANDLER_ID] = buttonHandler; - appButtonHandler = handler; - BUTTON_CONFIG(); - Hal_delay(100); - BUTTON_ENABLE(); +void Hal_gpioEnable(Hal_Handler handler) { + uint8_t id; + uint16_t mask; + + for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1) { + handlerTab[id] = gpioHandler; + appGpioHandler = handler; + (P1DIR &= ~mask, P1REN |= mask, P1OUT |= mask, P1IES |= mask); + Hal_delay(100); + (P1IFG &= ~mask, P1IE |= mask); + } } void Hal_connected(void) { @@ -162,13 +167,21 @@ void Hal_init(void) { /* setup clocks */ WDTCTL = WDTPW + WDTHOLD; + /* MCLK = DCOCLK */ + /* MCLK divider = /1 */ + /* SMCLK divider = /1 */ BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0; if (CALBC1_1MHZ != 0xFF) { DCOCTL = 0x00; BCSCTL1 = CALBC1_1MHZ; /* Set DCO to 1MHz */ DCOCTL = CALDCO_1MHZ; } - BCSCTL1 |= XT2OFF + DIVA_0; + /* XT2 is off (Not used for MCLK/SMCLK) */ + /* ACLK divider = /8 */ + BCSCTL1 |= XT2OFF + DIVA_3; + /* XT2 range = 0.4 - 1 MHz */ + /* LFXT1 range/VLO = VLOCLK (or 3-16 MHz if XTS=1) */ + /* Capacitor 6 pF */ BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1; /* setup LEDs */ @@ -328,12 +341,13 @@ void Em_Hal_watchOn(void) { /* -------- INTERNAL FUNCTIONS -------- */ -static void buttonHandler(uint8_t id) { - Hal_delay(BUTTON_DEBOUNCE_MSECS); - if (BUTTON_PRESSED() && appButtonHandler) { - appButtonHandler(); - } - BUTTON_ENABLE(); +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) { @@ -350,10 +364,15 @@ static void postEvent(uint8_t handlerId) { #ifdef __TI_COMPILER_VERSION__ #pragma vector=PORT1_VECTOR #endif -INTERRUPT void buttonIsr(void) { - if (BUTTON_FIRED()) - postEvent(BUTTON_HANDLER_ID); - BUTTON_DISABLE(); +INTERRUPT void gpioIsr(void) { + uint8_t id; + uint16_t mask; + + for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1) + if (GPIO_FIRED(mask)) { + postEvent(id); + GPIO_DISABLE(mask); + } WAKEUP(); }