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