-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserial.c
255 lines (221 loc) · 5.54 KB
/
serial.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
/*
* Part of ols-fwloader - Serial port routines
* Inspired by pirate-loader, written by Piotr Pawluczuk
* http://the-bus-pirate.googlecode.com/svn/trunk/bootloader-v4/pirate-loader/source/pirate-loader.c
*
* Copyright (C) 2010 Piotr Pawluczuk
* Copyright (C) 2011 Michal Demin <michal.demin@gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#if _WIN32
#else
#include <errno.h>
#endif
#include "serial.h"
int serial_setup(int fd, unsigned long speed)
{
#if _WIN32
COMMTIMEOUTS timeouts;
DCB dcb = {0};
HANDLE hCom = (HANDLE)fd;
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = speed;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if( !SetCommState(hCom, &dcb) ){
return -1;
}
timeouts.ReadIntervalTimeout = 100;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 100;
timeouts.WriteTotalTimeoutConstant = 2550;
if (!SetCommTimeouts(hCom, &timeouts)) {
return -1;
}
return 0;
#else
struct termios t_opt;
unsigned long baud;
// printf("Serial: Speed= %lu\n",speed);
switch (speed) {
case 921600:
baud = 921600;
break;
case 115200:
baud = 115200;
break;
case 1000000:
baud = 1000000;
break;
case 1500000:
baud = 1500000;
break;
default:
printf("unknown speed setting: %lu \n",speed);
baud=115200;
printf("setting to default: %lu\n",baud);
//return -1;
break;
}
/* set the serial port parameters */
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &t_opt);
cfsetispeed(&t_opt, baud);
cfsetospeed(&t_opt, baud);
t_opt.c_cflag |= (CLOCAL | CREAD);
t_opt.c_cflag &= ~PARENB;
t_opt.c_cflag &= ~CSTOPB;
t_opt.c_cflag &= ~CSIZE;
t_opt.c_cflag |= CS8;
t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
t_opt.c_iflag &= ~(IXON | IXOFF | IXANY);
t_opt.c_iflag &= ~(ICRNL | INLCR);
t_opt.c_oflag &= ~(OCRNL | ONLCR);
t_opt.c_oflag &= ~OPOST;
t_opt.c_cc[VMIN] = 0;
t_opt.c_cc[VTIME] = 10;
#if IS_DARWIN
if (tcsetattr(fd, TCSANOW, &t_opt) < 0) {
return -1;
}
return ioctl( fd, IOSSIOSPEED, &baud );
#else
tcflush(fd, TCIOFLUSH);
return tcsetattr(fd, TCSANOW, &t_opt);
#endif
#endif
}
int serial_write(int fd, const char *buf, int size) {
return serial_write(fd, (char*)buf, size);
}
int serial_write(int fd, char *buf, int size)
{
int ret = 0;
#if _WIN32
HANDLE hCom = (HANDLE)fd;
int res = 0;
unsigned long bwritten = 0;
res = WriteFile(hCom, buf, size, &bwritten, NULL);
//DWORD dw = GetLastError();
// printf("serial.c: res = %i, bwritten= %lu, size = %i, handle= %lu, GetLastError=%lu\n\n",res,bwritten,size,hCom,dw);
if( res == FALSE ) {
ret = -1;
} else {
ret = bwritten;
}
#else
ret = write(fd, buf, size);
#endif
#if DEBUG
if (ret != size) {
if (ret == -1) {
fprintf(stderr, "Error code: %d\n", ret);
} else {
fprintf(stderr, "Error sending %d bytes (wrote %d)\n", size, ret);
}
}
#endif
return ret;
}
int serial_read(int fd, char *buf, int size, int retry_limit)
{
int len = 0;
int rbytes = 0;
#if _WIN32
HANDLE hCom = (HANDLE)fd;
unsigned long bread = 0;
rbytes = ReadFile(hCom, buf, size, &bread, NULL);
if (rbytes == FALSE || rbytes == -1) {
len = -1;
} else {
len = bread;
}
#else
int retries = 0;
while (len < size) {
rbytes = read(fd, buf + len, size - len);
//fprintf(stderr, "\tread %d (%d of %d) bytes\t(%d of %d)\n", rbytes, len, size, retries, retry_limit);
if (rbytes == -1){
//fprintf(stderr, "rbytes -1");
return -1;
} else if (rbytes == 0) {
retries++;
if (retries >= retry_limit) {
break;
}
} else {
retries = 0;
len += rbytes;
}
}
#endif
#if DEBUG
if (len != size) {
fprintf(stderr, "Error receiving %d bytes (read %d)\n", size, len);
}
#endif
return len;
}
int serial_open(char *port)
{
int fd;
#if _WIN32
static char full_path[32] = {0};
HANDLE hCom = NULL;
if( port[0] != '\\' ) {
_snprintf(full_path, sizeof(full_path) - 1, "\\\\.\\%s", port);
port = full_path;
}
hCom = CreateFileA(port, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( !hCom || hCom == INVALID_HANDLE_VALUE ) {
fd = -1;
} else {
fd = (int)hCom;
}
#else
fprintf(stderr, "opening serial port %s\n", port);
// O_NDELAY is superceded by O_NONBLOCK in POSIX
fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1) {
fprintf(stderr, "Could not open serial port: ");
if (errno == ENOENT) {
fprintf(stderr, "No such file or directory: %s\n", port);
} else if (errno == EAGAIN) {
fprintf(stderr, "Serial port %s is already memory-mapped by another process.\n", port);
} else {
fprintf(stderr, "errno: %d\n", errno);
}
return -1;
}
#endif
return fd;
}
int serial_close(int fd)
{
#if _WIN32
HANDLE hCom = (HANDLE)fd;
return CloseHandle(hCom);
#else
return close(fd);
#endif
}