UART.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018, 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 UART driver interface
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  * # Overview #
47  * A UART is used to translate data between the chip and a serial port.
48  * The UART driver simplifies reading and writing to any of the UART
49  * peripherals on the board, with multiple modes of operation and performance.
50  * These include blocking, non-blocking, and polling, as well as text/binary
51  * mode, echo and return characters.
52  *
53  * The APIs in this driver serve as an interface to a typical RTOS
54  * application. The specific peripheral implementations are responsible for
55  * creating all the RTOS specific primitives to allow for thread-safe
56  * operation.
57  *
58  * # Usage #
59  *
60  * The UART driver interface provides device independent APIs, data types,
61  * and macros. The following code example opens a UART instance, reads
62  * a byte from the UART, and then writes the byte back to the UART.
63  *
64  * @code
65  * char input;
66  * UART_Handle uart;
67  * UART_Params uartParams;
68  *
69  * // Initialize the UART driver.
70  * UART_init();
71  *
72  * // Create a UART with data processing off.
73  * UART_Params_init(&uartParams);
74  * uartParams.writeDataMode = UART_DATA_BINARY;
75  * uartParams.readDataMode = UART_DATA_BINARY;
76  * uartParams.readReturnMode = UART_RETURN_FULL;
77  * uartParams.readEcho = UART_ECHO_OFF;
78  * uartParams.baudRate = 115200;
79  *
80  * // Open an instance of the UART drivers
81  * uart = UART_open(Board_UART0, &uartParams);
82  *
83  * if (uart == NULL) {
84  * // UART_open() failed
85  * while (1);
86  * }
87  *
88  * // Loop forever echoing
89  * while (1) {
90  * UART_read(uart, &input, 1);
91  * UART_write(uart, &input, 1);
92  * }
93  * @endcode
94  *
95  * Details for the example code above are described in the following
96  * subsections.
97  *
98  *
99  * ### UART Driver Configuration #
100  *
101  * In order to use the UART APIs, the application is required
102  * to provide device-specific UART configuration in the Board.c file.
103  * The UART driver interface defines a configuration data structure:
104  *
105  * @code
106  * typedef struct UART_Config_ {
107  * UART_FxnTable const *fxnTablePtr;
108  * void *object;
109  * void const *hwAttrs;
110  * } UART_Config;
111  * @endcode
112  *
113  * The application must declare an array of UART_Config elements, named
114  * UART_config[]. Each element of UART_config[] are populated with
115  * pointers to a device specific UART driver implementation's function
116  * table, driver object, and hardware attributes. The hardware attributes
117  * define properties such as the UART peripheral's base address, and
118  * the pins for RX and TX. Each element in UART_config[] corresponds to
119  * a UART instance, and none of the elements should have NULL pointers.
120  * There is no correlation between the index and the peripheral designation
121  * (such as UART0 or UART1). For example, it is possible to use
122  * UART_config[0] for UART1.
123  *
124  * You will need to check the device-specific UART driver implementation's
125  * header file for example configuration. Please also refer to the
126  * Board.c file of any of your examples to see the UART configuration.
127  *
128  * ### Initializing the UART Driver #
129  *
130  * UART_init() must be called before any other UART APIs. This function
131  * calls the device implementation's UART initialization function, for each
132  * element of UART_config[].
133  *
134  * ### Opening the UART Driver #
135  *
136  * Opening a UART requires four steps:
137  * 1. Create and initialize a UART_Params structure.
138  * 2. Fill in the desired parameters.
139  * 3. Call UART_open(), passing the index of the UART in the UART_config
140  * structure, and the address of the UART_Params structure. The
141  * UART instance is specified by the index in the UART_config structure.
142  * 4. Check that the UART handle returned by UART_open() is non-NULL,
143  * and save it. The handle will be used to read and write to the
144  * UART you just opened.
145  *
146  * Only one UART index can be used at a time; calling UART_open() a second
147  * time with the same index previosly passed to UART_open() will result in
148  * an error. You can, though, re-use the index if the instance is closed
149  * via UART_close().
150  * In the example code, Board_UART0 is passed to UART_open(). This macro
151  * is defined in the example's Board.h file.
152  *
153  *
154  * ### Modes of Operation #
155  *
156  * The UART driver can operate in blocking mode or callback mode, by
157  * setting the writeMode and readMode parameters passed to UART_open().
158  * If these parameters are not set, as in the example code, the UART
159  * driver defaults to blocking mode. Options for the writeMode and
160  * readMode parameters are #UART_MODE_BLOCKING and #UART_MODE_CALLBACK:
161  *
162  * - #UART_MODE_BLOCKING uses a semaphore to block while data is being sent.
163  * The context of calling UART_read() or UART_write() must be a Task when
164  * using #UART_MODE_BLOCKING. The UART_write() or UART_read() call
165  * will block until all data is sent or received, or the write timeout or
166  * read timeout expires, whichever happens first.
167  *
168  * - #UART_MODE_CALLBACK is non-blocking and UART_read() and UART_write()
169  * will return while data is being sent in the context of a hardware
170  * interrupt. When the read or write finishes, the UART driver will call
171  * the user's callback function. In some cases, the UART data transfer
172  * may have been canceled, or a newline may have been received, so the
173  * number of bytes sent/received are passed to the callback function. Your
174  * implementation of the callback function can use this information
175  * as needed. Since the user's callback may be called in the context of an
176  * ISR, the callback function must not make any RTOS blocking calls.
177  * The buffer passed to UART_write() in #UART_MODE_CALLBACK is not copied.
178  * The buffer must remain coherent until all the characters have been sent
179  * (ie until the tx callback has been called with a byte count equal to
180  * that passed to UART_write()).
181  *
182  * The example sets the writeDataMode and readDataMode parameters to
183  * #UART_DATA_BINARY. Options for these parameters are #UART_DATA_BINARY
184  * and #UART_DATA_TEXT:
185  *
186  * - #UART_DATA_BINARY: The data is passed as is, without processing.
187  *
188  * - #UART_DATA_TEXT: Write actions add a carriage return before a
189  * newline character, and read actions replace a return with a newline.
190  * This effectively treats all device line endings as LF and all host
191  * PC line endings as CRLF.
192  *
193  * Other parameters set by the example are readReturnMode and readEcho.
194  * Options for the readReturnMode parameter are #UART_RETURN_FULL and
195  * #UART_RETURN_NEWLINE:
196  *
197  * - #UART_RETURN_FULL: The read action unblocks or returns when the buffer
198  * is full.
199  * - #UART_RETURN_NEWLINE: The read action unblocks or returns when a
200  * newline character is read, before the buffer is full.
201  *
202  * Options for the readEcho parameter are #UART_ECHO_OFF and #UART_ECHO_ON.
203  * This parameter determines whether the driver echoes data back to the
204  * UART. When echo is turned on, each character that is read by the target
205  * is written back, independent of any write operations. If data is
206  * received in the middle of a write and echo is turned on, the echoed
207  * characters will be mixed in with the write data.
208  *
209  * ### Reading and Writing data #
210  *
211  * The example code reads one byte frome the UART instance, and then writes
212  * one byte back to the same instance:
213  *
214  * @code
215  * UART_read(uart, &input, 1);
216  * UART_write(uart, &input, 1);
217  * @endcode
218  *
219  * The UART driver allows full duplex data transfers. Therefore, it is
220  * possible to call UART_read() and UART_write() at the same time (for
221  * either blocking or callback modes). It is not possible, however,
222  * to issue multiple concurrent operations in the same direction.
223  * For example, if one thread calls UART_read(uart0, buffer0...),
224  * any other thread attempting UART_read(uart0, buffer1...) will result in
225  * an error of UART_STATUS_ERROR, until all the data from the first UART_read()
226  * has been transferred to buffer0. This applies to both blocking and
227  * and callback modes. So applications must either synchronize
228  * UART_read() (or UART_write()) calls that use the same UART handle, or
229  * check for the UART_STATUS_ERROR return code indicating that a transfer is
230  * still ongoing.
231  *
232  * # Implementation #
233  *
234  * The UART driver interface module is joined (at link time) to an
235  * array of UART_Config data structures named *UART_config*.
236  * UART_config is implemented in the application with each entry being an
237  * instance of a UART peripheral. Each entry in *UART_config* contains a:
238  * - (UART_FxnTable *) to a set of functions that implement a UART peripheral
239  * - (void *) data object that is associated with the UART_FxnTable
240  * - (void *) hardware attributes that are associated with the UART_FxnTable
241  *
242  * The UART APIs are redirected to the device specific implementations
243  * using the UART_FxnTable pointer of the UART_config entry.
244  * In order to use device specific functions of the UART driver directly,
245  * link in the correct driver library for your device and include the
246  * device specific UART driver header file (which in turn includes UART.h).
247  * For example, for the MSP432 family of devices, you would include the
248  * following header file:
249  * @code
250  * #include <ti/drivers/uart/UARTMSP432.h>
251  * @endcode
252  *
253  * ============================================================================
254  */
255 
256 #ifndef ti_drivers_UART__include
257 #define ti_drivers_UART__include
258 
259 #ifdef __cplusplus
260 extern "C" {
261 #endif
262 
263 #include <stddef.h>
264 #include <stdint.h>
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 UART_Mode_ {
436 
443 } UART_Mode;
444 
463 typedef enum UART_ReturnMode_ {
466 
470 
485 typedef enum UART_DataMode_ {
488 } UART_DataMode;
489 
503 typedef enum UART_Echo_ {
506 } UART_Echo;
507 
513 typedef enum UART_LEN_ {
518 } UART_LEN;
519 
525 typedef enum UART_STOP_ {
528 } UART_STOP;
529 
535 typedef enum UART_PAR_ {
541 } UART_PAR;
542 
551 typedef struct UART_Params_ {
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 UART_FxnTable_ {
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 
841 extern int_fast32_t UART_write(UART_Handle handle, const void *buffer, size_t size);
842 
866 extern int_fast32_t UART_writePolling(UART_Handle handle, const void *buffer, size_t size);
867 
879 extern void UART_writeCancel(UART_Handle handle);
880 
917 extern int_fast32_t UART_read(UART_Handle handle, void *buffer, size_t size);
918 
939 extern int_fast32_t UART_readPolling(UART_Handle handle, void *buffer, size_t size);
940 
952 extern void UART_readCancel(UART_Handle handle);
953 
954 #ifdef __cplusplus
955 }
956 #endif
957 
958 #endif /* ti_drivers_UART__include */
UART_Mode writeMode
Definition: UART.h:553
UART_WriteFxn writeFxn
Definition: UART.h:660
UART_Mode readMode
Definition: UART.h:552
uint32_t baudRate
Definition: UART.h:562
Definition: UART.h:435
void(* UART_CloseFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_CloseFxn().
Definition: UART.h:573
Definition: UART.h:465
enum UART_Echo_ UART_Echo
UART echo settings.
UART_Callback writeCallback
Definition: UART.h:557
void * object
Definition: UART.h:685
UART_CloseFxn closeFxn
Definition: UART.h:639
struct UART_Params_ UART_Params
UART Parameters.
UART_ReturnMode_
UART return mode settings.
Definition: UART.h:463
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:487
UART_ReadPollingFxn readPollingFxn
Definition: UART.h:654
UART_Callback readCallback
Definition: UART.h:556
enum UART_PAR_ UART_PAR
UART parity type settings.
enum UART_LEN_ UART_LEN
UART data length settings.
Definition: UART.h:468
enum UART_Mode_ UART_Mode
UART mode settings.
UART_LEN dataLength
Definition: UART.h:563
Definition: UART.h:517
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_STOP_
UART stop bit settings.
Definition: UART.h:525
UART_DataMode writeDataMode
Definition: UART.h:560
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...
void * custom
Definition: UART.h:566
void UART_close(UART_Handle handle)
Function to close a UART peripheral specified by the UART handle.
Definition: UART.h:486
void UART_readCancel(UART_Handle handle)
Function that cancels a UART_read() function call.
struct UART_FxnTable_ UART_FxnTable
The definition of a UART function table that contains the required set of functions to control a spec...
UART Global configuration.
Definition: UART.h:680
Definition: UART.h:515
void const * hwAttrs
Definition: UART.h:688
void UART_init(void)
Function to initialize the UART module.
UART_OpenFxn openFxn
Definition: UART.h:648
UART_LEN_
UART data length settings.
Definition: UART.h:513
UART_ReadCancelFxn readCancelFxn
Definition: UART.h:657
UART_STOP stopBits
Definition: UART.h:564
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
struct UART_Config_ UART_Config
UART Global configuration.
The definition of a UART function table that contains the required set of functions to control a spec...
Definition: UART.h:637
void UART_Params_init(UART_Params *params)
Function to initialize the UART_Params struct to its defaults.
Definition: UART.h:539
Definition: UART.h:540
UART_WriteCancelFxn writeCancelFxn
Definition: UART.h:666
int_fast32_t UART_write(UART_Handle handle, const void *buffer, size_t size)
Function that writes data to a UART with interrupts enabled.
Definition: UART.h:516
void UART_writeCancel(UART_Handle handle)
Function that cancels a UART_write() function call.
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
UART_PAR_
UART parity type settings.
Definition: UART.h:535
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...
void(* UART_InitFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_InitFxn().
Definition: UART.h:585
UART_Handle UART_open(uint_least8_t index, UART_Params *params)
Function to initialize a given UART peripheral.
Definition: UART.h:526
enum UART_STOP_ UART_STOP
UART stop bit settings.
enum UART_ReturnMode_ UART_ReturnMode
UART return mode settings.
uint32_t writeTimeout
Definition: UART.h:555
UART_Echo readEcho
Definition: UART.h:561
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 Parameters.
Definition: UART.h:551
void(* UART_ReadCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_ReadCancelFxn().
Definition: UART.h:610
UART_ControlFxn controlFxn
Definition: UART.h:642
UART_ReturnMode readReturnMode
Definition: UART.h:558
struct UART_Config_ * UART_Handle
A handle that is returned from a UART_open() call.
Definition: UART.h:410
UART_DataMode readDataMode
Definition: UART.h:559
int_fast32_t UART_read(UART_Handle handle, void *buffer, size_t size)
Function that reads data from a UART with interrupt enabled.
UART_ReadFxn readFxn
Definition: UART.h:651
Definition: UART.h:537
UART_DataMode_
UART data mode settings.
Definition: UART.h:485
Definition: UART.h:505
UART_PAR parityType
Definition: UART.h:565
Definition: UART.h:514
UART_Echo_
UART echo settings.
Definition: UART.h:503
void(* UART_WriteCancelFxn)(UART_Handle handle)
A function pointer to a driver specific implementation of UART_WriteCancelFxn().
Definition: UART.h:630
Definition: UART.h:538
UART_InitFxn initFxn
Definition: UART.h:645
uint32_t readTimeout
Definition: UART.h:554
Definition: UART.h:442
Definition: UART.h:527
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_Mode_
UART mode settings.
Definition: UART.h:430
enum UART_DataMode_ UART_DataMode
UART data mode settings.
Definition: UART.h:536
UART_WritePollingFxn writePollingFxn
Definition: UART.h:663
Definition: UART.h:504
© Copyright 1995-2018, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale