1 /******************************************************************************
2 *  Filename:       uart.h
3 *  Revised:        2020-02-14 11:30:20 +0100 (Fri, 14 Feb 2020)
4 *  Revision:       56760
5 *
6 *  Description:    Defines and prototypes for the UART.
7 *
8 *  Copyright (c) 2015 - 2020, 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 //*****************************************************************************
40 //
41 //! \addtogroup peripheral_group
42 //! @{
43 //! \addtogroup uart_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #ifndef __UART_H__
49 #define __UART_H__
50 
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include "../inc/hw_types.h"
65 #include "../inc/hw_uart.h"
66 #include "../inc/hw_memmap.h"
67 #include "../inc/hw_ints.h"
68 #include "interrupt.h"
69 #include "debug.h"
70 
71 //*****************************************************************************
72 //
73 // Support for DriverLib in ROM:
74 // This section renames all functions that are not "static inline", so that
75 // calling these functions will default to implementation in flash. At the end
76 // of this file a second renaming will change the defaults to implementation in
77 // ROM for available functions.
78 //
79 // To force use of the implementation in flash, e.g. for debugging:
80 // - Globally: Define DRIVERLIB_NOROM at project level
81 // - Per function: Use prefix "NOROM_" when calling the function
82 //
83 //*****************************************************************************
84 #if !defined(DOXYGEN)
85     #define UARTFIFOLevelGet                NOROM_UARTFIFOLevelGet
86     #define UARTConfigSetExpClk             NOROM_UARTConfigSetExpClk
87     #define UARTConfigGetExpClk             NOROM_UARTConfigGetExpClk
88     #define UARTDisable                     NOROM_UARTDisable
89     #define UARTCharGetNonBlocking          NOROM_UARTCharGetNonBlocking
90     #define UARTCharGet                     NOROM_UARTCharGet
91     #define UARTCharPutNonBlocking          NOROM_UARTCharPutNonBlocking
92     #define UARTCharPut                     NOROM_UARTCharPut
93     #define UARTIntRegister                 NOROM_UARTIntRegister
94     #define UARTIntUnregister               NOROM_UARTIntUnregister
95 #endif
96 
97 //*****************************************************************************
98 //
99 // Values that can be passed to UARTIntEnable, UARTIntDisable, and UARTIntClear
100 // as the ui32IntFlags parameter, and returned from UARTIntStatus.
101 //
102 //*****************************************************************************
103 #define UART_INT_EOT   ( UART_IMSC_EOTIM  ) // End Of Transmission Interrupt Mask
104 #define UART_INT_OE    ( UART_IMSC_OEIM   ) // Overrun Error Interrupt Mask
105 #define UART_INT_BE    ( UART_IMSC_BEIM   ) // Break Error Interrupt Mask
106 #define UART_INT_PE    ( UART_IMSC_PEIM   ) // Parity Error Interrupt Mask
107 #define UART_INT_FE    ( UART_IMSC_FEIM   ) // Framing Error Interrupt Mask
108 #define UART_INT_RT    ( UART_IMSC_RTIM   ) // Receive Timeout Interrupt Mask
109 #define UART_INT_TX    ( UART_IMSC_TXIM   ) // Transmit Interrupt Mask
110 #define UART_INT_RX    ( UART_IMSC_RXIM   ) // Receive Interrupt Mask
111 #define UART_INT_CTS   ( UART_IMSC_CTSMIM ) // CTS Modem Interrupt Mask
112 
113 //*****************************************************************************
114 //
115 // Values that can be passed to UARTConfigSetExpClk as the ui32Config parameter
116 // and returned by UARTConfigGetExpClk in the pui32Config parameter.
117 // Additionally, the UART_CONFIG_PAR_* subset can be passed to
118 // UARTParityModeSet as the ui32Parity parameter, and are returned by
119 // UARTParityModeGet.
120 //
121 //*****************************************************************************
122 #define UART_CONFIG_WLEN_MASK   0x00000060  // Mask for extracting word length
123 #define UART_CONFIG_WLEN_8      0x00000060  // 8 bit data
124 #define UART_CONFIG_WLEN_7      0x00000040  // 7 bit data
125 #define UART_CONFIG_WLEN_6      0x00000020  // 6 bit data
126 #define UART_CONFIG_WLEN_5      0x00000000  // 5 bit data
127 #define UART_CONFIG_STOP_MASK   0x00000008  // Mask for extracting stop bits
128 #define UART_CONFIG_STOP_ONE    0x00000000  // One stop bit
129 #define UART_CONFIG_STOP_TWO    0x00000008  // Two stop bits
130 #define UART_CONFIG_PAR_MASK    0x00000086  // Mask for extracting parity
131 #define UART_CONFIG_PAR_NONE    0x00000000  // No parity
132 #define UART_CONFIG_PAR_EVEN    0x00000006  // Even parity
133 #define UART_CONFIG_PAR_ODD     0x00000002  // Odd parity
134 #define UART_CONFIG_PAR_ONE     0x00000082  // Parity bit is one
135 #define UART_CONFIG_PAR_ZERO    0x00000086  // Parity bit is zero
136 
137 //*****************************************************************************
138 //
139 // Values that can be passed to UARTFIFOLevelSet as the ui32TxLevel parameter
140 // and returned by UARTFIFOLevelGet in the pui32TxLevel.
141 //
142 //*****************************************************************************
143 #define UART_FIFO_TX1_8         0x00000000  // Transmit interrupt at 1/8 Full
144 #define UART_FIFO_TX2_8         0x00000001  // Transmit interrupt at 1/4 Full
145 #define UART_FIFO_TX4_8         0x00000002  // Transmit interrupt at 1/2 Full
146 #define UART_FIFO_TX6_8         0x00000003  // Transmit interrupt at 3/4 Full
147 #define UART_FIFO_TX7_8         0x00000004  // Transmit interrupt at 7/8 Full
148 
149 //*****************************************************************************
150 //
151 // Values that can be passed to UARTFIFOLevelSet as the ui32RxLevel parameter
152 // and returned by UARTFIFOLevelGet in the pui32RxLevel.
153 //
154 //*****************************************************************************
155 #define UART_FIFO_RX1_8         0x00000000  // Receive interrupt at 1/8 Full
156 #define UART_FIFO_RX2_8         0x00000008  // Receive interrupt at 1/4 Full
157 #define UART_FIFO_RX4_8         0x00000010  // Receive interrupt at 1/2 Full
158 #define UART_FIFO_RX6_8         0x00000018  // Receive interrupt at 3/4 Full
159 #define UART_FIFO_RX7_8         0x00000020  // Receive interrupt at 7/8 Full
160 
161 //*****************************************************************************
162 //
163 // Values that can be passed to UARTDMAEnable() and UARTDMADisable().
164 //
165 //*****************************************************************************
166 #define UART_DMA_ERR_RXSTOP     0x00000004  // Stop DMA receive if UART error
167 #define UART_DMA_TX             0x00000002  // Enable DMA for transmit
168 #define UART_DMA_RX             0x00000001  // Enable DMA for receive
169 
170 //*****************************************************************************
171 //
172 // Values returned from UARTRxErrorGet().
173 //
174 //*****************************************************************************
175 #define UART_RXERROR_OVERRUN    0x00000008
176 #define UART_RXERROR_BREAK      0x00000004
177 #define UART_RXERROR_PARITY     0x00000002
178 #define UART_RXERROR_FRAMING    0x00000001
179 
180 //*****************************************************************************
181 //
182 // Values returned from the UARTBusy().
183 //
184 //*****************************************************************************
185 #define UART_BUSY               0x00000001
186 #define UART_IDLE               0x00000000
187 
188 //*****************************************************************************
189 //
190 // API Functions and prototypes
191 //
192 //*****************************************************************************
193 
194 #ifdef DRIVERLIB_DEBUG
195 //*****************************************************************************
196 //
197 //! \internal
198 //!
199 //! \brief Checks a UART base address.
200 //!
201 //! This function determines if a UART port base address is valid.
202 //!
203 //! \param ui32Base is the base address of the UART port.
204 //!
205 //! \return Returns \c true if the base address is valid and \c false
206 //! otherwise.
207 //
208 //*****************************************************************************
209 static bool
UARTBaseValid(uint32_t ui32Base)210 UARTBaseValid(uint32_t ui32Base)
211 {
212     return(( ui32Base == UART0_BASE ) || ( ui32Base == UART0_NONBUF_BASE ) ||
213            ( ui32Base == UART1_BASE ) || ( ui32Base == UART1_NONBUF_BASE )    );
214 }
215 #endif
216 
217 //*****************************************************************************
218 //
219 //! \brief Sets the type of parity.
220 //!
221 //! This function sets the type of parity to use for transmitting and expect
222 //! when receiving.
223 //!
224 //! \param ui32Base is the base address of the UART port.
225 //! \param ui32Parity specifies the type of parity to use. The last two allow
226 //! direct control of the parity bit; it is always either one or zero based on
227 //! the mode.
228 //! - \ref UART_CONFIG_PAR_NONE
229 //! - \ref UART_CONFIG_PAR_EVEN
230 //! - \ref UART_CONFIG_PAR_ODD
231 //! - \ref UART_CONFIG_PAR_ONE
232 //! - \ref UART_CONFIG_PAR_ZERO
233 //!
234 //! \return None
235 //
236 //*****************************************************************************
237 __STATIC_INLINE void
UARTParityModeSet(uint32_t ui32Base,uint32_t ui32Parity)238 UARTParityModeSet(uint32_t ui32Base, uint32_t ui32Parity)
239 {
240     // Check the arguments.
241     ASSERT(UARTBaseValid(ui32Base));
242     ASSERT((ui32Parity == UART_CONFIG_PAR_NONE) ||
243            (ui32Parity == UART_CONFIG_PAR_EVEN) ||
244            (ui32Parity == UART_CONFIG_PAR_ODD) ||
245            (ui32Parity == UART_CONFIG_PAR_ONE) ||
246            (ui32Parity == UART_CONFIG_PAR_ZERO));
247 
248     // Set the parity mode.
249     HWREG(ui32Base + UART_O_LCRH) = ((HWREG(ui32Base + UART_O_LCRH) &
250                                       ~(UART_LCRH_SPS | UART_LCRH_EPS |
251                                         UART_LCRH_PEN)) | ui32Parity);
252 }
253 
254 //*****************************************************************************
255 //
256 //! \brief Gets the type of parity currently being used.
257 //!
258 //! This function gets the type of parity used for transmitting data and
259 //! expected when receiving data.
260 //!
261 //! \param ui32Base is the base address of the UART port.
262 //!
263 //! \return Returns the current parity settings, specified as one of:
264 //! - \ref UART_CONFIG_PAR_NONE
265 //! - \ref UART_CONFIG_PAR_EVEN
266 //! - \ref UART_CONFIG_PAR_ODD
267 //! - \ref UART_CONFIG_PAR_ONE
268 //! - \ref UART_CONFIG_PAR_ZERO
269 //
270 //*****************************************************************************
271 __STATIC_INLINE uint32_t
UARTParityModeGet(uint32_t ui32Base)272 UARTParityModeGet(uint32_t ui32Base)
273 {
274     // Check the arguments.
275     ASSERT(UARTBaseValid(ui32Base));
276 
277     // Return the current parity setting
278     return(HWREG(ui32Base + UART_O_LCRH) &
279            (UART_LCRH_SPS | UART_LCRH_EPS | UART_LCRH_PEN));
280 }
281 
282 //*****************************************************************************
283 //
284 //! \brief Sets the FIFO level at which interrupts are generated.
285 //!
286 //! This function sets the FIFO level at which transmit and receive interrupts
287 //! are generated.
288 //!
289 //! \param ui32Base is the base address of the UART port.
290 //! \param ui32TxLevel is the transmit FIFO interrupt level, specified as one of:
291 //! - \ref UART_FIFO_TX1_8
292 //! - \ref UART_FIFO_TX2_8
293 //! - \ref UART_FIFO_TX4_8
294 //! - \ref UART_FIFO_TX6_8
295 //! - \ref UART_FIFO_TX7_8
296 //! \param ui32RxLevel is the receive FIFO interrupt level, specified as one of:
297 //! - \ref UART_FIFO_RX1_8
298 //! - \ref UART_FIFO_RX2_8
299 //! - \ref UART_FIFO_RX4_8
300 //! - \ref UART_FIFO_RX6_8
301 //! - \ref UART_FIFO_RX7_8
302 //!
303 //! \return None
304 //
305 //*****************************************************************************
306 __STATIC_INLINE void
UARTFIFOLevelSet(uint32_t ui32Base,uint32_t ui32TxLevel,uint32_t ui32RxLevel)307 UARTFIFOLevelSet(uint32_t ui32Base, uint32_t ui32TxLevel,
308                  uint32_t ui32RxLevel)
309 {
310     // Check the arguments.
311     ASSERT(UARTBaseValid(ui32Base));
312     ASSERT((ui32TxLevel == UART_FIFO_TX1_8) ||
313            (ui32TxLevel == UART_FIFO_TX2_8) ||
314            (ui32TxLevel == UART_FIFO_TX4_8) ||
315            (ui32TxLevel == UART_FIFO_TX6_8) ||
316            (ui32TxLevel == UART_FIFO_TX7_8));
317     ASSERT((ui32RxLevel == UART_FIFO_RX1_8) ||
318            (ui32RxLevel == UART_FIFO_RX2_8) ||
319            (ui32RxLevel == UART_FIFO_RX4_8) ||
320            (ui32RxLevel == UART_FIFO_RX6_8) ||
321            (ui32RxLevel == UART_FIFO_RX7_8));
322 
323     // Set the FIFO interrupt levels.
324     HWREG(ui32Base + UART_O_IFLS) = ui32TxLevel | ui32RxLevel;
325 }
326 
327 //*****************************************************************************
328 //
329 //! \brief Gets the FIFO level at which interrupts are generated.
330 //!
331 //! This function gets the FIFO level at which transmit and receive interrupts
332 //! are generated.
333 //!
334 //! \param ui32Base is the base address of the UART port.
335 //! \param pui32TxLevel is a pointer to storage for the transmit FIFO level,
336 //! returned as one of:
337 //! - \ref UART_FIFO_TX1_8
338 //! - \ref UART_FIFO_TX2_8
339 //! - \ref UART_FIFO_TX4_8
340 //! - \ref UART_FIFO_TX6_8
341 //! - \ref UART_FIFO_TX7_8
342 //! \param pui32RxLevel is a pointer to storage for the receive FIFO level,
343 //! returned as one of:
344 //! - \ref UART_FIFO_RX1_8
345 //! - \ref UART_FIFO_RX2_8
346 //! - \ref UART_FIFO_RX4_8
347 //! - \ref UART_FIFO_RX6_8
348 //! - \ref UART_FIFO_RX7_8
349 //!
350 //! \return None
351 //
352 //*****************************************************************************
353 extern void UARTFIFOLevelGet(uint32_t ui32Base, uint32_t *pui32TxLevel,
354                              uint32_t *pui32RxLevel);
355 
356 //*****************************************************************************
357 //
358 //! \brief Sets the configuration of a UART.
359 //!
360 //! This function configures the UART for operation in the specified data
361 //! format.
362 //!
363 //! \note The peripheral clock is not necessarily the same as the processor
364 //! clock. The frequency of the peripheral clock is set by the system control.
365 //!
366 //! \param ui32Base is the base address of the UART port.
367 //! \param ui32UARTClk is the rate of the clock supplied to the UART module.
368 //! \param ui32Baud is the desired baud rate.
369 //! - Minimum baud rate: ui32Baud >= ceil(ui32UARTClk / 1,048,559.875)
370 //! - Maximum baud rate: ui32Baud <= floor(ui32UARTClk / 15.875)
371 //! \param ui32Config is the data format for the port.
372 //! The parameter is the bitwise OR of three values:
373 //! - Number of data bits
374 //!   - \ref UART_CONFIG_WLEN_8 : 8 data bits per byte.
375 //!   - \ref UART_CONFIG_WLEN_7 : 7 data bits per byte.
376 //!   - \ref UART_CONFIG_WLEN_6 : 6 data bits per byte.
377 //!   - \ref UART_CONFIG_WLEN_5 : 5 data bits per byte.
378 //! - Number of stop bits
379 //!   - \ref UART_CONFIG_STOP_ONE : One stop bit.
380 //!   - \ref UART_CONFIG_STOP_TWO : Two stop bits.
381 //! - Parity
382 //!   - \ref UART_CONFIG_PAR_NONE
383 //!   - \ref UART_CONFIG_PAR_EVEN
384 //!   - \ref UART_CONFIG_PAR_ODD
385 //!   - \ref UART_CONFIG_PAR_ONE
386 //!   - \ref UART_CONFIG_PAR_ZERO
387 //!
388 //! \return None
389 //
390 //*****************************************************************************
391 extern void UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
392                                 uint32_t ui32Baud, uint32_t ui32Config);
393 
394 //*****************************************************************************
395 //
396 //! \brief Gets the current configuration of a UART.
397 //!
398 //! The baud rate and data format for the UART is determined, given an
399 //! explicitly provided peripheral clock (hence the ExpClk suffix). The
400 //! returned baud rate is the actual baud rate; it may not be the exact baud
401 //! rate requested or an "official" baud rate. The data format returned in
402 //! \c pui32Config is enumerated the same as the \c ui32Config parameter of
403 //! \ref UARTConfigSetExpClk().
404 //!
405 //! \note The peripheral clock is not necessarily the same as the processor
406 //! clock. The frequency of the peripheral clock is set by the system control.
407 //!
408 //! \param ui32Base is the base address of the UART port.
409 //! \param ui32UARTClk is the rate of the clock supplied to the UART module.
410 //! \param pui32Baud is a pointer to storage for the baud rate.
411 //! \param pui32Config is a pointer to storage for the data format.
412 //!
413 //! \return None
414 //
415 //*****************************************************************************
416 extern void UARTConfigGetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
417                                 uint32_t *pui32Baud, uint32_t *pui32Config);
418 
419 //*****************************************************************************
420 //
421 //! \brief Enables transmitting and receiving.
422 //!
423 //! This function sets the UARTEN, TXE, and RXE bits, and enables the transmit
424 //! and receive FIFOs.
425 //!
426 //! \param ui32Base is the base address of the UART port.
427 //!
428 //! \return None
429 //
430 //*****************************************************************************
431 __STATIC_INLINE void
UARTEnable(uint32_t ui32Base)432 UARTEnable(uint32_t ui32Base)
433 {
434     // Check the arguments.
435     ASSERT(UARTBaseValid(ui32Base));
436 
437     // Enable the FIFO.
438     HWREG(ui32Base + UART_O_LCRH) |= UART_LCRH_FEN;
439 
440     // Enable RX, TX, and the UART.
441     HWREG(ui32Base + UART_O_CTL) |= (UART_CTL_UARTEN | UART_CTL_TXE |
442                                      UART_CTL_RXE);
443 }
444 
445 //*****************************************************************************
446 //
447 //! \brief Disables transmitting and receiving.
448 //!
449 //! This function clears the UARTEN, TXE, and RXE bits, waits for the end of
450 //! transmission of the current character, and flushes the transmit FIFO.
451 //!
452 //! \param ui32Base is the base address of the UART port.
453 //!
454 //! \return None
455 //
456 //*****************************************************************************
457 extern void UARTDisable(uint32_t ui32Base);
458 
459 //*****************************************************************************
460 //
461 //! \brief Enables the transmit and receive FIFOs.
462 //!
463 //! This functions enables the transmit and receive FIFOs in the UART.
464 //!
465 //! \param ui32Base is the base address of the UART port.
466 //!
467 //! \return None
468 //
469 //*****************************************************************************
470 __STATIC_INLINE void
UARTFIFOEnable(uint32_t ui32Base)471 UARTFIFOEnable(uint32_t ui32Base)
472 {
473     // Check the arguments.
474     ASSERT(UARTBaseValid(ui32Base));
475 
476     // Enable the FIFO.
477     HWREG(ui32Base + UART_O_LCRH) |= UART_LCRH_FEN;
478 }
479 
480 //*****************************************************************************
481 //
482 //! \brief Disables the transmit and receive FIFOs.
483 //!
484 //! This functions disables the transmit and receive FIFOs in the UART.
485 //!
486 //! \param ui32Base is the base address of the UART port.
487 //!
488 //! \return None
489 //
490 //*****************************************************************************
491 __STATIC_INLINE void
UARTFIFODisable(uint32_t ui32Base)492 UARTFIFODisable(uint32_t ui32Base)
493 {
494     // Check the arguments.
495     ASSERT(UARTBaseValid(ui32Base));
496 
497     // Disable the FIFO.
498     HWREG(ui32Base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
499 }
500 
501 //*****************************************************************************
502 //
503 //! \brief Determines if there are any characters in the receive FIFO.
504 //!
505 //! This function returns a flag indicating whether or not there is data
506 //! available in the receive FIFO.
507 //!
508 //! \param ui32Base is the base address of the UART port.
509 //!
510 //! \return Returns status of the receive FIFO.
511 //! - \c true  : There is data in the receive FIFO.
512 //! - \c false : There is no data in the receive FIFO.
513 //
514 //*****************************************************************************
515 __STATIC_INLINE bool
UARTCharsAvail(uint32_t ui32Base)516 UARTCharsAvail(uint32_t ui32Base)
517 {
518     // Check the arguments.
519     ASSERT(UARTBaseValid(ui32Base));
520 
521     // Return the availability of characters.
522     return((HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE) ? false : true);
523 }
524 
525 //*****************************************************************************
526 //
527 //! \brief Determines if there is any space in the transmit FIFO.
528 //!
529 //! This function returns a flag indicating whether or not there is space
530 //! available in the transmit FIFO.
531 //!
532 //! \param ui32Base is the base address of the UART port.
533 //!
534 //! \return Returns status of the transmit FIFO.
535 //! - \c true  : There is space available in the transmit FIFO.
536 //! - \c false : There is no space available in the transmit FIFO.
537 //
538 //*****************************************************************************
539 __STATIC_INLINE bool
UARTSpaceAvail(uint32_t ui32Base)540 UARTSpaceAvail(uint32_t ui32Base)
541 {
542     // Check the arguments.
543     ASSERT(UARTBaseValid(ui32Base));
544 
545     // Return the availability of space.
546     return((HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF) ? false : true);
547 }
548 
549 //*****************************************************************************
550 //
551 //! \brief Receives a character from the specified port.
552 //!
553 //! This function gets a character from the receive FIFO for the specified
554 //! port.
555 //!
556 //! \note The \ref UARTCharsAvail() function should be called before
557 //! attempting to call this function.
558 //!
559 //! \param ui32Base is the base address of the UART port.
560 //!
561 //! \return Returns the character read from the specified port, cast as an
562 //! \c int32_t. A \c -1 is returned if there are no characters present in the
563 //! receive FIFO.
564 //!
565 //! \sa \ref UARTCharsAvail()
566 //
567 //*****************************************************************************
568 extern int32_t UARTCharGetNonBlocking(uint32_t ui32Base);
569 
570 //*****************************************************************************
571 //
572 //! \brief Waits for a character from the specified port.
573 //!
574 //! This function gets a character from the receive FIFO for the specified
575 //! port. If there are no characters available, this function waits until a
576 //! character is received before returning.
577 //!
578 //! \param ui32Base is the base address of the UART port.
579 //!
580 //! \return Returns the character read from the specified port, cast as an
581 //! \c int32_t.
582 //
583 //*****************************************************************************
584 extern int32_t UARTCharGet(uint32_t ui32Base);
585 
586 //*****************************************************************************
587 //
588 //! \brief Sends a character to the specified port.
589 //!
590 //! This function writes the character \c ui8Data to the transmit FIFO for the
591 //! specified port. This function does not block, so if there is no space
592 //! available, then a \c false is returned, and the application must retry the
593 //! function later.
594 //!
595 //! \param ui32Base is the base address of the UART port.
596 //! \param ui8Data is the character to be transmitted.
597 //!
598 //! \return Returns status of the character transmit.
599 //! - \c true  : The character was successfully placed in the transmit FIFO.
600 //! - \c false : There was no space available in the transmit FIFO. Try again later.
601 //
602 //*****************************************************************************
603 extern bool UARTCharPutNonBlocking(uint32_t ui32Base, uint8_t ui8Data);
604 
605 //*****************************************************************************
606 //
607 //! \brief Waits to send a character from the specified port.
608 //!
609 //! This function sends the character \c ui8Data to the transmit FIFO for the
610 //! specified port. If there is no space available in the transmit FIFO, this
611 //! function waits until there is space available before returning.
612 //!
613 //! \param ui32Base is the base address of the UART port.
614 //! \param ui8Data is the character to be transmitted.
615 //!
616 //! \return None
617 //
618 //*****************************************************************************
619 extern void UARTCharPut(uint32_t ui32Base, uint8_t ui8Data);
620 
621 //*****************************************************************************
622 //
623 //! \brief Determines whether the UART transmitter is busy or not.
624 //!
625 //! Allows the caller to determine whether all transmitted bytes have cleared
626 //! the transmitter hardware. If \c false is returned, the transmit FIFO is
627 //! empty and all bits of the last transmitted character, including all stop
628 //! bits, have left the hardware shift register.
629 //!
630 //! \param ui32Base is the base address of the UART port.
631 //!
632 //! \return Returns status of UART transmitter.
633 //! - \c true  : UART is transmitting.
634 //! - \c false : All transmissions are complete.
635 //
636 //*****************************************************************************
637 __STATIC_INLINE bool
UARTBusy(uint32_t ui32Base)638 UARTBusy(uint32_t ui32Base)
639 {
640     // Check the argument.
641     ASSERT(UARTBaseValid(ui32Base));
642 
643     // Determine if the UART is busy.
644     return((HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY) ?
645            UART_BUSY : UART_IDLE);
646 }
647 
648 //*****************************************************************************
649 //
650 //! \brief Causes a BREAK to be sent.
651 //!
652 //! \note For proper transmission of a break
653 //! command, the break must be asserted for at least two complete frames.
654 //!
655 //! \param ui32Base is the base address of the UART port.
656 //! \param bBreakState controls the output level.
657 //! - \c true  : Asserts a break condition on the UART.
658 //! - \c false : Removes the break condition.
659 //!
660 //! \return None
661 //
662 //*****************************************************************************
663 __STATIC_INLINE void
UARTBreakCtl(uint32_t ui32Base,bool bBreakState)664 UARTBreakCtl(uint32_t ui32Base, bool bBreakState)
665 {
666     // Check the arguments.
667     ASSERT(UARTBaseValid(ui32Base));
668 
669     // Set the break condition as requested.
670     HWREG(ui32Base + UART_O_LCRH) =
671          (bBreakState ?
672          (HWREG(ui32Base + UART_O_LCRH) | UART_LCRH_BRK) :
673          (HWREG(ui32Base + UART_O_LCRH) & ~(UART_LCRH_BRK)));
674 }
675 
676 //*****************************************************************************
677 //
678 //! \brief Registers an interrupt handler for a UART interrupt in the dynamic interrupt table.
679 //!
680 //! \note Only use this function if you want to use the dynamic vector table (in SRAM)!
681 //!
682 //! This function registers a function as the interrupt handler for a specific
683 //! interrupt and enables the corresponding interrupt in the interrupt controller.
684 //!
685 //! Specific UART interrupts must be enabled via \ref UARTIntEnable(). It is the
686 //! interrupt handler's responsibility to clear the interrupt source.
687 //!
688 //! \param ui32Base is the base address of the UART module.
689 //! \param pfnHandler is a pointer to the function to be called when the
690 //! UART interrupt occurs.
691 //!
692 //! \return None
693 //!
694 //! \sa \ref IntRegister() for important information about registering interrupt
695 //! handlers.
696 //
697 //*****************************************************************************
698 extern void UARTIntRegister(uint32_t ui32Base, void (*pfnHandler)(void));
699 
700 //*****************************************************************************
701 //
702 //! \brief Unregisters an interrupt handler for a UART interrupt in the dynamic interrupt table.
703 //!
704 //! This function does the actual unregistering of the interrupt handler. It
705 //! clears the handler to be called when a UART interrupt occurs.  This
706 //! function also masks off the interrupt in the interrupt controller so that
707 //! the interrupt handler no longer is called.
708 //!
709 //! \param ui32Base is the base address of the UART module.
710 //!
711 //! \return None
712 //!
713 //! \sa \ref IntRegister() for important information about registering interrupt
714 //! handlers.
715 //
716 //*****************************************************************************
717 extern void UARTIntUnregister(uint32_t ui32Base);
718 
719 //*****************************************************************************
720 //
721 //! \brief Enables individual UART interrupt sources.
722 //!
723 //! This function enables the indicated UART interrupt sources. Only the
724 //! sources that are enabled can be reflected to the processor interrupt;
725 //! disabled sources have no effect on the processor.
726 //!
727 //! \param ui32Base is the base address of the UART port.
728 //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
729 //! The parameter is the bitwise OR of any of the following:
730 //! - \ref UART_INT_EOT : End Of Transmission interrupt.
731 //! - \ref UART_INT_OE  : Overrun Error interrupt.
732 //! - \ref UART_INT_BE  : Break Error interrupt.
733 //! - \ref UART_INT_PE  : Parity Error interrupt.
734 //! - \ref UART_INT_FE  : Framing Error interrupt.
735 //! - \ref UART_INT_RT  : Receive Timeout interrupt.
736 //! - \ref UART_INT_TX  : Transmit interrupt.
737 //! - \ref UART_INT_RX  : Receive interrupt.
738 //! - \ref UART_INT_CTS : CTS interrupt.
739 //!
740 //! \return None
741 //
742 //*****************************************************************************
743 __STATIC_INLINE void
UARTIntEnable(uint32_t ui32Base,uint32_t ui32IntFlags)744 UARTIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
745 {
746     // Check the arguments.
747     ASSERT(UARTBaseValid(ui32Base));
748 
749     // Enable the specified interrupts.
750     HWREG(ui32Base + UART_O_IMSC) |= ui32IntFlags;
751 }
752 
753 //*****************************************************************************
754 //
755 //! \brief Disables individual UART interrupt sources.
756 //!
757 //! This function disables the indicated UART interrupt sources. Only the
758 //! sources that are enabled can be reflected to the processor interrupt;
759 //! disabled sources have no effect on the processor.
760 //!
761 //! \param ui32Base is the base address of the UART port.
762 //! \param ui32IntFlags is the bit mask of the interrupt sources to be disabled.
763 //! - \ref UART_INT_EOT : End Of Transmission interrupt.
764 //! - \ref UART_INT_OE  : Overrun Error interrupt.
765 //! - \ref UART_INT_BE  : Break Error interrupt.
766 //! - \ref UART_INT_PE  : Parity Error interrupt.
767 //! - \ref UART_INT_FE  : Framing Error interrupt.
768 //! - \ref UART_INT_RT  : Receive Timeout interrupt.
769 //! - \ref UART_INT_TX  : Transmit interrupt.
770 //! - \ref UART_INT_RX  : Receive interrupt.
771 //! - \ref UART_INT_CTS : CTS interrupt.
772 //!
773 //! \return None
774 //
775 //*****************************************************************************
776 __STATIC_INLINE void
UARTIntDisable(uint32_t ui32Base,uint32_t ui32IntFlags)777 UARTIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
778 {
779     // Check the arguments.
780     ASSERT(UARTBaseValid(ui32Base));
781 
782     // Disable the specified interrupts.
783     HWREG(ui32Base + UART_O_IMSC) &= ~(ui32IntFlags);
784 }
785 
786 //*****************************************************************************
787 //
788 //! \brief Gets the current interrupt status.
789 //!
790 //! This function returns the interrupt status for the specified UART. Either
791 //! the raw interrupt status or the status of interrupts that are allowed to
792 //! reflect to the processor can be returned.
793 //!
794 //! \param ui32Base is the base address of the UART port.
795 //! \param bMasked selects either raw or masked interrupt.
796 //! - \c true  : Masked interrupt status is required.
797 //! - \c false : Raw interrupt status is required.
798 //!
799 //! \return Returns the current interrupt status, enumerated as a bit field of:
800 //! - \ref UART_INT_EOT : End Of Transmission interrupt.
801 //! - \ref UART_INT_OE  : Overrun Error interrupt.
802 //! - \ref UART_INT_BE  : Break Error interrupt.
803 //! - \ref UART_INT_PE  : Parity Error interrupt.
804 //! - \ref UART_INT_FE  : Framing Error interrupt.
805 //! - \ref UART_INT_RT  : Receive Timeout interrupt.
806 //! - \ref UART_INT_TX  : Transmit interrupt.
807 //! - \ref UART_INT_RX  : Receive interrupt.
808 //! - \ref UART_INT_CTS : CTS interrupt.
809 //
810 //*****************************************************************************
811 __STATIC_INLINE uint32_t
UARTIntStatus(uint32_t ui32Base,bool bMasked)812 UARTIntStatus(uint32_t ui32Base, bool bMasked)
813 {
814     // Check the arguments.
815     ASSERT(UARTBaseValid(ui32Base));
816 
817     // Return either the interrupt status or the raw interrupt status as
818     // requested.
819     if(bMasked)
820     {
821         return(HWREG(ui32Base + UART_O_MIS));
822     }
823     else
824     {
825         return(HWREG(ui32Base + UART_O_RIS));
826     }
827 }
828 
829 //*****************************************************************************
830 //
831 //! \brief Clears UART interrupt sources.
832 //!
833 //! The specified UART interrupt sources are cleared, so that they no longer
834 //! assert. This function must be called in the interrupt handler to keep the
835 //! interrupt from being recognized again immediately upon exit.
836 //!
837 //! \note Due to write buffers and synchronizers in the system it may take several
838 //! clock cycles from a register write clearing an event in a module and until the
839 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
840 //! clear the event source early in the interrupt service routine (ISR) to allow
841 //! the event clear to propagate to the NVIC before returning from the ISR.
842 //! At the same time, an early event clear allows new events of the same type to be
843 //! pended instead of ignored if the event is cleared later in the ISR.
844 //! It is the responsibility of the programmer to make sure that enough time has passed
845 //! before returning from the ISR to avoid false re-triggering of the cleared event.
846 //! A simple, although not necessarily optimal, way of clearing an event before
847 //! returning from the ISR is:
848 //! -# Write to clear event (interrupt source). (buffered write)
849 //! -# Dummy read from the event source module. (making sure the write has propagated)
850 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any synchronizers)
851 //!
852 //! \param ui32Base is the base address of the UART port.
853 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
854 //! - \ref UART_INT_EOT : End Of Transmission interrupt.
855 //! - \ref UART_INT_OE  : Overrun Error interrupt.
856 //! - \ref UART_INT_BE  : Break Error interrupt.
857 //! - \ref UART_INT_PE  : Parity Error interrupt.
858 //! - \ref UART_INT_FE  : Framing Error interrupt.
859 //! - \ref UART_INT_RT  : Receive Timeout interrupt.
860 //! - \ref UART_INT_TX  : Transmit interrupt.
861 //! - \ref UART_INT_RX  : Receive interrupt.
862 //! - \ref UART_INT_CTS : CTS interrupt.
863 //!
864 //! \return None
865 //
866 //*****************************************************************************
867 __STATIC_INLINE void
UARTIntClear(uint32_t ui32Base,uint32_t ui32IntFlags)868 UARTIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
869 {
870     // Check the arguments
871     ASSERT(UARTBaseValid(ui32Base));
872 
873     // Clear the requested interrupt sources
874     HWREG(ui32Base + UART_O_ICR) = ui32IntFlags;
875 }
876 
877 //*****************************************************************************
878 //
879 //! \brief Enable UART DMA operation.
880 //!
881 //! The specified UART DMA features are enabled. The UART can be
882 //! configured to use DMA for transmit or receive, and to disable
883 //! receive if an error occurs.
884 //!
885 //! \note The uDMA controller must also be set up before DMA can be used
886 //! with the UART.
887 //!
888 //! \param ui32Base is the base address of the UART port.
889 //! \param ui32DMAFlags is a bit mask of the DMA features to enable.
890 //! The parameter is the bitwise OR of any of the following values:
891 //! - UART_DMA_RX         : Enable DMA for receive.
892 //! - UART_DMA_TX         : Enable DMA for transmit.
893 //! - UART_DMA_ERR_RXSTOP : Disable DMA receive on UART error.
894 //!
895 //! \return None
896 //
897 //*****************************************************************************
898 __STATIC_INLINE void
UARTDMAEnable(uint32_t ui32Base,uint32_t ui32DMAFlags)899 UARTDMAEnable(uint32_t ui32Base, uint32_t ui32DMAFlags)
900 {
901     // Check the arguments.
902     ASSERT(UARTBaseValid(ui32Base));
903 
904     // Set the requested bits in the UART DMA control register.
905     HWREG(ui32Base + UART_O_DMACTL) |= ui32DMAFlags;
906 }
907 
908 //*****************************************************************************
909 //
910 //! \brief Disable UART DMA operation.
911 //!
912 //! This function is used to disable UART DMA features that were enabled
913 //! by \ref UARTDMAEnable(). The specified UART DMA features are disabled.
914 //!
915 //! \param ui32Base is the base address of the UART port.
916 //! \param ui32DMAFlags is a bit mask of the DMA features to disable.
917 //! The parameter is the bitwise OR of any of the following values:
918 //! - UART_DMA_RX         : Enable DMA for receive.
919 //! - UART_DMA_TX         : Enable DMA for transmit.
920 //! - UART_DMA_ERR_RXSTOP : Disable DMA receive on UART error.
921 //!
922 //! \return None
923 //
924 //*****************************************************************************
925 __STATIC_INLINE void
UARTDMADisable(uint32_t ui32Base,uint32_t ui32DMAFlags)926 UARTDMADisable(uint32_t ui32Base, uint32_t ui32DMAFlags)
927 {
928     // Check the arguments.
929     ASSERT(UARTBaseValid(ui32Base));
930 
931     // Clear the requested bits in the UART DMA control register.
932     HWREG(ui32Base + UART_O_DMACTL) &= ~ui32DMAFlags;
933 }
934 
935 //*****************************************************************************
936 //
937 //! \brief Gets current receiver errors.
938 //!
939 //! This function returns the current state of each of the 4 receiver error
940 //! sources. The returned errors are equivalent to the four error bits
941 //! returned via the previous call to \ref UARTCharGet() or \ref UARTCharGetNonBlocking()
942 //! with the exception that the overrun error is set immediately the overrun
943 //! occurs rather than when a character is next read.
944 //!
945 //! \param ui32Base is the base address of the UART port.
946 //!
947 //! \return Returns a bitwise OR combination of the receiver error flags:
948 //! - \ref UART_RXERROR_FRAMING
949 //! - \ref UART_RXERROR_PARITY
950 //! - \ref UART_RXERROR_BREAK
951 //! - \ref UART_RXERROR_OVERRUN
952 //
953 //*****************************************************************************
954 __STATIC_INLINE uint32_t
UARTRxErrorGet(uint32_t ui32Base)955 UARTRxErrorGet(uint32_t ui32Base)
956 {
957     // Check the arguments.
958     ASSERT(UARTBaseValid(ui32Base));
959 
960     // Return the current value of the receive status register.
961     return(HWREG(ui32Base + UART_O_RSR) & 0x0000000F);
962 }
963 
964 //*****************************************************************************
965 //
966 //! \brief Clears all reported receiver errors.
967 //!
968 //! This function is used to clear all receiver error conditions reported via
969 //! \ref UARTRxErrorGet(). If using the overrun, framing error, parity error or
970 //! break interrupts, this function must be called after clearing the interrupt
971 //! to ensure that later errors of the same type trigger another interrupt.
972 //!
973 //! \param ui32Base is the base address of the UART port.
974 //!
975 //! \return None
976 //
977 //*****************************************************************************
978 __STATIC_INLINE void
UARTRxErrorClear(uint32_t ui32Base)979 UARTRxErrorClear(uint32_t ui32Base)
980 {
981     // Check the arguments.
982     ASSERT(UARTBaseValid(ui32Base));
983 
984     // Any write to the Error Clear Register will clear all bits which are
985     // currently set.
986     HWREG(ui32Base + UART_O_ECR) = 0;
987 }
988 
989 //*****************************************************************************
990 //
991 //! \brief Enables hardware flow control for both CTS and RTS
992 //!
993 //! Hardware flow control is disabled by default.
994 //!
995 //! \param ui32Base is the base address of the UART port.
996 //!
997 //! \return None
998 //
999 //*****************************************************************************
1000 __STATIC_INLINE void
UARTHwFlowControlEnable(uint32_t ui32Base)1001 UARTHwFlowControlEnable( uint32_t ui32Base )
1002 {
1003     // Check the arguments.
1004     ASSERT( UARTBaseValid( ui32Base ));
1005 
1006     HWREG( ui32Base + UART_O_CTL ) |= ( UART_CTL_CTSEN | UART_CTL_RTSEN );
1007 }
1008 
1009 //*****************************************************************************
1010 //
1011 //! \brief Disables hardware flow control for both CTS and RTS
1012 //!
1013 //! Hardware flow control is disabled by default.
1014 //!
1015 //! \param ui32Base is the base address of the UART port.
1016 //!
1017 //! \return None
1018 //
1019 //*****************************************************************************
1020 __STATIC_INLINE void
UARTHwFlowControlDisable(uint32_t ui32Base)1021 UARTHwFlowControlDisable( uint32_t ui32Base )
1022 {
1023     // Check the arguments.
1024     ASSERT( UARTBaseValid( ui32Base ));
1025 
1026     HWREG( ui32Base + UART_O_CTL ) &= ~( UART_CTL_CTSEN | UART_CTL_RTSEN );
1027 }
1028 
1029 
1030 //*****************************************************************************
1031 //
1032 // Support for DriverLib in ROM:
1033 // Redirect to implementation in ROM when available.
1034 //
1035 //*****************************************************************************
1036 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
1037     #include "../driverlib/rom.h"
1038     #ifdef ROM_UARTFIFOLevelGet
1039         #undef  UARTFIFOLevelGet
1040         #define UARTFIFOLevelGet                ROM_UARTFIFOLevelGet
1041     #endif
1042     #ifdef ROM_UARTConfigSetExpClk
1043         #undef  UARTConfigSetExpClk
1044         #define UARTConfigSetExpClk             ROM_UARTConfigSetExpClk
1045     #endif
1046     #ifdef ROM_UARTConfigGetExpClk
1047         #undef  UARTConfigGetExpClk
1048         #define UARTConfigGetExpClk             ROM_UARTConfigGetExpClk
1049     #endif
1050     #ifdef ROM_UARTDisable
1051         #undef  UARTDisable
1052         #define UARTDisable                     ROM_UARTDisable
1053     #endif
1054     #ifdef ROM_UARTCharGetNonBlocking
1055         #undef  UARTCharGetNonBlocking
1056         #define UARTCharGetNonBlocking          ROM_UARTCharGetNonBlocking
1057     #endif
1058     #ifdef ROM_UARTCharGet
1059         #undef  UARTCharGet
1060         #define UARTCharGet                     ROM_UARTCharGet
1061     #endif
1062     #ifdef ROM_UARTCharPutNonBlocking
1063         #undef  UARTCharPutNonBlocking
1064         #define UARTCharPutNonBlocking          ROM_UARTCharPutNonBlocking
1065     #endif
1066     #ifdef ROM_UARTCharPut
1067         #undef  UARTCharPut
1068         #define UARTCharPut                     ROM_UARTCharPut
1069     #endif
1070     #ifdef ROM_UARTIntRegister
1071         #undef  UARTIntRegister
1072         #define UARTIntRegister                 ROM_UARTIntRegister
1073     #endif
1074     #ifdef ROM_UARTIntUnregister
1075         #undef  UARTIntUnregister
1076         #define UARTIntUnregister               ROM_UARTIntUnregister
1077     #endif
1078 #endif
1079 
1080 //*****************************************************************************
1081 //
1082 // Mark the end of the C bindings section for C++ compilers.
1083 //
1084 //*****************************************************************************
1085 #ifdef __cplusplus
1086 }
1087 #endif
1088 
1089 #endif //  __UART_H__
1090 
1091 //*****************************************************************************
1092 //
1093 //! Close the Doxygen group.
1094 //! @}
1095 //! @}
1096 //
1097 //*****************************************************************************
1098