1 /*
2  * Copyright (c) 2015-2017, 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  *  ======== UART.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/UART.h>
43 
44 extern const UART_Config UART_config[];
45 extern const uint_least8_t UART_count;
46 
47 /* Default UART parameters structure */
48 const UART_Params UART_defaultParams = {
49     UART_MODE_BLOCKING,   /* readMode */
50     UART_MODE_BLOCKING,   /* writeMode */
51     UART_WAIT_FOREVER,    /* readTimeout */
52     UART_WAIT_FOREVER,    /* writeTimeout */
53     NULL,                 /* readCallback */
54     NULL,                 /* writeCallback */
55     UART_RETURN_NEWLINE,  /* readReturnMode */
56     UART_DATA_TEXT,       /* readDataMode */
57     UART_DATA_TEXT,       /* writeDataMode */
58     UART_ECHO_ON,         /* readEcho */
59     115200,               /* baudRate */
60     UART_LEN_8,           /* dataLength */
61     UART_STOP_ONE,        /* stopBits */
62     UART_PAR_NONE,        /* parityType */
63     NULL                  /* custom */
64 };
65 
66 static bool isInitialized = false;
67 
68 /*
69  *  ======== UART_close ========
70  */
UART_close(UART_Handle handle)71 void UART_close(UART_Handle handle)
72 {
73     handle->fxnTablePtr->closeFxn(handle);
74 }
75 
76 /*
77  *  ======== UART_control ========
78  */
UART_control(UART_Handle handle,uint_fast16_t cmd,void * arg)79 int_fast16_t UART_control(UART_Handle handle, uint_fast16_t cmd, void *arg)
80 {
81     return (handle->fxnTablePtr->controlFxn(handle, cmd, arg));
82 }
83 
84 /*
85  *  ======== UART_init ========
86  */
UART_init(void)87 void UART_init(void)
88 {
89     uint_least8_t i;
90     uint_fast32_t key;
91 
92     key = HwiP_disable();
93 
94     if (!isInitialized) {
95         isInitialized = (bool) true;
96 
97         /* Call each driver's init function */
98         for (i = 0; i < UART_count; i++) {
99             UART_config[i].fxnTablePtr->initFxn((UART_Handle) &(UART_config[i]));
100         }
101     }
102 
103     HwiP_restore(key);
104 }
105 
106 /*
107  *  ======== UART_open ========
108  */
UART_open(uint_least8_t index,UART_Params * params)109 UART_Handle UART_open(uint_least8_t index, UART_Params *params)
110 {
111     UART_Handle handle = NULL;
112 
113     if (isInitialized && (index < UART_count)) {
114         /* If params are NULL use defaults */
115         if (params == NULL) {
116             params = (UART_Params *) &UART_defaultParams;
117         }
118 
119         /* Get handle for this driver instance */
120         handle = (UART_Handle)&(UART_config[index]);
121         handle = handle->fxnTablePtr->openFxn(handle, params);
122     }
123 
124     return (handle);
125 }
126 
127 /*
128  *  ======== UART_Params_init ========
129  */
UART_Params_init(UART_Params * params)130 void UART_Params_init(UART_Params *params)
131 {
132     *params = UART_defaultParams;
133 }
134 
135 /*
136  *  ======== UART_read ========
137  */
UART_read(UART_Handle handle,void * buffer,size_t size)138 int_fast32_t UART_read(UART_Handle handle, void *buffer, size_t size)
139 {
140     return (handle->fxnTablePtr->readFxn(handle, buffer, size));
141 }
142 
143 /*
144  *  ======== UART_readPolling ========
145  */
UART_readPolling(UART_Handle handle,void * buffer,size_t size)146 int_fast32_t UART_readPolling(UART_Handle handle, void *buffer, size_t size)
147 {
148     return (handle->fxnTablePtr->readPollingFxn(handle, buffer, size));
149 }
150 
151 /*
152  *  ======== UART_readCancel ========
153  */
UART_readCancel(UART_Handle handle)154 void UART_readCancel(UART_Handle handle)
155 {
156     handle->fxnTablePtr->readCancelFxn(handle);
157 }
158 
159 /*
160  *  ======== UART_write ========
161  */
UART_write(UART_Handle handle,const void * buffer,size_t size)162 int_fast32_t UART_write(UART_Handle handle, const void *buffer, size_t size)
163 {
164     return (handle->fxnTablePtr->writeFxn(handle, buffer, size));
165 }
166 
167 /*
168  *  ======== UART_writePolling ========
169  */
UART_writePolling(UART_Handle handle,const void * buffer,size_t size)170 int_fast32_t UART_writePolling(UART_Handle handle, const void *buffer,
171     size_t size)
172 {
173     return (handle->fxnTablePtr->writePollingFxn(handle, buffer, size));
174 }
175 
176 /*
177  *  ======== UART_writeCancel ========
178  */
UART_writeCancel(UART_Handle handle)179 void UART_writeCancel(UART_Handle handle)
180 {
181     handle->fxnTablePtr->writeCancelFxn(handle);
182 }
183