]> git.karo-electronics.de Git - oswald.git/blob - metawatch/mw_uart.c
Add option for debug UART on digital watch
[oswald.git] / metawatch / mw_uart.c
1 #include <msp430.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <ctype.h>
5
6 #include "mw_main.h"
7
8 #include "mw_uart.h"
9
10 static char UART_RX_CHAR = 0;
11
12 #if defined MW_DEVBOARD_V2 || MW_DEBUG_UART
13
14 void debug_uart_tx_char(char c);
15
16 #pragma vector=USCI_A3_VECTOR
17 __interrupt void UCA_ISR (void)
18 {
19         switch (UCA3IV) {
20                 case 2: // RXIFG
21                         /* clear IRQ flag */
22                         UCA3IFG  &= ~UCRXIFG;
23                         UART_RX_CHAR = UCA3RXBUF;
24                         _event_src |= DBG_UART_RCV_EVENT;
25                         /* wake up to handle the received char */
26                         // LPM3_EXIT;
27                         LPM3_EXIT_ISR();
28                         break;
29                 case 4: // TXIFG
30                         break;
31                 default:
32                         break;
33         }
34 }
35
36 void init_debug_uart(void)
37 {
38         /* assert reset */
39         UCA3CTL1 = UCSWRST;
40
41         /* reset default SMCLK = 1.048MHz */
42         UCA3CTL1 |= UCSSEL__SMCLK;
43         /* CLK        baud   BRx  BRSx  BRFx */
44         /* 1,048,576  115200 9    1     0 */
45         /* 16,000,000 115200 138  7     0 */
46
47         UCA3BR0 = 138;
48         UCA3MCTL = UCBRS_7 | UCBRF_0;
49
50         /* set P10.4 & P10.5 to UCA function */
51         P10SEL |= BIT4;
52         P10SEL |= BIT5;
53
54         UCA3STAT = 0;
55
56         /* deassert reset */
57         UCA3CTL1 &= ~UCSWRST;
58
59         /* enable receive interrupt */
60         UCA3IE = UCRXIE;
61         /* clear interrup flags */
62         UCA3IFG = 0;
63 }
64
65 void debug_uart_tx_char(const char c)
66 {
67         while (UCA3STAT & UCBUSY)
68                 nop();
69         UCA3TXBUF = c;
70         while ((UCA3IFG & UCTXIFG) == 0 )
71                 nop();
72 }
73
74 void debug_uart_tx(const char *buf)
75 {
76         unsigned char i = 0;
77
78         while (buf[i] != 0) {
79                 debug_uart_tx_char(buf[i]);
80                 if (buf[i++] == '\n')
81                         debug_uart_tx_char('\r');
82         }
83         while (UCA3STAT & UCBUSY)
84                 nop();
85 }
86
87 void debug_dump_hex(const uint16_t len, const void *buf)
88 {
89         int i;
90         char tstr[8];
91
92         for (i=0; i<len; i++) {
93                 snprintf(tstr, 8, "0x%02x ", *(uint8_t *)(buf+i));
94                 debug_uart_tx(tstr);
95         }
96         debug_uart_tx("\n");
97 }
98
99 void debug_dump_ascii(const uint16_t len, const void *buf)
100 {
101         int i;
102
103         for (i=0; i<len; i++) {
104                 debug_uart_tx_char(isprint(*(uint8_t *)(buf+i)) ? *(uint8_t *)(buf+i) : '.');
105         }
106         debug_uart_tx("\n");
107 }
108
109 char debug_uart_rx_char(char *c)
110 {
111         if (UART_RX_CHAR != 0) {
112                 *c = UART_RX_CHAR;
113                 UART_RX_CHAR = 0;
114                 return 1;
115         } else {
116                 *c = 0;
117                 return 0;
118         }
119 }
120 #endif
121