]>
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 | |
76 | return CLAIMED_SERIAL_PORT; | |
5bcc76c4 | 77 | } |
62ee4fed | 78 | |
79 | // Try to retrieve the old (current) terminal info struct | |
80 | if(tcgetattr(sp->fd,&sp->tiOld) == -1) { | |
5bcc76c4 | 81 | uart_close(sp); |
62ee4fed | 82 | return INVALID_SERIAL_PORT; |
5bcc76c4 | 83 | } |
13dbdd6b | 84 | |
62ee4fed | 85 | // Duplicate the (old) terminal info struct |
5bcc76c4 | 86 | sp->tiNew = sp->tiOld; |
13dbdd6b | 87 | |
62ee4fed | 88 | // Configure the serial port |
5bcc76c4 | 89 | sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD; |
62ee4fed | 90 | sp->tiNew.c_iflag = IGNPAR; |
5bcc76c4 | 91 | sp->tiNew.c_oflag = 0; |
92 | sp->tiNew.c_lflag = 0; | |
62ee4fed | 93 | |
94 | // Block until n bytes are received | |
95 | sp->tiNew.c_cc[VMIN] = 0; | |
96 | // Block until a timer expires (n * 100 mSec.) | |
97 | sp->tiNew.c_cc[VTIME] = 0; | |
0a24369c | 98 | |
62ee4fed | 99 | // Try to set the new terminal info struct |
100 | if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) { | |
5bcc76c4 | 101 | uart_close(sp); |
102 | return INVALID_SERIAL_PORT; | |
103 | } | |
13dbdd6b | 104 | |
62ee4fed | 105 | // Flush all lingering data that may exist |
0a24369c | 106 | tcflush(sp->fd, TCIOFLUSH); |
62ee4fed | 107 | |
5bcc76c4 | 108 | return sp; |
109 | } | |
110 | ||
62ee4fed | 111 | void uart_close(const serial_port sp) { |
112 | serial_port_unix* spu = (serial_port_unix*)sp; | |
113 | tcflush(spu->fd,TCIOFLUSH); | |
114 | tcsetattr(spu->fd,TCSANOW,&(spu->tiOld)); | |
115 | struct flock fl; | |
116 | fl.l_type = F_UNLCK; | |
117 | fl.l_whence = SEEK_SET; | |
118 | fl.l_start = 0; | |
119 | fl.l_len = 0; | |
120 | fl.l_pid = getpid(); | |
121 | fcntl(spu->fd, F_SETLK, &fl); | |
122 | close(spu->fd); | |
123 | free(sp); | |
124 | } | |
125 | ||
13dbdd6b | 126 | bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { |
5bcc76c4 | 127 | const serial_port_unix* spu = (serial_port_unix*)sp; |
62ee4fed | 128 | speed_t stPortSpeed; |
129 | switch (uiPortSpeed) { | |
130 | case 0: stPortSpeed = B0; break; | |
131 | case 50: stPortSpeed = B50; break; | |
132 | case 75: stPortSpeed = B75; break; | |
133 | case 110: stPortSpeed = B110; break; | |
134 | case 134: stPortSpeed = B134; break; | |
135 | case 150: stPortSpeed = B150; break; | |
136 | case 300: stPortSpeed = B300; break; | |
137 | case 600: stPortSpeed = B600; break; | |
138 | case 1200: stPortSpeed = B1200; break; | |
139 | case 1800: stPortSpeed = B1800; break; | |
140 | case 2400: stPortSpeed = B2400; break; | |
141 | case 4800: stPortSpeed = B4800; break; | |
142 | case 9600: stPortSpeed = B9600; break; | |
143 | case 19200: stPortSpeed = B19200; break; | |
144 | case 38400: stPortSpeed = B38400; break; | |
145 | # ifdef B57600 | |
146 | case 57600: stPortSpeed = B57600; break; | |
147 | # endif | |
148 | # ifdef B115200 | |
149 | case 115200: stPortSpeed = B115200; break; | |
150 | # endif | |
151 | # ifdef B230400 | |
152 | case 230400: stPortSpeed = B230400; break; | |
153 | # endif | |
154 | # ifdef B460800 | |
155 | case 460800: stPortSpeed = B460800; break; | |
156 | # endif | |
157 | # ifdef B921600 | |
158 | case 921600: stPortSpeed = B921600; break; | |
159 | # endif | |
160 | default: return false; | |
161 | }; | |
162 | struct termios ti; | |
163 | if (tcgetattr(spu->fd,&ti) == -1) return false; | |
164 | // Set port speed (Input and Output) | |
165 | cfsetispeed(&ti,stPortSpeed); | |
166 | cfsetospeed(&ti,stPortSpeed); | |
167 | return (tcsetattr(spu->fd,TCSANOW,&ti) != -1); | |
5bcc76c4 | 168 | } |
169 | ||
13dbdd6b | 170 | uint32_t uart_get_speed(const serial_port sp) { |
62ee4fed | 171 | struct termios ti; |
172 | uint32_t uiPortSpeed; | |
5bcc76c4 | 173 | const serial_port_unix* spu = (serial_port_unix*)sp; |
62ee4fed | 174 | if (tcgetattr(spu->fd,&ti) == -1) return 0; |
175 | // Set port speed (Input) | |
176 | speed_t stPortSpeed = cfgetispeed(&ti); | |
177 | switch (stPortSpeed) { | |
178 | case B0: uiPortSpeed = 0; break; | |
179 | case B50: uiPortSpeed = 50; break; | |
180 | case B75: uiPortSpeed = 75; break; | |
181 | case B110: uiPortSpeed = 110; break; | |
182 | case B134: uiPortSpeed = 134; break; | |
183 | case B150: uiPortSpeed = 150; break; | |
184 | case B300: uiPortSpeed = 300; break; | |
185 | case B600: uiPortSpeed = 600; break; | |
186 | case B1200: uiPortSpeed = 1200; break; | |
187 | case B1800: uiPortSpeed = 1800; break; | |
188 | case B2400: uiPortSpeed = 2400; break; | |
189 | case B4800: uiPortSpeed = 4800; break; | |
190 | case B9600: uiPortSpeed = 9600; break; | |
191 | case B19200: uiPortSpeed = 19200; break; | |
192 | case B38400: uiPortSpeed = 38400; break; | |
193 | # ifdef B57600 | |
194 | case B57600: uiPortSpeed = 57600; break; | |
195 | # endif | |
196 | # ifdef B115200 | |
197 | case B115200: uiPortSpeed = 115200; break; | |
198 | # endif | |
199 | # ifdef B230400 | |
200 | case B230400: uiPortSpeed = 230400; break; | |
201 | # endif | |
202 | # ifdef B460800 | |
203 | case B460800: uiPortSpeed = 460800; break; | |
204 | # endif | |
205 | # ifdef B921600 | |
206 | case B921600: uiPortSpeed = 921600; break; | |
207 | # endif | |
208 | default: return 0; | |
209 | }; | |
5bcc76c4 | 210 | return uiPortSpeed; |
211 | } | |
212 | ||
62ee4fed | 213 | bool uart_set_parity(serial_port sp, serial_port_parity spp) { |
214 | struct termios ti; | |
215 | const serial_port_unix* spu = (serial_port_unix*)sp; | |
216 | if (tcgetattr(spu->fd,&ti) == -1) return false; | |
217 | switch(spp) { | |
218 | case SP_INVALID: return false; | |
219 | case SP_NONE: ti.c_cflag &= ~(PARENB | PARODD); break; | |
220 | case SP_EVEN: ti.c_cflag |= PARENB; ti.c_cflag &= ~(PARODD); break; | |
221 | case SP_ODD: ti.c_cflag |= PARENB | PARODD; break; | |
222 | } | |
223 | return (tcsetattr(spu->fd,TCSANOW,&ti) != -1); | |
224 | } | |
225 | ||
226 | serial_port_parity uart_get_parity(const serial_port sp) { | |
227 | struct termios ti; | |
228 | const serial_port_unix* spu = (serial_port_unix*)sp; | |
229 | if (tcgetattr(spu->fd,&ti) == -1) return SP_INVALID; | |
230 | ||
231 | if (ti.c_cflag & PARENB) { | |
232 | if (ti.c_cflag & PARODD) { | |
233 | return SP_ODD; | |
234 | } else { | |
235 | return SP_EVEN; | |
236 | } | |
237 | } else { | |
238 | return SP_NONE; | |
239 | } | |
5bcc76c4 | 240 | } |
241 | ||
13dbdd6b | 242 | bool uart_cts(const serial_port sp) { |
5bcc76c4 | 243 | char status; |
244 | if (ioctl(((serial_port_unix*)sp)->fd,TIOCMGET,&status) < 0) return false; | |
245 | return (status & TIOCM_CTS); | |
246 | } | |
247 | ||
13dbdd6b | 248 | bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) { |
62ee4fed | 249 | |
5bcc76c4 | 250 | int res; |
251 | int byteCount; | |
252 | fd_set rfds; | |
253 | struct timeval tv; | |
13dbdd6b | 254 | |
255 | // Reset the output count | |
5bcc76c4 | 256 | *pszRxLen = 0; |
13dbdd6b | 257 | |
5bcc76c4 | 258 | do { |
259 | // Reset file descriptor | |
260 | FD_ZERO(&rfds); | |
261 | FD_SET(((serial_port_unix*)sp)->fd,&rfds); | |
262 | tv = timeout; | |
263 | res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv); | |
13dbdd6b | 264 | |
5bcc76c4 | 265 | // Read error |
266 | if (res < 0) { | |
5bcc76c4 | 267 | return false; |
268 | } | |
babfcaa0 | 269 | |
5bcc76c4 | 270 | // Read time-out |
271 | if (res == 0) { | |
272 | if (*pszRxLen == 0) { | |
273 | // Error, we received no data | |
5bcc76c4 | 274 | return false; |
275 | } else { | |
276 | // We received some data, but nothing more is available | |
277 | return true; | |
278 | } | |
279 | } | |
babfcaa0 | 280 | |
5bcc76c4 | 281 | // Retrieve the count of the incoming bytes |
282 | res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount); | |
283 | if (res < 0) return false; | |
babfcaa0 | 284 | |
5bcc76c4 | 285 | // There is something available, read the data |
286 | res = read(((serial_port_unix*)sp)->fd,pbtRx+(*pszRxLen),byteCount); | |
babfcaa0 | 287 | |
5bcc76c4 | 288 | // Stop if the OS has some troubles reading the data |
289 | if (res <= 0) return false; | |
babfcaa0 | 290 | |
5bcc76c4 | 291 | *pszRxLen += res; |
babfcaa0 | 292 | |
293 | if(res==byteCount) | |
294 | return true; | |
13dbdd6b | 295 | |
5bcc76c4 | 296 | } while (byteCount); |
babfcaa0 | 297 | |
5bcc76c4 | 298 | return true; |
299 | } | |
300 | ||
13dbdd6b | 301 | bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { |
5bcc76c4 | 302 | int32_t res; |
303 | size_t szPos = 0; | |
304 | fd_set rfds; | |
305 | struct timeval tv; | |
13dbdd6b | 306 | |
62ee4fed | 307 | while (szPos < szTxLen) { |
5bcc76c4 | 308 | // Reset file descriptor |
309 | FD_ZERO(&rfds); | |
310 | FD_SET(((serial_port_unix*)sp)->fd,&rfds); | |
311 | tv = timeout; | |
312 | res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv); | |
13dbdd6b | 313 | |
5bcc76c4 | 314 | // Write error |
315 | if (res < 0) { | |
5bcc76c4 | 316 | return false; |
317 | } | |
13dbdd6b | 318 | |
5bcc76c4 | 319 | // Write time-out |
320 | if (res == 0) { | |
5bcc76c4 | 321 | return false; |
322 | } | |
13dbdd6b | 323 | |
5bcc76c4 | 324 | // Send away the bytes |
325 | res = write(((serial_port_unix*)sp)->fd,pbtTx+szPos,szTxLen-szPos); | |
326 | ||
327 | // Stop if the OS has some troubles sending the data | |
328 | if (res <= 0) return false; | |
13dbdd6b | 329 | |
5bcc76c4 | 330 | szPos += res; |
331 | } | |
332 | return true; | |
333 | } | |
334 | ||
335 | #else | |
336 | // The windows serial port implementation | |
337 | ||
13dbdd6b | 338 | typedef struct { |
5bcc76c4 | 339 | HANDLE hPort; // Serial port handle |
340 | DCB dcb; // Device control settings | |
341 | COMMTIMEOUTS ct; // Serial port time-out configuration | |
342 | } serial_port_windows; | |
343 | ||
a0bbdb76 | 344 | void upcase(char *p) { |
62ee4fed | 345 | while(*p != '\0') { |
346 | if(*p >= 97 && *p <= 122) { | |
347 | *p -= 32; | |
a0bbdb76 | 348 | } |
62ee4fed | 349 | ++p; |
350 | } | |
a0bbdb76 | 351 | } |
352 | ||
13dbdd6b | 353 | serial_port uart_open(const char* pcPortName) { |
5bcc76c4 | 354 | char acPortName[255]; |
355 | serial_port_windows* sp = malloc(sizeof(serial_port_windows)); | |
13dbdd6b | 356 | |
5bcc76c4 | 357 | // Copy the input "com?" to "\\.\COM?" format |
358 | sprintf(acPortName,"\\\\.\\%s",pcPortName); | |
a0bbdb76 | 359 | upcase(acPortName); |
13dbdd6b | 360 | |
5bcc76c4 | 361 | // Try to open the serial port |
362 | sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); | |
62ee4fed | 363 | if (sp->hPort == INVALID_HANDLE_VALUE) { |
5bcc76c4 | 364 | uart_close(sp); |
365 | return INVALID_SERIAL_PORT; | |
366 | } | |
13dbdd6b | 367 | |
5bcc76c4 | 368 | // Prepare the device control |
369 | memset(&sp->dcb, 0, sizeof(DCB)); | |
370 | sp->dcb.DCBlength = sizeof(DCB); | |
62ee4fed | 371 | if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) { |
5bcc76c4 | 372 | uart_close(sp); |
373 | return INVALID_SERIAL_PORT; | |
374 | } | |
13dbdd6b | 375 | |
5bcc76c4 | 376 | // Update the active serial port |
62ee4fed | 377 | if(!SetCommState(sp->hPort,&sp->dcb)) { |
5bcc76c4 | 378 | uart_close(sp); |
379 | return INVALID_SERIAL_PORT; | |
380 | } | |
13dbdd6b | 381 | |
5bcc76c4 | 382 | sp->ct.ReadIntervalTimeout = 0; |
383 | sp->ct.ReadTotalTimeoutMultiplier = 0; | |
384 | sp->ct.ReadTotalTimeoutConstant = 30; | |
385 | sp->ct.WriteTotalTimeoutMultiplier = 0; | |
386 | sp->ct.WriteTotalTimeoutConstant = 30; | |
13dbdd6b | 387 | |
62ee4fed | 388 | if(!SetCommTimeouts(sp->hPort,&sp->ct)) { |
5bcc76c4 | 389 | uart_close(sp); |
390 | return INVALID_SERIAL_PORT; | |
391 | } | |
13dbdd6b | 392 | |
5bcc76c4 | 393 | PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR); |
13dbdd6b | 394 | |
5bcc76c4 | 395 | return sp; |
396 | } | |
397 | ||
13dbdd6b | 398 | void uart_close(const serial_port sp) { |
5bcc76c4 | 399 | CloseHandle(((serial_port_windows*)sp)->hPort); |
400 | free(sp); | |
401 | } | |
402 | ||
13dbdd6b | 403 | bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { |
5bcc76c4 | 404 | serial_port_windows* spw; |
5bcc76c4 | 405 | spw = (serial_port_windows*)sp; |
406 | spw->dcb.BaudRate = uiPortSpeed; | |
13dbdd6b | 407 | return SetCommState(spw->hPort, &spw->dcb); |
5bcc76c4 | 408 | } |
409 | ||
13dbdd6b | 410 | uint32_t uart_get_speed(const serial_port sp) { |
5bcc76c4 | 411 | const serial_port_windows* spw = (serial_port_windows*)sp; |
13dbdd6b | 412 | if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) { |
5bcc76c4 | 413 | return spw->dcb.BaudRate; |
13dbdd6b | 414 | } |
5bcc76c4 | 415 | return 0; |
416 | } | |
417 | ||
13dbdd6b | 418 | bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) { |
5bcc76c4 | 419 | ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,*pszRxLen,(LPDWORD)pszRxLen,NULL); |
420 | return (*pszRxLen != 0); | |
421 | } | |
422 | ||
13dbdd6b | 423 | bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { |
5bcc76c4 | 424 | DWORD dwTxLen = 0; |
425 | return WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,szTxLen,&dwTxLen,NULL); | |
426 | return (dwTxLen != 0); | |
427 | } | |
428 | ||
429 | #endif |