-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart.c
255 lines (245 loc) · 6.82 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* uart.c
*
* Created on: Mar 12, 2014
* Author: Michael
*/
#include "uart.h"
struct {
buffer_t * rx;
buffer_t * tx;
} uart[NUM_UARTS];
#if NUM_UARTS == 8
void UART0_ISR(void);
void UART1_ISR(void);
void UART2_ISR(void);
void UART3_ISR(void);
void UART4_ISR(void);
void UART5_ISR(void);
void UART6_ISR(void);
void UART7_ISR(void);
void UART_ISR(uint32_t base, int index);
void UART0_TxCallback(buffer_t * buf);
void UART1_TxCallback(buffer_t * buf);
void UART2_TxCallback(buffer_t * buf);
void UART3_TxCallback(buffer_t * buf);
void UART4_TxCallback(buffer_t * buf);
void UART5_TxCallback(buffer_t * buf);
void UART6_TxCallback(buffer_t * buf);
void UART7_TxCallback(buffer_t * buf);
#endif
void TxCallback(buffer_t * buf);
void UART_Init(uint32_t base, buffer_t * rx, buffer_t * tx, uint32_t baud) {
UART_InitHardware(base);
UART_Disable(base);
UART_SetBaud(base,baud);
UART_Configure(base);
#if NUM_UARTS == 8
switch(base) {
case UART0_BASE:
uart[0].rx = rx;
uart[0].tx = tx;
UARTIntRegister(UART0_BASE, UART0_ISR);
BufferSetCallback(tx, UART0_TxCallback);
break;
case UART1_BASE:
uart[1].rx = rx;
uart[1].tx = tx;
UARTIntRegister(UART1_BASE, UART1_ISR);
BufferSetCallback(tx, UART1_TxCallback);
break;
case UART2_BASE:
uart[2].rx = rx;
uart[2].tx = tx;
UARTIntRegister(UART2_BASE, UART2_ISR);
BufferSetCallback(tx, UART2_TxCallback);
break;
case UART3_BASE:
uart[3].rx = rx;
uart[3].tx = tx;
UARTIntRegister(UART3_BASE, UART3_ISR);
BufferSetCallback(tx, UART3_TxCallback);
break;
case UART4_BASE:
uart[4].rx = rx;
uart[4].tx = tx;
UARTIntRegister(UART4_BASE, UART4_ISR);
BufferSetCallback(tx, UART4_TxCallback);
break;
case UART5_BASE:
uart[5].rx = rx;
uart[5].tx = tx;
UARTIntRegister(UART5_BASE, UART5_ISR);
BufferSetCallback(tx, UART5_TxCallback);
break;
case UART6_BASE:
uart[6].rx = rx;
uart[6].tx = tx;
UARTIntRegister(UART6_BASE, UART6_ISR);
BufferSetCallback(tx, UART6_TxCallback);
break;
case UART7_BASE:
uart[7].rx = rx;
uart[7].tx = tx;
UARTIntRegister(UART7_BASE, UART7_ISR);
BufferSetCallback(tx, UART7_TxCallback);
break;
default:
break;
}
#else
uart[0].rx = rx;
uart[0].tx = tx;
BufferSetCallback(tx, TxCallback);
#endif
UART_EnableInterrupts(base);
}
void TxCallback(buffer_t * buf) {
// make sure the interrupt is enabled
SET_UART_TX_IE(0);
}
#ifdef UART_TX_ISR
UART_TX_ISR(void) {
while(UART_CAN_TRANSMIT(0))
{
if(GetSize(uart[0].tx)){
UART_TX_Char(0,Pop(uart[0].tx));
}
else {
CLEAR_UART_TX_IE(0);
break;
}
}
}
#endif
#ifdef UART_RX_ISR
UART_RX_ISR(void) {
while(UART_DATA_AVAILABLE(0)) {
Push(uart[0].rx,UART_RX_Char(0));
}
}
#endif
////////////////// uC Specific
#if NUM_UARTS == 8
void UART0_ISR(void) { UART_ISR(UART0_BASE, 0); }
void UART1_ISR(void) { UART_ISR(UART1_BASE, 1); }
void UART2_ISR(void) { UART_ISR(UART2_BASE, 2); }
void UART3_ISR(void) { UART_ISR(UART3_BASE, 3); }
void UART4_ISR(void) { UART_ISR(UART4_BASE, 4); }
void UART5_ISR(void) { UART_ISR(UART5_BASE, 5); }
void UART6_ISR(void) { UART_ISR(UART6_BASE, 6); }
void UART7_ISR(void) { UART_ISR(UART7_BASE, 7); }
void UART0_TxCallback(buffer_t * buf) {
if(UARTSpaceAvail(UART0_BASE)) {
if(GetSize(uart[0].tx)){
UART_TX_Char(UART0_BASE, Pop(uart[0].tx));
}
} else SET_UART_TX_IE(UART0_BASE);
}
void UART1_TxCallback(buffer_t * buf) {
if(UARTSpaceAvail(UART1_BASE)) {
if(GetSize(uart[1].tx)){
UART_TX_Char(UART1_BASE, Pop(uart[1].tx));
}
} else SET_UART_TX_IE(UART1_BASE);
}
/*
if(UARTIntStatus(UART1_BASE, 1) & UART_INT_TX == 0) {
if(UARTSpaceAvail(UART1_BASE)) {
if(GetSize(uart[1].tx)){
UART_TX_Char(UART1_BASE, Pop(uart[1].tx));
}
}
}
SET_UART_TX_IE(UART1_BASE);
}*/
void UART2_TxCallback(buffer_t * buf) { SET_UART_TX_IE(UART2_BASE); }
void UART3_TxCallback(buffer_t * buf) { SET_UART_TX_IE(UART3_BASE); }
void UART4_TxCallback(buffer_t * buf) { SET_UART_TX_IE(UART4_BASE); }
void UART5_TxCallback(buffer_t * buf) { SET_UART_TX_IE(UART5_BASE); }
void UART6_TxCallback(buffer_t * buf) { SET_UART_TX_IE(UART6_BASE); }
void UART7_TxCallback(buffer_t * buf) { SET_UART_TX_IE(UART7_BASE); }
void UART_ISR(uint32_t base, int index) {
while(UARTCharsAvail(base)) {
Push(uart[index].rx,UART_RX_Char(base));
}
if(UARTIntStatus(base, 1) & UART_INT_TX) {
while(UARTSpaceAvail(base)) {
if(GetSize(uart[index].tx)){
UART_TX_Char(base, Pop(uart[index].tx));
}
else {
UARTIntDisable(base, UART_INT_TX);
break;
}
}
}
}
#endif
#ifdef PART_TM4C123GH6PM
void UART_InitHardware(uint32_t base) {
switch(base) {
case UART0_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
break;
case UART1_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB0_U1RX);
GPIOPinConfigure(GPIO_PB1_U1TX);
GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
break;
case UART2_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinConfigure(GPIO_PD6_U2RX);
GPIOPinConfigure(GPIO_PD7_U2TX);
GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_6 | GPIO_PIN_7);
break;
case UART3_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
GPIOPinConfigure(GPIO_PC6_U3RX);
GPIOPinConfigure(GPIO_PC7_U3TX);
GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7);
break;
case UART4_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
GPIOPinConfigure(GPIO_PC4_U4RX);
GPIOPinConfigure(GPIO_PC5_U4TX);
GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
break;
case UART5_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART5);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinConfigure(GPIO_PE4_U5RX);
GPIOPinConfigure(GPIO_PE5_U5TX);
GPIOPinTypeUART(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);
break;
case UART6_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART6);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinConfigure(GPIO_PD4_U6RX);
GPIOPinConfigure(GPIO_PD5_U6TX);
GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);
break;
case UART7_BASE:
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART7);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinConfigure(GPIO_PE0_U7RX);
GPIOPinConfigure(GPIO_PE1_U7TX);
GPIOPinTypeUART(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1);
break;
default:
#ifdef __DEBUG__
while(1);
#endif
break;
}
}
#endif // PART_TM4C123GH6PM