UART.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2020, 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  * To use the UART driver, ensure that the correct driver library for your
37  * device is linked in and include this header file as follows:
38  * @code
39  * #include <ti/drivers/UART.h>
40  * @endcode
41  *
42  * This module serves as the main interface for applications. Its purpose
43  * is to redirect the UART APIs to specific driver implementations
44  * which are specified using a pointer to a #UART_FxnTable.
45  *
46  * @anchor ti_drivers_UART_Overview
47  * # Overview
48  * A UART is used to translate data between the chip and a serial port.
49  * The UART driver simplifies reading and writing to any of the UART
50  * peripherals on the board, with multiple modes of operation and performance.
51  * These include blocking, non-blocking, and polling, as well as text/binary
52  * mode, echo and return characters.
53  *
54  * The UART driver interface provides device independent APIs, data types,
55  * and macros. The APIs in this driver serve as an interface to a typical RTOS
56  * application. The specific peripheral implementations are responsible for
57  * creating all the RTOS specific primitives to allow for thread-safe
58  * operation.
59  *
60  * <hr>
61  * @anchor ti_drivers_UART_Usage
62  * # Usage
63  *
64  * This documentation provides a basic @ref ti_drivers_UART_Synopsis
65  * "usage summary" and a set of @ref ti_drivers_UART_Examples "examples"
66  * in the form of commented code fragments. Detailed descriptions of the
67  * APIs are provided in subsequent sections.
68  *
69  * @anchor ti_drivers_UART_Synopsis
70  * ## Synopsis
71  * @anchor ti_drivers_UART_Synopsis_Code
72  * @code
73  * // Import the UART driver definitions
74  * #include <ti/drivers/UART.h>
75  *
76  * // One-time initialization of UART driver
77  * UART_init();
78  *
79  * // Initialize UART parameters
80  * UART_Params params;
81  * UART_Params_init(&params);
82  * params.baudRate = 9600;
83  * params.readMode = UART_MODE_BLOCKING;
84  * params.writeMode = UART_MODE_BLOCKING;
85  * params.readTimeout = UART_WAIT_FOREVER;
86  * params.writeTimeout = UART_WAIT_FOREVER;
87  *
88  * // Open the UART
89  * UART_Handle uart;
90  * uart = UART_open(CONFIG_UART0, &params);
91  *
92  * // Read from the UART
93  * int32_t readCount;
94  * uint8_t buffer[BUFSIZE];
95  * readCount = UART_read(uart, buffer, BUFSIZE);
96  *
97  * // Write to the UART
98  * UART_write(uart, buffer, BUFSIZE);
99  *
100  * // Close the UART
101  * UART_close(uart);
102  * @endcode
103  *
104  * <hr>
105  * @anchor ti_drivers_UART_Examples
106  * # Examples
107  * The following code example opens a UART instance, reads
108  * a byte from the UART, and then writes the byte back to the UART.
109  *
110  * @code
111  * char input;
112  * UART_Handle uart;
113  * UART_Params uartParams;
114  *
115  * // Initialize the UART driver. UART_init() must be called before
116  * // calling any other UART APIs.
117  * UART_init();
118  *
119  * // Create a UART with data processing off.
120  * UART_Params_init(&uartParams);
121  * uartParams.writeDataMode = UART_DATA_BINARY;
122  * uartParams.readDataMode = UART_DATA_BINARY;
123  * uartParams.readReturnMode = UART_RETURN_FULL;
124  * uartParams.baudRate = 115200;
125  *
126  * // Open an instance of the UART drivers
127  * uart = UART_open(CONFIG_UART0, &uartParams);
128  *
129  * if (uart == NULL) {
130  * // UART_open() failed
131  * while (1);
132  * }
133  *
134  * // Loop forever echoing
135  * while (1) {
136  * UART_read(uart, &input, 1);
137  * UART_write(uart, &input, 1);
138  * }
139  * @endcode
140  *
141  * Details for the example code above are described in the following
142  * subsections.
143  *
144  * ### Opening the UART Driver #
145  *
146  * Opening a UART requires four steps:
147  * 1. Create and initialize a UART_Params structure.
148  * 2. Fill in the desired parameters.
149  * 3. Call UART_open(), passing the index of the UART in the UART_config
150  * structure, and the address of the UART_Params structure. The
151  * UART instance is specified by the index in the UART_config structure.
152  * 4. Check that the UART handle returned by UART_open() is non-NULL,
153  * and save it. The handle will be used to read and write to the
154  * UART you just opened.
155  *
156  * Only one UART index can be used at a time; calling UART_open() a second
157  * time with the same index previosly passed to UART_open() will result in
158  * an error. You can, though, re-use the index if the instance is closed
159  * via UART_close().
160  * In the example code, CONFIG_UART0 is passed to UART_open(). This macro
161  * is defined in the example's ti_drivers_config.h file.
162  *
163  *
164  * ### Modes of Operation #
165  *
166  * The UART driver can operate in blocking mode or callback mode, by
167  * setting the writeMode and readMode parameters passed to UART_open().
168  * If these parameters are not set, as in the example code, the UART
169  * driver defaults to blocking mode. Options for the writeMode and
170  * readMode parameters are #UART_MODE_BLOCKING and #UART_MODE_CALLBACK:
171  *
172  * - #UART_MODE_BLOCKING uses a semaphore to block while data is being sent.
173  * The context of calling UART_read() or UART_write() must be a Task when
174  * using #UART_MODE_BLOCKING. The UART_write() or UART_read() call
175  * will block until all data is sent or received, or the write timeout or
176  * read timeout expires, whichever happens first.
177  *
178  * - #UART_MODE_CALLBACK is non-blocking and UART_read() and UART_write()
179  * will return while data is being sent in the context of a hardware
180  * interrupt. When the read or write finishes, the UART driver will call
181  * the user's callback function. In some cases, the UART data transfer
182  * may have been canceled, or a newline may have been received, so the
183  * number of bytes sent/received are passed to the callback function. Your
184  * implementation of the callback function can use this information
185  * as needed. Since the user's callback may be called in the context of an
186  * ISR, the callback function must not make any RTOS blocking calls.
187  * The buffer passed to UART_write() in #UART_MODE_CALLBACK is not copied.
188  * The buffer must remain coherent until all the characters have been sent
189  * (ie until the tx callback has been called with a byte count equal to
190  * that passed to UART_write()).
191  *
192  * The example sets the writeDataMode and readDataMode parameters to
193  * #UART_DATA_BINARY. Options for these parameters are #UART_DATA_BINARY
194  * and #UART_DATA_TEXT:
195  *
196  * - #UART_DATA_BINARY: The data is passed as is, without processing.
197  *
198  * - #UART_DATA_TEXT: Write actions add a carriage return before a
199  * newline character, and read actions replace a return with a newline.
200  * This effectively treats all device line endings as LF and all host
201  * PC line endings as CRLF.
202  *
203  * Other parameters set by the example are readReturnMode and readEcho.
204  * Options for the readReturnMode parameter are #UART_RETURN_FULL and
205  * #UART_RETURN_NEWLINE:
206  *
207  * - #UART_RETURN_FULL: The read action unblocks or returns when the buffer
208  * is full.
209  * - #UART_RETURN_NEWLINE: The read action unblocks or returns when a
210  * newline character is read, before the buffer is full.
211  *
212  * Options for the readEcho parameter are #UART_ECHO_OFF and #UART_ECHO_ON.
213  * This parameter determines whether the driver echoes data back to the
214  * UART. When echo is turned on, each character that is read by the target
215  * is written back, independent of any write operations. If data is
216  * received in the middle of a write and echo is turned on, the echoed
217  * characters will be mixed in with the write data.
218  *
219  * ### Reading and Writing data #
220  *
221  * The example code reads one byte frome the UART instance, and then writes
222  * one byte back to the same instance:
223  *
224  * @code
225  * UART_read(uart, &input, 1);
226  * UART_write(uart, &input, 1);
227  * @endcode
228  *
229  * The UART driver allows full duplex data transfers. Therefore, it is
230  * possible to call UART_read() and UART_write() at the same time (for
231  * either blocking or callback modes). It is not possible, however,
232  * to issue multiple concurrent operations in the same direction.
233  * For example, if one thread calls UART_read(uart0, buffer0...),
234  * any other thread attempting UART_read(uart0, buffer1...) will result in
235  * an error of UART_STATUS_ERROR, until all the data from the first UART_read()
236  * has been transferred to buffer0. This applies to both blocking and
237  * and callback modes. So applications must either synchronize
238  * UART_read() (or UART_write()) calls that use the same UART handle, or
239  * check for the UART_STATUS_ERROR return code indicating that a transfer is
240  * still ongoing.
241  *
242  * <hr>
243  * @anchor ti_drivers_UART_Configuration
244  * # Configuration
245  *
246  * Refer to the @ref driver_configuration "Driver's Configuration" section
247  * for driver configuration information.
248  * <hr>
249  *
250  * ============================================================================
251  */
252 
253 #ifndef ti_drivers_UART__include
254 #define ti_drivers_UART__include
255 
256 #include <stddef.h>
257 #include <stdint.h>
258 
259 #ifdef __cplusplus
260 extern "C" {
261 #endif
262 
280 #define UART_CMD_RESERVED (32)
281 
294 #define UART_STATUS_RESERVED (-32)
295 
309 #define UART_STATUS_SUCCESS (0)
310 
317 #define UART_STATUS_ERROR (-1)
318 
326 #define UART_STATUS_UNDEFINEDCMD (-2)
327 
345 #define UART_CMD_PEEK (0)
346 
356 #define UART_CMD_ISAVAILABLE (1)
357 
367 #define UART_CMD_GETRXCOUNT (2)
368 
379 #define UART_CMD_RXENABLE (3)
380 
392 #define UART_CMD_RXDISABLE (4)
393 
397 #define UART_ERROR (UART_STATUS_ERROR)
398 
402 #define UART_WAIT_FOREVER (~(0U))
403 
407 typedef struct UART_Config_ *UART_Handle;
408 
420 typedef void (*UART_Callback) (UART_Handle handle, void *buf, size_t count);
421 
427 typedef enum {
433 
440 } UART_Mode;
441 
460 typedef enum {
463 
467 
482 typedef enum {
485 } UART_DataMode;
486 
500 typedef enum {
503 } UART_Echo;
504 
510 typedef enum {
515 } UART_LEN;
516 
522 typedef enum {
525 } UART_STOP;
526 
532 typedef enum {
538 } UART_PAR;
539 
548 typedef struct {
549  UART_Mode readMode;
550  UART_Mode writeMode;
551  uint32_t readTimeout;
552  uint32_t writeTimeout;
555  UART_ReturnMode readReturnMode;
559  uint32_t baudRate;
563  void *custom;
564 } UART_Params;
565 
570 typedef void (*UART_CloseFxn) (UART_Handle handle);
571 
576 typedef int_fast16_t (*UART_ControlFxn) (UART_Handle handle, uint_fast16_t cmd, void *arg);
577 
582 typedef void (*UART_InitFxn) (UART_Handle handle);
583 
588 typedef UART_Handle (*UART_OpenFxn) (UART_Handle handle, UART_Params *params);
593 typedef int_fast32_t (*UART_ReadFxn) (UART_Handle handle, void *buffer,
594  size_t size);
595 
600 typedef int_fast32_t (*UART_ReadPollingFxn) (UART_Handle handle, void *buffer,
601  size_t size);
602 
607 typedef void (*UART_ReadCancelFxn) (UART_Handle handle);
608 
613 typedef int_fast32_t (*UART_WriteFxn) (UART_Handle handle, const void *buffer,
614  size_t size);
615 
620 typedef int_fast32_t (*UART_WritePollingFxn) (UART_Handle handle,
621  const void *buffer, size_t size);
622 
627 typedef void (*UART_WriteCancelFxn) (UART_Handle handle);
628 
634 typedef struct {
637 
640 
643 
646 
649 
652 
655 
658 
661 
664 } UART_FxnTable;
665 
677 typedef struct UART_Config_ {
680 
682  void *object;
683 
685  void const *hwAttrs;
686 } UART_Config;
687 
699 extern void UART_close(UART_Handle handle);
700 
738 extern int_fast16_t UART_control(UART_Handle handle, uint_fast16_t cmd, void *arg);
739 
747 extern void UART_init(void);
748 
770 extern UART_Handle UART_open(uint_least8_t index, UART_Params *params);
771 
794 extern void UART_Params_init(UART_Params *params);
795 
835 extern int_fast32_t UART_write(UART_Handle handle, const void *buffer, size_t size);
836 
860 extern int_fast32_t UART_writePolling(UART_Handle handle, const void *buffer, size_t size);
861 
873 extern void UART_writeCancel(UART_Handle handle);
874 
908 extern int_fast32_t UART_read(UART_Handle handle, void *buffer, size_t size);
909 
930 extern int_fast32_t UART_readPolling(UART_Handle handle, void *buffer, size_t size);
931 
943 extern void UART_readCancel(UART_Handle handle);
944 
945 #ifdef __cplusplus
946 }
947 #endif
948 
949 #endif /* ti_drivers_UART__include */
Definition: UART.h:533
ADC_Params params
Definition: Driver_Init.h:11
UART_STOP
UART stop bit settings.
Definition: UART.h:522
void(* UART_CloseFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_CloseFxn().
Definition: UART.h:570
void * object
Definition: UART.h:682
UART_Callback writeCallback
Definition: UART.h:554
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:439
UART_LEN dataLength
Definition: UART.h:560
Definition: UART.h:535
The definition of a UART function table that contains the required set of functions to control a spec...
Definition: UART.h:634
Definition: UART.h:514
UART_Callback readCallback
Definition: UART.h:553
UART_PAR
UART parity type settings.
Definition: UART.h:532
Definition: UART.h:501
UART_ControlFxn controlFxn
Definition: UART.h:639
UART_Mode
UART mode settings.
Definition: UART.h:427
Definition: UART.h:537
Definition: UART.h:484
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:593
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:576
UART_PAR parityType
Definition: UART.h:562
UART_DataMode
UART data mode settings.
Definition: UART.h:482
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:558
UART_ReturnMode
UART return mode settings.
Definition: UART.h:460
UART_WritePollingFxn writePollingFxn
Definition: UART.h:660
void UART_close(UART_Handle handle)
Function to close a UART peripheral specified by the UART handle.
Definition: UART.h:432
void UART_readCancel(UART_Handle handle)
Function that cancels a UART_read() function call.
UART_Echo
UART echo settings.
Definition: UART.h:500
Definition: UART.h:523
UART Global configuration.
Definition: UART.h:677
void const * hwAttrs
Definition: UART.h:685
UART_Mode writeMode
Definition: UART.h:550
UART_STOP stopBits
Definition: UART.h:561
UART_ReturnMode readReturnMode
Definition: UART.h:555
UART_CloseFxn closeFxn
Definition: UART.h:636
Definition: UART.h:465
void UART_init(void)
Function to initialize the UART module.
uint32_t baudRate
Definition: UART.h:559
UART_Handle(* UART_OpenFxn)(UART_Handle handle, UART_Params *params)
A function pointer to a driver specific implementation of UART_OpenFxn().
Definition: UART.h:588
Definition: UART.h:483
struct UART_Config_ UART_Config
UART Global configuration.
Definition: UART.h:536
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:511
Definition: UART.h:512
UART_DataMode readDataMode
Definition: UART.h:556
UART_WriteCancelFxn writeCancelFxn
Definition: UART.h:663
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:613
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:600
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:502
Definition: UART.h:524
void(* UART_InitFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_InitFxn().
Definition: UART.h:582
uint32_t readTimeout
Definition: UART.h:551
Definition: UART.h:513
UART_Handle UART_open(uint_least8_t index, UART_Params *params)
Function to initialize a given UART peripheral.
UART_ReadPollingFxn readPollingFxn
Definition: UART.h:651
Definition: UART.h:534
UART_ReadCancelFxn readCancelFxn
Definition: UART.h:654
UART_FxnTable const * fxnTablePtr
Definition: UART.h:679
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:620
UART_LEN
UART data length settings.
Definition: UART.h:510
void * custom
Definition: UART.h:563
void(* UART_ReadCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_ReadCancelFxn().
Definition: UART.h:607
UART Parameters.
Definition: UART.h:548
struct UART_Config_ * UART_Handle
A handle that is returned from a UART_open() call.
Definition: UART.h:407
UART_ReadFxn readFxn
Definition: UART.h:648
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:645
void(* UART_WriteCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_WriteCancelFxn().
Definition: UART.h:627
Definition: UART.h:462
UART_DataMode writeDataMode
Definition: UART.h:557
UART_Mode readMode
Definition: UART.h:549
UART_WriteFxn writeFxn
Definition: UART.h:657
uint32_t writeTimeout
Definition: UART.h:552
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:420
UART_InitFxn initFxn
Definition: UART.h:642
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale