-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy patherpc_serial.cpp
More file actions
342 lines (291 loc) · 7.59 KB
/
erpc_serial.cpp
File metadata and controls
342 lines (291 loc) · 7.59 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
333
334
335
336
337
338
339
340
341
342
/*
* This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/).
*
* Written and maintained by the Bus Pirate project and http://dangerousprototypes.com
*
* To the extent possible under law, the project has
* waived all copyright and related or neighboring rights to Bus Pirate. This
* work is published from United States.
*
* For details see: http://creativecommons.org/publicdomain/zero/1.0/.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* OS independent serial interface
*
* Heavily based on Pirate-Loader:
* http://the-bus-pirate.googlecode.com/svn/trunk/bootloader-v4/pirate-loader/source/pirate-loader.c
*
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <errno.h>
#include <string.h>
/*included this here for cygwin. need to figure out proper include handling for cygwin */
//#include <termios.h>
//#define LINUX 1
//#ifdef LINUX
//#include <termios.h>
//#endif
// included from serial.h
#include "erpc_serial.h"
#ifdef _WIN32
static OVERLAPPED s_writeOverlap;
static OVERLAPPED s_readOverlap;
#define TX_BUF_BYTES 1024U
#define RX_BUF_BYTES 1024U
#endif
int serial_setup(int fd, speed_t speed)
{
#ifdef _WIN32
COMMTIMEOUTS timeouts;
DCB dcb = { 0 };
HANDLE hCom = (HANDLE)fd;
DWORD errors;
COMSTAT status;
ClearCommError(hCom, &errors, &status);
PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
memset(&timeouts, 0, sizeof(timeouts));
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.WriteTotalTimeoutConstant = 500;
if (!SetCommTimeouts(hCom, &timeouts))
{
return -1;
}
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = TRUE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
if (!SetCommState(hCom, &dcb))
{
return -1;
}
s_writeOverlap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
s_readOverlap.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
SetCommMask(hCom, EV_RXCHAR);
#else
(void)speed;
struct termios tty;
memset(&tty, 0x00, sizeof(tty));
cfmakeraw(&tty);
tty.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
tty.c_cflag |= (CS8 | CLOCAL | CREAD);
tty.c_oflag = 0;
tty.c_lflag = 0;
#ifdef __linux__
switch (speed)
{
case 9600:
speed = B9600;
break;
case 38400:
speed = B38400;
break;
case 115200:
speed = B115200;
break;
case 57600:
default:
speed = B57600;
break;
}
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);
#endif
// Completely non-blocking read
// VMIN = 0 and VTIME = 0
// Completely non-blocking read
// reference: http://www.unixwiz.net/techtips/termios-vmin-vtime.html
// currently set as total blocking - must see at least 1 bit
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 0;
if (tcsetattr(fd, TCSAFLUSH, &tty) < 0)
{
return -1;
}
#ifdef __APPLE__
return ioctl(fd, IOSSIOSPEED, &speed);
#endif //#ifdef __APPLE__
#endif // _WIN32
return 0;
}
int serial_set_read_timeout(int fd, uint8_t vtime, uint8_t vmin)
{
#ifdef _WIN32
// TODO
#else
struct termios tty;
/*memset(&tty, 0x00, sizeof(tty));
cfmakeraw(&tty); */
tcgetattr(fd, &tty);
/************************************************************
* Timeout Options:
* VMIN = 0; VTIME = 0 : completely non-blocking
* VMIN > 0; VTIME = 0 : pure blocking until VMIN bytes read
* VMIN = 0; VTIME > 0 : pure timed read
* VMIN > 0; VTIME > 0 : interbyte timeout;
************************************************************/
// NOTE: VTIME is in 0.1 second intervals
tty.c_cc[VTIME] = vtime;
tty.c_cc[VMIN] = vmin;
if (tcsetattr(fd, TCSAFLUSH, &tty) < 0)
{
return -1;
}
#endif // _WIN32
return 0;
}
int serial_write(int fd, char *buf, int size)
{
#ifdef _WIN32
HANDLE hCom = (HANDLE)fd;
DWORD errors;
COMSTAT status;
unsigned long bwritten = 0;
ClearCommError(hCom, &errors, &status);
if (!WriteFile(hCom, buf, size, &bwritten, &s_writeOverlap))
{
if (GetLastError() == ERROR_IO_PENDING)
{
if (!GetOverlappedResult(hCom, &s_writeOverlap, &bwritten, TRUE))
{
return 0;
}
}
else
{
return 0;
;
}
}
ClearCommError(hCom, &errors, &status);
return bwritten;
#else
return write(fd, buf, size);
#endif
}
int serial_read(int fd, char *buf, int size)
{
#ifdef _WIN32
HANDLE hCom = (HANDLE)fd;
char temp[RX_BUF_BYTES] = { 0 };
DWORD errors;
DWORD totalBytesRead = 0;
DWORD bytesRead = 0;
DWORD ret = 0;
while (totalBytesRead != size)
{
do
{
ClearCommError(hCom, &errors, NULL);
if (!ReadFile(hCom, temp, size - totalBytesRead, &bytesRead, &s_readOverlap))
{
if (GetLastError() == ERROR_IO_PENDING)
{
ret = WaitForSingleObject(s_readOverlap.hEvent, INFINITE);
if (WAIT_OBJECT_0 == ret)
{
if (!GetOverlappedResult(hCom, &s_readOverlap, &bytesRead, FALSE))
{
bytesRead = 0;
totalBytesRead = 0;
}
}
else
{
bytesRead = 0;
totalBytesRead = 0;
}
}
else
{
bytesRead = 0;
totalBytesRead = 0;
}
}
totalBytesRead += bytesRead;
if (bytesRead)
{
memcpy(buf, temp, bytesRead);
buf += bytesRead;
}
} while ((bytesRead > 0) && (size <= RX_BUF_BYTES) && (RX_BUF_BYTES >= totalBytesRead));
}
return totalBytesRead;
#else
int len = 0;
int ret = 0;
int timeout = 0;
while (len < size)
{
ret = read(fd, buf + len, size - len);
if (ret == -1)
{
return -1;
}
if (ret == 0)
{
timeout++;
if (timeout >= 10)
{
break;
}
continue;
}
len += ret;
}
return len;
#endif
}
int serial_open(const char *port)
{
int fd;
#ifdef _WIN32
static char full_path[32] = { 0 };
HANDLE hCom = NULL;
if (memcmp(port, "\\\\.\\", 4))
{
_snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", port);
}
else
{
memcpy(full_path, port, strnlen_s(port, sizeof(full_path) - 1));
}
hCom = CreateFileA(full_path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (!hCom || hCom == INVALID_HANDLE_VALUE)
{
fd = -1;
}
else
{
fd = (int)hCom;
}
#else
fd = open(port, O_RDWR | O_NOCTTY);
if (fd == -1)
{
fprintf(stderr, "Could not open serial port.");
return -1;
}
#endif
return fd;
}
int serial_close(int fd)
{
#ifdef _WIN32
HANDLE hCom = (HANDLE)fd;
CloseHandle(hCom);
#else
close(fd);
#endif
return 0;
}