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  *  ======== UART2.c ========
34  */
35 
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 
41 #include <ti/drivers/dpl/HwiP.h>
42 #include <ti/drivers/UART2.h>
43 
44 extern const UART2_Config UART2_config[];
45 extern const uint_least8_t UART2_count;
46 
47 /* Default UART parameters structure */
48 const UART2_Params UART2_defaultParams = {
49     UART2_Mode_BLOCKING,     /* readMode */
50     UART2_Mode_BLOCKING,     /* writeMode */
51     NULL,                    /* readCallback */
52     NULL,                    /* writeCallback */
53     UART2_ReadReturnMode_FULL, /* readReturnMode */
54     115200,                  /* baudRate */
55     UART2_DataLen_8,         /* dataLength */
56     UART2_StopBits_1,        /* stopBits */
57     UART2_Parity_NONE,       /* parityType */
58     NULL                     /* userArg */
59 };
60 
61 /*
62  *  ======== UART2_close ========
63  */
64 
UART2_close(UART2_Handle handle)65 void __attribute__((weak)) UART2_close(UART2_Handle handle)
66 {
67     handle->fxnTablePtr->closeFxn(handle);
68 }
69 
70 /*
71  *  ======== UART2_flushRx ========
72  */
UART2_flushRx(UART2_Handle handle)73 void __attribute__((weak)) UART2_flushRx(UART2_Handle handle)
74 {
75     handle->fxnTablePtr->flushRxFxn(handle);
76 }
77 
78 /*
79  *  ======== UART2_open ========
80  */
UART2_open(uint_least8_t index,UART2_Params * params)81 UART2_Handle __attribute__((weak)) UART2_open(uint_least8_t index,
82         UART2_Params *params)
83 {
84     UART2_Handle handle = NULL;
85 
86     if (index < UART2_count) {
87         /* If params are NULL use defaults */
88         if (params == NULL) {
89             params = (UART2_Params *) &UART2_defaultParams;
90         }
91 
92         /* Get handle for this driver instance */
93         handle = UART2_config[index].fxnTablePtr->openFxn(index, params);
94     }
95 
96     return (handle);
97 }
98 
99 /*
100  *  ======== UART2_Params_init ========
101  */
UART2_Params_init(UART2_Params * params)102 void UART2_Params_init(UART2_Params *params)
103 {
104     *params = UART2_defaultParams;
105 }
106 
107 /*
108  *  ======== UART2_read ========
109  */
UART2_read(UART2_Handle handle,void * buffer,size_t size,size_t * bytesRead)110 int_fast16_t __attribute__((weak)) UART2_read(UART2_Handle handle, void *buffer,
111         size_t size, size_t *bytesRead)
112 {
113     return (handle->fxnTablePtr->readFxn(handle, buffer, size,
114             bytesRead, UART2_WAIT_FOREVER));
115 }
116 
117 /*
118  *  ======== UART2_readTimeout ========
119  */
UART2_readTimeout(UART2_Handle handle,void * buffer,size_t size,size_t * bytesRead,uint32_t timeout)120 int_fast16_t __attribute__((weak)) UART2_readTimeout(UART2_Handle handle,
121         void *buffer, size_t size, size_t *bytesRead, uint32_t timeout)
122 {
123     return (handle->fxnTablePtr->readFxn(handle, buffer, size,
124             bytesRead, timeout));
125 }
126 
127 /*
128  *  ======== UART2_readCancel ========
129  */
UART2_readCancel(UART2_Handle handle)130 void __attribute__((weak)) UART2_readCancel(UART2_Handle handle)
131 {
132     handle->fxnTablePtr->readCancelFxn(handle);
133 }
134 
135 /*
136  *  ======== UART2_write ========
137  */
UART2_write(UART2_Handle handle,const void * buffer,size_t size,size_t * bytesWritten)138 int_fast16_t __attribute__((weak)) UART2_write(UART2_Handle handle,
139         const void *buffer, size_t size, size_t *bytesWritten)
140 {
141     return (handle->fxnTablePtr->writeFxn(handle, buffer, size,
142                     bytesWritten, UART2_WAIT_FOREVER));
143 }
144 
145 /*
146  *  ======== UART2_write ========
147  */
UART2_writeTimeout(UART2_Handle handle,const void * buffer,size_t size,size_t * bytesWritten,uint32_t timeout)148 int_fast16_t __attribute__((weak)) UART2_writeTimeout(UART2_Handle handle,
149         const void *buffer, size_t size, size_t *bytesWritten,
150         uint32_t timeout)
151 {
152     return (handle->fxnTablePtr->writeFxn(handle, buffer, size,
153                     bytesWritten, timeout));
154 }
155 
156 /*
157  *  ======== UART2_writeCancel ========
158  */
UART2_writeCancel(UART2_Handle handle)159 void __attribute__((weak)) UART2_writeCancel(UART2_Handle handle)
160 {
161     handle->fxnTablePtr->writeCancelFxn(handle);
162 }
163