AM273x MCU+ SDK  08.02.00
uart_sci.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * 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
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
48 #ifndef UART_SCI_H_
49 #define UART_SCI_H_
50 
51 /* ========================================================================== */
52 /* Include Files */
53 /* ========================================================================== */
54 
55 #include <stdint.h>
56 #include <kernel/dpl/SystemP.h>
57 #include <kernel/dpl/SemaphoreP.h>
58 #include <kernel/dpl/HwiP.h>
59 #include <drivers/hw_include/cslr.h>
60 #include <drivers/hw_include/cslr_uart.h>
61 #include <drivers/hw_include/hw_types.h>
62 #include <drivers/edma.h>
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 /* ========================================================================== */
69 /* Macros & Typedefs */
70 /* ========================================================================== */
71 
73 typedef void *UART_Handle;
74 
84 #define UART_TRANSFER_STATUS_SUCCESS (0U)
85 
86 #define UART_TRANSFER_STATUS_TIMEOUT (1U)
87 
88 #define UART_TRANSFER_STATUS_ERROR_BI (2U)
89 
90 #define UART_TRANSFER_STATUS_ERROR_FE (3U)
91 
92 #define UART_TRANSFER_STATUS_ERROR_PE (4U)
93 
94 #define UART_TRANSFER_STATUS_ERROR_OE (5U)
95 
96 #define UART_TRANSFER_STATUS_CANCELLED (6U)
97 
98 #define UART_TRANSFER_STATUS_STARTED (7U)
99 
100 #define UART_TRANSFER_STATUS_READ_TIMEOUT (8U)
101 
102 #define UART_TRANSFER_STATUS_ERROR_INUSE (9U)
103 
104 #define UART_TRANSFER_STATUS_ERROR_OTH (10U)
105 
126 #define UART_TRANSFER_MODE_BLOCKING (0U)
127 
131 #define UART_TRANSFER_MODE_CALLBACK (1U)
132 
142 #define UART_LEN_1 (0U)
143 #define UART_LEN_2 (1U)
144 #define UART_LEN_3 (2U)
145 #define UART_LEN_4 (3U)
146 #define UART_LEN_5 (4U)
147 #define UART_LEN_6 (5U)
148 #define UART_LEN_7 (6U)
149 #define UART_LEN_8 (7U)
150 
160 #define UART_STOPBITS_1 (0U)
161 #define UART_STOPBITS_2 (1U)
162 
172 #define UART_PARITY_NONE (0x00U)
173 #define UART_PARITY_ODD (0x01U)
174 #define UART_PARITY_EVEN (0x03U)
175 
186 #define UART_CONFIG_MODE_POLLED (0x00U)
187 #define UART_CONFIG_MODE_INTERRUPT (0x01U)
188 #define UART_CONFIG_MODE_USER_INTR (0x02U)
189 #define UART_CONFIG_MODE_DMA (0x03U)
190 
192 /* ========================================================================== */
193 /* Structures and Enums */
194 /* ========================================================================== */
195 
200 typedef struct
201 {
202  void *buf;
205  uint32_t count;
209  uint32_t timeout;
211  uint32_t status;
213  void *args;
216 
224 typedef void (*UART_CallbackFxn) (UART_Handle handle,
225  UART_Transaction *transaction);
226 
237 typedef struct
238 {
239  uint32_t baudRate;
241  uint32_t dataLength;
243  uint32_t stopBits;
245  uint32_t parityType;
247  uint32_t readMode;
249  uint32_t writeMode;
255  /*
256  * Driver configuration
257  */
258  uint32_t transferMode;
260  uint32_t intrNum;
262  uint8_t intrPriority;
264  uint32_t edmaInst;
266  uint32_t rxEvtNum;
268  uint32_t txEvtNum;
270 } UART_Params;
271 
273 typedef struct
274 {
275  /*
276  * SOC configuration
277  */
278  uint32_t baseAddr;
280  uint32_t inputClkFreq;
282 } UART_Attrs;
283 
290 typedef struct
291 {
292  uint32_t edmaTcc;
294  uint32_t edmaChId;
296  uint32_t edmaParam;
298  uint32_t edmaRegionId;
300  uint32_t edmaBaseAddr;
302  uint32_t isIntEnabled;
307 
308 
309 /* ========================================================================== */
310 /* Internal/Private Structure Declarations */
311 /* ========================================================================== */
312 
316 typedef struct
317 {
318  /*
319  * User parameters
320  */
325  /*
326  * UART write variables
327  */
328  const void *writeBuf;
330  uint32_t writeCount;
334  /*
335  * UART receive variables
336  */
337  void *readBuf;
339  uint32_t readCount;
343  uint32_t rxTimeoutCnt;
345  uint32_t readErrorCnt;
347  /*
348  * UART ransaction status variables
349  */
354  /*
355  * State variables
356  */
357  uint32_t isOpen;
359  void *lock;
373  void *hwiHandle;
382 
383  CSL_sciRegs *pSCIRegs;
391 } UART_Object;
392 
402 typedef struct
403 {
408 } UART_Config;
409 
411 extern UART_Config gUartConfig[];
413 extern uint32_t gUartConfigNum;
414 
415 /* ========================================================================== */
416 /* Global Variables Declarations */
417 /* ========================================================================== */
418 
419 /* None */
420 
421 /* ========================================================================== */
422 /* Function Declarations */
423 /* ========================================================================== */
424 
428 void UART_init(void);
429 
433 void UART_deinit(void);
434 
451 UART_Handle UART_open(uint32_t index, const UART_Params *prms);
452 
462 void UART_close(UART_Handle handle);
463 
502 int32_t UART_write(UART_Handle handle, UART_Transaction *trans);
503 
538 int32_t UART_read(UART_Handle handle, UART_Transaction *trans);
539 
572 
605 
614 UART_Handle UART_getHandle(uint32_t index);
615 
626 
632 static inline void UART_Params_init(UART_Params *prms);
633 
640 static inline void UART_Transaction_init(UART_Transaction *trans);
641 
642 /* ========================================================================== */
643 /* Static Function Definitions */
644 /* ========================================================================== */
645 
646 static inline void UART_Params_init(UART_Params *prms)
647 {
648  if(prms != NULL)
649  {
650  prms->baudRate = 115200U;
651  prms->dataLength = UART_LEN_8;
652  prms->stopBits = UART_STOPBITS_1;
656  prms->readCallbackFxn = NULL;
657  prms->writeCallbackFxn = NULL;
659  prms->intrNum = 210U;
660  prms->intrPriority = 4U;
661  }
662 }
663 
664 static inline void UART_Transaction_init(UART_Transaction *trans)
665 {
666  if(trans != NULL)
667  {
668  trans->buf = NULL;
669  trans->count = 0U;
670  trans->timeout = SystemP_WAIT_FOREVER;
672  trans->args = NULL;
673  }
674 }
675 
676 #ifdef __cplusplus
677 }
678 #endif
679 
680 #endif /* #ifndef UART_SCI_H_ */
681 
UART_deinit
void UART_deinit(void)
This function de-initializes the UART module.
UART_Object::readTransferSem
void * readTransferSem
Definition: uart_sci.h:363
UART_TRANSFER_MODE_BLOCKING
#define UART_TRANSFER_MODE_BLOCKING
UART read/write APIs blocks execution. This mode can only be used when called within a Task context.
Definition: uart_sci.h:126
UART_Object::lockObj
SemaphoreP_Object lockObj
Definition: uart_sci.h:361
UART_Object::shiftJustification
uint8_t shiftJustification
Definition: uart_sci.h:385
UART_Transaction_init
static void UART_Transaction_init(UART_Transaction *trans)
Function to initialize the UART_Transaction struct to its defaults.
Definition: uart_sci.h:664
UART_Object::readTransferSemObj
SemaphoreP_Object readTransferSemObj
Definition: uart_sci.h:366
UART_EdmaParams::edmaParam
uint32_t edmaParam
Definition: uart_sci.h:296
UART_STOPBITS_1
#define UART_STOPBITS_1
Definition: uart_sci.h:160
UART_TRANSFER_STATUS_SUCCESS
#define UART_TRANSFER_STATUS_SUCCESS
Transaction success.
Definition: uart_sci.h:84
UART_Object::pSCIRegs
CSL_sciRegs * pSCIRegs
Definition: uart_sci.h:383
UART_Object::handle
UART_Handle handle
Definition: uart_sci.h:321
UART_Object::hwiHandle
void * hwiHandle
Definition: uart_sci.h:373
UART_Object::writeBuf
const void * writeBuf
Definition: uart_sci.h:328
Edma_IntrObject
EDMA interrupt configuration object. The object is passed to the EDMA_registerIntr() function....
Definition: edma/v0/edma.h:449
SystemP.h
UART_Transaction::timeout
uint32_t timeout
Definition: uart_sci.h:209
UART_Object::rxEdmaParams
UART_EdmaParams rxEdmaParams
Definition: uart_sci.h:377
UART_Handle
void * UART_Handle
A handle that is returned from a UART_open() call.
Definition: uart_sci.h:73
UART_Object::writeTrans
UART_Transaction * writeTrans
Definition: uart_sci.h:352
UART_getHandle
UART_Handle UART_getHandle(uint32_t index)
Function to return a open'ed UART handle given a UART instance index.
UART_writeCancel
int32_t UART_writeCancel(UART_Handle handle, UART_Transaction *trans)
Function to perform UART canceling of current write transaction.
UART_Config
UART global configuration array.
Definition: uart_sci.h:403
UART_Params::writeMode
uint32_t writeMode
Definition: uart_sci.h:249
UART_EdmaParams::edmaTcc
uint32_t edmaTcc
Definition: uart_sci.h:292
UART_Object::writeSizeRemaining
uint32_t writeSizeRemaining
Definition: uart_sci.h:332
UART_CallbackFxn
void(* UART_CallbackFxn)(UART_Handle handle, UART_Transaction *transaction)
The definition of a callback function used by the UART driver when used in UART_TRANSFER_MODE_CALLBAC...
Definition: uart_sci.h:224
UART_Params::dataLength
uint32_t dataLength
Definition: uart_sci.h:241
SystemP_WAIT_FOREVER
#define SystemP_WAIT_FOREVER
Value to use when needing a timeout of infinity or wait forver until resource is available.
Definition: SystemP.h:83
UART_EdmaParams::edmaChId
uint32_t edmaChId
Definition: uart_sci.h:294
UART_Params::intrPriority
uint8_t intrPriority
Definition: uart_sci.h:262
UART_write
int32_t UART_write(UART_Handle handle, UART_Transaction *trans)
Function to perform UART write operation.
UART_Params::readMode
uint32_t readMode
Definition: uart_sci.h:247
SemaphoreP.h
UART_PARITY_NONE
#define UART_PARITY_NONE
Definition: uart_sci.h:172
UART_close
void UART_close(UART_Handle handle)
Function to close a UART peripheral specified by the UART handle.
gUartConfig
UART_Config gUartConfig[]
Externally defined driver configuration array.
UART_Transaction::args
void * args
Definition: uart_sci.h:213
edma.h
UART_EdmaParams::edmaIntrObj
Edma_IntrObject edmaIntrObj
Definition: uart_sci.h:304
UART_Object::uartEdmaHandle
EDMA_Handle uartEdmaHandle
Definition: uart_sci.h:381
UART_Object::isOpen
uint32_t isOpen
Definition: uart_sci.h:357
UART_flushTxFifo
void UART_flushTxFifo(UART_Handle handle)
Function to flush a TX FIFO of peripheral specified by the UART handle.
UART_Params::transferMode
uint32_t transferMode
Definition: uart_sci.h:258
UART_Config::attrs
UART_Attrs * attrs
Definition: uart_sci.h:404
UART_Object::writeTransferSemObj
SemaphoreP_Object writeTransferSemObj
Definition: uart_sci.h:371
UART_Object::lock
void * lock
Definition: uart_sci.h:359
UART_Object::readCount
uint32_t readCount
Definition: uart_sci.h:339
UART_Params::parityType
uint32_t parityType
Definition: uart_sci.h:245
UART_Object::rxTimeoutCnt
uint32_t rxTimeoutCnt
Definition: uart_sci.h:343
UART_EdmaParams::edmaBaseAddr
uint32_t edmaBaseAddr
Definition: uart_sci.h:300
UART_Object::writeCount
uint32_t writeCount
Definition: uart_sci.h:330
UART_Object::readSizeRemaining
uint32_t readSizeRemaining
Definition: uart_sci.h:341
HwiP.h
UART_init
void UART_init(void)
This function initializes the UART module.
UART_Params
UART Parameters.
Definition: uart_sci.h:238
UART_readCancel
int32_t UART_readCancel(UART_Handle handle, UART_Transaction *trans)
Function to perform UART canceling of current read transaction.
UART_Params::baudRate
uint32_t baudRate
Definition: uart_sci.h:239
UART_Params::readCallbackFxn
UART_CallbackFxn readCallbackFxn
Definition: uart_sci.h:251
UART_Object::txEdmaParams
UART_EdmaParams txEdmaParams
Definition: uart_sci.h:379
UART_Object::readErrorCnt
uint32_t readErrorCnt
Definition: uart_sci.h:345
UART_Object::writeTransferSem
void * writeTransferSem
Definition: uart_sci.h:368
UART_Transaction::status
uint32_t status
Definition: uart_sci.h:211
UART_Config::object
UART_Object * object
Definition: uart_sci.h:406
UART_Transaction
Data structure used with UART_read() and UART_write()
Definition: uart_sci.h:201
UART_LEN_8
#define UART_LEN_8
Definition: uart_sci.h:149
UART_Params::intrNum
uint32_t intrNum
Definition: uart_sci.h:260
UART_Attrs::inputClkFreq
uint32_t inputClkFreq
Definition: uart_sci.h:280
UART_open
UART_Handle UART_open(uint32_t index, const UART_Params *prms)
This function opens a given UART peripheral.
UART_CONFIG_MODE_INTERRUPT
#define UART_CONFIG_MODE_INTERRUPT
Definition: uart_sci.h:187
UART_Params::writeCallbackFxn
UART_CallbackFxn writeCallbackFxn
Definition: uart_sci.h:253
HwiP_Object
Opaque Hwi object used with the Hwi APIs.
Definition: HwiP.h:91
UART_Object
UART driver object.
Definition: uart_sci.h:317
UART_Object::readTrans
UART_Transaction * readTrans
Definition: uart_sci.h:350
UART_Params::stopBits
uint32_t stopBits
Definition: uart_sci.h:243
UART_EdmaParams
UART EDMA Parameters.
Definition: uart_sci.h:291
EDMA_Handle
void * EDMA_Handle
A handle that is returned from a EDMA_open() call.
Definition: edma/v0/edma.h:469
UART_Object::readBuf
void * readBuf
Definition: uart_sci.h:337
UART_Params::edmaInst
uint32_t edmaInst
Definition: uart_sci.h:264
UART_Params::rxEvtNum
uint32_t rxEvtNum
Definition: uart_sci.h:266
UART_Params_init
static void UART_Params_init(UART_Params *prms)
Function to initialize the UART_Params struct to its defaults.
Definition: uart_sci.h:646
gUartConfigNum
uint32_t gUartConfigNum
Externally defined driver configuration array size.
SemaphoreP_Object
Opaque semaphore object used with the semaphore APIs.
Definition: SemaphoreP.h:59
UART_read
int32_t UART_read(UART_Handle handle, UART_Transaction *trans)
Function to perform UART read operation.
UART_Attrs
UART instance attributes - used during init time.
Definition: uart_sci.h:274
UART_Params::txEvtNum
uint32_t txEvtNum
Definition: uart_sci.h:268
UART_EdmaParams::isIntEnabled
uint32_t isIntEnabled
Definition: uart_sci.h:302
UART_Attrs::baseAddr
uint32_t baseAddr
Definition: uart_sci.h:278
UART_EdmaParams::edmaRegionId
uint32_t edmaRegionId
Definition: uart_sci.h:298
UART_Transaction::buf
void * buf
Definition: uart_sci.h:202
UART_Object::hwiObj
HwiP_Object hwiObj
Definition: uart_sci.h:375
UART_Object::prms
UART_Params prms
Definition: uart_sci.h:323
UART_Transaction::count
uint32_t count
Definition: uart_sci.h:205