]>
Commit | Line | Data |
---|---|---|
5bcc76c4 | 1 | /* |
13dbdd6b | 2 | * Generic uart / rs232/ serial port library |
5bcc76c4 | 3 | * |
13dbdd6b | 4 | * Copyright (c) 2013, Roel Verdult |
5bcc76c4 | 5 | * All rights reserved. |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3. Neither the name of the copyright holders nor the | |
15 | * names of its contributors may be used to endorse or promote products | |
16 | * derived from this software without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | |
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | * | |
29 | * @file uart.c | |
30 | * @brief | |
31 | * | |
5bcc76c4 | 32 | */ |
33 | ||
34 | #include "uart.h" | |
5bcc76c4 | 35 | |
36 | // Test if we are dealing with unix operating systems | |
37 | #ifndef _WIN32 | |
38 | ||
39 | #include <termios.h> | |
40 | typedef struct termios term_info; | |
41 | typedef struct { | |
42 | int fd; // Serial port file descriptor | |
43 | term_info tiOld; // Terminal info before using the port | |
44 | term_info tiNew; // Terminal info during the transaction | |
45 | } serial_port_unix; | |
46 | ||
47 | // Set time-out on 30 miliseconds | |
13dbdd6b | 48 | const struct timeval timeout = { |
5bcc76c4 | 49 | .tv_sec = 0, // 0 second |
50 | .tv_usec = 30000 // 30000 micro seconds | |
51 | }; | |
52 | ||
5bcc76c4 | 53 | serial_port uart_open(const char* pcPortName) |
54 | { | |
55 | serial_port_unix* sp = malloc(sizeof(serial_port_unix)); | |
5bcc76c4 | 56 | if (sp == 0) return INVALID_SERIAL_PORT; |
13dbdd6b | 57 | |
5bcc76c4 | 58 | sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); |
62ee4fed | 59 | if(sp->fd == -1) { |
5bcc76c4 | 60 | uart_close(sp); |
61 | return INVALID_SERIAL_PORT; | |
62 | } | |
62ee4fed | 63 | |
64 | // Finally figured out a way to claim a serial port interface under unix | |
65 | // We just try to set a (advisory) lock on the file descriptor | |
66 | struct flock fl; | |
67 | fl.l_type = F_WRLCK; | |
68 | fl.l_whence = SEEK_SET; | |
69 | fl.l_start = 0; | |
70 | fl.l_len = 0; | |
71 | fl.l_pid = getpid(); | |
13dbdd6b | 72 | |
62ee4fed | 73 | // Does the system allows us to place a lock on this file descriptor |
74 | if (fcntl(sp->fd, F_SETLK, &fl) == -1) { | |
75 | // A conflicting lock is held by another process | |
1b492a97 | 76 | free(sp); |
62ee4fed | 77 | return CLAIMED_SERIAL_PORT; |
5bcc76c4 | 78 | } |
62ee4fed | 79 | |
80 | // Try to retrieve the old (current) terminal info struct | |
81 | if(tcgetattr(sp->fd,&sp->tiOld) == -1) { | |
5bcc76c4 | 82 | uart_close(sp); |
62ee4fed | 83 | return INVALID_SERIAL_PORT; |
5bcc76c4 | 84 | } |
13dbdd6b | 85 | |
62ee4fed | 86 | // Duplicate the (old) terminal info struct |
5bcc76c4 | 87 | sp->tiNew = sp->tiOld; |
13dbdd6b | 88 | |
62ee4fed | 89 | // Configure the serial port |
5bcc76c4 | 90 | sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD; |
62ee4fed | 91 | sp->tiNew.c_iflag = IGNPAR; |
5bcc76c4 | 92 | sp->tiNew.c_oflag = 0; |
93 | sp->tiNew.c_lflag = 0; | |
62ee4fed | 94 | |
95 | // Block until n bytes are received | |
96 | sp->tiNew.c_cc[VMIN] = 0; | |
97 | // Block until a timer expires (n * 100 mSec.) | |
98 | sp->tiNew.c_cc[VTIME] = 0; | |
0a24369c | 99 | |
62ee4fed | 100 | // Try to set the new terminal info struct |
101 | if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) { | |
5bcc76c4 | 102 | uart_close(sp); |
103 | return INVALID_SERIAL_PORT; | |
104 | } | |
13dbdd6b | 105 | |
62ee4fed | 106 | // Flush all lingering data that may exist |
0a24369c | 107 | tcflush(sp->fd, TCIOFLUSH); |
62ee4fed | 108 | |
5ed5e418 | 109 | // set speed, works for UBUNTU 14.04 |
110 | bool err = uart_set_speed(sp, 460800); | |
111 | if (!err) | |
112 | uart_set_speed(sp, 115200); | |
113 | ||
5bcc76c4 | 114 | return sp; |
115 | } | |
116 | ||
62ee4fed | 117 | void uart_close(const serial_port sp) { |
118 | serial_port_unix* spu = (serial_port_unix*)sp; | |
119 | tcflush(spu->fd,TCIOFLUSH); | |
120 | tcsetattr(spu->fd,TCSANOW,&(spu->tiOld)); | |
121 | struct flock fl; | |
122 | fl.l_type = F_UNLCK; | |
123 | fl.l_whence = SEEK_SET; | |
124 | fl.l_start = 0; | |
125 | fl.l_len = 0; | |
126 | fl.l_pid = getpid(); | |
b85385a0 | 127 | |
128 | // Does the system allows us to place a lock on this file descriptor | |
9965e0d1 | 129 | int err = fcntl(spu->fd, F_SETLK, &fl); |
b85385a0 | 130 | if ( err == -1) { |
04da5cd9 | 131 | //perror("fcntl"); |
b85385a0 | 132 | } |
62ee4fed | 133 | close(spu->fd); |
134 | free(sp); | |
135 | } | |
136 | ||
13dbdd6b | 137 | bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { |
5bcc76c4 | 138 | const serial_port_unix* spu = (serial_port_unix*)sp; |
62ee4fed | 139 | speed_t stPortSpeed; |
140 | switch (uiPortSpeed) { | |
141 | case 0: stPortSpeed = B0; break; | |
142 | case 50: stPortSpeed = B50; break; | |
143 | case 75: stPortSpeed = B75; break; | |
144 | case 110: stPortSpeed = B110; break; | |
145 | case 134: stPortSpeed = B134; break; | |
146 | case 150: stPortSpeed = B150; break; | |
147 | case 300: stPortSpeed = B300; break; | |
148 | case 600: stPortSpeed = B600; break; | |
149 | case 1200: stPortSpeed = B1200; break; | |
150 | case 1800: stPortSpeed = B1800; break; | |
151 | case 2400: stPortSpeed = B2400; break; | |
152 | case 4800: stPortSpeed = B4800; break; | |
153 | case 9600: stPortSpeed = B9600; break; | |
154 | case 19200: stPortSpeed = B19200; break; | |
155 | case 38400: stPortSpeed = B38400; break; | |
156 | # ifdef B57600 | |
157 | case 57600: stPortSpeed = B57600; break; | |
158 | # endif | |
159 | # ifdef B115200 | |
160 | case 115200: stPortSpeed = B115200; break; | |
161 | # endif | |
162 | # ifdef B230400 | |
163 | case 230400: stPortSpeed = B230400; break; | |
164 | # endif | |
165 | # ifdef B460800 | |
166 | case 460800: stPortSpeed = B460800; break; | |
167 | # endif | |
168 | # ifdef B921600 | |
169 | case 921600: stPortSpeed = B921600; break; | |
170 | # endif | |
171 | default: return false; | |
172 | }; | |
173 | struct termios ti; | |
174 | if (tcgetattr(spu->fd,&ti) == -1) return false; | |
175 | // Set port speed (Input and Output) | |
176 | cfsetispeed(&ti,stPortSpeed); | |
177 | cfsetospeed(&ti,stPortSpeed); | |
178 | return (tcsetattr(spu->fd,TCSANOW,&ti) != -1); | |
5bcc76c4 | 179 | } |
180 | ||
13dbdd6b | 181 | uint32_t uart_get_speed(const serial_port sp) { |
62ee4fed | 182 | struct termios ti; |
183 | uint32_t uiPortSpeed; | |
5bcc76c4 | 184 | const serial_port_unix* spu = (serial_port_unix*)sp; |
62ee4fed | 185 | if (tcgetattr(spu->fd,&ti) == -1) return 0; |
186 | // Set port speed (Input) | |
187 | speed_t stPortSpeed = cfgetispeed(&ti); | |
188 | switch (stPortSpeed) { | |
189 | case B0: uiPortSpeed = 0; break; | |
190 | case B50: uiPortSpeed = 50; break; | |
191 | case B75: uiPortSpeed = 75; break; | |
192 | case B110: uiPortSpeed = 110; break; | |
193 | case B134: uiPortSpeed = 134; break; | |
194 | case B150: uiPortSpeed = 150; break; | |
195 | case B300: uiPortSpeed = 300; break; | |
196 | case B600: uiPortSpeed = 600; break; | |
197 | case B1200: uiPortSpeed = 1200; break; | |
198 | case B1800: uiPortSpeed = 1800; break; | |
199 | case B2400: uiPortSpeed = 2400; break; | |
200 | case B4800: uiPortSpeed = 4800; break; | |
201 | case B9600: uiPortSpeed = 9600; break; | |
202 | case B19200: uiPortSpeed = 19200; break; | |
203 | case B38400: uiPortSpeed = 38400; break; | |
204 | # ifdef B57600 | |
205 | case B57600: uiPortSpeed = 57600; break; | |
206 | # endif | |
207 | # ifdef B115200 | |
208 | case B115200: uiPortSpeed = 115200; break; | |
209 | # endif | |
210 | # ifdef B230400 | |
211 | case B230400: uiPortSpeed = 230400; break; | |
212 | # endif | |
213 | # ifdef B460800 | |
214 | case B460800: uiPortSpeed = 460800; break; | |
215 | # endif | |
216 | # ifdef B921600 | |
217 | case B921600: uiPortSpeed = 921600; break; | |
218 | # endif | |
219 | default: return 0; | |
220 | }; | |
5bcc76c4 | 221 | return uiPortSpeed; |
222 | } | |
223 | ||
62ee4fed | 224 | bool uart_set_parity(serial_port sp, serial_port_parity spp) { |
225 | struct termios ti; | |
226 | const serial_port_unix* spu = (serial_port_unix*)sp; | |
227 | if (tcgetattr(spu->fd,&ti) == -1) return false; | |
228 | switch(spp) { | |
229 | case SP_INVALID: return false; | |
230 | case SP_NONE: ti.c_cflag &= ~(PARENB | PARODD); break; | |
231 | case SP_EVEN: ti.c_cflag |= PARENB; ti.c_cflag &= ~(PARODD); break; | |
232 | case SP_ODD: ti.c_cflag |= PARENB | PARODD; break; | |
233 | } | |
234 | return (tcsetattr(spu->fd,TCSANOW,&ti) != -1); | |
235 | } | |
236 | ||
237 | serial_port_parity uart_get_parity(const serial_port sp) { | |
238 | struct termios ti; | |
239 | const serial_port_unix* spu = (serial_port_unix*)sp; | |
240 | if (tcgetattr(spu->fd,&ti) == -1) return SP_INVALID; | |
241 | ||
242 | if (ti.c_cflag & PARENB) { | |
243 | if (ti.c_cflag & PARODD) { | |
244 | return SP_ODD; | |
245 | } else { | |
246 | return SP_EVEN; | |
247 | } | |
248 | } else { | |
249 | return SP_NONE; | |
250 | } | |
5bcc76c4 | 251 | } |
252 | ||
13dbdd6b | 253 | bool uart_cts(const serial_port sp) { |
5bcc76c4 | 254 | char status; |
255 | if (ioctl(((serial_port_unix*)sp)->fd,TIOCMGET,&status) < 0) return false; | |
256 | return (status & TIOCM_CTS); | |
257 | } | |
258 | ||
13dbdd6b | 259 | bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) { |
62ee4fed | 260 | |
5bcc76c4 | 261 | int res; |
262 | int byteCount; | |
263 | fd_set rfds; | |
264 | struct timeval tv; | |
13dbdd6b | 265 | |
266 | // Reset the output count | |
5bcc76c4 | 267 | *pszRxLen = 0; |
13dbdd6b | 268 | |
5bcc76c4 | 269 | do { |
270 | // Reset file descriptor | |
271 | FD_ZERO(&rfds); | |
272 | FD_SET(((serial_port_unix*)sp)->fd,&rfds); | |
273 | tv = timeout; | |
274 | res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv); | |
13dbdd6b | 275 | |
5bcc76c4 | 276 | // Read error |
277 | if (res < 0) { | |
5bcc76c4 | 278 | return false; |
279 | } | |
babfcaa0 | 280 | |
5bcc76c4 | 281 | // Read time-out |
282 | if (res == 0) { | |
283 | if (*pszRxLen == 0) { | |
284 | // Error, we received no data | |
5bcc76c4 | 285 | return false; |
286 | } else { | |
287 | // We received some data, but nothing more is available | |
288 | return true; | |
289 | } | |
290 | } | |
babfcaa0 | 291 | |
5bcc76c4 | 292 | // Retrieve the count of the incoming bytes |
293 | res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount); | |
294 | if (res < 0) return false; | |
babfcaa0 | 295 | |
5bcc76c4 | 296 | // There is something available, read the data |
297 | res = read(((serial_port_unix*)sp)->fd,pbtRx+(*pszRxLen),byteCount); | |
babfcaa0 | 298 | |
5bcc76c4 | 299 | // Stop if the OS has some troubles reading the data |
300 | if (res <= 0) return false; | |
babfcaa0 | 301 | |
5bcc76c4 | 302 | *pszRxLen += res; |
babfcaa0 | 303 | |
304 | if(res==byteCount) | |
305 | return true; | |
13dbdd6b | 306 | |
5bcc76c4 | 307 | } while (byteCount); |
babfcaa0 | 308 | |
5bcc76c4 | 309 | return true; |
310 | } | |
311 | ||
13dbdd6b | 312 | bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { |
5bcc76c4 | 313 | int32_t res; |
314 | size_t szPos = 0; | |
315 | fd_set rfds; | |
316 | struct timeval tv; | |
13dbdd6b | 317 | |
62ee4fed | 318 | while (szPos < szTxLen) { |
5bcc76c4 | 319 | // Reset file descriptor |
320 | FD_ZERO(&rfds); | |
321 | FD_SET(((serial_port_unix*)sp)->fd,&rfds); | |
322 | tv = timeout; | |
323 | res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv); | |
13dbdd6b | 324 | |
5bcc76c4 | 325 | // Write error |
326 | if (res < 0) { | |
e772774b | 327 | printf("write error\n"); |
328 | return false; | |
5bcc76c4 | 329 | } |
13dbdd6b | 330 | |
5bcc76c4 | 331 | // Write time-out |
332 | if (res == 0) { | |
e772774b | 333 | printf("write time-out\n"); |
334 | return false; | |
5bcc76c4 | 335 | } |
13dbdd6b | 336 | |
5bcc76c4 | 337 | // Send away the bytes |
338 | res = write(((serial_port_unix*)sp)->fd,pbtTx+szPos,szTxLen-szPos); | |
339 | ||
340 | // Stop if the OS has some troubles sending the data | |
e772774b | 341 | if (res <= 0) { |
342 | printf("os troubles\n"); | |
343 | return false; | |
344 | } | |
13dbdd6b | 345 | |
5bcc76c4 | 346 | szPos += res; |
347 | } | |
348 | return true; | |
349 | } | |
350 | ||
351 | #else | |
352 | // The windows serial port implementation | |
353 | ||
13dbdd6b | 354 | typedef struct { |
5bcc76c4 | 355 | HANDLE hPort; // Serial port handle |
356 | DCB dcb; // Device control settings | |
357 | COMMTIMEOUTS ct; // Serial port time-out configuration | |
358 | } serial_port_windows; | |
359 | ||
a0bbdb76 | 360 | void upcase(char *p) { |
62ee4fed | 361 | while(*p != '\0') { |
362 | if(*p >= 97 && *p <= 122) { | |
363 | *p -= 32; | |
a0bbdb76 | 364 | } |
62ee4fed | 365 | ++p; |
366 | } | |
a0bbdb76 | 367 | } |
368 | ||
13dbdd6b | 369 | serial_port uart_open(const char* pcPortName) { |
5bcc76c4 | 370 | char acPortName[255]; |
371 | serial_port_windows* sp = malloc(sizeof(serial_port_windows)); | |
13dbdd6b | 372 | |
5bcc76c4 | 373 | // Copy the input "com?" to "\\.\COM?" format |
374 | sprintf(acPortName,"\\\\.\\%s",pcPortName); | |
a0bbdb76 | 375 | upcase(acPortName); |
13dbdd6b | 376 | |
5bcc76c4 | 377 | // Try to open the serial port |
378 | sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); | |
62ee4fed | 379 | if (sp->hPort == INVALID_HANDLE_VALUE) { |
5bcc76c4 | 380 | uart_close(sp); |
381 | return INVALID_SERIAL_PORT; | |
382 | } | |
13dbdd6b | 383 | |
5bcc76c4 | 384 | // Prepare the device control |
385 | memset(&sp->dcb, 0, sizeof(DCB)); | |
386 | sp->dcb.DCBlength = sizeof(DCB); | |
0fcfad0e | 387 | if(!BuildCommDCBA("baud=115200 parity=N data=8 stop=1",&sp->dcb)) { |
f62b5e12 | 388 | uart_close(sp); |
389 | return INVALID_SERIAL_PORT; | |
390 | } | |
13dbdd6b | 391 | |
5bcc76c4 | 392 | // Update the active serial port |
62ee4fed | 393 | if(!SetCommState(sp->hPort,&sp->dcb)) { |
5bcc76c4 | 394 | uart_close(sp); |
395 | return INVALID_SERIAL_PORT; | |
396 | } | |
5ed5e418 | 397 | // all zero's configure: no timeout for read/write used. |
398 | sp->ct.ReadIntervalTimeout = 0;//1; | |
399 | sp->ct.ReadTotalTimeoutMultiplier = 0;//1; | |
5bcc76c4 | 400 | sp->ct.ReadTotalTimeoutConstant = 30; |
5ed5e418 | 401 | sp->ct.WriteTotalTimeoutMultiplier = 0;//1; |
5bcc76c4 | 402 | sp->ct.WriteTotalTimeoutConstant = 30; |
13dbdd6b | 403 | |
62ee4fed | 404 | if(!SetCommTimeouts(sp->hPort,&sp->ct)) { |
5bcc76c4 | 405 | uart_close(sp); |
406 | return INVALID_SERIAL_PORT; | |
407 | } | |
13dbdd6b | 408 | |
5bcc76c4 | 409 | PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR); |
13dbdd6b | 410 | |
5ed5e418 | 411 | bool err = uart_set_speed(sp, 460800); |
412 | if (!err) | |
413 | uart_set_speed(sp, 115200); | |
414 | ||
5bcc76c4 | 415 | return sp; |
416 | } | |
417 | ||
13dbdd6b | 418 | void uart_close(const serial_port sp) { |
5bcc76c4 | 419 | CloseHandle(((serial_port_windows*)sp)->hPort); |
420 | free(sp); | |
421 | } | |
422 | ||
13dbdd6b | 423 | bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { |
5bcc76c4 | 424 | serial_port_windows* spw; |
5bcc76c4 | 425 | spw = (serial_port_windows*)sp; |
426 | spw->dcb.BaudRate = uiPortSpeed; | |
13dbdd6b | 427 | return SetCommState(spw->hPort, &spw->dcb); |
5bcc76c4 | 428 | } |
429 | ||
13dbdd6b | 430 | uint32_t uart_get_speed(const serial_port sp) { |
5bcc76c4 | 431 | const serial_port_windows* spw = (serial_port_windows*)sp; |
13dbdd6b | 432 | if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) { |
5bcc76c4 | 433 | return spw->dcb.BaudRate; |
13dbdd6b | 434 | } |
5bcc76c4 | 435 | return 0; |
436 | } | |
437 | ||
13dbdd6b | 438 | bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) { |
5bcc76c4 | 439 | ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,*pszRxLen,(LPDWORD)pszRxLen,NULL); |
440 | return (*pszRxLen != 0); | |
441 | } | |
442 | ||
13dbdd6b | 443 | bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { |
5bcc76c4 | 444 | DWORD dwTxLen = 0; |
e772774b | 445 | return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL); |
5bcc76c4 | 446 | return (dwTxLen != 0); |
447 | } | |
448 | ||
449 | #endif |