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