UART.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2019, 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.readEcho = UART_ECHO_OFF;
125  * uartParams.baudRate = 115200;
126  *
127  * // Open an instance of the UART drivers
128  * uart = UART_open(CONFIG_UART0, &uartParams);
129  *
130  * if (uart == NULL) {
131  * // UART_open() failed
132  * while (1);
133  * }
134  *
135  * // Loop forever echoing
136  * while (1) {
137  * UART_read(uart, &input, 1);
138  * UART_write(uart, &input, 1);
139  * }
140  * @endcode
141  *
142  * Details for the example code above are described in the following
143  * subsections.
144  *
145  * ### Opening the UART Driver #
146  *
147  * Opening a UART requires four steps:
148  * 1. Create and initialize a UART_Params structure.
149  * 2. Fill in the desired parameters.
150  * 3. Call UART_open(), passing the index of the UART in the UART_config
151  * structure, and the address of the UART_Params structure. The
152  * UART instance is specified by the index in the UART_config structure.
153  * 4. Check that the UART handle returned by UART_open() is non-NULL,
154  * and save it. The handle will be used to read and write to the
155  * UART you just opened.
156  *
157  * Only one UART index can be used at a time; calling UART_open() a second
158  * time with the same index previosly passed to UART_open() will result in
159  * an error. You can, though, re-use the index if the instance is closed
160  * via UART_close().
161  * In the example code, CONFIG_UART0 is passed to UART_open(). This macro
162  * is defined in the example's ti_drivers_config.h file.
163  *
164  *
165  * ### Modes of Operation #
166  *
167  * The UART driver can operate in blocking mode or callback mode, by
168  * setting the writeMode and readMode parameters passed to UART_open().
169  * If these parameters are not set, as in the example code, the UART
170  * driver defaults to blocking mode. Options for the writeMode and
171  * readMode parameters are #UART_MODE_BLOCKING and #UART_MODE_CALLBACK:
172  *
173  * - #UART_MODE_BLOCKING uses a semaphore to block while data is being sent.
174  * The context of calling UART_read() or UART_write() must be a Task when
175  * using #UART_MODE_BLOCKING. The UART_write() or UART_read() call
176  * will block until all data is sent or received, or the write timeout or
177  * read timeout expires, whichever happens first.
178  *
179  * - #UART_MODE_CALLBACK is non-blocking and UART_read() and UART_write()
180  * will return while data is being sent in the context of a hardware
181  * interrupt. When the read or write finishes, the UART driver will call
182  * the user's callback function. In some cases, the UART data transfer
183  * may have been canceled, or a newline may have been received, so the
184  * number of bytes sent/received are passed to the callback function. Your
185  * implementation of the callback function can use this information
186  * as needed. Since the user's callback may be called in the context of an
187  * ISR, the callback function must not make any RTOS blocking calls.
188  * The buffer passed to UART_write() in #UART_MODE_CALLBACK is not copied.
189  * The buffer must remain coherent until all the characters have been sent
190  * (ie until the tx callback has been called with a byte count equal to
191  * that passed to UART_write()).
192  *
193  * The example sets the writeDataMode and readDataMode parameters to
194  * #UART_DATA_BINARY. Options for these parameters are #UART_DATA_BINARY
195  * and #UART_DATA_TEXT:
196  *
197  * - #UART_DATA_BINARY: The data is passed as is, without processing.
198  *
199  * - #UART_DATA_TEXT: Write actions add a carriage return before a
200  * newline character, and read actions replace a return with a newline.
201  * This effectively treats all device line endings as LF and all host
202  * PC line endings as CRLF.
203  *
204  * Other parameters set by the example are readReturnMode and readEcho.
205  * Options for the readReturnMode parameter are #UART_RETURN_FULL and
206  * #UART_RETURN_NEWLINE:
207  *
208  * - #UART_RETURN_FULL: The read action unblocks or returns when the buffer
209  * is full.
210  * - #UART_RETURN_NEWLINE: The read action unblocks or returns when a
211  * newline character is read, before the buffer is full.
212  *
213  * Options for the readEcho parameter are #UART_ECHO_OFF and #UART_ECHO_ON.
214  * This parameter determines whether the driver echoes data back to the
215  * UART. When echo is turned on, each character that is read by the target
216  * is written back, independent of any write operations. If data is
217  * received in the middle of a write and echo is turned on, the echoed
218  * characters will be mixed in with the write data.
219  *
220  * ### Reading and Writing data #
221  *
222  * The example code reads one byte frome the UART instance, and then writes
223  * one byte back to the same instance:
224  *
225  * @code
226  * UART_read(uart, &input, 1);
227  * UART_write(uart, &input, 1);
228  * @endcode
229  *
230  * The UART driver allows full duplex data transfers. Therefore, it is
231  * possible to call UART_read() and UART_write() at the same time (for
232  * either blocking or callback modes). It is not possible, however,
233  * to issue multiple concurrent operations in the same direction.
234  * For example, if one thread calls UART_read(uart0, buffer0...),
235  * any other thread attempting UART_read(uart0, buffer1...) will result in
236  * an error of UART_STATUS_ERROR, until all the data from the first UART_read()
237  * has been transferred to buffer0. This applies to both blocking and
238  * and callback modes. So applications must either synchronize
239  * UART_read() (or UART_write()) calls that use the same UART handle, or
240  * check for the UART_STATUS_ERROR return code indicating that a transfer is
241  * still ongoing.
242  *
243  * <hr>
244  * @anchor ti_drivers_UART_Configuration
245  * # Configuration
246  *
247  * Refer to the @ref driver_configuration "Driver's Configuration" section
248  * for driver configuration information.
249  * <hr>
250  *
251  * ============================================================================
252  */
253 
254 #ifndef ti_drivers_UART__include
255 #define ti_drivers_UART__include
256 
257 #include <stddef.h>
258 #include <stdint.h>
259 
260 #ifdef __cplusplus
261 extern "C" {
262 #endif
263 
281 #define UART_CMD_RESERVED (32)
282 
295 #define UART_STATUS_RESERVED (-32)
296 
310 #define UART_STATUS_SUCCESS (0)
311 
318 #define UART_STATUS_ERROR (-1)
319 
327 #define UART_STATUS_UNDEFINEDCMD (-2)
328 
346 #define UART_CMD_PEEK (0)
347 
357 #define UART_CMD_ISAVAILABLE (1)
358 
368 #define UART_CMD_GETRXCOUNT (2)
369 
380 #define UART_CMD_RXENABLE (3)
381 
393 #define UART_CMD_RXDISABLE (4)
394 
398 #define UART_ERROR (UART_STATUS_ERROR)
399 
403 #define UART_WAIT_FOREVER (~(0U))
404 
408 typedef struct UART_Config_ *UART_Handle;
409 
421 typedef void (*UART_Callback) (UART_Handle handle, void *buf, size_t count);
422 
428 typedef enum {
434 
441 } UART_Mode;
442 
461 typedef enum {
464 
468 
483 typedef enum {
486 } UART_DataMode;
487 
501 typedef enum {
504 } UART_Echo;
505 
511 typedef enum {
516 } UART_LEN;
517 
523 typedef enum {
526 } UART_STOP;
527 
533 typedef enum {
539 } UART_PAR;
540 
549 typedef struct {
550  UART_Mode readMode;
551  UART_Mode writeMode;
552  uint32_t readTimeout;
553  uint32_t writeTimeout;
556  UART_ReturnMode readReturnMode;
560  uint32_t baudRate;
564  void *custom;
565 } UART_Params;
566 
571 typedef void (*UART_CloseFxn) (UART_Handle handle);
572 
577 typedef int_fast16_t (*UART_ControlFxn) (UART_Handle handle, uint_fast16_t cmd, void *arg);
578 
583 typedef void (*UART_InitFxn) (UART_Handle handle);
584 
589 typedef UART_Handle (*UART_OpenFxn) (UART_Handle handle, UART_Params *params);
594 typedef int_fast32_t (*UART_ReadFxn) (UART_Handle handle, void *buffer,
595  size_t size);
596 
601 typedef int_fast32_t (*UART_ReadPollingFxn) (UART_Handle handle, void *buffer,
602  size_t size);
603 
608 typedef void (*UART_ReadCancelFxn) (UART_Handle handle);
609 
614 typedef int_fast32_t (*UART_WriteFxn) (UART_Handle handle, const void *buffer,
615  size_t size);
616 
621 typedef int_fast32_t (*UART_WritePollingFxn) (UART_Handle handle,
622  const void *buffer, size_t size);
623 
628 typedef void (*UART_WriteCancelFxn) (UART_Handle handle);
629 
635 typedef struct {
638 
641 
644 
647 
650 
653 
656 
659 
662 
665 } UART_FxnTable;
666 
678 typedef struct UART_Config_ {
681 
683  void *object;
684 
686  void const *hwAttrs;
687 } UART_Config;
688 
700 extern void UART_close(UART_Handle handle);
701 
739 extern int_fast16_t UART_control(UART_Handle handle, uint_fast16_t cmd, void *arg);
740 
748 extern void UART_init(void);
749 
771 extern UART_Handle UART_open(uint_least8_t index, UART_Params *params);
772 
795 extern void UART_Params_init(UART_Params *params);
796 
839 extern int_fast32_t UART_write(UART_Handle handle, const void *buffer, size_t size);
840 
864 extern int_fast32_t UART_writePolling(UART_Handle handle, const void *buffer, size_t size);
865 
877 extern void UART_writeCancel(UART_Handle handle);
878 
915 extern int_fast32_t UART_read(UART_Handle handle, void *buffer, size_t size);
916 
937 extern int_fast32_t UART_readPolling(UART_Handle handle, void *buffer, size_t size);
938 
950 extern void UART_readCancel(UART_Handle handle);
951 
952 #ifdef __cplusplus
953 }
954 #endif
955 
956 #endif /* ti_drivers_UART__include */
Definition: UART.h:534
UART_STOP
UART stop bit settings.
Definition: UART.h:523
void(* UART_CloseFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_CloseFxn().
Definition: UART.h:571
void * object
Definition: UART.h:683
UART_Callback writeCallback
Definition: UART.h:555
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:440
UART_LEN dataLength
Definition: UART.h:561
Definition: UART.h:536
The definition of a UART function table that contains the required set of functions to control a spec...
Definition: UART.h:635
Definition: UART.h:515
UART_Callback readCallback
Definition: UART.h:554
UART_PAR
UART parity type settings.
Definition: UART.h:533
Definition: UART.h:502
UART_ControlFxn controlFxn
Definition: UART.h:640
UART_Mode
UART mode settings.
Definition: UART.h:428
Definition: UART.h:538
Definition: UART.h:485
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:594
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:577
UART_PAR parityType
Definition: UART.h:563
UART_DataMode
UART data mode settings.
Definition: UART.h:483
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:559
UART_ReturnMode
UART return mode settings.
Definition: UART.h:461
UART_WritePollingFxn writePollingFxn
Definition: UART.h:661
void UART_close(UART_Handle handle)
Function to close a UART peripheral specified by the UART handle.
Definition: UART.h:433
void UART_readCancel(UART_Handle handle)
Function that cancels a UART_read() function call.
UART_Echo
UART echo settings.
Definition: UART.h:501
Definition: UART.h:524
UART Global configuration.
Definition: UART.h:678
void const * hwAttrs
Definition: UART.h:686
UART_Mode writeMode
Definition: UART.h:551
UART_STOP stopBits
Definition: UART.h:562
UART_ReturnMode readReturnMode
Definition: UART.h:556
UART_CloseFxn closeFxn
Definition: UART.h:637
Definition: UART.h:466
void UART_init(void)
Function to initialize the UART module.
uint32_t baudRate
Definition: UART.h:560
UART_Handle(* UART_OpenFxn)(UART_Handle handle, UART_Params *params)
A function pointer to a driver specific implementation of UART_OpenFxn().
Definition: UART.h:589
Definition: UART.h:484
struct UART_Config_ UART_Config
UART Global configuration.
Definition: UART.h:537
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:512
Definition: UART.h:513
UART_DataMode readDataMode
Definition: UART.h:557
UART_WriteCancelFxn writeCancelFxn
Definition: UART.h:664
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:614
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:601
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:503
Definition: UART.h:525
void(* UART_InitFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_InitFxn().
Definition: UART.h:583
uint32_t readTimeout
Definition: UART.h:552
Definition: UART.h:514
UART_Handle UART_open(uint_least8_t index, UART_Params *params)
Function to initialize a given UART peripheral.
UART_ReadPollingFxn readPollingFxn
Definition: UART.h:652
Definition: UART.h:535
UART_ReadCancelFxn readCancelFxn
Definition: UART.h:655
UART_FxnTable const * fxnTablePtr
Definition: UART.h:680
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:621
UART_LEN
UART data length settings.
Definition: UART.h:511
void * custom
Definition: UART.h:564
void(* UART_ReadCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_ReadCancelFxn().
Definition: UART.h:608
UART Parameters.
Definition: UART.h:549
struct UART_Config_ * UART_Handle
A handle that is returned from a UART_open() call.
Definition: UART.h:408
UART_ReadFxn readFxn
Definition: UART.h:649
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:646
void(* UART_WriteCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_WriteCancelFxn().
Definition: UART.h:628
Definition: UART.h:463
UART_DataMode writeDataMode
Definition: UART.h:558
UART_Mode readMode
Definition: UART.h:550
UART_WriteFxn writeFxn
Definition: UART.h:658
uint32_t writeTimeout
Definition: UART.h:553
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:421
UART_InitFxn initFxn
Definition: UART.h:643
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale