UART2.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-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 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  *
85  * // Open the UART
86  * UART2_Handle uart;
87  * uart = UART2_open(CONFIG_UART0, &params);
88  *
89  * // Read from the UART.
90  * size_t bytesRead;
91  * uint8_t buffer[BUFSIZE];
92  * int32_t status;
93  * status = UART2_read(uart, buffer, BUFSIZE, &bytesRead);
94  *
95  * // Write to the UART
96  * size_t bytesWritten;
97  * status = UART2_write(uart, buffer, BUFSIZE, &bytesWritten);
98  *
99  * // Close the UART
100  * UART2_close(uart);
101  * @endcode
102  *
103  * <hr>
104  * @anchor ti_drivers_UART2_Examples
105  * # Examples
106  * The following code example opens a UART instance, reads
107  * a byte from the UART, and then writes the byte back to the UART.
108  *
109  * @code
110  * char input;
111  * UART2_Handle uart;
112  * UART2_Params uartParams;
113  *
114  * // Open an instance of the UART2 driver
115  * UART2_Params_init(&uartParams);
116  * uartParams.baudRate = 115200;
117  * uart = UART2_open(CONFIG_UART0, &uartParams);
118  *
119  * if (uart == NULL) {
120  * // UART2_open() failed
121  * while (1);
122  * }
123  *
124  * // Loop forever echoing
125  * while (1) {
126  * status = UART2_read(uart, &input, 1, &bytesRead);
127  * status = UART2_write(uart, &input, 1, &bytesWritten);
128  * }
129  * @endcode
130  *
131  * Details for the example code above are described in the following
132  * subsections.
133  *
134  * ### Opening the UART2 Driver #
135  *
136  * Opening a UART requires four steps:
137  * 1. Create and initialize a UART2_Params structure.
138  * 2. Fill in the desired parameters.
139  * 3. Call UART2_open(), passing the index of the UART in the UART2_config
140  * structure, and the address of the UART2_Params structure. The
141  * UART2 instance is specified by the index in the UART2_config structure.
142  * 4. Check that the UART2 handle returned by UART2_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 UART2_open() a second
147  * time with the same index previosly passed to UART2_open() will result in
148  * an error. You can, though, re-use the index if the instance is closed
149  * via UART2_close().
150  * In the previous example code, CONFIG_UART0 is passed to UART2_open().
151  * This macro is defined in the example's ti_drivers_config.h file.
152  *
153  *
154  * ### Modes of Operation #
155  *
156  * The UART driver can operate in blocking, callback, or polling mode, by
157  * setting the writeMode and readMode parameters passed to UART2_open().
158  * If these parameters are not set, as in the example code, the UART2
159  * driver defaults to blocking mode. Options for the writeMode and
160  * readMode parameters are #UART2_Mode_BLOCKING, #UART2_Mode_CALLBACK, and
161  * #UART2_Mode_POLLING:
162  *
163  * - #UART2_Mode_BLOCKING uses a semaphore to block while data is being sent.
164  * The context of calling UART2_read() and UART2_write() must be a Task when
165  * using #UART2_Mode_BLOCKING. The UART2_write() or UART2_read() call
166  * will block until all data is sent or received, or an error occurs (e.g.,
167  * framing or FIFO overrun). In #UART2_Mode_BLOCKING, UART2_readTimeout()
168  * can be used to specify a timeout in system clock ticks, to wait for
169  * data. UART2_readTimeout() will return when all data is received, or
170  * the specified timeout expires, or an error occurs, whichever happens
171  * first.
172  *
173  * - #UART2_Mode_CALLBACK is non-blocking and UART2_read() and UART2_write()
174  * will return while data is being sent in the context of a hardware
175  * interrupt. When the read or write finishes, the UART2 driver will call
176  * the user's callback function. In some cases, the UART data transfer
177  * may have been cancelled, so the number of bytes sent/received are
178  * passed to the callback function. Your implementation of the callback
179  * function can use this information as needed.
180  * Since the user's callback may be called in the context of a hardware
181  * interrupt, the callback function must not make any RTOS blocking calls.
182  * The buffer passed to UART2_write() in #UART2_Mode_CALLBACK is not copied.
183  * The buffer must remain coherent until all the characters have been sent
184  * (ie until the tx callback has been called with a byte count equal to
185  * that passed to UART2_write()).
186  *
187  * ### Reading and Writing data #
188  *
189  * The example code reads one byte frome the UART instance, and then writes
190  * one byte back to the same instance:
191  *
192  * @code
193  * status = UART2_read(uart, &input, 1, &bytesRead);
194  * status = UART2_write(uart, &input, 1, &bytesWritten);
195  * @endcode
196  *
197  * The UART2 driver allows full duplex data transfers. Therefore, it is
198  * possible to call UART2_read() and UART2_write() at the same time (for
199  * either blocking or callback modes). It is not possible, however,
200  * to issue multiple concurrent operations in the same direction.
201  * For example, if one thread calls UART2_read(uart0, buffer0...),
202  * any other thread attempting UART2_read(uart0, buffer1...) will result in
203  * an error of UART2_STATUS_EINUSE, until all the data from the first
204  * UART2_read() has been transferred to buffer0. This applies to blocking,
205  * callback, and polling modes. So applications must either synchronize
206  * UART2_read() (or UART2_write()) calls that use the same UART handle, or
207  * check for the UART2_STATUS_EINUSE return code indicating that a transfer is
208  * still ongoing.
209  *
210  * <hr>
211  * @anchor ti_drivers_UART2_Configuration
212  * # Configuration
213  *
214  * Refer to the @ref driver_configuration "Driver's Configuration" section
215  * for driver configuration information.
216  * <hr>
217  *
218  * ============================================================================
219  */
220 
221 #ifndef ti_drivers_UART2__include
222 #define ti_drivers_UART2__include
223 
224 #include <stddef.h>
225 #include <stdint.h>
226 
227 #ifdef __cplusplus
228 extern "C" {
229 #endif
230 
237 #define UART2_STATUS_SUCCESS (0)
238 
242 #define UART2_STATUS_SREADTIMEOUT (1)
243 
247 #define UART2_STATUS_EFRAMING (-1)
248 
252 #define UART2_STATUS_EPARITY (-2)
253 
257 #define UART2_STATUS_EBREAK (-4)
258 
262 #define UART2_STATUS_EOVERRUN (-8)
263 
267 #define UART2_STATUS_EINUSE (-9)
268 
272 #define UART2_STATUS_EINVALID (-10)
273 
277 #define UART2_STATUS_EFAIL (-11)
278 
282 #define UART2_STATUS_EMEMORY (-12)
283 
287 #define UART2_STATUS_ETIMEOUT (-13)
288 
292 #define UART2_STATUS_ECANCELLED (-14)
293 
297 #define UART2_STATUS_ENOTOPEN (-15)
298 
304 #define UART2_WAIT_FOREVER (~(0U))
305 
309 typedef struct UART2_Config_ *UART2_Handle;
310 
328 typedef void (*UART2_Callback) (UART2_Handle handle, void *buf, size_t count,
329  void *userArg, int_fast16_t status);
330 
336 typedef enum {
342 
349 
356 } UART2_Mode;
357 
373 typedef enum {
376 
380 
386 typedef enum {
391 } UART2_DataLen;
392 
398 typedef enum {
402 
408 typedef enum {
414 } UART2_Parity;
415 
424 typedef struct {
425  UART2_Mode readMode;
426  UART2_Mode writeMode;
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,
459  void *buffer, size_t size, size_t *bytesRead, uint32_t timeout);
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  uint32_t timeout);
474 
479 typedef void (*UART2_WriteCancelFxn) (UART2_Handle handle);
480 
486 typedef struct {
489 
492 
495 
498 
501 
504 
508 
516 typedef struct UART2_Config_ {
519 
521  void *object;
522 
524  void const *hwAttrs;
525 } UART2_Config;
526 
527 extern const UART2_Config UART2_config[];
528 extern const uint_least8_t UART2_count;
529 
542 extern void UART2_close(UART2_Handle handle);
543 
554 extern void UART2_flushRx(UART2_Handle handle);
555 
571 extern UART2_Handle UART2_open(uint_least8_t index, UART2_Params *params);
572 
591 extern void UART2_Params_init(UART2_Params *params);
592 
658 extern int_fast16_t UART2_read(UART2_Handle handle, void *buffer, size_t size,
659  size_t *bytesRead);
660 
739 extern int_fast16_t UART2_readTimeout(UART2_Handle handle, void *buffer,
740  size_t size, size_t *bytesRead, uint32_t timeout);
741 
763 extern void UART2_readCancel(UART2_Handle handle);
764 
819 extern int_fast16_t UART2_write(UART2_Handle handle, const void *buffer,
820  size_t size, size_t *bytesWritten);
821 
886 extern int_fast16_t UART2_writeTimeout(UART2_Handle handle, const void *buffer,
887  size_t size, size_t *bytesWritten, uint32_t timeout);
888 
909 extern void UART2_writeCancel(UART2_Handle handle);
910 
911 #ifdef __cplusplus
912 }
913 #endif
914 
915 #endif /* ti_drivers_UART2__include */
UART2 Global configuration.
Definition: UART2.h:516
UART2_ReadCancelFxn readCancelFxn
Definition: UART2.h:497
int_fast16_t UART2_readTimeout(UART2_Handle handle, void *buffer, size_t size, size_t *bytesRead, uint32_t timeout)
Function that reads data from a UART, with a specified timeout for blocking mode. ...
Definition: UART2.h:387
UART2_ReadReturnMode
UART2 return mode settings.
Definition: UART2.h:373
const uint_least8_t UART2_count
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:348
UART2_Parity
UART2 parity type settings.
Definition: UART2.h:408
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:494
const UART2_Config UART2_config[]
UART2_WriteCancelFxn writeCancelFxn
Definition: UART2.h:503
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:412
Definition: UART2.h:399
Definition: UART2.h:410
UART2_Callback readCallback
Definition: UART2.h:427
void * userArg
Definition: UART2.h:434
Definition: UART2.h:388
int_fast16_t(* UART2_WriteFxn)(UART2_Handle handle, const void *buffer, size_t size, size_t *bytesWritten, uint32_t timeout)
A function pointer to a driver specific implementation of UART2_WriteFxn().
Definition: UART2.h:471
Definition: UART2.h:375
struct UART2_Config_ * UART2_Handle
A handle that is returned from a UART2_open() call.
Definition: UART2.h:309
Definition: UART2.h:390
void const * hwAttrs
Definition: UART2.h:524
The definition of a UART2 function table that contains the required set of functions to control a spe...
Definition: UART2.h:486
UART2_StopBits stopBits
Definition: UART2.h:432
void * object
Definition: UART2.h:521
UART2_Mode writeMode
Definition: UART2.h:426
void(* UART2_WriteCancelFxn)(UART2_Handle handle)
A function pointer to a driver specific implementation of UART2_WriteCancelFxn(). ...
Definition: UART2.h:479
UART2_Parity parityType
Definition: UART2.h:433
Definition: UART2.h:341
Definition: UART2.h:389
void UART2_flushRx(UART2_Handle handle)
Function to flush data in the UART RX FIFO.
UART2_Mode
UART2 mode settings.
Definition: UART2.h:336
UART2_Handle UART2_open(uint_least8_t index, UART2_Params *params)
Function to initialize a given UART peripheral.
UART2_FxnTable const * fxnTablePtr
Definition: UART2.h:518
UART2_FlushRxFxn flushRxFxn
Definition: UART2.h:506
int_fast16_t(* UART2_ReadFxn)(UART2_Handle handle, void *buffer, size_t size, size_t *bytesRead, uint32_t timeout)
A function pointer to a driver specific implementation of UART2_ReadFxn().
Definition: UART2.h:458
void UART2_close(UART2_Handle handle)
Function to close a UART peripheral specified by the UART2 handle.
Definition: UART2.h:355
Definition: UART2.h:400
UART2_Callback writeCallback
Definition: UART2.h:428
UART2 Parameters.
Definition: UART2.h:424
struct UART2_Config_ UART2_Config
UART2 Global configuration.
Definition: UART2.h:411
UART2_StopBits
UART2 stop bit settings.
Definition: UART2.h:398
UART2_WriteFxn writeFxn
Definition: UART2.h:500
UART2_CloseFxn closeFxn
Definition: UART2.h:488
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.
int_fast16_t UART2_writeTimeout(UART2_Handle handle, const void *buffer, size_t size, size_t *bytesWritten, uint32_t timeout)
Function that writes data to a UART, with a specified timeout.
Definition: UART2.h:409
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:378
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:328
UART2_Mode readMode
Definition: UART2.h:425
Definition: UART2.h:413
UART2_DataLen
UART2 data length settings.
Definition: UART2.h:386
void UART2_writeCancel(UART2_Handle handle)
Function that cancels a UART2_write() function call.
UART2_OpenFxn openFxn
Definition: UART2.h:491
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-2020, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale