UART2.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022, 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  * The UART2 driver is an updated version of the UART driver. The name
40  * UART2 was given due to changes in the API, to support backwards
41  * compatibility with applications using the existing UART driver.
42  * Key differences between the UART and UART2 drivers:
43  * - UART2 has both RX and TX ring buffers for receiving/sending data.
44  * - UART2 uses DMA to transfer data between the UART FIFOs and the
45  * RX and TX ring buffers (in nonblocking mode). In blocking mode and
46  * callback mode, DMA will transfer data straight between the hardware
47  * FIFO and the source/destination buffer supplied by the application.
48  * NOTE: If the source-buffer for a TX operation resides in flash,
49  * the driver will constrain the flash to remain on during idle.
50  * - The UART2 APIs for reading and writing data have been made more
51  * posix-like.
52  * - UART2 provides for event notification, allowing the application
53  * to receive TX start and completion events, and RX error events.
54  * @note These events are synchronous to what can be observed on the data
55  * lines. A UART2_EVENT_TX_FINISHED event will for example only occur
56  * after all data has been shifted from the hardware FIFO out onto the
57  * TX-pin. In contrast, read and write-callbacks are invoked when the
58  * driver has finished writing data into the hardware FIFO.
59  *
60  * To use the UART2 driver, ensure that the correct driver library for your
61  * device is linked in and include this header file as follows:
62  * @code
63  * #include <ti/drivers/UART2.h>
64  * @endcode
65  *
66  * This module serves as the main interface for applications. Its purpose
67  * is to implement common code between the device specific implementations
68  * of UART2. Any device specific code that differs from the common code is
69  * called through functions prefaced with the "UART2_" naming convention.
70  * These functions are implemented in each device specific implementation.
71  *
72  * @anchor ti_drivers_UART2_Overview
73  * # Overview
74  * A UART is used to translate data between the chip and a serial port.
75  * The UART2 driver simplifies reading and writing to any of the UART
76  * peripherals on the board, with multiple modes of operation and performance.
77  * These include blocking and nonblocking modes.
78  *
79  * The UART2 driver interface provides device independent APIs, data types,
80  * and macros. The APIs in this driver serve as an interface to a typical RTOS
81  * application. The specific peripheral implementations are responsible for
82  * creating all the RTOS specific primitives to allow for thread-safe
83  * operation.
84  *
85  * <hr>
86  * @anchor ti_drivers_UART2_Usage
87  * # Usage
88  *
89  * This documentation provides a basic @ref ti_drivers_UART2_Synopsis
90  * "usage summary" and a set of @ref ti_drivers_UART2_Examples "examples"
91  * in the form of commented code fragments. Detailed descriptions of the
92  * APIs are provided in subsequent sections.
93  *
94  * @anchor ti_drivers_UART2_Synopsis
95  * ## Synopsis
96  * @anchor ti_drivers_UART2_Synopsis_Code
97  * @code
98  * // Import the UART2 driver definitions
99  * #include <ti/drivers/UART2.h>
100  *
101  * // Initialize UART2 parameters
102  * UART2_Params params;
103  * UART2_Params_init(&params);
104  * params.baudRate = 9600;
105  * params.readMode = UART2_Mode_BLOCKING;
106  * params.writeMode = UART2_Mode_BLOCKING;
107  *
108  * // Open the UART
109  * UART2_Handle uart;
110  * uart = UART2_open(CONFIG_UART0, &params);
111  *
112  * // Enable receiver, inhibit low power mode
113  * UART2_rxEnable(uart);
114  *
115  * // Read from the UART.
116  * size_t bytesRead;
117  * uint8_t buffer[BUFSIZE];
118  * int32_t status;
119  * status = UART2_read(uart, buffer, BUFSIZE, &bytesRead);
120  *
121  * // Write to the UART
122  * size_t bytesWritten;
123  * status = UART2_write(uart, buffer, BUFSIZE, &bytesWritten);
124  *
125  * // Close the UART
126  * UART2_close(uart);
127  * @endcode
128  *
129  * <hr>
130  * @anchor ti_drivers_UART2_Examples
131  * # Examples
132  * The following code example opens a UART instance, reads
133  * a byte from the UART, and then writes the byte back to the UART.
134  *
135  * @code
136  * char input;
137  * UART2_Handle uart;
138  * UART2_Params uartParams;
139  *
140  * // Open an instance of the UART2 driver
141  * UART2_Params_init(&uartParams);
142  * uartParams.baudRate = 115200;
143  * uart = UART2_open(CONFIG_UART0, &uartParams);
144  *
145  * if (uart == NULL) {
146  * // UART2_open() failed
147  * while (1);
148  * }
149  *
150  * // Enable receiver, inhibit low power mode
151  * UART2_rxEnable(uart);
152  *
153  * // Loop forever echoing
154  * while (1) {
155  * status = UART2_read(uart, &input, 1, &bytesRead);
156  * status = UART2_write(uart, &input, 1, &bytesWritten);
157  * }
158  * @endcode
159  *
160  * Details for the example code above are described in the following
161  * subsections.
162  *
163  * ### Opening the UART2 Driver #
164  *
165  * Opening a UART requires four steps:
166  * 1. Create and initialize a UART2_Params structure.
167  * 2. Fill in the desired parameters.
168  * 3. Call UART2_open(), passing the index of the UART in the UART2_config
169  * structure, and the address of the UART2_Params structure. The
170  * UART2 instance is specified by the index in the UART2_config structure.
171  * 4. Check that the UART2 handle returned by UART2_open() is non-NULL,
172  * and save it. The handle will be used to read and write to the
173  * UART you just opened.
174  *
175  * Only one UART index can be used at a time; calling UART2_open() a second
176  * time with the same index previosly passed to UART2_open() will result in
177  * an error. You can, though, re-use the index if the instance is closed
178  * via UART2_close().
179  * In the previous example code, CONFIG_UART0 is passed to UART2_open().
180  * This macro is defined in the example's ti_drivers_config.h file.
181  *
182  *
183  * ### Modes of Operation #
184  *
185  * The UART driver can operate in blocking, nonblocking, or callback mode, by
186  * setting the writeMode and readMode parameters passed to UART2_open().
187  * If these parameters are not set, as in the example code, the UART2
188  * driver defaults to blocking mode. Options for the writeMode and
189  * readMode parameters are #UART2_Mode_BLOCKING, #UART2_Mode_NONBLOCKING, and
190  * #UART2_Mode_CALLBACK:
191  *
192  * - #UART2_Mode_BLOCKING uses a semaphore to block while data is being sent,
193  * or while waiting for some data to be received. The context of calling
194  * UART2_read() and UART2_write() in blocking mode must always be a Task.
195  * The UART2_write() call will block until all data has been transmitted
196  * onto the TX pin. The UART2_read() calls can be configured to
197  * have two different behaviors, using the #UART2_ReadReturnMode of the
198  * #UART2_Params. In #UART2_ReadReturnMode_FULL (the default),
199  * UART2_read() will block until the requested number of bytes has been
200  * received. In #UART2_ReadReturnMode_PARTIAL, UART2_read() will block
201  * until either the requested number of bytes has been received,
202  * or a UART hardware read timeout has occurred. Using
203  * UART2_ReadReturnMode_PARTIAL is a good choice if the number of
204  * incoming data bytes is unknown. In UART2_Mode_BLOCKING,
205  * UART2_read() will always return at least some data.
206  * UART2_readTimeout() can be used to specify a timeout in system
207  * clock ticks, to wait for data.
208  * UART2_readTimeout() will return when all data is received, or
209  * the specified timeout expires, or, if using the mode
210  * UART2_ReadReturnMode_PARTIAL, a hardware read timeout occurs,
211  * whichever happens first.
212  *
213  * - #UART2_Mode_NONBLOCKING does not block waiting for data to be sent
214  * or received. UART2_write() and UART2_read() will return immediately
215  * having transferred as much data as the driver can immediately accept
216  * or has available, respectively. If no data can be accepted or
217  * received, UART2_write() and UART2_read() return UART2_STATUS_EAGAIN.
218  *
219  * - #UART2_Mode_CALLBACK is nonblocking and UART2_read() and UART2_write()
220  * will return while data is being sent in the context of a hardware
221  * interrupt. When all data has been read from, or written to the hardware
222  * FIFO, the UART2 driver will call the user's callback function, and the
223  * driver is ready to accept another read or write operation.
224  * @note When transmitting, it is therefore not guaranteed that all data has
225  * been shifted out to the TX pin when the write-callback is invoked. This is
226  * instead signalled by the UART2_EVENT_TX_FINISHED event.
227  * In some cases, the UART data transfer may have been cancelled,
228  * so the number of bytes sent/received are passed to the callback function.
229  * Your implementation of the callback function can use this information as needed.
230  * Since the user's callback may be called in the context of a hardware
231  * interrupt, the callback function must not make any RTOS blocking calls.
232  * The buffer passed to UART2_write() in UART2_Mode_CALLBACK must remain
233  * coherent until all the characters have been sent
234  * (ie until the tx callback has been called with a byte count equal to
235  * that passed to UART2_write()).
236  *
237  * ### Enabling the Receiver #
238  *
239  * The example code enables the collection of data into the RX ring buffer
240  * \a before the first call to UART2_read():
241  *
242  * @code
243  * UART2_rxEnable(uart);
244  * @endcode
245  *
246  * Note that this call is not necessary if the first UART2_read() is called
247  * in time to prevent the RX FIFO from overrun, or if flow control is used.
248  *
249  * ### Reading and Writing Data #
250  *
251  * The example code reads one byte frome the UART instance, and then writes
252  * one byte back to the same instance:
253  *
254  * @code
255  * status = UART2_read(uart, &input, 1, &bytesRead);
256  * status = UART2_write(uart, &input, 1, &bytesWritten);
257  * @endcode
258  *
259  * The UART2 driver allows full duplex data transfers. Therefore, it is
260  * possible to call UART2_read() and UART2_write() at the same time.
261  * It is not possible, however,
262  * to issue multiple concurrent operations in the same direction.
263  * For example, if one thread calls UART2_read(uart0, buffer0...),
264  * any other thread attempting UART2_read(uart0, buffer1...) will result in
265  * an error of UART2_STATUS_EINUSE, until all the data from the first
266  * UART2_read() has been transferred to buffer0. This applies to blocking,
267  * callback, and nonblocking modes. So applications must either synchronize
268  * UART2_read() (or UART2_write()) calls that use the same UART handle, or
269  * check for the UART2_STATUS_EINUSE return code indicating that a transfer is
270  * still ongoing.
271  *
272  * <hr>
273  * @anchor ti_drivers_UART2_Configuration
274  * # Configuration
275  *
276  * Refer to the @ref driver_configuration "Driver's Configuration" section
277  * for driver configuration information.
278  * <hr>
279  *
280  * ============================================================================
281  */
282 
283 #ifndef ti_drivers_UART2__include
284 #define ti_drivers_UART2__include
285 
286 #include <stddef.h>
287 #include <stdint.h>
288 #include <stdbool.h>
289 
290 #include <ti/drivers/dpl/ClockP.h>
291 #include <ti/drivers/dpl/HwiP.h>
292 #include <ti/drivers/dpl/SemaphoreP.h>
294 
295 #ifdef __cplusplus
296 extern "C" {
297 #endif
298 
302 #define UART2_FLOWCTRL_NONE 0
303 
307 #define UART2_FLOWCTRL_HARDWARE 1
308 
315 #define UART2_STATUS_SUCCESS (0)
316 
320 #define UART2_STATUS_SREADTIMEOUT (1)
321 
325 #define UART2_STATUS_EFRAMING (-1)
326 
330 #define UART2_STATUS_EPARITY (-2)
331 
335 #define UART2_STATUS_EBREAK (-4)
336 
340 #define UART2_STATUS_EOVERRUN (-8)
341 
345 #define UART2_STATUS_EINUSE (-9)
346 
350 #define UART2_STATUS_EINVALID (-10)
351 
355 #define UART2_STATUS_EFAIL (-11)
356 
360 #define UART2_STATUS_EMEMORY (-12)
361 
365 #define UART2_STATUS_ETIMEOUT (-13)
366 
370 #define UART2_STATUS_ECANCELLED (-14)
371 
375 #define UART2_STATUS_ENOTOPEN (-15)
376 
381 #define UART2_STATUS_EAGAIN (-16)
382 
392 #define UART2_EVENT_OVERRUN (0x08)
393 
397 #define UART2_EVENT_BREAK (0x04)
398 
402 #define UART2_EVENT_PARITY (0x02)
403 
407 #define UART2_EVENT_FRAMING (0x01)
408 
414 #define UART2_EVENT_TX_BEGIN (0x10)
415 
421 #define UART2_EVENT_TX_FINISHED (0x20)
422 
428 #define UART2_WAIT_FOREVER (~(0U))
429 
433 typedef struct UART2_Config_ *UART2_Handle;
434 
452 typedef void (*UART2_Callback)(UART2_Handle handle, void *buf, size_t count, void *userArg, int_fast16_t status);
453 
475 typedef void (*UART2_EventCallback)(UART2_Handle handle, uint32_t event, uint32_t data, void *userArg);
476 
482 typedef enum
483 {
490 
502 
511  /* Added for backwards compatibility. */
512  UART2_Mode_POLLING = UART2_Mode_NONBLOCKING
514 } UART2_Mode;
515 
531 typedef enum
532 {
535 
539 
545 typedef enum
546 {
551 } UART2_DataLen;
552 
558 typedef enum
559 {
563 
569 typedef enum
570 {
576 } UART2_Parity;
577 
586 typedef struct
587 {
588  UART2_Mode readMode;
589  UART2_Mode writeMode;
593  uint32_t eventMask;
594  UART2_ReadReturnMode readReturnMode;
595  uint32_t baudRate;
599  void *userArg;
600 } UART2_Params;
601 
603 #define UART2_BASE_OBJECT \
604  /* UART2 state variable */ \
605  struct \
606  { \
607  uint32_t overrunCount; \
608  UART2_Mode readMode; \
609  UART2_Mode writeMode; \
610  UART2_ReadReturnMode readReturnMode; \
611  bool opened; \
612  bool txEnabled; \
613  bool rxEnabled; \
614  bool rxCancelled; \
615  bool txCancelled; \
616  bool readTimedOut; \
617  bool writeTimedOut; \
618  bool overrunActive; \
619  bool inReadCallback; \
620  bool readCallbackPending; \
621  bool inWriteCallback; \
622  bool writeCallbackPending; \
623  bool readToRingbuf; \
624  } state; \
625  \
626  HwiP_Struct hwi; \
627  uint32_t baudRate; \
628  UART2_DataLen dataLength; \
629  UART2_StopBits stopBits; \
630  UART2_Parity parityType; \
631  int32_t rxStatus; \
632  int32_t txStatus; \
633  UART2_EventCallback eventCallback; \
634  uint32_t eventMask; \
635  void *userArg; \
636  \
637  /* UART read variables */ \
638  RingBuf_Object rxBuffer; \
639  bool readInUse; \
640  unsigned char *readBuf; \
641  size_t readSize; \
642  size_t readCount; \
643  size_t rxSize; \
644  size_t bytesRead; \
645  SemaphoreP_Struct readSem; \
646  UART2_Callback readCallback; \
647  \
648  /* UART write variables */ \
649  RingBuf_Object txBuffer; \
650  volatile bool writeInUse; \
651  const unsigned char *writeBuf; \
652  size_t writeSize; \
653  size_t writeCount; \
654  size_t txSize; \
655  size_t bytesWritten; \
656  SemaphoreP_Struct writeSem; \
657  UART2_Callback writeCallback; \
658  \
659  /* For Power management */ \
660  unsigned int powerMgrId; \
661 
668 typedef struct
669 {
670  UART2_BASE_OBJECT
671 } UART2_Object;
675 #define UART2_BASE_HWATTRS \
676  \
677  uint32_t baseAddr; \
678  \
679  int intNum; \
680  \
681  uint8_t intPriority; \
682  \
683  unsigned char *rxBufPtr; \
684  \
685  size_t rxBufSize; \
686  \
687  unsigned char *txBufPtr; \
688  \
689  size_t txBufSize; \
690  \
691  uint32_t flowControl; \
692  \
693  uint32_t rxPin; \
694  \
695  uint32_t txPin; \
696  \
697  uint32_t ctsPin; \
698  \
699  uint32_t rtsPin; \
700 
706 typedef struct
707 {
708  UART2_BASE_HWATTRS
709 } UART2_HWAttrs;
719 typedef struct UART2_Config_
720 {
722  void *object;
723 
725  void const *hwAttrs;
726 } UART2_Config;
727 
728 extern const UART2_Config UART2_config[];
729 extern const uint_least8_t UART2_count;
730 
743 extern void UART2_close(UART2_Handle handle);
744 
756 extern void UART2_flushRx(UART2_Handle handle);
757 
770 extern size_t UART2_getRxCount(UART2_Handle handle);
771 
787 extern UART2_Handle UART2_open(uint_least8_t index, UART2_Params *params);
788 
810 
879 extern int_fast16_t UART2_read(UART2_Handle handle, void *buffer, size_t size, size_t *bytesRead);
880 
882 extern int_fast16_t __attribute__((weak))
883 UART2_readFull(UART2_Handle handle, void *buffer, size_t size, size_t *bytesRead);
965 extern int_fast16_t UART2_readTimeout(UART2_Handle handle,
966  void *buffer,
967  size_t size,
968  size_t *bytesRead,
969  uint32_t timeout);
970 
992 extern void UART2_readCancel(UART2_Handle handle);
993 
1055 extern int_fast16_t UART2_write(UART2_Handle handle, const void *buffer, size_t size, size_t *bytesWritten);
1056 
1071 extern void UART2_rxDisable(UART2_Handle handle);
1072 
1087 extern void UART2_rxEnable(UART2_Handle handle);
1088 
1158 extern int_fast16_t UART2_writeTimeout(UART2_Handle handle,
1159  const void *buffer,
1160  size_t size,
1161  size_t *bytesWritten,
1162  uint32_t timeout);
1163 
1184 extern void UART2_writeCancel(UART2_Handle handle);
1185 
1186 #ifdef __cplusplus
1187 }
1188 #endif
1189 
1190 #endif /* ti_drivers_UART2__include */
UART2 Global configuration.
Definition: UART2.h:719
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:547
ADC_Params params
Definition: Driver_Init.h:11
UART2_ReadReturnMode
UART2 return mode settings.
Definition: UART2.h:531
const uint_least8_t UART2_count
void UART2_readCancel(UART2_Handle handle)
Function that cancels a UART2_read() function call.
UART2_ReadReturnMode readReturnMode
Definition: UART2.h:594
Definition: UART2.h:501
size_t UART2_getRxCount(UART2_Handle handle)
Get the number of bytes available in the circular buffer.
UART2_Parity
UART2 parity type settings.
Definition: UART2.h:569
const UART2_Config UART2_config[]
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:574
Definition: UART2.h:560
Definition: UART2.h:572
UART2_Callback readCallback
Definition: UART2.h:590
void * userArg
Definition: UART2.h:599
Definition: UART2.h:548
Definition: UART2.h:534
struct UART2_Config_ * UART2_Handle
A handle that is returned from a UART2_open() call.
Definition: UART2.h:433
Definition: UART2.h:550
void const * hwAttrs
Definition: UART2.h:725
uint32_t eventMask
Definition: UART2.h:593
UART2_StopBits stopBits
Definition: UART2.h:597
void * object
Definition: UART2.h:722
UART2_Mode writeMode
Definition: UART2.h:589
void(* UART2_EventCallback)(UART2_Handle handle, uint32_t event, uint32_t data, void *userArg)
The definition of a callback function used by the UART driver. The callback can occur in task or inte...
Definition: UART2.h:475
UART2_Parity parityType
Definition: UART2.h:598
Definition: UART2.h:489
Definition: UART2.h:549
void UART2_flushRx(UART2_Handle handle)
Function to flush data in the UART RX FIFO.
UART2_Mode
UART2 mode settings.
Definition: UART2.h:482
UART2_Handle UART2_open(uint_least8_t index, UART2_Params *params)
Function to initialize a given UART peripheral.
void UART2_close(UART2_Handle handle)
Function to close a UART peripheral specified by the UART2 handle.
void UART2_rxDisable(UART2_Handle handle)
Function that disables collecting of RX data into the circular buffer.
Definition: UART2.h:561
UART2_Callback writeCallback
Definition: UART2.h:591
UART2 Parameters.
Definition: UART2.h:586
struct UART2_Config_ UART2_Config
UART2 Global configuration.
Definition: UART2.h:573
UART2_StopBits
UART2 stop bit settings.
Definition: UART2.h:558
void UART2_Params_init(UART2_Params *params)
Function to initialize the UART2_Params struct to its defaults.
UART2_DataLen dataLength
Definition: UART2.h:596
int_fast16_t UART2_read(UART2_Handle handle, void *buffer, size_t size, size_t *bytesRead)
Function that reads data from a UART.
void UART2_rxEnable(UART2_Handle handle)
Function that enables collecting of RX data into the circular buffer.
UART2_EventCallback eventCallback
Definition: UART2.h:592
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:571
Definition: UART2.h:509
Definition: UART2.h:537
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:452
UART2_Mode readMode
Definition: UART2.h:588
Definition: UART2.h:575
UART2_DataLen
UART2 data length settings.
Definition: UART2.h:545
void UART2_writeCancel(UART2_Handle handle)
Function that cancels a UART2_write() function call.
uint32_t baudRate
Definition: UART2.h:595
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale