-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
121 lines (108 loc) · 3.3 KB
/
uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* UART control for the TAP (Technology Access Platform)
*
* Copyright (C) 2016 TheLab.ms
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "main.h"
/* Y UART interrupt service routine */
#if defined(__TI_COMPILER_VERSION__) || (__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
#elif defined(__GNUC__) && (__MSP430__)
void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void)
#else
#error Compiler not found!
#endif
{
unsigned char ch;
switch (__even_in_range(UCA0IV, 4))
{
case 0: // Vector 0 - no interrupt
break;
case 2: // Vector 2 - RXIFG
put_byte_input(&serial_y, UCA0RXBUF);
break;
case 4: // Vector 4 - TXIFG
// Transmit data
if (get_byte_output(&y_out, &ch))
UCA0IE &= ~UCTXIE; // Disable interrupt
UCA0TXBUF = ch;
break;
default:
break;
}
}
/* X UART interrupt service routine */
#if defined(__TI_COMPILER_VERSION__) || (__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
#elif defined(__GNUC__) && (__MSP430__)
void __attribute__ ((interrupt(USCI_A1_VECTOR))) USCI_A1_ISR (void)
#else
#error Compiler not found!
#endif
{
unsigned char ch = 0;
switch (__even_in_range(UCA1IV, 4))
{
case 0: // Vector 0 - no interrupt
break;
case 2: // Vector 2 - RXIFG
put_byte_input(&serial_x, UCA1RXBUF);
break;
case 4: // Vector 4 - TXIFG
// Transmit data
if (get_byte_output(&x_out, &ch)) // no more bytes
UCA1IE &= ~UCTXIE; // Disable interrupt
UCA1TXBUF = ch;
break;
default:
break;
}
}
/* Enable serial transmit interrupt if necessary */
void tickle_serial_interrupt(channel_t *c)
{
if (c == &x_out)
{
// Enable interrupt
UCA1IE |= UCTXIE;
}
else if (c == &y_out)
{
// Enable interrupt
UCA0IE |= UCTXIE;
}
}
void UART_init()
{
P3SEL = (BIT3 + BIT4); // P3.3,4 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL__SMCLK; // Select SMCLK as our clock
UCA0BR0 = 217; // 25MHz 115200 baud (see User's Guide)
UCA0BR1 = 0; // 25MHz 115200 baud (see User's Guide)
UCA0MCTL = UCBRF_0; // Modln UCBRSx=0, UCBRFx=0
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
P4SEL = (BIT4 + BIT5); // P4.4,5 = USCI_A1 TXD/RXD
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL__SMCLK; // SMCLK
UCA1BR0 = 217; // 25MHz 115200 baud (see User's Guide)
UCA1BR1 = 0; // 25MHz 115200 baud (see User's Guide)
UCA0MCTL = UCBRF_0; // Modln UCBRSx=0, UCBRFx=9, over sampling
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA1IE |= UCRXIE; // Enable USCI_A1 RX interrupt
flush_channel(&serial_x);
flush_channel(&serial_y);
flush_channel(&x_out);
flush_channel(&y_out);
}