-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUARTUtils.c
308 lines (267 loc) · 7.8 KB
/
UARTUtils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (c) 2015, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ======== UARTUtils.c ========
*/
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/Assert.h>
#include <xdc/runtime/Diags.h>
#include <xdc/runtime/Log.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
/* TI-RTOS Header files */
#include <ti/drivers/UART.h>
/* Example/Board Header files */
#include "Board.h"
#include "UARTUtils.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define NUM_PORTS 1
/* Typedefs */
typedef struct {
UART_Handle handle; /* Handle for all UART APIs */
unsigned int base; /* Base address of the UART */
unsigned int open; /* Number of users for this UART */
bool binary; /* UART has been opened in binary mode */
} UARTPorts;
/* Static variables and handles */
static UART_Handle systemHandle = NULL;
static UART_Handle loggerHandle = NULL;
/* This example only uses UART0 */
static UARTPorts ports[NUM_PORTS] = {{NULL, Board_UART0, 0, false}};
/*
* ======== openHandle ========
* The UART driver will return NULL if there was an error creating a UART
*
* @param index Index into the ports array of UARTPorts
* @param binary Open the UART in binary mode
* @return UART_Handle to the opened UART
*/
static UART_Handle openHandle(unsigned int index, bool binary)
{
UART_Params uartParams;
/* Only UART 0 is supported in this example. */
if (index >= NUM_PORTS) {
System_printf("UART index %d not supported, valid range is 0-%d", index, (NUM_PORTS - 1));
return (NULL);
}
/* The UART driver only allows creating once, return if its already open. */
if (ports[index].open) {
/* Make sure the index is not already opened in the wrong mode */
if (binary != ports[index].binary) {
return (NULL);
}
ports[index].open++;
return (ports[index].handle);
}
/* Create a UART with the parameters below. */
UART_Params_init(&uartParams);
if (binary == true) {
uartParams.readEcho = UART_ECHO_OFF;
uartParams.writeDataMode = UART_DATA_BINARY;
ports[index].binary = true;
}
else {
ports[index].binary = false;
uartParams.baudRate = 9600;
}
ports[index].handle = UART_open(ports[index].base, &uartParams);
if (ports[index].handle != NULL) {
ports[index].open = 1;
}
return (ports[index].handle);
}
/*
* ======== closeHandle ========
*/
static void closeHandle(unsigned int index)
{
ports[index].open--;
if (ports[index].open == 0) {
UART_close(ports[index].handle);
}
}
/*
* ======== UARTUtils_loggerIdleInit ========
*/
void UARTUtils_loggerIdleInit(unsigned int index)
{
Assert_isTrue(ports[index].open == false, NULL);
loggerHandle = openHandle(index, true);
if (loggerHandle == NULL) {
System_printf("Failed to open UART %d", index);
}
}
/*
* ======== UARTUtils_loggerIdleSend ========
* Plugged into LoggerIdle to send log data during idle.
*/
Int UARTUtils_loggerIdleSend(UChar *a, Int size)
{
/* Make sure UART is initialized */
if (loggerHandle) {
/*
* Write up to 16 bytes at a time. This function runs during idle and
* should not tie up other idle functions from running. The idle loop
* is generally short enough that this function will run again before all
* 16 bytes have been transmitted from the FIFO.
*/
if (size < 16) {
return (UART_writePolling(loggerHandle, (void *)a, size));
}
else {
return (UART_writePolling(loggerHandle, (void *)a, 16));
}
}
else {
return (0);
}
}
/*
* ======== UARTUtils_deviceclose ========
*/
int UARTUtils_deviceclose(int fd)
{
/* Return if a UART other than UART 0 was specified. */
if (fd != 0) {
return (-1);
}
closeHandle(fd);
return (0);
}
/*
* ======== UARTUtils_devicelseek ========
*/
off_t UARTUtils_devicelseek(int fd, off_t offset, int origin)
{
return (-1);
}
/*
* ======== UARTUtils_deviceopen ========
*/
int UARTUtils_deviceopen(const char *path, unsigned flags, int mode)
{
int fd;
UART_Handle handle;
/* Get the UART specified for opening. */
fd = path[0] - '0';
handle = openHandle(fd, false);
if(handle == NULL) {
return (-1);
}
else {
return (fd);
}
}
/*
* ======== UARTUtils_deviceread ========
*/
int UARTUtils_deviceread(int fd, char *buffer, unsigned size)
{
int ret;
/* Return if a UART other than UART 0 was specified. */
if (fd != 0) {
return (-1);
}
/* Read character from the UART and block until a newline is received. */
ret = UART_read(ports[fd].handle, (uint8_t *)buffer, size);
return (ret);
}
/*
* ======== UARTUtils_devicewrite ========
*/
int UARTUtils_devicewrite(int fd, const char *buffer, unsigned size)
{
int ret;
/* Return if a UART other than UART 0 was specified. */
if (fd != 0) {
return (-1);
}
/* Write to the UART and block until the transfer is finished. */
ret = UART_write(ports[fd].handle, (uint8_t *)buffer, size);
return (ret);
}
/*
* ======== UARTUtils_deviceunlink ========
*/
int UARTUtils_deviceunlink(const char *path)
{
return (-1);
}
/*
* ======== UARTUtils_devicerename ========
*/
int UARTUtils_devicerename(const char *old_name, const char *new_name)
{
return (-1);
}
/*
* ======== UARTUtils_systemAbort ========
*/
Void UARTUtils_systemAbort(String str)
{
/* Make sure UART is initialized */
if (systemHandle) {
UART_writePolling(systemHandle, (uint8_t *)str, strlen(str));
}
}
/*
* ======== UARTUtils_systemInit ========
*/
void UARTUtils_systemInit(unsigned int index)
{
systemHandle = openHandle(index, false);
if (systemHandle == NULL) {
Log_print1(Diags_USER1, "Failed to open UART %d", index);
}
}
/*
* ======== UARTUtils_systemPutch ========
*/
Void UARTUtils_systemPutch(Char a)
{
/* Make sure UART is initialized */
if (systemHandle) {
UART_writePolling(systemHandle, (void *)&a, 1);
}
}
/*
* ======== UARTUtils_systemReady ========
*/
Bool UARTUtils_systemReady(Void)
{
return (systemHandle ? TRUE : FALSE);
}