This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ser_win32.c
408 lines (336 loc) · 8.75 KB
/
ser_win32.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2003, 2004 Martin J. Thomas <[email protected]>
* Copyright (C) 2006 Joerg Wunsch <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id$ */
/*
* Native Win32 serial interface for avrdude.
*/
#include "avrdude.h"
#if defined(WIN32NATIVE)
#include <windows.h>
#include <stdio.h>
#include <ctype.h> /* for isprint */
#include "serial.h"
long serial_recv_timeout = 5000; /* ms */
#define W32SERBUFSIZE 1024
struct baud_mapping {
long baud;
DWORD speed;
};
/* HANDLE hComPort=INVALID_HANDLE_VALUE; */
static struct baud_mapping baud_lookup_table [] = {
{ 1200, CBR_1200 },
{ 2400, CBR_2400 },
{ 4800, CBR_4800 },
{ 9600, CBR_9600 },
{ 19200, CBR_19200 },
{ 38400, CBR_38400 },
{ 57600, CBR_57600 },
{ 115200, CBR_115200 },
{ 0, 0 } /* Terminator. */
};
static DWORD serial_baud_lookup(long baud)
{
struct baud_mapping *map = baud_lookup_table;
while (map->baud) {
if (map->baud == baud)
return map->speed;
map++;
}
/*
* If a non-standard BAUD rate is used, issue
* a warning (if we are verbose) and return the raw rate
*/
if (verbose > 0)
fprintf(stderr, "%s: serial_baud_lookup(): Using non-standard baud rate: %ld",
progname, baud);
return baud;
}
static BOOL serial_w32SetTimeOut(HANDLE hComPort, DWORD timeout) // in ms
{
COMMTIMEOUTS ctmo;
ZeroMemory (&ctmo, sizeof(COMMTIMEOUTS));
ctmo.ReadIntervalTimeout = timeout;
ctmo.ReadTotalTimeoutMultiplier = timeout;
ctmo.ReadTotalTimeoutConstant = timeout;
return SetCommTimeouts(hComPort, &ctmo);
}
static int ser_setspeed(union filedescriptor *fd, long baud)
{
DCB dcb;
HANDLE hComPort = (HANDLE)fd->pfd;
ZeroMemory (&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = serial_baud_lookup (baud);
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hComPort, &dcb))
return -1;
return 0;
}
static int ser_open(char * port, long baud, union filedescriptor *fdp)
{
LPVOID lpMsgBuf;
HANDLE hComPort=INVALID_HANDLE_VALUE;
char *newname = 0;
/*
* If the port is of the form "net:<host>:<port>", then
* handle it as a TCP connection to a terminal server.
*
* This is curently not implemented for Win32.
*/
if (strncmp(port, "net:", strlen("net:")) == 0) {
fprintf(stderr,
"%s: ser_open(): network connects are currently not"
"implemented for Win32 environments\n",
progname);
return -1;
}
if (strncasecmp(port, "com", strlen("com")) == 0) {
// prepend "\\\\.\\" to name, required for port # >= 10
newname = malloc(strlen("\\\\.\\") + strlen(port) + 1);
if (newname == 0) {
fprintf(stderr,
"%s: ser_open(): out of memory\n",
progname);
exit(1);
}
strcpy(newname, "\\\\.\\");
strcat(newname, port);
port = newname;
}
hComPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hComPort == INVALID_HANDLE_VALUE) {
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
fprintf(stderr, "%s: ser_open(): can't open device \"%s\": %s\n",
progname, port, (char*)lpMsgBuf);
LocalFree( lpMsgBuf );
return -1;
}
if (!SetupComm(hComPort, W32SERBUFSIZE, W32SERBUFSIZE))
{
CloseHandle(hComPort);
fprintf(stderr, "%s: ser_open(): can't set buffers for \"%s\"\n",
progname, port);
return -1;
}
fdp->pfd = (void *)hComPort;
if (ser_setspeed(fdp, baud) != 0)
{
CloseHandle(hComPort);
fprintf(stderr, "%s: ser_open(): can't set com-state for \"%s\"\n",
progname, port);
return -1;
}
if (!serial_w32SetTimeOut(hComPort,0))
{
CloseHandle(hComPort);
fprintf(stderr, "%s: ser_open(): can't set initial timeout for \"%s\"\n",
progname, port);
return -1;
}
if (newname != 0) {
free(newname);
}
return 0;
}
static void ser_close(union filedescriptor *fd)
{
HANDLE hComPort=(HANDLE)fd->pfd;
if (hComPort != INVALID_HANDLE_VALUE)
CloseHandle (hComPort);
hComPort = INVALID_HANDLE_VALUE;
}
static int ser_set_dtr_rts(union filedescriptor *fd, int is_on)
{
HANDLE hComPort=(HANDLE)fd->pfd;
if (is_on) {
EscapeCommFunction(hComPort, SETDTR);
EscapeCommFunction(hComPort, SETRTS);
} else {
EscapeCommFunction(hComPort, CLRDTR);
EscapeCommFunction(hComPort, CLRRTS);
}
return 0;
}
static int ser_send(union filedescriptor *fd, unsigned char * buf, size_t buflen)
{
size_t len = buflen;
unsigned char c='\0';
DWORD written;
unsigned char * b = buf;
HANDLE hComPort=(HANDLE)fd->pfd;
if (hComPort == INVALID_HANDLE_VALUE) {
fprintf(stderr, "%s: ser_send(): port not open\n",
progname);
exit(1);
}
if (!len)
return 0;
if (verbose > 3)
{
fprintf(stderr, "%s: Send: ", progname);
while (len) {
c = *b;
if (isprint(c)) {
fprintf(stderr, "%c ", c);
}
else {
fprintf(stderr, ". ");
}
fprintf(stderr, "[%02x] ", c);
b++;
len--;
}
fprintf(stderr, "\n");
}
serial_w32SetTimeOut(hComPort,500);
if (!WriteFile (hComPort, buf, buflen, &written, NULL)) {
fprintf(stderr, "%s: ser_send(): write error: %s\n",
progname, "sorry no info avail"); // TODO
exit(1);
}
if (written != buflen) {
fprintf(stderr, "%s: ser_send(): size/send mismatch\n",
progname);
exit(1);
}
return 0;
}
static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen)
{
unsigned char c;
unsigned char * p = buf;
DWORD read;
HANDLE hComPort=(HANDLE)fd->pfd;
if (hComPort == INVALID_HANDLE_VALUE) {
fprintf(stderr, "%s: ser_read(): port not open\n",
progname);
exit(1);
}
serial_w32SetTimeOut(hComPort, serial_recv_timeout);
if (!ReadFile(hComPort, buf, buflen, &read, NULL)) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
fprintf(stderr, "%s: ser_recv(): read error: %s\n",
progname, (char*)lpMsgBuf);
LocalFree( lpMsgBuf );
exit(1);
}
p = buf;
if (verbose > 3)
{
fprintf(stderr, "%s: Recv: ", progname);
while (read) {
c = *p;
if (isprint(c)) {
fprintf(stderr, "%c ", c);
}
else {
fprintf(stderr, ". ");
}
fprintf(stderr, "[%02x] ", c);
p++;
read--;
}
fprintf(stderr, "\n");
}
return 0;
}
static int ser_drain(union filedescriptor *fd, int display)
{
// int rc;
unsigned char buf[10];
BOOL readres;
DWORD read;
HANDLE hComPort=(HANDLE)fd->pfd;
if (hComPort == INVALID_HANDLE_VALUE) {
fprintf(stderr, "%s: ser_drain(): port not open\n",
progname);
exit(1);
}
serial_w32SetTimeOut(hComPort,250);
if (display) {
fprintf(stderr, "drain>");
}
while (1) {
readres=ReadFile(hComPort, buf, 1, &read, NULL);
if (!readres) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
fprintf(stderr, "%s: ser_drain(): read error: %s\n",
progname, (char*)lpMsgBuf);
LocalFree( lpMsgBuf );
exit(1);
}
if (read) { // data avail
if (display) fprintf(stderr, "%02x ", buf[0]);
}
else { // no more data
if (display) fprintf(stderr, "<drain\n");
break;
}
} // while
return 0;
}
struct serial_device serial_serdev =
{
.open = ser_open,
.setspeed = ser_setspeed,
.close = ser_close,
.send = ser_send,
.recv = ser_recv,
.drain = ser_drain,
.set_dtr_rts = ser_set_dtr_rts,
.flags = SERDEV_FL_CANSETSPEED,
};
struct serial_device *serdev = &serial_serdev;
#endif /* WIN32NATIVE */