forked from atmelcorp/micro-sam-ba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomm.c
287 lines (245 loc) · 6.26 KB
/
comm.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
/*
* Copyright (c) 2015-2016, Atmel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "comm.h"
#include "utils.h"
static bool switch_to_binary(int fd);
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <Windows.h>
HANDLE hSerial;
char lastError[1024];
char deviceFullName[1024];
char * get_error()
{
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
return lastError;
}
int samba_open(const char* device)
{
strcpy(deviceFullName, "\\\\.\\");
strncat(deviceFullName, device, sizeof(deviceFullName));
hSerial = CreateFile(deviceFullName,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hSerial == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
//serial port does not exist. Inform user.
fprintf(stderr, "Port %s does not exist", device);
return -1;
}
//some other error occurred. Inform user.
perror("Could not open device");
return -1;
}
DCB dcbSerialParams = { 0 };
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
//error getting state
}
dcbSerialParams.BaudRate = CBR_115200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(hSerial, &dcbSerialParams)) {
//error setting serial port state
perror("Could not set serial port state");
return -1;
}
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hSerial, &timeouts)) {
//error occureed. Inform user
perror("Could not set serial port timeouts");
return -1;
}
if (!switch_to_binary(1)) {
//error occureed. Inform user
perror("Could not switch to binary mode");
CloseHandle(hSerial);
return -1;
}
return 1;
}
void samba_close(int fd)
{
if(fd > 0) CloseHandle(hSerial);
}
inline int SerialRead(int fd, void * buffer, unsigned int count)
{
DWORD dwBytesRead = 0;
if (fd <= 0) {
perror("Serial port not open!");
return -1;
}
if (!ReadFile(hSerial, buffer, count, &dwBytesRead, NULL)) {
//error occurred. Report to user.
perror("Read Error occured");
return -1;
}
return (int)dwBytesRead;
}
inline int SerialWrite(int fd, void const* buffer, unsigned int count)
{
DWORD dwBytesWritten = 0;
if (fd <= 0) {
perror("Serial port not open!");
return -1;
}
if (!WriteFile(hSerial, buffer, count, &dwBytesWritten, NULL)) {
//error occurred. Report to user.
perror("Write Error occured");
return -1;
}
return (int)dwBytesWritten;
}
#else
#include <termios.h>
static bool configure_tty(int fd, int speed)
{
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0) {
perror("error from tcgetattr: ");
return false;
}
if (speed) {
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);
}
tty.c_cflag &= ~(CSIZE | PARENB | PARODD | CSTOPB | CRTSCTS);
tty.c_cflag |= CS8 | CLOCAL | CREAD;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 5;
tty.c_iflag &= ~(ICRNL | IGNBRK | IXON | IXOFF | IXANY);
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
perror("error from tcsetattr: ");
return false;
}
return true;
}
/*static bool switch_to_binary(int fd)
{
char cmd[] = "N#";
if (write(fd, cmd, strlen(cmd)) != strlen(cmd))
return false;
return read(fd, cmd, 2) == 2;
}*/
int samba_open(const char* device)
{
int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
perror("Could not open device");
return -1;
}
if (!configure_tty(fd, B115200)) {
close(fd);
return -1;
}
if (!switch_to_binary(fd)) {
close(fd);
return -1;
}
return fd;
}
void samba_close(int fd)
{
close(fd);
}
#define SerialRead(fd, buffer, count) read(fd, buffer, count)
#define SerialWrite(fd, buffer, count) write(fd, buffer, count)
#endif
static bool switch_to_binary(int fd)
{
char cmd[] = "N#";
if (SerialWrite(fd, cmd, strlen(cmd)) != strlen(cmd))
return false;
return SerialRead(fd, cmd, 2) == 2;
}
bool samba_read_word(int fd, uint32_t addr, uint32_t* value)
{
char cmd[12];
snprintf(cmd, sizeof(cmd), "w%08x,#", addr);
if (SerialWrite(fd, cmd, strlen(cmd)) != strlen(cmd))
return false;
return SerialRead(fd, value, 4) == 4;
}
bool samba_write_word(int fd, uint32_t addr, uint32_t value)
{
char cmd[20];
snprintf(cmd, sizeof(cmd), "W%08x,%08x#", addr, value);
return SerialWrite(fd, cmd, strlen(cmd)) == strlen(cmd);
}
bool samba_read(int fd, uint8_t* buffer, uint32_t addr, uint32_t size)
{
char cmd[20];
while (size > 0) {
uint32_t count = MIN(size, 1024);
// workaround for bug when size is exactly 512
if (count == 512)
count = 1;
// workaround for other bug when size is exactly 1024
if (count == 1024)
count = 1024 - 1;
snprintf(cmd, sizeof(cmd), "R%08x,%08x#", addr, count);
printf("%s\n", cmd);
if (SerialWrite(fd, cmd, strlen(cmd)) != strlen(cmd))
return false;
if (SerialRead(fd, buffer, count) != count)
return false;
addr += count;
buffer += count;
size -= count;
}
return true;
}
bool samba_write(int fd, uint8_t* buffer, uint32_t addr, uint32_t size)
{
char cmd[20];
while (size > 0) {
uint32_t count = MIN(size, 1024);
// workaround for bug when size is exactly 512
if (count == 512)
count = 1;
snprintf(cmd, sizeof(cmd), "S%08x,%08x#", addr, count);
if (SerialWrite(fd, cmd, strlen(cmd)) != strlen(cmd))
return false;
if (SerialWrite(fd, buffer, count) != count)
return false;
buffer += count;
size -= count;
}
return true;
}