Repository navigation
Expand file tree
/
Copy pathuart_handler.c
More file actions
executable file
·332 lines (290 loc) · 9.04 KB
/
Copy pathuart_handler.c
File metadata and controls
executable file
·332 lines (290 loc) · 9.04 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//*****************************************************************************
//
// uart_handler.c
//
// Copyright (c) 2006-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the Tiva Firmware Development Package.
//
//*****************************************************************************
#include <stdint.h>
#ifdef __WIN32
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#else
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h> /* memset */
#include <unistd.h> /* close */
#endif
//*****************************************************************************
//
//! \addtogroup uart_handler UART Handler API
//! This section describes the functions that are responsible for handling the
//! transfer of data over the UART port. These functions use Microsoft Windows
//! APIs to communicate with the UART.
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! This variable holds the open handle to the UART in use by this
//! application.
//
//*****************************************************************************
#ifdef __WIN32
static HANDLE g_hComPort;
#else
static int32_t g_i32ComPort = -1;
#endif
void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error ("error %d getting term settings set_blocking");
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = should_block ? 5 : 0; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
error ("error setting term %sblocking", should_block ? "" : "no");
}
void FlushInBuffer(void){
set_blocking (g_i32ComPort, 0);
char buf [10000];
int n;
do {
n = read (g_i32ComPort, buf, sizeof buf);
} while (n > 0);
set_blocking (g_i32ComPort, 1);
}
//*****************************************************************************
//
//! OpenUART() opens the UART port.
//!
//! \param pcComPort is the text representation of the COM port to be
//! opened. "COM1" is the valid string to use to open COM port 1 on the
//! host.
//! \param ui32BaudRate is the baud rate to configure the UART to use.
//!
//! This function is used to open the host UART with the given baud rate. The
//! rest of the settings are fixed at No Parity, 8 data bits and 1 stop bit.
//!
//! \return The function returns zero to indicated success while any non-zero
//! value indicates a failure.
//
//*****************************************************************************
int32_t OpenUART(char *pcComPort, uint32_t ui32BaudRate) {
#ifdef __WIN32
DCB sDCB;
COMMTIMEOUTS sCommTimeouts;
g_hComPort = CreateFile(pcComPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if(g_hComPort == INVALID_HANDLE_VALUE)
{
return(-1);
}
if(GetCommState(g_hComPort, &sDCB) == 0)
{
return(-1);
}
sDCB.BaudRate = ui32BaudRate;
sDCB.ByteSize = 8;
sDCB.Parity = NOPARITY;
sDCB.StopBits = ONESTOPBIT;
sDCB.fAbortOnError = TRUE;
sDCB.fOutxCtsFlow = FALSE;
sDCB.fOutxDsrFlow = FALSE;
sDCB.fDtrControl = DTR_CONTROL_ENABLE;
if(SetCommState(g_hComPort, &sDCB) == 0)
{
return(-1);
}
if(GetCommTimeouts(g_hComPort, &sCommTimeouts) == 0)
{
return(-1);
}
sCommTimeouts.ReadIntervalTimeout = 8000;
sCommTimeouts.ReadTotalTimeoutConstant = 8000;
sCommTimeouts.ReadTotalTimeoutMultiplier = 8000;
sCommTimeouts.WriteTotalTimeoutConstant = 8000;
sCommTimeouts.WriteTotalTimeoutMultiplier = 8000;
if(SetCommTimeouts(g_hComPort, &sCommTimeouts) == 0)
{
return(-1);
}
return(0);
#else
struct termios sOptions;
g_i32ComPort = open(pcComPort, O_RDWR | O_NOCTTY | O_NDELAY);
if (g_i32ComPort == -1) {
return (-1);
}
fcntl(g_i32ComPort, F_SETFL, 0);
tcgetattr(g_i32ComPort, &sOptions);
if (ui32BaudRate == 9600) {
cfsetispeed(&sOptions, B9600);
cfsetospeed(&sOptions, B9600);
} else if (ui32BaudRate == 19200) {
cfsetispeed(&sOptions, B19200);
cfsetospeed(&sOptions, B19200);
} else if (ui32BaudRate == 38400) {
cfsetispeed(&sOptions, B38400);
cfsetospeed(&sOptions, B38400);
} else if (ui32BaudRate == 57600) {
cfsetispeed(&sOptions, B57600);
cfsetospeed(&sOptions, B57600);
} else if (ui32BaudRate == 115200) {
cfsetispeed(&sOptions, B115200);
cfsetospeed(&sOptions, B115200);
} else {
cfsetispeed(&sOptions, B230400);
cfsetospeed(&sOptions, B230400);
}
sOptions.c_cflag |= (CLOCAL | CREAD);
sOptions.c_cflag &= ~(CSIZE);
sOptions.c_cflag |= CS8;
sOptions.c_cflag &= ~(PARENB);
sOptions.c_cflag &= ~(CSTOPB);
sOptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
sOptions.c_oflag &= ~(OPOST);
tcsetattr(g_i32ComPort, TCSANOW, &sOptions);
FlushInBuffer();
return (0);
#endif
}
//*****************************************************************************
//
//! CloseUART() closes the UART port.
//!
//! This function closes the UART port that was opened by a call to OpenUART().
//!
//! \returns This function returns zero to indicate success while any non-zero
//! value indicates a failure.
//
//*****************************************************************************
int32_t CloseUART(void) {
#ifdef __WIN32
return(CloseHandle(g_hComPort));
#else
if (g_i32ComPort != -1) {
close(g_i32ComPort);
}
return (0);
#endif
}
//*****************************************************************************
//
//! UARTSendData() sends data over a UART port.
//!
//! \param pui8Data
//! The buffer to write out to the UART port.
//! \param ui8Size
//! The number of bytes provided in pui8Data buffer that should be written
//! out to the port.
//!
//! This function sends ui8Size bytes of data from the buffer pointed to by
//! pui8Data via the UART port that was opened by a call to OpenUART().
//!
//! \return This function returns zero to indicate success while any non-zero
//! value indicates a failure.
//
//*****************************************************************************
int32_t UARTSendData(uint8_t const *pui8Data, uint8_t ui8Size) {
#ifdef __WIN32
unsigned long ulNumBytes;
//
// Send the Ack back to the device.
//
if(WriteFile(g_hComPort, pui8Data, ui8Size, &ulNumBytes, NULL) == 0)
{
return(-1);
}
if(ulNumBytes != ui8Size)
{
return(-1);
}
return(0);
#else
// printf("wCmd: ");
//
// int i;
// for (i = 0; i < ui8Size; ++i) {
// printf("%x ", pui8Data[i]);
// }
//
// printf("\n");
if (write(g_i32ComPort, pui8Data, ui8Size) != ui8Size) {
return (-1);
}
return (0);
#endif
}
//*****************************************************************************
//
//! UARTReceiveData() receives data over a UART port.
//!
//! \param pui8Data is the buffer to read data into from the UART port.
//! \param ui8Size is the number of bytes provided in pui8Data buffer that should
//! be written with data from the UART port.
//!
//! This function reads back ui8Size bytes of data from the UART port, that was
//! opened by a call to OpenUART(), into the buffer that is pointed to by
//! pui8Data.
//!
//! \return This function returns zero to indicate success while any non-zero
//! value indicates a failure.
//
//*****************************************************************************
int32_t UARTReceiveData(uint8_t *pui8Data, uint8_t ui8Size) {
#ifdef __WIN32
unsigned long ulNumBytes;
if(ReadFile(g_hComPort, pui8Data, ui8Size, &ulNumBytes, NULL) == 0)
{
return(-1);
}
if(ulNumBytes != ui8Size)
{
return(-1);
}
return(0);
#else
if (read(g_i32ComPort, pui8Data, ui8Size) != ui8Size) {
return (-1);
}
// printf("rCmd: ");
//
// int i;
// for (i = 0; i < ui8Size; ++i) {
// printf("%x ", pui8Data[i]);
// }
//
// printf("\n");
return (0);
#endif
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************