CC13xx Driver Library
uart.c
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: uart.c
3 * Revised: 2015-01-13 16:59:55 +0100 (Tue, 13 Jan 2015)
4 * Revision: 42365
5 *
6 * Description: Driver for the UART.
7 *
8 * Copyright (c) 2015, Texas Instruments Incorporated
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * 1) Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * 2) Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 #include <driverlib/uart.h>
40 
41 //*****************************************************************************
42 //
43 // Handle support for DriverLib in ROM:
44 // This section will undo prototype renaming made in the header file
45 //
46 //*****************************************************************************
47 #if !defined(DOXYGEN)
48  #undef UARTFIFOLevelGet
49  #define UARTFIFOLevelGet NOROM_UARTFIFOLevelGet
50  #undef UARTConfigSetExpClk
51  #define UARTConfigSetExpClk NOROM_UARTConfigSetExpClk
52  #undef UARTConfigGetExpClk
53  #define UARTConfigGetExpClk NOROM_UARTConfigGetExpClk
54  #undef UARTDisable
55  #define UARTDisable NOROM_UARTDisable
56  #undef UARTCharGetNonBlocking
57  #define UARTCharGetNonBlocking NOROM_UARTCharGetNonBlocking
58  #undef UARTCharGet
59  #define UARTCharGet NOROM_UARTCharGet
60  #undef UARTCharPutNonBlocking
61  #define UARTCharPutNonBlocking NOROM_UARTCharPutNonBlocking
62  #undef UARTCharPut
63  #define UARTCharPut NOROM_UARTCharPut
64  #undef UARTIntRegister
65  #define UARTIntRegister NOROM_UARTIntRegister
66  #undef UARTIntUnregister
67  #define UARTIntUnregister NOROM_UARTIntUnregister
68 #endif
69 
70 //*****************************************************************************
71 //
73 //
74 //*****************************************************************************
75 void
76 UARTFIFOLevelGet(uint32_t ui32Base, uint32_t *pui32TxLevel,
77  uint32_t *pui32RxLevel)
78 {
79  uint32_t ui32Temp;
80 
81  //
82  // Check the arguments.
83  //
84  ASSERT(UARTBaseValid(ui32Base));
85 
86  //
87  // Read the FIFO level register.
88  //
89  ui32Temp = HWREG(ui32Base + UART_O_IFLS);
90 
91  //
92  // Extract the transmit and receive FIFO levels.
93  //
94  *pui32TxLevel = ui32Temp & UART_IFLS_TXSEL_M;
95  *pui32RxLevel = ui32Temp & UART_IFLS_RXSEL_M;
96 }
97 
98 //*****************************************************************************
99 //
101 //
102 //*****************************************************************************
103 void
104 UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
105  uint32_t ui32Baud, uint32_t ui32Config)
106 {
107  uint32_t ui32Div;
108 
109  //
110  // Check the arguments.
111  //
112  ASSERT(UARTBaseValid(ui32Base));
113  ASSERT(ui32Baud != 0);
114 
115  //
116  // Stop the UART.
117  //
118  UARTDisable(ui32Base);
119 
120  //
121  // Compute the fractional baud rate divider.
122  //
123  ui32Div = (((ui32UARTClk * 8) / ui32Baud) + 1) / 2;
124 
125  //
126  // Set the baud rate.
127  //
128  HWREG(ui32Base + UART_O_IBRD) = ui32Div / 64;
129  HWREG(ui32Base + UART_O_FBRD) = ui32Div % 64;
130 
131  //
132  // Set parity, data length, and number of stop bits.
133  //
134  HWREG(ui32Base + UART_O_LCRH) = ui32Config;
135 
136  //
137  // Clear the flags register.
138  //
139  HWREG(ui32Base + UART_O_FR) = 0;
140 }
141 
142 //*****************************************************************************
143 //
145 //
146 //*****************************************************************************
147 void
148 UARTConfigGetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
149  uint32_t *pui32Baud, uint32_t *pui32Config)
150 {
151  uint32_t ui32Int, ui32Frac;
152 
153  //
154  // Check the arguments.
155  //
156  ASSERT(UARTBaseValid(ui32Base));
157 
158  //
159  // Compute the baud rate.
160  //
161  ui32Int = HWREG(ui32Base + UART_O_IBRD);
162  ui32Frac = HWREG(ui32Base + UART_O_FBRD);
163  *pui32Baud = (ui32UARTClk * 4) / ((64 * ui32Int) + ui32Frac);
164 
165  //
166  // Get the parity, data length, and number of stop bits.
167  //
168  *pui32Config = (HWREG(ui32Base + UART_O_LCRH) &
171 }
172 
173 //*****************************************************************************
174 //
176 //
177 //*****************************************************************************
178 void
179 UARTDisable(uint32_t ui32Base)
180 {
181 
182  //
183  // Check the arguments.
184  //
185  ASSERT(UARTBaseValid(ui32Base));
186 
187  //
188  // Wait for end of TX.
189  //
190  while(HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY)
191  {
192  }
193 
194  //
195  // Disable the FIFO.
196  //
197  HWREG(ui32Base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
198 
199  //
200  // Disable the UART.
201  //
202  HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_UARTEN | UART_CTL_TXE |
203  UART_CTL_RXE);
204 }
205 
206 //*****************************************************************************
207 //
209 //
210 //*****************************************************************************
211 int32_t
212 UARTCharGetNonBlocking(uint32_t ui32Base)
213 {
214  //
215  // Check the arguments.
216  //
217  ASSERT(UARTBaseValid(ui32Base));
218 
219  //
220  // See if there are any characters in the receive FIFO.
221  //
222  if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE))
223  {
224  //
225  // Read and return the next character.
226  //
227  return(HWREG(ui32Base + UART_O_DR));
228  }
229  else
230  {
231  //
232  // There are no characters, so return a failure.
233  //
234  return(-1);
235  }
236 }
237 
238 //*****************************************************************************
239 //
241 //
242 //*****************************************************************************
243 int32_t
244 UARTCharGet(uint32_t ui32Base)
245 {
246  //
247  // Check the arguments.
248  //
249  ASSERT(UARTBaseValid(ui32Base));
250 
251  //
252  // Wait until a char is available.
253  //
254  while(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE)
255  {
256  }
257 
258  //
259  // Now get the character.
260  //
261  return(HWREG(ui32Base + UART_O_DR));
262 }
263 
264 //*****************************************************************************
265 //
267 //
268 //*****************************************************************************
269 bool
270 UARTCharPutNonBlocking(uint32_t ui32Base, uint8_t ui8Data)
271 {
272  //
273  // Check the arguments.
274  //
275  ASSERT(UARTBaseValid(ui32Base));
276 
277  //
278  // See if there is space in the transmit FIFO.
279  //
280  if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF))
281  {
282  //
283  // Write this character to the transmit FIFO.
284  //
285  HWREG(ui32Base + UART_O_DR) = ui8Data;
286 
287  //
288  // Success.
289  //
290  return(true);
291  }
292  else
293  {
294  //
295  // There is no space in the transmit FIFO, so return a failure.
296  //
297  return(false);
298  }
299 }
300 
301 //*****************************************************************************
302 //
304 //
305 //*****************************************************************************
306 void
307 UARTCharPut(uint32_t ui32Base, uint8_t ui8Data)
308 {
309  //
310  // Check the arguments.
311  //
312  ASSERT(UARTBaseValid(ui32Base));
313 
314  //
315  // Wait until space is available.
316  //
317  while(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF)
318  {
319  }
320 
321  //
322  // Send the char.
323  //
324  HWREG(ui32Base + UART_O_DR) = ui8Data;
325 }
326 
327 //*****************************************************************************
328 //
330 //
331 //*****************************************************************************
332 void
333 UARTIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
334 {
335  //
336  // Check the arguments.
337  //
338  ASSERT(UARTBaseValid(ui32Base));
339 
340  //
341  // Register the interrupt handler.
342  //
343  IntRegister(INT_UART0, pfnHandler);
344 
345  //
346  // Enable the UART interrupt.
347  //
348  IntEnable(INT_UART0);
349 }
350 
351 //*****************************************************************************
352 //
354 //
355 //*****************************************************************************
356 void
357 UARTIntUnregister(uint32_t ui32Base)
358 {
359  //
360  // Check the arguments.
361  //
362  ASSERT(UARTBaseValid(ui32Base));
363 
364  //
365  // Disable the interrupt.
366  //
367  IntDisable(INT_UART0);
368 
369  //
370  // Unregister the interrupt handler.
371  //
372  IntUnregister(INT_UART0);
373 }
void UARTCharPut(uint32_t ui32Base, uint8_t ui8Data)
Waits to send a character from the specified port.
Definition: uart.c:307
void UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk, uint32_t ui32Baud, uint32_t ui32Config)
Sets the configuration of a UART.
Definition: uart.c:104
void UARTIntRegister(uint32_t ui32Base, void(*pfnHandler)(void))
Registers an interrupt handler for a UART interrupt.
Definition: uart.c:333
void UARTDisable(uint32_t ui32Base)
Disables transmitting and receiving.
Definition: uart.c:179
int32_t UARTCharGetNonBlocking(uint32_t ui32Base)
Receives a character from the specified port.
Definition: uart.c:212
#define ASSERT(expr)
Definition: debug.h:74
void UARTConfigGetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk, uint32_t *pui32Baud, uint32_t *pui32Config)
Gets the current configuration of a UART.
Definition: uart.c:148
int32_t UARTCharGet(uint32_t ui32Base)
Waits for a character from the specified port.
Definition: uart.c:244
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:203
bool UARTCharPutNonBlocking(uint32_t ui32Base, uint8_t ui8Data)
Sends a character to the specified port.
Definition: uart.c:270
void UARTFIFOLevelGet(uint32_t ui32Base, uint32_t *pui32TxLevel, uint32_t *pui32RxLevel)
Gets the FIFO level at which interrupts are generated.
Definition: uart.c:76
void UARTIntUnregister(uint32_t ui32Base)
Unregisters an interrupt handler for a UART interrupt.
Definition: uart.c:357
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:381
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:155
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:321