2 * ============ Hardware Abstraction Layer for MSP-EXP430G2 LaunchPad ============
6 #include "Em_Message.h"
11 /* -------- INTERNAL FEATURES -------- */
13 #define GREEN_LED_CONFIG() (P1DIR |= BIT6)
14 #define GREEN_LED_ON() (P1OUT |= BIT6)
15 #define GREEN_LED_OFF() (P1OUT &= ~BIT6)
16 #define GREEN_LED_READ() (P1OUT & BIT6)
17 #define GREEN_LED_TOGGLE() (P1OUT ^= BIT6)
19 #define RED_LED_CONFIG() (P1DIR |= BIT0)
20 #define RED_LED_ON() (P1OUT |= BIT0)
21 #define RED_LED_OFF() (P1OUT &= ~BIT0)
22 #define RED_LED_READ() (P1OUT & BIT0)
23 #define RED_LED_TOGGLE() (P1OUT ^= BIT0)
25 #define GPIO_CONFIG(mask) (P1DIR &= ~mask, P1REN |= mask, P1OUT |= mask, P1IES |= mask);
26 #define GPIO_ENABLE(mask) (P1IFG &= ~mask, P1IE |= mask)
27 #define GPIO_DISABLE(mask) (P1IE &= ~mask, P1IFG &= ~mask)
28 #define GPIO_FIRED(mask) (P1IFG & mask)
29 #define GPIO_LOW(mask) (!(P1IN & mask))
30 #define GPIO_DEBOUNCE_MSECS 100
32 #define EAP_RX_BUF UCA0RXBUF
33 #define EAP_TX_BUF UCA0TXBUF
35 #define EAP_RX_VECTOR USCIAB0RX_VECTOR
36 #define EAP_TX_VECTOR USCIAB0TX_VECTOR
37 #define EAP_TX_ACK_VECTOR PORT2_VECTOR
39 #define EAP_RX_ENABLE() (P1SEL |= BIT1, P1SEL2 |= BIT1)
40 #define EAP_RX_DISABLE() (P1SEL &= ~BIT1, P1SEL2 &= ~BIT1)
41 #define EAP_TX_ENABLE() (P1SEL |= BIT2, P1SEL2 |= BIT2)
42 #define EAP_TX_DISABLE() (P1SEL &= ~BIT2, P1SEL2 &= ~BIT2)
44 #define EAP_RX_ACK_CONFIG() (P2DIR |= BIT0)
45 #define EAP_RX_ACK_SET() (P2OUT |= BIT0)
46 #define EAP_RX_ACK_CLR() (P2OUT &= ~BIT0)
48 #define EAP_TX_ACK_CONFIG() (P2DIR &= ~BIT1, P2IES |= BIT1, P2IFG &= ~BIT1, P2IE |= BIT1)
49 #define EAP_TX_ACK_TST() (P2IFG & BIT1)
50 #define EAP_TX_ACK_CLR() (P2IFG &= ~BIT1)
52 #define EAP_RX_INT_CLR() (IFG2 &= ~UCA0RXIFG)
53 #define EAP_RX_INT_ENABLE() (IE2 |= UCA0RXIE)
54 #define EAP_TX_INT_CLR() (IFG2 &= ~UCA0TXIFG)
55 #define EAP_TX_INT_DISABLE() (IE2 &= ~UCA0TXIE)
56 #define EAP_TX_INT_ENABLE() (IE2 |= UCA0TXIE)
58 #define MCLK_TICKS_PER_MS 1000L
59 #define ACLK_TICKS_PER_SECOND 1500L /* was 12000L with divider /1 */
60 #define UART_WATCHDOG_PERIOD (ACLK_TICKS_PER_SECOND * 250) / 1000
62 #define UART_WATCH_DISABLE() (TA1CCTL1 = 0) // Turn off CCR1 Interrupt
63 #define UART_WATCH_ENABLE() (TA1CCR1 = TA1R + UART_WATCHDOG_PERIOD, TA1CCTL1 = CCIE) // Set CCR1, and Enable CCR1 Interrupt
66 #define DINT() __disable_interrupt()
67 #define EINT() __enable_interrupt()
69 #define SLEEP() _BIS_SR(LPM3_bits + GIE)
70 #define WAKEUP() _BIC_SR_IRQ(LPM3_bits)
73 #ifdef __TI_COMPILER_VERSION__
74 #define DINT() (_disable_interrupt())
75 #define EINT() (_enable_interrupt())
76 #define INTERRUPT interrupt
77 #define SLEEP() (__bis_SR_register(LPM3_bits + GIE))
78 #define WAKEUP() (__bic_SR_register_on_exit(LPM3_bits))
81 #define NUM_HANDLERS 5
83 #define EVENT3_HANDLER_ID 0
84 #define EVENT4_HANDLER_ID 1
85 #define EVENT5_HANDLER_ID 2
86 #define TICK_HANDLER_ID 3
87 #define DISPATCH_HANDLER_ID 4
89 static void gpioHandler(uint8_t id);
90 static void postEvent(uint8_t handlerId);
92 static Hal_Handler appGpioHandler;
93 static volatile uint16_t handlerEvents = 0;
94 static uint16_t clockTick = 0;
95 static Hal_Handler handlerTab[NUM_HANDLERS];
96 static uint32_t gpioCount[3];
97 static bool timerActive[3] = {false, false, false};
98 static uint16_t timerPoint[3];
100 /* -------- INTERNAL FUNCTIONS -------- */
102 static void gpioHandler(uint8_t id) {
108 timerActive[id] = true;
110 timerPoint[id] = now + ACLK_TICKS_PER_SECOND; // One second ahead
111 left = ACLK_TICKS_PER_SECOND;
112 for (i = 0; i < 3; i++)
113 if (timerActive[i] && (timerPoint[i] - now) < left) {
114 left = timerPoint[i] - now;
116 TA1CCR0 = now + left;
120 static void tickHandler(uint16_t clock) {
123 for (i = 0; i < 3; i++)
124 if (timerActive[i] && timerPoint[i] == clock) {
125 uint32_t count = Hal_gpioCount(i);
128 ; // update timer; call jitter handler
130 ; // clear timer; call app gpio handler
133 // if all timers are unset, disable ticker.
136 static void postEvent(uint8_t handlerId) {
137 uint8_t key = Em_Hal_lock();
138 handlerEvents |= 1 << handlerId;
142 /* -------- APP-HAL INTERFACE -------- */
144 void Hal_gpioEnable(Hal_Handler handler) {
148 for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1) {
149 handlerTab[id] = gpioHandler;
150 appGpioHandler = handler;
151 (P1DIR &= ~mask, P1REN |= mask, P1OUT |= mask, P1IES |= mask);
153 (P1IFG &= ~mask, P1IE |= mask);
155 handlerTab[TICK_HANDLER_ID] = tickHandler;
158 void Hal_connected(void) {
161 void Hal_delay(uint16_t msecs) {
163 __delay_cycles(MCLK_TICKS_PER_MS);
167 void Hal_disconnected(void) {
170 void Hal_init(void) {
174 WDTCTL = WDTPW + WDTHOLD;
176 /* MCLK divider = /1 */
177 /* SMCLK divider = /1 */
178 BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0;
179 if (CALBC1_1MHZ != 0xFF) {
181 BCSCTL1 = CALBC1_1MHZ; /* Set DCO to 1MHz */
182 DCOCTL = CALDCO_1MHZ;
184 /* XT2 is off (Not used for MCLK/SMCLK) */
185 /* ACLK divider = /8 */
186 BCSCTL1 |= XT2OFF + DIVA_3;
187 /* XT2 range = 0.4 - 1 MHz */
188 /* LFXT1 range/VLO = VLOCLK (or 3-16 MHz if XTS=1) */
190 BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1;
200 TA1CTL = TASSEL_1 + MC_2; // ACLK, Continuous mode
201 UART_WATCH_DISABLE();
218 UCA0CTL1 = UCSSEL_2 + UCSWRST;
219 UCA0MCTL = UCBRF_0 + UCBRS_6;
221 UCA0CTL1 &= ~UCSWRST;
223 handlerTab[DISPATCH_HANDLER_ID] = Em_Message_dispatch;
226 void Hal_idleLoop(void) {
231 // atomically read/clear all handlerEvents
233 uint16_t events = handlerEvents;
236 if (events) { // dispatch all current events
240 for (id = 0, mask = 0x1; id < NUM_HANDLERS; id++, mask <<= 1) {
241 if ((events & mask) && handlerTab[id]) {
242 if (id == TICK_HANDLER_ID)
243 handlerTab[id](TA1R);
249 else { // await more events
250 SLEEP(); // this also enables interrupts
255 void Hal_greenLedOn(void) {
259 void Hal_greenLedOff(void) {
263 bool Hal_greenLedRead(void) {
264 return GREEN_LED_READ();
267 void Hal_greenLedToggle(void) {
271 void Hal_redLedOn(void) {
275 void Hal_redLedOff(void) {
279 bool Hal_redLedRead(void) {
280 return RED_LED_READ();
283 void Hal_redLedToggle(void) {
287 uint16_t Hal_tickStart(uint16_t msecs, void (*handler)(uint16_t clock)) {
288 handlerTab[TICK_HANDLER_ID] = handler;
289 uint16_t clockTick = (ACLK_TICKS_PER_SECOND * msecs) / 1000;
290 uint16_t then = TA1R + clockTick;
291 TA1CCR0 = then; // Set the CCR0 interrupt for msecs from now.
292 TA1CCTL0 = CCIE; // Enable the CCR0 interrupt
296 void Hal_tickStop(void) {
297 handlerTab[TICK_HANDLER_ID] = 0;
302 uint32_t Hal_gpioCount(uint8_t id) {
304 uint32_t count = gpioCount[id];
310 /* -------- SRT-HAL INTERFACE -------- */
312 uint8_t Em_Hal_lock(void) {
313 uint8_t key = _get_interrupt_state();
315 __disable_interrupt();
317 #ifdef __TI_COMPILER_VERSION__
318 _disable_interrupt();
323 void Em_Hal_reset(void) {
324 uint8_t key = Em_Hal_lock();
325 EAP_RX_ACK_CLR(); // suspend the MCM
327 EAP_RX_ACK_SET(); // reset the MCM
336 void Em_Hal_startSend() {
337 EAP_TX_BUF = Em_Message_startTx();
340 void Em_Hal_unlock(uint8_t key) {
341 _set_interrupt_state(key);
344 void Em_Hal_watchOff(void) {
345 UART_WATCH_DISABLE();
348 void Em_Hal_watchOn(void) {
352 /* -------- INTERRUPT SERVICE ROUTINES -------- */
355 __attribute__((interrupt(PORT1_VECTOR)))
357 #ifdef __TI_COMPILER_VERSION__
358 #pragma vector=PORT1_VECTOR
360 INTERRUPT void gpioIsr(void) {
364 for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1)
365 if (GPIO_FIRED(mask)) {
373 __attribute__((interrupt(EAP_RX_VECTOR)))
375 #ifdef __TI_COMPILER_VERSION__
376 #pragma vector=EAP_RX_VECTOR
378 INTERRUPT void rxIsr(void) {
379 uint8_t b = EAP_RX_BUF;
380 Em_Message_startRx();
383 if (Em_Message_addByte(b)) {
384 postEvent(DISPATCH_HANDLER_ID);
390 __attribute__((interrupt(TIMER1_A0_VECTOR)))
392 #ifdef __TI_COMPILER_VERSION__
393 #pragma vector=TIMER1_A0_VECTOR
395 INTERRUPT void timerIsr(void) {
396 TA1CCR0 += clockTick;
397 postEvent(TICK_HANDLER_ID);
402 __attribute__((interrupt(EAP_TX_ACK_VECTOR)))
404 #ifdef __TI_COMPILER_VERSION__
405 #pragma vector=EAP_TX_ACK_VECTOR
407 INTERRUPT void txAckIsr(void) {
408 if (EAP_TX_ACK_TST()) {
410 if (Em_Message_getByte(&b)) {
419 __attribute__((interrupt(TIMER1_A1_VECTOR)))
421 #ifdef __TI_COMPILER_VERSION__
422 #pragma vector=TIMER1_A1_VECTOR
424 INTERRUPT void uartWatchdogIsr(void) {
427 UART_WATCH_DISABLE();
428 Em_Message_restart();