UART.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2021, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  * @file UART.h
34  * @brief Universal Asynchronous Receiver-Transmitter (UART) Driver
35  *
36  * @warning "The UART1 driver will be removed in the 2Q22 release.
37  * Please port your application to the UART2 driver."
38  *
39  * To use the UART driver, ensure that the correct driver library for your
40  * device is linked in and include this header file as follows:
41  * @code
42  * #include <ti/drivers/UART.h>
43  * @endcode
44  *
45  * This module serves as the main interface for applications. Its purpose
46  * is to redirect the UART APIs to specific driver implementations
47  * which are specified using a pointer to a #UART_FxnTable.
48  *
49  * @anchor ti_drivers_UART_Overview
50  * # Overview
51  * A UART is used to translate data between the chip and a serial port.
52  * The UART driver simplifies reading and writing to any of the UART
53  * peripherals on the board, with multiple modes of operation and performance.
54  * These include blocking, non-blocking, and polling, as well as text/binary
55  * mode, echo and return characters.
56  *
57  * The UART driver interface provides device independent APIs, data types,
58  * and macros. The APIs in this driver serve as an interface to a typical RTOS
59  * application. The specific peripheral implementations are responsible for
60  * creating all the RTOS specific primitives to allow for thread-safe
61  * operation.
62  *
63  * <hr>
64  * @anchor ti_drivers_UART_Usage
65  * # Usage
66  *
67  * This documentation provides a basic @ref ti_drivers_UART_Synopsis
68  * "usage summary" and a set of @ref ti_drivers_UART_Examples "examples"
69  * in the form of commented code fragments. Detailed descriptions of the
70  * APIs are provided in subsequent sections.
71  *
72  * @anchor ti_drivers_UART_Synopsis
73  * ## Synopsis
74  * @anchor ti_drivers_UART_Synopsis_Code
75  * @code
76  * // Import the UART driver definitions
77  * #include <ti/drivers/UART.h>
78  *
79  * // One-time initialization of UART driver
80  * UART_init();
81  *
82  * // Initialize UART parameters
83  * UART_Params params;
84  * UART_Params_init(&params);
85  * params.baudRate = 9600;
86  * params.readMode = UART_MODE_BLOCKING;
87  * params.writeMode = UART_MODE_BLOCKING;
88  * params.readTimeout = UART_WAIT_FOREVER;
89  * params.writeTimeout = UART_WAIT_FOREVER;
90  *
91  * // Open the UART
92  * UART_Handle uart;
93  * uart = UART_open(CONFIG_UART0, &params);
94  *
95  * // Read from the UART
96  * int32_t readCount;
97  * uint8_t buffer[BUFSIZE];
98  * readCount = UART_read(uart, buffer, BUFSIZE);
99  *
100  * // Write to the UART
101  * UART_write(uart, buffer, BUFSIZE);
102  *
103  * // Close the UART
104  * UART_close(uart);
105  * @endcode
106  *
107  * <hr>
108  * @anchor ti_drivers_UART_Examples
109  * # Examples
110  * The following code example opens a UART instance, reads
111  * a byte from the UART, and then writes the byte back to the UART.
112  *
113  * @code
114  * char input;
115  * UART_Handle uart;
116  * UART_Params uartParams;
117  *
118  * // Initialize the UART driver. UART_init() must be called before
119  * // calling any other UART APIs.
120  * UART_init();
121  *
122  * // Create a UART with data processing off.
123  * UART_Params_init(&uartParams);
124  * uartParams.writeDataMode = UART_DATA_BINARY;
125  * uartParams.readDataMode = UART_DATA_BINARY;
126  * uartParams.readReturnMode = UART_RETURN_FULL;
127  * uartParams.baudRate = 115200;
128  *
129  * // Open an instance of the UART drivers
130  * uart = UART_open(CONFIG_UART0, &uartParams);
131  *
132  * if (uart == NULL) {
133  * // UART_open() failed
134  * while (1);
135  * }
136  *
137  * // Loop forever echoing
138  * while (1) {
139  * UART_read(uart, &input, 1);
140  * UART_write(uart, &input, 1);
141  * }
142  * @endcode
143  *
144  * Details for the example code above are described in the following
145  * subsections.
146  *
147  * ### Opening the UART Driver #
148  *
149  * Opening a UART requires four steps:
150  * 1. Create and initialize a UART_Params structure.
151  * 2. Fill in the desired parameters.
152  * 3. Call UART_open(), passing the index of the UART in the UART_config
153  * structure, and the address of the UART_Params structure. The
154  * UART instance is specified by the index in the UART_config structure.
155  * 4. Check that the UART handle returned by UART_open() is non-NULL,
156  * and save it. The handle will be used to read and write to the
157  * UART you just opened.
158  *
159  * Only one UART index can be used at a time; calling UART_open() a second
160  * time with the same index previosly passed to UART_open() will result in
161  * an error. You can, though, re-use the index if the instance is closed
162  * via UART_close().
163  * In the example code, CONFIG_UART0 is passed to UART_open(). This macro
164  * is defined in the example's ti_drivers_config.h file.
165  *
166  *
167  * ### Modes of Operation #
168  *
169  * The UART driver can operate in blocking mode or callback mode, by
170  * setting the writeMode and readMode parameters passed to UART_open().
171  * If these parameters are not set, as in the example code, the UART
172  * driver defaults to blocking mode. Options for the writeMode and
173  * readMode parameters are #UART_MODE_BLOCKING and #UART_MODE_CALLBACK:
174  *
175  * - #UART_MODE_BLOCKING uses a semaphore to block while data is being sent.
176  * The context of calling UART_read() or UART_write() must be a Task when
177  * using #UART_MODE_BLOCKING. The UART_write() or UART_read() call
178  * will block until all data is sent or received, or the write timeout or
179  * read timeout expires, whichever happens first.
180  *
181  * - #UART_MODE_CALLBACK is non-blocking and UART_read() and UART_write()
182  * will return while data is being sent in the context of a hardware
183  * interrupt. When the read or write finishes, the UART driver will call
184  * the user's callback function. In some cases, the UART data transfer
185  * may have been canceled, or a newline may have been received, so the
186  * number of bytes sent/received are passed to the callback function. Your
187  * implementation of the callback function can use this information
188  * as needed. Since the user's callback may be called in the context of an
189  * ISR, the callback function must not make any RTOS blocking calls.
190  * The buffer passed to UART_write() in #UART_MODE_CALLBACK is not copied.
191  * The buffer must remain coherent until all the characters have been sent
192  * (ie until the tx callback has been called with a byte count equal to
193  * that passed to UART_write()).
194  *
195  * The example sets the writeDataMode and readDataMode parameters to
196  * #UART_DATA_BINARY. Options for these parameters are #UART_DATA_BINARY
197  * and #UART_DATA_TEXT:
198  *
199  * - #UART_DATA_BINARY: The data is passed as is, without processing.
200  *
201  * - #UART_DATA_TEXT: Write actions add a carriage return before a
202  * newline character, and read actions replace a return with a newline.
203  * This effectively treats all device line endings as LF and all host
204  * PC line endings as CRLF.
205  *
206  * Other parameters set by the example are readReturnMode and readEcho.
207  * Options for the readReturnMode parameter are #UART_RETURN_FULL and
208  * #UART_RETURN_NEWLINE:
209  *
210  * - #UART_RETURN_FULL: The read action unblocks or returns when the buffer
211  * is full.
212  * - #UART_RETURN_NEWLINE: The read action unblocks or returns when a
213  * newline character is read, before the buffer is full.
214  *
215  * Options for the readEcho parameter are #UART_ECHO_OFF and #UART_ECHO_ON.
216  * This parameter determines whether the driver echoes data back to the
217  * UART. When echo is turned on, each character that is read by the target
218  * is written back, independent of any write operations. If data is
219  * received in the middle of a write and echo is turned on, the echoed
220  * characters will be mixed in with the write data.
221  *
222  * ### Reading and Writing data #
223  *
224  * The example code reads one byte frome the UART instance, and then writes
225  * one byte back to the same instance:
226  *
227  * @code
228  * UART_read(uart, &input, 1);
229  * UART_write(uart, &input, 1);
230  * @endcode
231  *
232  * The UART driver allows full duplex data transfers. Therefore, it is
233  * possible to call UART_read() and UART_write() at the same time (for
234  * either blocking or callback modes). It is not possible, however,
235  * to issue multiple concurrent operations in the same direction.
236  * For example, if one thread calls UART_read(uart0, buffer0...),
237  * any other thread attempting UART_read(uart0, buffer1...) will result in
238  * an error of UART_STATUS_ERROR, until all the data from the first UART_read()
239  * has been transferred to buffer0. This applies to both blocking and
240  * and callback modes. So applications must either synchronize
241  * UART_read() (or UART_write()) calls that use the same UART handle, or
242  * check for the UART_STATUS_ERROR return code indicating that a transfer is
243  * still ongoing.
244  *
245  * <hr>
246  * @anchor ti_drivers_UART_Configuration
247  * # Configuration
248  *
249  * Refer to the @ref driver_configuration "Driver's Configuration" section
250  * for driver configuration information.
251  * <hr>
252  *
253  * ============================================================================
254  */
255 
256 #ifndef ti_drivers_UART__include
257 #define ti_drivers_UART__include
258 
259 #include <stddef.h>
260 #include <stdint.h>
261 
262 #ifdef __cplusplus
263 extern "C" {
264 #endif
265 
283 #define UART_CMD_RESERVED (32)
284 
297 #define UART_STATUS_RESERVED (-32)
298 
312 #define UART_STATUS_SUCCESS (0)
313 
320 #define UART_STATUS_ERROR (-1)
321 
329 #define UART_STATUS_UNDEFINEDCMD (-2)
330 
348 #define UART_CMD_PEEK (0)
349 
359 #define UART_CMD_ISAVAILABLE (1)
360 
370 #define UART_CMD_GETRXCOUNT (2)
371 
382 #define UART_CMD_RXENABLE (3)
383 
395 #define UART_CMD_RXDISABLE (4)
396 
400 #define UART_ERROR (UART_STATUS_ERROR)
401 
405 #define UART_WAIT_FOREVER (~(0U))
406 
410 typedef struct UART_Config_ *UART_Handle;
411 
423 typedef void (*UART_Callback) (UART_Handle handle, void *buf, size_t count);
424 
430 typedef enum {
436 
443 } UART_Mode;
444 
463 typedef enum {
466 
470 
485 typedef enum {
488 } UART_DataMode;
489 
503 typedef enum {
506 } UART_Echo;
507 
513 typedef enum {
518 } UART_LEN;
519 
525 typedef enum {
528 } UART_STOP;
529 
535 typedef enum {
541 } UART_PAR;
542 
551 typedef struct {
552  UART_Mode readMode;
553  UART_Mode writeMode;
554  uint32_t readTimeout;
555  uint32_t writeTimeout;
558  UART_ReturnMode readReturnMode;
562  uint32_t baudRate;
566  void *custom;
567 } UART_Params;
568 
573 typedef void (*UART_CloseFxn) (UART_Handle handle);
574 
579 typedef int_fast16_t (*UART_ControlFxn) (UART_Handle handle, uint_fast16_t cmd, void *arg);
580 
585 typedef void (*UART_InitFxn) (UART_Handle handle);
586 
591 typedef UART_Handle (*UART_OpenFxn) (UART_Handle handle, UART_Params *params);
596 typedef int_fast32_t (*UART_ReadFxn) (UART_Handle handle, void *buffer,
597  size_t size);
598 
603 typedef int_fast32_t (*UART_ReadPollingFxn) (UART_Handle handle, void *buffer,
604  size_t size);
605 
610 typedef void (*UART_ReadCancelFxn) (UART_Handle handle);
611 
616 typedef int_fast32_t (*UART_WriteFxn) (UART_Handle handle, const void *buffer,
617  size_t size);
618 
623 typedef int_fast32_t (*UART_WritePollingFxn) (UART_Handle handle,
624  const void *buffer, size_t size);
625 
630 typedef void (*UART_WriteCancelFxn) (UART_Handle handle);
631 
637 typedef struct {
640 
643 
646 
649 
652 
655 
658 
661 
664 
667 } UART_FxnTable;
668 
680 typedef struct UART_Config_ {
683 
685  void *object;
686 
688  void const *hwAttrs;
689 } UART_Config;
690 
702 extern void UART_close(UART_Handle handle);
703 
741 extern int_fast16_t UART_control(UART_Handle handle, uint_fast16_t cmd, void *arg);
742 
750 extern void UART_init(void);
751 
773 extern UART_Handle UART_open(uint_least8_t index, UART_Params *params);
774 
797 extern void UART_Params_init(UART_Params *params);
798 
838 extern int_fast32_t UART_write(UART_Handle handle, const void *buffer, size_t size);
839 
863 extern int_fast32_t UART_writePolling(UART_Handle handle, const void *buffer, size_t size);
864 
876 extern void UART_writeCancel(UART_Handle handle);
877 
911 extern int_fast32_t UART_read(UART_Handle handle, void *buffer, size_t size);
912 
933 extern int_fast32_t UART_readPolling(UART_Handle handle, void *buffer, size_t size);
934 
946 extern void UART_readCancel(UART_Handle handle);
947 
948 #ifdef __cplusplus
949 }
950 #endif
951 
952 #endif /* ti_drivers_UART__include */
Definition: UART.h:536
ADC_Params params
Definition: Driver_Init.h:11
UART_STOP
UART stop bit settings.
Definition: UART.h:525
void(* UART_CloseFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_CloseFxn().
Definition: UART.h:573
void * object
Definition: UART.h:685
UART_Callback writeCallback
Definition: UART.h:557
int_fast16_t UART_control(UART_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a given UART_Handle.
Definition: UART.h:442
UART_LEN dataLength
Definition: UART.h:563
Definition: UART.h:538
The definition of a UART function table that contains the required set of functions to control a spec...
Definition: UART.h:637
Definition: UART.h:517
UART_Callback readCallback
Definition: UART.h:556
UART_PAR
UART parity type settings.
Definition: UART.h:535
Definition: UART.h:504
UART_ControlFxn controlFxn
Definition: UART.h:642
UART_Mode
UART mode settings.
Definition: UART.h:430
Definition: UART.h:540
Definition: UART.h:487
int_fast32_t(* UART_ReadFxn)(UART_Handle handle, void *buffer, size_t size)
A function pointer to a driver specific implementation of UART_ReadFxn().
Definition: UART.h:596
int_fast16_t(* UART_ControlFxn)(UART_Handle handle, uint_fast16_t cmd, void *arg)
A function pointer to a driver specific implementation of UART_ControlFxn().
Definition: UART.h:579
UART_PAR parityType
Definition: UART.h:565
UART_DataMode
UART data mode settings.
Definition: UART.h:485
int_fast32_t UART_writePolling(UART_Handle handle, const void *buffer, size_t size)
Function that writes data to a UART, polling the peripheral to wait until new data can be written...
UART_Echo readEcho
Definition: UART.h:561
UART_ReturnMode
UART return mode settings.
Definition: UART.h:463
UART_WritePollingFxn writePollingFxn
Definition: UART.h:663
void UART_close(UART_Handle handle)
Function to close a UART peripheral specified by the UART handle.
Definition: UART.h:435
void UART_readCancel(UART_Handle handle)
Function that cancels a UART_read() function call.
UART_Echo
UART echo settings.
Definition: UART.h:503
Definition: UART.h:526
UART Global configuration.
Definition: UART.h:680
void const * hwAttrs
Definition: UART.h:688
UART_Mode writeMode
Definition: UART.h:553
UART_STOP stopBits
Definition: UART.h:564
UART_ReturnMode readReturnMode
Definition: UART.h:558
UART_CloseFxn closeFxn
Definition: UART.h:639
Definition: UART.h:468
void UART_init(void)
Function to initialize the UART module.
uint32_t baudRate
Definition: UART.h:562
UART_Handle(* UART_OpenFxn)(UART_Handle handle, UART_Params *params)
A function pointer to a driver specific implementation of UART_OpenFxn().
Definition: UART.h:591
Definition: UART.h:486
struct UART_Config_ UART_Config
UART Global configuration.
Definition: UART.h:539
void UART_Params_init(UART_Params *params)
Function to initialize the UART_Params struct to its defaults.
int_fast32_t UART_write(UART_Handle handle, const void *buffer, size_t size)
Function that writes data to a UART with interrupts enabled.
void UART_writeCancel(UART_Handle handle)
Function that cancels a UART_write() function call.
Definition: UART.h:514
Definition: UART.h:515
UART_DataMode readDataMode
Definition: UART.h:559
UART_WriteCancelFxn writeCancelFxn
Definition: UART.h:666
int_fast32_t(* UART_WriteFxn)(UART_Handle handle, const void *buffer, size_t size)
A function pointer to a driver specific implementation of UART_WriteFxn().
Definition: UART.h:616
int_fast32_t(* UART_ReadPollingFxn)(UART_Handle handle, void *buffer, size_t size)
A function pointer to a driver specific implementation of UART_ReadPollingFxn().
Definition: UART.h:603
int_fast32_t UART_readPolling(UART_Handle handle, void *buffer, size_t size)
Function that reads data from a UART without interrupts. This API must be used mutually exclusive wit...
Definition: UART.h:505
Definition: UART.h:527
void(* UART_InitFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_InitFxn().
Definition: UART.h:585
uint32_t readTimeout
Definition: UART.h:554
Definition: UART.h:516
UART_Handle UART_open(uint_least8_t index, UART_Params *params)
Function to initialize a given UART peripheral.
UART_ReadPollingFxn readPollingFxn
Definition: UART.h:654
Definition: UART.h:537
UART_ReadCancelFxn readCancelFxn
Definition: UART.h:657
UART_FxnTable const * fxnTablePtr
Definition: UART.h:682
int_fast32_t(* UART_WritePollingFxn)(UART_Handle handle, const void *buffer, size_t size)
A function pointer to a driver specific implementation of UART_WritePollingFxn(). ...
Definition: UART.h:623
UART_LEN
UART data length settings.
Definition: UART.h:513
void * custom
Definition: UART.h:566
void(* UART_ReadCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_ReadCancelFxn().
Definition: UART.h:610
UART Parameters.
Definition: UART.h:551
struct UART_Config_ * UART_Handle
A handle that is returned from a UART_open() call.
Definition: UART.h:410
UART_ReadFxn readFxn
Definition: UART.h:651
int_fast32_t UART_read(UART_Handle handle, void *buffer, size_t size)
Function that reads data from a UART with interrupt enabled.
UART_OpenFxn openFxn
Definition: UART.h:648
void(* UART_WriteCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_WriteCancelFxn().
Definition: UART.h:630
Definition: UART.h:465
UART_DataMode writeDataMode
Definition: UART.h:560
UART_Mode readMode
Definition: UART.h:552
UART_WriteFxn writeFxn
Definition: UART.h:660
uint32_t writeTimeout
Definition: UART.h:555
void(* UART_Callback)(UART_Handle handle, void *buf, size_t count)
The definition of a callback function used by the UART driver when used in UART_MODE_CALLBACK The cal...
Definition: UART.h:423
UART_InitFxn initFxn
Definition: UART.h:645
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale