-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuart.c
296 lines (258 loc) · 9.36 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/******************************************************************************
* Filename: uart.c
* Revised: 2017-06-05 12:13:49 +0200 (Mon, 05 Jun 2017)
* Revision: 49096
*
* Description: Driver for the UART.
*
* Copyright (c) 2015 - 2017, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3) Neither the name of the ORGANIZATION nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#include "uart.h"
//*****************************************************************************
//
// Handle support for DriverLib in ROM:
// This section will undo prototype renaming made in the header file
//
//*****************************************************************************
#if !defined(DOXYGEN)
#undef UARTFIFOLevelGet
#define UARTFIFOLevelGet NOROM_UARTFIFOLevelGet
#undef UARTConfigSetExpClk
#define UARTConfigSetExpClk NOROM_UARTConfigSetExpClk
#undef UARTConfigGetExpClk
#define UARTConfigGetExpClk NOROM_UARTConfigGetExpClk
#undef UARTDisable
#define UARTDisable NOROM_UARTDisable
#undef UARTCharGetNonBlocking
#define UARTCharGetNonBlocking NOROM_UARTCharGetNonBlocking
#undef UARTCharGet
#define UARTCharGet NOROM_UARTCharGet
#undef UARTCharPutNonBlocking
#define UARTCharPutNonBlocking NOROM_UARTCharPutNonBlocking
#undef UARTCharPut
#define UARTCharPut NOROM_UARTCharPut
#undef UARTIntRegister
#define UARTIntRegister NOROM_UARTIntRegister
#undef UARTIntUnregister
#define UARTIntUnregister NOROM_UARTIntUnregister
#endif
//*****************************************************************************
//
// Gets the FIFO level at which interrupts are generated
//
//*****************************************************************************
void
UARTFIFOLevelGet(uint32_t ui32Base, uint32_t *pui32TxLevel,
uint32_t *pui32RxLevel)
{
uint32_t ui32Temp;
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// Read the FIFO level register.
ui32Temp = HWREG(ui32Base + UART_O_IFLS);
// Extract the transmit and receive FIFO levels.
*pui32TxLevel = ui32Temp & UART_IFLS_TXSEL_M;
*pui32RxLevel = ui32Temp & UART_IFLS_RXSEL_M;
}
//*****************************************************************************
//
// Sets the configuration of a UART
//
//*****************************************************************************
void
UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
uint32_t ui32Baud, uint32_t ui32Config)
{
uint32_t ui32Div;
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
ASSERT(ui32Baud != 0);
// Stop the UART.
UARTDisable(ui32Base);
// Compute the fractional baud rate divider.
ui32Div = (((ui32UARTClk * 8) / ui32Baud) + 1) / 2;
// Set the baud rate.
HWREG(ui32Base + UART_O_IBRD) = ui32Div / 64;
HWREG(ui32Base + UART_O_FBRD) = ui32Div % 64;
// Set parity, data length, and number of stop bits.
HWREG(ui32Base + UART_O_LCRH) = ui32Config;
}
//*****************************************************************************
//
// Gets the current configuration of a UART
//
//*****************************************************************************
void
UARTConfigGetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
uint32_t *pui32Baud, uint32_t *pui32Config)
{
uint32_t ui32Int, ui32Frac;
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// Compute the baud rate.
ui32Int = HWREG(ui32Base + UART_O_IBRD);
ui32Frac = HWREG(ui32Base + UART_O_FBRD);
*pui32Baud = (ui32UARTClk * 4) / ((64 * ui32Int) + ui32Frac);
// Get the parity, data length, and number of stop bits.
*pui32Config = (HWREG(ui32Base + UART_O_LCRH) &
(UART_LCRH_SPS | UART_LCRH_WLEN_M | UART_LCRH_STP2 |
UART_LCRH_EPS | UART_LCRH_PEN));
}
//*****************************************************************************
//
// Disables transmitting and receiving
//
//*****************************************************************************
void
UARTDisable(uint32_t ui32Base)
{
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// Wait for end of TX.
while(HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY)
{
}
// Disable the FIFO.
HWREG(ui32Base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
// Disable the UART.
HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_UARTEN | UART_CTL_TXE |
UART_CTL_RXE);
}
//*****************************************************************************
//
// Receives a character from the specified port
//
//*****************************************************************************
int32_t
UARTCharGetNonBlocking(uint32_t ui32Base)
{
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// See if there are any characters in the receive FIFO.
if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE))
{
// Read and return the next character.
return(HWREG(ui32Base + UART_O_DR));
}
else
{
// There are no characters, so return a failure.
return(-1);
}
}
//*****************************************************************************
//
// Waits for a character from the specified port
//
//*****************************************************************************
int32_t
UARTCharGet(uint32_t ui32Base)
{
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// Wait until a char is available.
while(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE)
{
}
// Now get the character.
return(HWREG(ui32Base + UART_O_DR));
}
//*****************************************************************************
//
// Sends a character to the specified port
//
//*****************************************************************************
bool
UARTCharPutNonBlocking(uint32_t ui32Base, uint8_t ui8Data)
{
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// See if there is space in the transmit FIFO.
if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF))
{
// Write this character to the transmit FIFO.
HWREG(ui32Base + UART_O_DR) = ui8Data;
// Success.
return(true);
}
else
{
// There is no space in the transmit FIFO, so return a failure.
return(false);
}
}
//*****************************************************************************
//
// Waits to send a character from the specified port
//
//*****************************************************************************
void
UARTCharPut(uint32_t ui32Base, uint8_t ui8Data)
{
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// Wait until space is available.
while(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF)
{
}
// Send the char.
HWREG(ui32Base + UART_O_DR) = ui8Data;
}
//*****************************************************************************
//
// Registers an interrupt handler for a UART interrupt
//
//*****************************************************************************
void
UARTIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
{
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// Register the interrupt handler.
IntRegister(INT_UART0_COMB, pfnHandler);
// Enable the UART interrupt.
IntEnable(INT_UART0_COMB);
}
//*****************************************************************************
//
// Unregisters an interrupt handler for a UART interrupt
//
//*****************************************************************************
void
UARTIntUnregister(uint32_t ui32Base)
{
// Check the arguments.
ASSERT(UARTBaseValid(ui32Base));
// Disable the interrupt.
IntDisable(INT_UART0_COMB);
// Unregister the interrupt handler.
IntUnregister(INT_UART0_COMB);
}