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 DEBUG1_CONFIG() (P2DIR |= BIT3)
33 #define DEBUG1_ON() (P2OUT |= BIT3)
34 #define DEBUG1_OFF() (P2OUT &= ~BIT3)
36 #define DEBUG2_CONFIG() (P2DIR |= BIT4)
37 #define DEBUG2_ON() (P2OUT |= BIT4)
38 #define DEBUG2_OFF() (P2OUT &= ~BIT4)
40 #define EAP_RX_BUF UCA0RXBUF
41 #define EAP_TX_BUF UCA0TXBUF
43 #define EAP_RX_VECTOR USCIAB0RX_VECTOR
44 #define EAP_TX_VECTOR USCIAB0TX_VECTOR
45 #define EAP_TX_ACK_VECTOR PORT2_VECTOR
47 #define EAP_RX_ENABLE() (P1SEL |= BIT1, P1SEL2 |= BIT1)
48 #define EAP_RX_DISABLE() (P1SEL &= ~BIT1, P1SEL2 &= ~BIT1)
49 #define EAP_TX_ENABLE() (P1SEL |= BIT2, P1SEL2 |= BIT2)
50 #define EAP_TX_DISABLE() (P1SEL &= ~BIT2, P1SEL2 &= ~BIT2)
52 #define EAP_RX_ACK_CONFIG() (P2DIR |= BIT0)
53 #define EAP_RX_ACK_SET() (P2OUT |= BIT0)
54 #define EAP_RX_ACK_CLR() (P2OUT &= ~BIT0)
56 #define EAP_TX_ACK_CONFIG() (P2DIR &= ~BIT1, P2IES |= BIT1, P2IFG &= ~BIT1, P2IE |= BIT1)
57 #define EAP_TX_ACK_TST() (P2IFG & BIT1)
58 #define EAP_TX_ACK_CLR() (P2IFG &= ~BIT1)
60 #define EAP_RX_INT_CLR() (IFG2 &= ~UCA0RXIFG)
61 #define EAP_RX_INT_ENABLE() (IE2 |= UCA0RXIE)
62 #define EAP_TX_INT_CLR() (IFG2 &= ~UCA0TXIFG)
63 #define EAP_TX_INT_DISABLE() (IE2 &= ~UCA0TXIE)
64 #define EAP_TX_INT_ENABLE() (IE2 |= UCA0TXIE)
66 #define MCLK_TICKS_PER_MS 1000L
67 #define ACLK_TICKS_PER_SECOND 1500L /* was 12000L with divider /1 */
68 #define UART_WATCHDOG_PERIOD (ACLK_TICKS_PER_SECOND * 250) / 1000
70 #define UART_WATCH_DISABLE() (TA1CCTL1 = 0) // Turn off CCR1 Interrupt
71 #define UART_WATCH_ENABLE() (TA1CCR1 = TA1R + UART_WATCHDOG_PERIOD, TA1CCTL1 = CCIE) // Set CCR1, and Enable CCR1 Interrupt
74 #define DINT() __disable_interrupt()
75 #define EINT() __enable_interrupt()
77 #define SLEEP() _BIS_SR(LPM3_bits + GIE)
78 #define WAKEUP() _BIC_SR_IRQ(LPM3_bits)
81 #ifdef __TI_COMPILER_VERSION__
82 #define DINT() (_disable_interrupt())
83 #define EINT() (_enable_interrupt())
84 #define INTERRUPT interrupt
85 #define SLEEP() (__bis_SR_register(LPM3_bits + GIE))
86 #define WAKEUP() (__bic_SR_register_on_exit(LPM3_bits))
89 #define NUM_HANDLERS 5
91 #define EVENT3_HANDLER_ID 0
92 #define EVENT4_HANDLER_ID 1
93 #define EVENT5_HANDLER_ID 2
94 #define TICK_HANDLER_ID 3
95 #define DISPATCH_HANDLER_ID 4
97 static void gpioHandler(uint8_t id);
98 static void postEvent(uint8_t handlerId);
100 static Hal_Handler appGpioHandler;
101 static volatile uint16_t handlerEvents = 0;
102 static uint16_t clockTick = 0;
103 static Hal_Handler handlerTab[NUM_HANDLERS];
106 /* -------- APP-HAL INTERFACE -------- */
108 void Hal_gpioEnable(Hal_Handler handler) {
112 for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1) {
113 handlerTab[id] = gpioHandler;
114 appGpioHandler = handler;
115 (P1DIR &= ~mask, P1REN |= mask, P1OUT |= mask, P1IES |= mask);
117 (P1IFG &= ~mask, P1IE |= mask);
121 void Hal_connected(void) {
124 void Hal_debugOn(uint8_t line) {
134 void Hal_debugOff(uint8_t line) {
144 void Hal_debugPulse(uint8_t line) {
156 void Hal_delay(uint16_t msecs) {
158 __delay_cycles(MCLK_TICKS_PER_MS);
162 void Hal_disconnected(void) {
165 void Hal_init(void) {
169 WDTCTL = WDTPW + WDTHOLD;
171 /* MCLK divider = /1 */
172 /* SMCLK divider = /1 */
173 BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0;
174 if (CALBC1_1MHZ != 0xFF) {
176 BCSCTL1 = CALBC1_1MHZ; /* Set DCO to 1MHz */
177 DCOCTL = CALDCO_1MHZ;
179 /* XT2 is off (Not used for MCLK/SMCLK) */
180 /* ACLK divider = /8 */
181 BCSCTL1 |= XT2OFF + DIVA_3;
182 /* XT2 range = 0.4 - 1 MHz */
183 /* LFXT1 range/VLO = VLOCLK (or 3-16 MHz if XTS=1) */
185 BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1;
194 /* setup debug pins */
196 DEBUG1_CONFIG(); DEBUG1_OFF();
197 DEBUG2_CONFIG(); DEBUG2_OFF();
199 DEBUG1_ON(); DEBUG1_OFF();
202 TA1CTL = TASSEL_1 + MC_2; // ACLK, Continuous mode
203 UART_WATCH_DISABLE();
220 UCA0CTL1 = UCSSEL_2 + UCSWRST;
221 UCA0MCTL = UCBRF_0 + UCBRS_6;
223 UCA0CTL1 &= ~UCSWRST;
225 handlerTab[DISPATCH_HANDLER_ID] = Em_Message_dispatch;
228 void Hal_idleLoop(void) {
233 // atomically read/clear all handlerEvents
235 uint16_t events = handlerEvents;
238 if (events) { // dispatch all current events
242 for (id = 0, mask = 0x1; id < NUM_HANDLERS; id++, mask <<= 1) {
243 if ((events & mask) && handlerTab[id]) {
248 else { // await more events
254 void Hal_greenLedOn(void) {
258 void Hal_greenLedOff(void) {
262 bool Hal_greenLedRead(void) {
263 return GREEN_LED_READ();
266 void Hal_greenLedToggle(void) {
270 void Hal_redLedOn(void) {
274 void Hal_redLedOff(void) {
278 bool Hal_redLedRead(void) {
279 return RED_LED_READ();
282 void Hal_redLedToggle(void) {
286 void Hal_tickStart(uint16_t msecs, Hal_Handler handler) {
287 handlerTab[TICK_HANDLER_ID] = handler;
288 clockTick = (ACLK_TICKS_PER_SECOND * msecs) / 1000;
289 TA1CCR0 = TA1R + clockTick; // Set the CCR0 interrupt for msecs from now.
290 TA1CCTL0 = CCIE; // Enable the CCR0 interrupt
293 void Hal_tickStop(void) {
294 handlerTab[TICK_HANDLER_ID] = 0;
299 /* -------- SRT-HAL INTERFACE -------- */
301 uint8_t Em_Hal_lock(void) {
302 uint8_t key = _get_interrupt_state();
304 __disable_interrupt();
306 #ifdef __TI_COMPILER_VERSION__
307 _disable_interrupt();
312 void Em_Hal_reset(void) {
313 uint8_t key = Em_Hal_lock();
314 EAP_RX_ACK_CLR(); // suspend the MCM
316 EAP_RX_ACK_SET(); // reset the MCM
325 void Em_Hal_startSend() {
326 EAP_TX_BUF = Em_Message_startTx();
329 void Em_Hal_unlock(uint8_t key) {
330 _set_interrupt_state(key);
333 void Em_Hal_watchOff(void) {
334 UART_WATCH_DISABLE();
337 void Em_Hal_watchOn(void) {
342 /* -------- INTERNAL FUNCTIONS -------- */
344 static void gpioHandler(uint8_t id) {
345 uint16_t mask = BIT3 << id;
347 Hal_delay(GPIO_DEBOUNCE_MSECS);
348 if (GPIO_LOW(mask) && appGpioHandler)
353 static void postEvent(uint8_t handlerId) {
354 uint8_t key = Em_Hal_lock();
355 handlerEvents |= 1 << handlerId;
359 /* -------- INTERRUPT SERVICE ROUTINES -------- */
362 __attribute__((interrupt(PORT1_VECTOR)))
364 #ifdef __TI_COMPILER_VERSION__
365 #pragma vector=PORT1_VECTOR
367 INTERRUPT void gpioIsr(void) {
371 for (id = 0, mask = BIT3; id < 3; id++, mask <<= 1)
372 if (GPIO_FIRED(mask)) {
380 __attribute__((interrupt(EAP_RX_VECTOR)))
382 #ifdef __TI_COMPILER_VERSION__
383 #pragma vector=EAP_RX_VECTOR
385 INTERRUPT void rxIsr(void) {
386 uint8_t b = EAP_RX_BUF;
387 Em_Message_startRx();
390 if (Em_Message_addByte(b)) {
391 postEvent(DISPATCH_HANDLER_ID);
397 __attribute__((interrupt(TIMER1_A0_VECTOR)))
399 #ifdef __TI_COMPILER_VERSION__
400 #pragma vector=TIMER1_A0_VECTOR
402 INTERRUPT void timerIsr(void) {
403 TA1CCR0 += clockTick;
404 postEvent(TICK_HANDLER_ID);
409 __attribute__((interrupt(EAP_TX_ACK_VECTOR)))
411 #ifdef __TI_COMPILER_VERSION__
412 #pragma vector=EAP_TX_ACK_VECTOR
414 INTERRUPT void txAckIsr(void) {
415 if (EAP_TX_ACK_TST()) {
417 if (Em_Message_getByte(&b)) {
426 __attribute__((interrupt(TIMER1_A1_VECTOR)))
428 #ifdef __TI_COMPILER_VERSION__
429 #pragma vector=TIMER1_A1_VECTOR
431 INTERRUPT void uartWatchdogIsr(void) {
434 UART_WATCH_DISABLE();
435 Em_Message_restart();