]> git.karo-electronics.de Git - oswald.git/blob - metawatch/mw_uart.c
79b650e7f73ca46373c476c541f4a3f1b2011850
[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         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                         break;
28                 case 4: // TXIFG
29                         break;
30                 default:
31                         break;
32         }
33 }
34
35 void init_debug_uart(void)
36 {
37         /* assert reset */
38         UCA3CTL1 = UCSWRST;
39
40         /* reset default SMCLK = 1.048MHz */
41         UCA3CTL1 |= UCSSEL__SMCLK;
42         /* CLK        baud   BRx  BRSx  BRFx */
43         /* 1,048,576  115200 9    1     0 */
44         /* 16,000,000 115200 138  7     0 */
45
46         UCA3BR0 = 138;
47         UCA3MCTL = UCBRS_7 | UCBRF_0;
48
49         /* set P10.4 & P10.5 to UCA function */
50         P10SEL |= BIT4;
51         P10SEL |= BIT5;
52
53         UCA3STAT = 0;
54
55         /* deassert reset */
56         UCA3CTL1 &= ~UCSWRST;
57
58         /* enable receive interrupt */
59         UCA3IE = UCRXIE;
60         /* clear interrup flags */
61         UCA3IFG = 0;
62 }
63
64 void debug_uart_tx_char(const char c)
65 {
66         while (UCA3STAT & UCBUSY)
67                 nop();
68         UCA3TXBUF = c;
69         while ((UCA3IFG & UCTXIFG) == 0 )
70                 nop();
71 }
72
73 void debug_uart_tx(const char *buf)
74 {
75         unsigned char i = 0;
76
77         while (buf[i] != 0) {
78                 debug_uart_tx_char(buf[i]);
79                 if (buf[i++] == '\n')
80                         debug_uart_tx_char('\r');
81         }
82         while (UCA3STAT & UCBUSY)
83                 nop();
84 }
85
86 void debug_dump_hex(const uint16_t len, const void *buf)
87 {
88         int i;
89         char tstr[8];
90
91         for (i=0; i<len; i++) {
92                 snprintf(tstr, 8, "0x%02x ", *(uint8_t *)(buf+i));
93                 debug_uart_tx(tstr);
94         }
95         debug_uart_tx("\n");
96 }
97
98 void debug_dump_ascii(const uint16_t len, const void *buf)
99 {
100         int i;
101
102         for (i=0; i<len; i++) {
103                 debug_uart_tx_char(isprint(*(uint8_t *)(buf+i)) ? *(uint8_t *)(buf+i) : '.');
104         }
105         debug_uart_tx("\n");
106 }
107
108 char debug_uart_rx_char(char *c)
109 {
110         if (UART_RX_CHAR != 0) {
111                 *c = UART_RX_CHAR;
112                 UART_RX_CHAR = 0;
113                 return 1;
114         } else {
115                 *c = 0;
116                 return 0;
117         }
118 }
119 #endif
120