forked from Polarisru/updiprog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
com.c
303 lines (268 loc) · 8.13 KB
/
com.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
#ifdef __MINGW32__
#include <windows.h>
#include <winbase.h>
#endif
#ifdef __linux
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "log.h"
#define __linux
#ifdef __MINGW32__
static HANDLE hSerial;
#endif
#ifdef __linux
static int fd;
#endif
static int _fd = -1;
char *array_to_str(char * str, uint8_t *array, unsigned int n) {
int r;
if (n == 0) return 0;
if (n == 1) r = sprintf(str, "%X", array[0]);
else r = sprintf(str, "%X, ", array[0]);
array_to_str(str + r, array + 1, n - 1);
return str;
}
static uint32_t COM_Baudrate = 115200;
/** \brief Open COM port with settings
*
* \param [in] port Port name as string
* \param [in] baudrate Port baudrate
* \param [in] have_parity true if parity should be switched on
* \param [in] two_stopbits true if 2 stop bits used
* \return true if succeed
*
*/
bool COM_Open(char *port, uint32_t baudrate, bool have_parity, bool two_stopbits)
{
//printf("Opening %s at %u baud\n", port, baudrate);
COM_Baudrate = baudrate;
#ifdef __MINGW32__
char str[64];
uint8_t multiplier;
sprintf(str, "\\\\.\\%s", port);
hSerial = CreateFile(str, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
if (hSerial == INVALID_HANDLE_VALUE)
return false;
DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
GetCommState(hSerial, &dcbSerialParams);
dcbSerialParams.BaudRate = baudrate; // Setting BaudRate
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
if (two_stopbits == true)
dcbSerialParams.StopBits = TWOSTOPBITS;
else
dcbSerialParams.StopBits = ONESTOPBIT;
if (have_parity == true)
dcbSerialParams.Parity = EVENPARITY; // Setting Parity = None
else
dcbSerialParams.Parity = NOPARITY;
dcbSerialParams.fDtrControl = DTR_CONTROL_DISABLE;
SetCommState(hSerial, &dcbSerialParams);
COMMTIMEOUTS timeouts;
multiplier = (uint8_t)ceil((float)100000 / baudrate);
timeouts.ReadIntervalTimeout = 20 * multiplier;
timeouts.ReadTotalTimeoutMultiplier = 1 * multiplier;
timeouts.ReadTotalTimeoutConstant = 100 * multiplier;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
SetCommTimeouts(hSerial, &timeouts);
//COM_Bytes = 0;
#endif
#ifdef __linux
fd = open(port, O_RDWR | O_NOCTTY );
if (fd <0)
return false;
struct termios SerialPortSettings;
tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
/* Setting the Baud rate */
switch (baudrate)
{
case 300:
cfsetispeed(&SerialPortSettings, B300);
cfsetospeed(&SerialPortSettings, B300);
break;
case 4800:
cfsetispeed(&SerialPortSettings, B4800);
cfsetospeed(&SerialPortSettings, B4800);
break;
case 9600:
cfsetispeed(&SerialPortSettings, B9600);
cfsetospeed(&SerialPortSettings, B9600);
break;
case 38400:
cfsetispeed(&SerialPortSettings, B38400);
cfsetospeed(&SerialPortSettings, B38400);
break;
case 57600:
cfsetispeed(&SerialPortSettings, B57600);
cfsetospeed(&SerialPortSettings, B57600);
break;
default:
case 115200:
cfsetispeed(&SerialPortSettings, B115200);
cfsetospeed(&SerialPortSettings, B115200);
break;
}
cfmakeraw(&SerialPortSettings); /* Set raw mode (special processing disabled) */
if (have_parity == true)
SerialPortSettings.c_cflag |= PARENB; /* Enables the Parity Enable bit(PARENB) */
else
SerialPortSettings.c_cflag &= ~PARENB; /* Disables the Parity Enable bit(PARENB), so No Parity */
if (two_stopbits == true)
SerialPortSettings.c_cflag |= CSTOPB; /* CSTOPB = 2 Stop bits */
else
SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
SerialPortSettings.c_cflag |= (CREAD | CLOCAL); /* Enable receiver,Ignore Modem Control lines */
SerialPortSettings.c_cc[VMIN] = 0; // read doesn't block
SerialPortSettings.c_cc[VTIME] = 5; // 0.1 seconds read timeout
tcsetattr(fd, TCSANOW, &SerialPortSettings); /* Set the attributes to the termios structure*/
tcflush(fd, TCIFLUSH);
#endif
return true;
}
/** \brief Write data to COM port
*
* \param [in] data Data buffer for writing
* \param [in] len Length of data buffer
* \return 0 if everything Ok
*
*/
int COM_Write(uint8_t *data, uint16_t len)
{
usleep(4000);
char buf[512];
LOG_Print(LOG_LEVEL_INFO, "COM_Write(%s)", array_to_str(buf, data, len));
#ifdef __MINGW32__
DWORD dwBytesWritten = 0;
//DWORD signal;
//OVERLAPPED ov = { 0 };
//int res;
//ov.hEvent = CreateEvent(NULL, true, true, NULL);
if (!WriteFile(hSerial, data, len, &dwBytesWritten, NULL))
return -1;
//COM_Bytes += dwBytesWritten;
// WriteFile(hSerial, data, len, &dwBytesWritten, &ov);
// signal = WaitForSingleObject(ov.hEvent, INFINITE);
// if ((signal == WAIT_OBJECT_0) && (GetOverlappedResult(hSerial, &ov, &dwBytesWritten, true)))
// res = 0;
// else
// res = -1;
// CloseHandle(ov.hEvent);
// return res;
#endif
#ifdef __linux
tcflush(fd, TCIOFLUSH);
int iOut = write(fd, data, len);
if (iOut < 0)
return -1;
#endif
return 0;
}
/** \brief Read data from COM port
*
* \param [out] data Data buffer to read data in
* \param [in] len Length of data to read
* \return number of received bytes as int
*
*/
int COM_Read(uint8_t *data, uint16_t len)
{
usleep(5000);
char buf[512];
#ifdef __MINGW32__
//OVERLAPPED ov = { 0 };
//COMSTAT status;
//DWORD errors;
//DWORD mask, btr, temp, signal;
DWORD dwBytesRead = 0;
// ClearCommError(hSerial, &errors, &status);
// if (!ReadFile(hSerial, data, len, &dwBytesRead, &ov))
// return -1;
// btr = 0;
// while (btr < len)
// {
// SetCommMask(hSerial, EV_RXCHAR);
// WaitCommEvent(hSerial, &mask, NULL);
// if (mask & EV_ERR)
// break;
// ClearCommError(hSerial, &temp, &status);
// btr = status.cbInQue;
// if (btr >= len)
// {
// ReadFile(hSerial, data, len, &dwBytesRead, NULL);
// }
// }
ReadFile(hSerial, data, len, &dwBytesRead, NULL);
#endif
#ifdef __linux
// Variables
uint16_t bytes_remaining;
int byte_cnt=0;
int ret;
// give system some time to settle before starting a read
//
LOG_Print(LOG_LEVEL_INFO, "Trying to read %u bytes", len);
// Copy len to remainting bytes to read var
bytes_remaining = len;
while (bytes_remaining) {
// Try and read a single byte on the current position in the recieve buffer
ret = read(fd, &data[byte_cnt], 1);
// Check number of bytes that have been read(ret)
if (ret <= 0) {
LOG_Print(LOG_LEVEL_WARNING,"Read fail byte %u, return code: %u, read bytes sofar(%s)", byte_cnt, ret, array_to_str(buf, data, byte_cnt));
return false;
}
if (ret>1){
LOG_Print(LOG_LEVEL_WARNING,"Read more than one byte!?(%u)", ret);
}
bytes_remaining -=ret;
byte_cnt += ret;
}
LOG_Print(LOG_LEVEL_INFO, "COM_Read(%s)", array_to_str(buf, data, byte_cnt));
return byte_cnt;
#endif
}
/** \brief Calculate time for transmission with current baudrate
*
* \param [in] len Length of transmitted data
* \return time in milliseconds as uint16_t
*
*/
uint16_t COM_GetTransTime(uint16_t len)
{
return (uint16_t)(len * 1000 * 11 / COM_Baudrate + 1);
}
#ifdef __MINGW32__
void COM_WaitForTransmit(void)
{
COMSTAT rStat;
DWORD nErr;
do {
ClearCommError(hSerial, &nErr, &rStat);
} while (rStat.cbOutQue > 0);
}
#endif // __MINGW32__
/** \brief Close current COM port
*
* \return Nothing
*
*/
void COM_Close(void)
{
//printf("Closing COM port\n");
#ifdef __MINGW32__
CloseHandle(hSerial);
#endif
#ifdef __linux
close(fd);
#endif
}