UART2.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 UART2.h
34  * @brief <b>PRELIMINARY</b> UART driver interface
35  *
36  * <b>WARNING</b> These APIs are <b>PRELIMINARY</b>, and subject to
37  * change in the next few months.
38  *
39  * To use the UART2 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/UART2.h>
43  * @endcode
44  *
45  * This module serves as the main interface for applications. Its purpose
46  * is to redirect the UART2 APIs to specific driver implementations
47  * which are specified using a pointer to a #UART2_FxnTable.
48  *
49  * @anchor ti_drivers_UART2_Overview
50  * # Overview
51  * A UART is used to translate data between the chip and a serial port.
52  * The UART2 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 modes.
55  *
56  * The UART2 driver interface provides device independent APIs, data types,
57  * and macros. The APIs in this driver serve as an interface to a typical RTOS
58  * application. The specific peripheral implementations are responsible for
59  * creating all the RTOS specific primitives to allow for thread-safe
60  * operation.
61  *
62  * <hr>
63  * @anchor ti_drivers_UART2_Usage
64  * # Usage
65  *
66  * This documentation provides a basic @ref ti_drivers_UART2_Synopsis
67  * "usage summary" and a set of @ref ti_drivers_UART2_Examples "examples"
68  * in the form of commented code fragments. Detailed descriptions of the
69  * APIs are provided in subsequent sections.
70  *
71  * @anchor ti_drivers_UART2_Synopsis
72  * ## Synopsis
73  * @anchor ti_drivers_UART2_Synopsis_Code
74  * @code
75  * // Import the UART2 driver definitions
76  * #include <ti/drivers/UART2.h>
77  *
78  * // Initialize UART2 parameters
79  * UART2_Params params;
80  * UART2_Params_init(&params);
81  * params.baudRate = 9600;
82  * params.readMode = UART2_Mode_BLOCKING;
83  * params.writeMode = UART2_Mode_BLOCKING;
84  * params.readTimeout = UART2_WAIT_FOREVER;
85  * params.writeTimeout = UART2_WAIT_FOREVER;
86  *
87  * // Open the UART
88  * UART2_Handle uart;
89  * uart = UART2_open(CONFIG_UART0, &params);
90  *
91  * // Read from the UART.
92  * size_t bytesRead;
93  * uint8_t buffer[BUFSIZE];
94  * int32_t status;
95  * status = UART2_read(uart, buffer, BUFSIZE, &bytesRead);
96  *
97  * // Write to the UART
98  * size_t bytesWritten;
99  * status = UART2_write(uart, buffer, BUFSIZE, &bytesWritten);
100  *
101  * // Close the UART
102  * UART2_close(uart);
103  * @endcode
104  *
105  * <hr>
106  * @anchor ti_drivers_UART2_Examples
107  * # Examples
108  * The following code example opens a UART instance, reads
109  * a byte from the UART, and then writes the byte back to the UART.
110  *
111  * @code
112  * char input;
113  * UART2_Handle uart;
114  * UART2_Params uartParams;
115  *
116  * // Open an instance of the UART2 driver
117  * UART2_Params_init(&uartParams);
118  * uartParams.baudRate = 115200;
119  * uart = UART2_open(CONFIG_UART0, &uartParams);
120  *
121  * if (uart == NULL) {
122  * // UART2_open() failed
123  * while (1);
124  * }
125  *
126  * // Loop forever echoing
127  * while (1) {
128  * status = UART2_read(uart, &input, 1, &bytesRead);
129  * status = UART2_write(uart, &input, 1, &bytesWritten);
130  * }
131  * @endcode
132  *
133  * Details for the example code above are described in the following
134  * subsections.
135  *
136  * ### Opening the UART2 Driver #
137  *
138  * Opening a UART requires four steps:
139  * 1. Create and initialize a UART2_Params structure.
140  * 2. Fill in the desired parameters.
141  * 3. Call UART2_open(), passing the index of the UART in the UART2_config
142  * structure, and the address of the UART2_Params structure. The
143  * UART2 instance is specified by the index in the UART2_config structure.
144  * 4. Check that the UART2 handle returned by UART2_open() is non-NULL,
145  * and save it. The handle will be used to read and write to the
146  * UART you just opened.
147  *
148  * Only one UART index can be used at a time; calling UART2_open() a second
149  * time with the same index previosly passed to UART2_open() will result in
150  * an error. You can, though, re-use the index if the instance is closed
151  * via UART2_close().
152  * In the previous example code, CONFIG_UART0 is passed to UART2_open().
153  * This macro is defined in the example's ti_drivers_config.h file.
154  *
155  *
156  * ### Modes of Operation #
157  *
158  * The UART driver can operate in blocking, callback, or polling mode, by
159  * setting the writeMode and readMode parameters passed to UART2_open().
160  * If these parameters are not set, as in the example code, the UART2
161  * driver defaults to blocking mode. Options for the writeMode and
162  * readMode parameters are #UART2_Mode_BLOCKING, #UART2_Mode_CALLBACK, and
163  * #UART2_Mode_POLLING:
164  *
165  * - #UART2_Mode_BLOCKING uses a semaphore to block while data is being sent.
166  * The context of calling UART2_read() or UART2_write() must be a Task when
167  * using #UART2_Mode_BLOCKING. The UART2_write() or UART2_read() call
168  * will block until all data is sent or received, or the write timeout or
169  * read timeout expires, whichever happens first.
170  *
171  * - #UART2_Mode_CALLBACK is non-blocking and UART2_read() and UART2_write()
172  * will return while data is being sent in the context of a hardware
173  * interrupt. When the read or write finishes, the UART2 driver will call
174  * the user's callback function. In some cases, the UART data transfer
175  * may have been canceled, so the number of bytes sent/received are
176  * passed to the callback function. Your implementation of the callback
177  * function can use this information as needed.
178  * Since the user's callback may be called in the context of a SWI,
179  * the callback function must not make any RTOS blocking calls.
180  * The buffer passed to UART2_write() in #UART2_Mode_CALLBACK is not copied.
181  * The buffer must remain coherent until all the characters have been sent
182  * (ie until the tx callback has been called with a byte count equal to
183  * that passed to UART2_write()).
184  *
185  * ### Reading and Writing data #
186  *
187  * The example code reads one byte frome the UART instance, and then writes
188  * one byte back to the same instance:
189  *
190  * @code
191  * status = UART2_read(uart, &input, 1, &bytesRead);
192  * status = UART2_write(uart, &input, 1, &bytesWritten);
193  * @endcode
194  *
195  * The UART2 driver allows full duplex data transfers. Therefore, it is
196  * possible to call UART2_read() and UART2_write() at the same time (for
197  * either blocking or callback modes). It is not possible, however,
198  * to issue multiple concurrent operations in the same direction.
199  * For example, if one thread calls UART2_read(uart0, buffer0...),
200  * any other thread attempting UART2_read(uart0, buffer1...) will result in
201  * an error of UART2_STATUS_EINUSE, until all the data from the first
202  * UART2_read() has been transferred to buffer0. This applies to blocking,
203  * callback, and polling modes. So applications must either synchronize
204  * UART2_read() (or UART2_write()) calls that use the same UART handle, or
205  * check for the UART2_STATUS_EINUSE return code indicating that a transfer is
206  * still ongoing.
207  *
208  * <hr>
209  * @anchor ti_drivers_UART2_Configuration
210  * # Configuration
211  *
212  * Refer to the @ref driver_configuration "Driver's Configuration" section
213  * for driver configuration information.
214  * <hr>
215  *
216  * ============================================================================
217  */
218 
219 #ifndef ti_drivers_UART2__include
220 #define ti_drivers_UART2__include
221 
222 #include <stddef.h>
223 #include <stdint.h>
224 
225 #ifdef __cplusplus
226 extern "C" {
227 #endif
228 
235 #define UART2_STATUS_SUCCESS (0)
236 
240 #define UART2_STATUS_SREADTIMEOUT (1)
241 
245 #define UART2_STATUS_EFRAMING (-1)
246 
250 #define UART2_STATUS_EPARITY (-2)
251 
255 #define UART2_STATUS_EBREAK (-4)
256 
260 #define UART2_STATUS_EOVERRUN (-8)
261 
265 #define UART2_STATUS_EINUSE (-9)
266 
270 #define UART2_STATUS_EINVALID (-10)
271 
275 #define UART2_STATUS_EFAIL (-11)
276 
280 #define UART2_STATUS_EMEMORY (-12)
281 
285 #define UART2_STATUS_ETIMEOUT (-13)
286 
290 #define UART2_STATUS_ECANCELLED (-14)
291 
295 #define UART2_STATUS_ENOTOPEN (-15)
296 
302 #define UART2_WAIT_FOREVER (~(0U))
303 
307 typedef struct UART2_Config *UART2_Handle;
308 
326 typedef void (*UART2_Callback) (UART2_Handle handle, void *buf, size_t count,
327  void *userArg, int_fast16_t status);
328 
334 typedef enum {
340 
347 
354 } UART2_Mode;
355 
371 typedef enum {
374 
378 
384 typedef enum {
389 } UART2_DataLen;
390 
396 typedef enum {
400 
406 typedef enum {
412 } UART2_Parity;
413 
422 typedef struct {
423  UART2_Mode readMode;
424  UART2_Mode writeMode;
425  uint32_t readTimeout;
426  uint32_t writeTimeout;
429  UART2_ReadReturnMode readReturnMode;
430  uint32_t baudRate;
434  void *userArg;
435 } UART2_Params;
436 
441 typedef void (*UART2_CloseFxn) (UART2_Handle handle);
442 
446 typedef void (*UART2_FlushRxFxn) (UART2_Handle handle);
447 
452 typedef UART2_Handle (*UART2_OpenFxn) (uint_least8_t index, UART2_Params *params);
453 
458 typedef int_fast16_t (*UART2_ReadFxn) (UART2_Handle handle, void *buffer,
459  size_t size, size_t *bytesRead);
460 
465 typedef void (*UART2_ReadCancelFxn) (UART2_Handle handle);
466 
471 typedef int_fast16_t (*UART2_WriteFxn) (UART2_Handle handle,
472  const void *buffer, size_t size, size_t *bytesWritten);
473 
478 typedef void (*UART2_WriteCancelFxn) (UART2_Handle handle);
479 
485 typedef struct {
488 
491 
494 
497 
500 
503 
507 
515 typedef struct UART2_Config {
518 
520  void *object;
521 
523  void const *hwAttrs;
524 } UART2_Config;
525 
538 extern void UART2_close(UART2_Handle handle);
539 
550 extern void UART2_flushRx(UART2_Handle handle);
551 
567 extern UART2_Handle UART2_open(uint_least8_t index, UART2_Params *params);
568 
588 extern void UART2_Params_init(UART2_Params *params);
589 
654 extern int_fast16_t UART2_read(UART2_Handle handle, void *buffer, size_t size,
655  size_t *bytesRead);
656 
678 extern void UART2_readCancel(UART2_Handle handle);
679 
735 extern int_fast16_t UART2_write(UART2_Handle handle, const void *buffer,
736  size_t size, size_t *bytesWritten);
737 
756 extern void UART2_writeCancel(UART2_Handle handle);
757 
758 #ifdef __cplusplus
759 }
760 #endif
761 
762 #endif /* ti_drivers_UART2__include */
UART2_ReadCancelFxn readCancelFxn
Definition: UART2.h:496
Definition: UART2.h:385
UART2_ReadReturnMode
UART2 return mode settings.
Definition: UART2.h:371
void(* UART2_CloseFxn)(UART2_Handle handle)
A function pointer to a driver specific implementation of UART2_CloseFxn().
Definition: UART2.h:441
void UART2_readCancel(UART2_Handle handle)
Function that cancels a UART2_read() function call.
UART2_ReadReturnMode readReturnMode
Definition: UART2.h:429
Definition: UART2.h:346
UART2_Parity
UART2 parity type settings.
Definition: UART2.h:406
void(* UART2_FlushRxFxn)(UART2_Handle handle)
A function to flush the RX data currently in the FIFO.
Definition: UART2.h:446
UART2_ReadFxn readFxn
Definition: UART2.h:493
UART2_WriteCancelFxn writeCancelFxn
Definition: UART2.h:502
int_fast16_t UART2_write(UART2_Handle handle, const void *buffer, size_t size, size_t *bytesWritten)
Function that writes data to a UART.
Definition: UART2.h:410
Definition: UART2.h:397
Definition: UART2.h:408
UART2_Callback readCallback
Definition: UART2.h:427
void * userArg
Definition: UART2.h:434
struct UART2_Config UART2_Config
UART2 Global configuration.
Definition: UART2.h:386
void * object
Definition: UART2.h:520
struct UART2_Config * UART2_Handle
A handle that is returned from a UART2_open() call.
Definition: UART2.h:307
Definition: UART2.h:373
Definition: UART2.h:388
The definition of a UART2 function table that contains the required set of functions to control a spe...
Definition: UART2.h:485
UART2_StopBits stopBits
Definition: UART2.h:432
UART2_Mode writeMode
Definition: UART2.h:424
void(* UART2_WriteCancelFxn)(UART2_Handle handle)
A function pointer to a driver specific implementation of UART2_WriteCancelFxn(). ...
Definition: UART2.h:478
UART2_Parity parityType
Definition: UART2.h:433
Definition: UART2.h:339
UART2 Global configuration.
Definition: UART2.h:515
Definition: UART2.h:387
void UART2_flushRx(UART2_Handle handle)
Function to flush data in the UART RX FIFO.
UART2_Mode
UART2 mode settings.
Definition: UART2.h:334
UART2_Handle UART2_open(uint_least8_t index, UART2_Params *params)
Function to initialize a given UART peripheral.
UART2_FlushRxFxn flushRxFxn
Definition: UART2.h:505
void UART2_close(UART2_Handle handle)
Function to close a UART peripheral specified by the UART2 handle.
Definition: UART2.h:353
Definition: UART2.h:398
UART2_Callback writeCallback
Definition: UART2.h:428
UART2 Parameters.
Definition: UART2.h:422
uint32_t writeTimeout
Definition: UART2.h:426
Definition: UART2.h:409
UART2_StopBits
UART2 stop bit settings.
Definition: UART2.h:396
UART2_WriteFxn writeFxn
Definition: UART2.h:499
int_fast16_t(* UART2_WriteFxn)(UART2_Handle handle, const void *buffer, size_t size, size_t *bytesWritten)
A function pointer to a driver specific implementation of UART2_WriteFxn().
Definition: UART2.h:471
UART2_CloseFxn closeFxn
Definition: UART2.h:487
void UART2_Params_init(UART2_Params *params)
Function to initialize the UART2_Params struct to its defaults.
UART2_DataLen dataLength
Definition: UART2.h:431
int_fast16_t UART2_read(UART2_Handle handle, void *buffer, size_t size, size_t *bytesRead)
Function that reads data from a UART.
void const * hwAttrs
Definition: UART2.h:523
uint32_t readTimeout
Definition: UART2.h:425
Definition: UART2.h:407
UART2_FxnTable const * fxnTablePtr
Definition: UART2.h:517
int_fast16_t(* UART2_ReadFxn)(UART2_Handle handle, void *buffer, size_t size, size_t *bytesRead)
A function pointer to a driver specific implementation of UART2_ReadFxn().
Definition: UART2.h:458
UART2_Handle(* UART2_OpenFxn)(uint_least8_t index, UART2_Params *params)
A function pointer to a driver specific implementation of UART2_OpenFxn().
Definition: UART2.h:452
Definition: UART2.h:376
void(* UART2_Callback)(UART2_Handle handle, void *buf, size_t count, void *userArg, int_fast16_t status)
The definition of a callback function used by the UART2 driver when used in UART2_Mode_CALLBACK The c...
Definition: UART2.h:326
UART2_Mode readMode
Definition: UART2.h:423
Definition: UART2.h:411
UART2_DataLen
UART2 data length settings.
Definition: UART2.h:384
void UART2_writeCancel(UART2_Handle handle)
Function that cancels a UART2_write() function call.
UART2_OpenFxn openFxn
Definition: UART2.h:490
void(* UART2_ReadCancelFxn)(UART2_Handle handle)
A function pointer to a driver specific implementation of UART2_ReadCancelFxn().
Definition: UART2.h:465
uint32_t baudRate
Definition: UART2.h:430
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale