CC23x0R5DriverLibrary
uart.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  * Filename: uart.h
4  *
5  * Description: Defines and prototypes for the UART peripheral.
6  *
7  * Copyright (c) 2022-2023 Texas Instruments Incorporated
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 copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without 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 #ifndef __UART_H__
38 #define __UART_H__
39 
40 //*****************************************************************************
41 //
46 //
47 //*****************************************************************************
48 
49 #include <stdbool.h>
50 #include <stdint.h>
51 #include "../inc/hw_types.h"
52 #include "../inc/hw_uart.h"
53 #include "../inc/hw_memmap.h"
54 #include "../inc/hw_ints.h"
55 #include "interrupt.h"
56 #include "debug.h"
57 
58 //*****************************************************************************
59 //
60 // If building with a C++ compiler, make all of the definitions in this header
61 // have a C binding.
62 //
63 //*****************************************************************************
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 //*****************************************************************************
69 //
70 // Values that can be passed to UARTEnableInt, UARTDisableInt, and UARTClearInt
71 // as the intFlags parameter, and returned from UARTIntStatus.
72 //
73 //*****************************************************************************
74 #define UART_INT_EOT (UART_IMSC_EOT)
75 #define UART_INT_OE (UART_IMSC_OE)
76 #define UART_INT_BE (UART_IMSC_BE)
77 #define UART_INT_PE (UART_IMSC_PE)
78 #define UART_INT_FE (UART_IMSC_FE)
79 #define UART_INT_RT (UART_IMSC_RT)
80 #define UART_INT_RX (UART_IMSC_TX)
81 #define UART_INT_TX (UART_IMSC_RX)
82 #define UART_INT_CTS (UART_IMSC_CTSM)
83 #define UART_INT_TXDMADONE (UART_IMSC_TXDMADONE)
84 #define UART_INT_RXDMADONE (UART_IMSC_RXDMADONE)
85 
86 //*****************************************************************************
87 //
88 // Values that can be passed to UARTConfigSetExpClk as the config parameter
89 // and returned by UARTConfigGetExpClk in the pconfig parameter.
90 //
91 //*****************************************************************************
92 #define UART_CONFIG_WLEN_MASK 0x00000060
93 #define UART_CONFIG_WLEN_8 0x00000060
94 #define UART_CONFIG_WLEN_7 0x00000040
95 #define UART_CONFIG_WLEN_6 0x00000020
96 #define UART_CONFIG_WLEN_5 0x00000000
97 #define UART_CONFIG_STOP_MASK 0x00000008
98 #define UART_CONFIG_STOP_ONE 0x00000000
99 #define UART_CONFIG_STOP_TWO 0x00000008
100 #define UART_CONFIG_PAR_MASK 0x00000086
101 #define UART_CONFIG_PAR_NONE 0x00000000
102 #define UART_CONFIG_PAR_EVEN 0x00000006
103 #define UART_CONFIG_PAR_ODD 0x00000002
104 #define UART_CONFIG_PAR_ONE 0x00000082
105 #define UART_CONFIG_PAR_ZERO 0x00000086
106 
107 //*****************************************************************************
108 //
109 // Values that can be passed to UARTSetFifoLevel as the txLevel parameter
110 //
111 //*****************************************************************************
112 #define UART_FIFO_TX2_8 0x00000001
113 #define UART_FIFO_TX4_8 0x00000002
114 #define UART_FIFO_TX6_8 0x00000003
115 
116 //*****************************************************************************
117 //
118 // Values that can be passed to UARTSetFifoLevel as the rxLevel parameter
119 //
120 //*****************************************************************************
121 #define UART_FIFO_RX2_8 0x00000008
122 #define UART_FIFO_RX4_8 0x00000010
123 #define UART_FIFO_RX6_8 0x00000018
124 
125 //*****************************************************************************
126 //
127 // Values that can be passed to UARTEnableDMA() and UARTDisableDMA().
128 //
129 //*****************************************************************************
130 #define UART_DMA_ERR_RXSTOP 0x00000004
131 #define UART_DMA_TX 0x00000002
132 #define UART_DMA_RX 0x00000001
133 
134 //*****************************************************************************
135 //
136 // Values returned from UARTGetRxError().
137 //
138 //*****************************************************************************
139 #define UART_RXERROR_OVERRUN 0x00000008
140 #define UART_RXERROR_BREAK 0x00000004
141 #define UART_RXERROR_PARITY 0x00000002
142 #define UART_RXERROR_FRAMING 0x00000001
143 
144 //*****************************************************************************
145 //
146 // API Functions and prototypes
147 //
148 //*****************************************************************************
149 
150 //*****************************************************************************
151 //
168 //
169 //*****************************************************************************
170 __STATIC_INLINE void UARTSetFifoLevel(uint32_t base, uint32_t txLevel, uint32_t rxLevel)
171 {
172  // Check the arguments.
173  ASSERT((txLevel == UART_FIFO_TX2_8) || (txLevel == UART_FIFO_TX4_8) || (txLevel == UART_FIFO_TX6_8));
174  ASSERT((rxLevel == UART_FIFO_RX2_8) || (rxLevel == UART_FIFO_RX4_8) || (rxLevel == UART_FIFO_RX6_8));
175 
176  // Set the FIFO interrupt levels.
177  HWREG(base + UART_O_IFLS) = txLevel | rxLevel;
178 }
179 
180 //*****************************************************************************
181 //
210 //
211 //*****************************************************************************
212 extern void UARTConfigSetExpClk(uint32_t base, uint32_t UARTClkFreq, uint32_t baudFreq, uint32_t config);
213 
214 //*****************************************************************************
215 //
224 //
225 //*****************************************************************************
226 extern void UARTDisable(uint32_t base);
227 
228 //*****************************************************************************
229 //
237 //
238 //*****************************************************************************
239 __STATIC_INLINE void UARTEnableFifo(uint32_t base)
240 {
241  // Enable the FIFO.
242  HWREG(base + UART_O_LCRH) |= UART_LCRH_FEN;
243 }
244 
245 //*****************************************************************************
246 //
254 //
255 //*****************************************************************************
256 __STATIC_INLINE void UARTDisableFIFO(uint32_t base)
257 {
258  // Disable the FIFO.
259  HWREG(base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
260 }
261 
262 //*****************************************************************************
263 //
274 //
275 //*****************************************************************************
277 {
278  // Return the availability of characters.
279  return ((HWREG(base + UART_O_FR) & UART_FR_RXFE) ? false : true);
280 }
281 
282 //*****************************************************************************
283 //
297 //
298 //*****************************************************************************
300 {
301  // Return a character from the data register
302  return (HWREGB(base + UART_O_DR));
303 }
304 
305 //*****************************************************************************
306 //
316 //
317 //*****************************************************************************
318 extern uint8_t UARTGetChar(uint32_t base);
319 
320 //*****************************************************************************
321 //
332 //
333 //*****************************************************************************
335 {
336  // Return the availability of space.
337  return ((HWREG(base + UART_O_FR) & UART_FR_TXFF) ? false : true);
338 }
339 
340 //*****************************************************************************
341 //
356 //
357 //*****************************************************************************
358 __STATIC_INLINE void UARTPutCharNonBlocking(uint32_t base, uint8_t data)
359 {
360  // Write this character to the transmit FIFO.
361  HWREG(base + UART_O_DR) = data;
362 }
363 
364 //*****************************************************************************
365 //
376 //
377 //*****************************************************************************
378 extern void UARTPutChar(uint32_t base, uint8_t data);
379 
380 //*****************************************************************************
381 //
394 //
395 //*****************************************************************************
396 __STATIC_INLINE bool UARTBusy(uint32_t base)
397 {
398  // Determine if the UART is busy.
399  return ((HWREG(base + UART_O_FR) & UART_FR_BUSY) ? true : false);
400 }
401 
402 //*****************************************************************************
403 //
426 //
427 //*****************************************************************************
428 __STATIC_INLINE void UARTEnableInt(uint32_t base, uint32_t intFlags)
429 {
430  // Enable the specified interrupts.
431  HWREG(base + UART_O_IMSC) |= intFlags;
432 }
433 
434 //*****************************************************************************
435 //
457 //
458 //*****************************************************************************
459 __STATIC_INLINE void UARTDisableInt(uint32_t base, uint32_t intFlags)
460 {
461  // Disable the specified interrupts.
462  HWREG(base + UART_O_IMSC) &= ~(intFlags);
463 }
464 
465 //*****************************************************************************
466 //
490 //
491 //*****************************************************************************
492 __STATIC_INLINE uint32_t UARTIntStatus(uint32_t base, bool masked)
493 {
494  // Return either the interrupt status or the raw interrupt status as
495  // requested.
496  if (masked)
497  {
498  return (HWREG(base + UART_O_MIS));
499  }
500  else
501  {
502  return (HWREG(base + UART_O_RIS));
503  }
504 }
505 
506 //*****************************************************************************
507 //
545 //
546 //*****************************************************************************
547 __STATIC_INLINE void UARTClearInt(uint32_t base, uint32_t intFlags)
548 {
549  // Clear the requested interrupt sources
550  HWREG(base + UART_O_ICR) = intFlags;
551 }
552 
553 //*****************************************************************************
554 //
572 //
573 //*****************************************************************************
574 __STATIC_INLINE void UARTEnableDMA(uint32_t base, uint32_t dmaFlags)
575 {
576  // Set the requested bits in the UART DMA control register.
577  HWREG(base + UART_O_DMACTL) |= dmaFlags;
578 }
579 
580 //*****************************************************************************
581 //
595 //
596 //*****************************************************************************
597 __STATIC_INLINE void UARTDisableDMA(uint32_t base, uint32_t dmaFlags)
598 {
599  // Clear the requested bits in the UART DMA control register.
600  HWREG(base + UART_O_DMACTL) &= ~dmaFlags;
601 }
602 
603 //*****************************************************************************
604 //
620 //
621 //*****************************************************************************
622 __STATIC_INLINE uint32_t UARTGetRxError(uint32_t base)
623 {
624  // Return the current value of the receive status register.
625  return (HWREG(base + UART_O_RSR_ECR) & 0x0000000F);
626 }
627 
628 //*****************************************************************************
629 //
640 //
641 //*****************************************************************************
643 {
644  // Any write to the Error Clear Register will clear all bits which are
645  // currently set.
646  HWREG(base + UART_O_RSR_ECR) = 0;
647 }
648 
649 //*****************************************************************************
650 //
658 //
659 //*****************************************************************************
660 __STATIC_INLINE void UARTEnableCTS(uint32_t base)
661 {
662  HWREG(base + UART_O_CTL) |= (UART_CTL_CTSEN);
663 }
664 
665 //*****************************************************************************
666 //
674 //
675 //*****************************************************************************
676 __STATIC_INLINE void UARTEnableRTS(uint32_t base)
677 {
678  HWREG(base + UART_O_CTL) |= (UART_CTL_RTSEN);
679 }
680 
681 //*****************************************************************************
682 //
690 //
691 //*****************************************************************************
692 __STATIC_INLINE void UARTDisableCTS(uint32_t base)
693 {
694  HWREG(base + UART_O_CTL) &= ~(UART_CTL_CTSEN);
695 }
696 
697 //*****************************************************************************
698 //
706 //
707 //*****************************************************************************
708 __STATIC_INLINE void UARTDisableRTS(uint32_t base)
709 {
710  HWREG(base + UART_O_CTL) &= ~(UART_CTL_RTSEN);
711 }
712 
713 //*****************************************************************************
714 //
715 // Mark the end of the C bindings section for C++ compilers.
716 //
717 //*****************************************************************************
718 #ifdef __cplusplus
719 }
720 #endif
721 
722 //*****************************************************************************
723 //
727 //
728 //*****************************************************************************
729 
730 #endif // __UART_H__
__STATIC_INLINE void UARTPutCharNonBlocking(uint32_t base, uint8_t data)
Sends a character to the specified port.
Definition: uart.h:358
#define UART_FIFO_RX2_8
Receive interrupt at 1/4 Full.
Definition: uart.h:121
__STATIC_INLINE uint32_t UARTIntStatus(uint32_t base, bool masked)
Gets the current interrupt status.
Definition: uart.h:492
__STATIC_INLINE uint8_t UARTGetCharNonBlocking(uint32_t base)
Receives a character from the specified port.
Definition: uart.h:299
__STATIC_INLINE void UARTSetFifoLevel(uint32_t base, uint32_t txLevel, uint32_t rxLevel)
Sets the FIFO level at which interrupts are generated.
Definition: uart.h:170
__STATIC_INLINE void UARTEnableInt(uint32_t base, uint32_t intFlags)
Enables individual UART interrupt sources.
Definition: uart.h:428
#define HWREG(x)
Definition: hw_types.h:79
#define __STATIC_INLINE
Definition: hw_types.h:58
#define UART_FIFO_RX6_8
Receive interrupt at 3/4 Full.
Definition: uart.h:123
#define UART_O_LCRH
Definition: hw_uart.h:61
#define UART_LCRH_FEN
Definition: hw_uart.h:393
__STATIC_INLINE bool UARTBusy(uint32_t base)
Determines whether the UART transmitter is busy or not.
Definition: uart.h:396
#define UART_O_DMACTL
Definition: hw_uart.h:82
#define UART_FR_BUSY
Definition: hw_uart.h:280
#define UART_O_MIS
Definition: hw_uart.h:76
__STATIC_INLINE void UARTDisableCTS(uint32_t base)
Disable CTS flow control.
Definition: uart.h:692
__STATIC_INLINE void UARTDisableFIFO(uint32_t base)
Disables the transmit and receive FIFOs.
Definition: uart.h:256
__STATIC_INLINE void UARTClearInt(uint32_t base, uint32_t intFlags)
Clears UART interrupt sources.
Definition: uart.h:547
void UARTDisable(uint32_t base)
Disables transmitting and receiving.
Definition: uart.c:69
__STATIC_INLINE void UARTEnableFifo(uint32_t base)
Enables the transmit and receive FIFOs.
Definition: uart.h:239
#define UART_O_RIS
Definition: hw_uart.h:73
#define UART_O_IFLS
Definition: hw_uart.h:67
__STATIC_INLINE void UARTDisableDMA(uint32_t base, uint32_t dmaFlags)
Disable UART DMA operation.
Definition: uart.h:597
#define ASSERT(expr)
Definition: debug.h:71
void UARTPutChar(uint32_t base, uint8_t data)
Waits to send a character to the specified port.
Definition: uart.c:100
#define UART_FIFO_TX6_8
Transmit interrupt at 3/4 Full.
Definition: uart.h:114
#define UART_O_DR
Definition: hw_uart.h:43
__STATIC_INLINE bool UARTSpaceAvailable(uint32_t base)
Determines if there is any space in the transmit FIFO.
Definition: uart.h:334
#define UART_FIFO_TX4_8
Transmit interrupt at 1/2 Full.
Definition: uart.h:113
uint8_t UARTGetChar(uint32_t base)
Waits for a character from the specified port.
Definition: uart.c:86
#define UART_FIFO_RX4_8
Receive interrupt at 1/2 Full.
Definition: uart.h:122
#define UART_O_CTL
Definition: hw_uart.h:64
#define UART_CTL_CTSEN
Definition: hw_uart.h:459
__STATIC_INLINE void UARTDisableInt(uint32_t base, uint32_t intFlags)
Disables individual UART interrupt sources.
Definition: uart.h:459
#define UART_O_IMSC
Definition: hw_uart.h:70
__STATIC_INLINE bool UARTCharAvailable(uint32_t base)
Determines if there are any characters in the receive FIFO.
Definition: uart.h:276
#define UART_FIFO_TX2_8
Transmit interrupt at 1/4 Full.
Definition: uart.h:112
void UARTConfigSetExpClk(uint32_t base, uint32_t UARTClkFreq, uint32_t baudFreq, uint32_t config)
Sets the configuration of a UART.
Definition: uart.c:43
#define UART_CTL_RTSEN
Definition: hw_uart.h:471
#define UART_O_ICR
Definition: hw_uart.h:79
#define UART_O_RSR_ECR
Definition: hw_uart.h:46
__STATIC_INLINE void UARTEnableDMA(uint32_t base, uint32_t dmaFlags)
Enable UART DMA operation.
Definition: uart.h:574
__STATIC_INLINE void UARTClearRxError(uint32_t base)
Clears all reported receiver errors.
Definition: uart.h:642
#define UART_FR_RXFE
Definition: hw_uart.h:268
__STATIC_INLINE uint32_t UARTGetRxError(uint32_t base)
Gets current receiver errors.
Definition: uart.h:622
#define UART_O_FR
Definition: hw_uart.h:49
__STATIC_INLINE void UARTEnableRTS(uint32_t base)
Enable RTS flow control.
Definition: uart.h:676
__STATIC_INLINE void UARTEnableCTS(uint32_t base)
Enable CTS flow control.
Definition: uart.h:660
__STATIC_INLINE void UARTDisableRTS(uint32_t base)
Disable RTS flow control.
Definition: uart.h:708
#define HWREGB(x)
Definition: hw_types.h:91
#define UART_FR_TXFF
Definition: hw_uart.h:256