]>
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 | ||
53 | // Work-around to claim uart interface using the c_iflag (software input processing) from the termios struct | |
13dbdd6b | 54 | #define CCLAIMED 0x00008000 |
55 | ||
56 | char pcPort[255]; | |
5bcc76c4 | 57 | |
58 | serial_port uart_open(const char* pcPortName) | |
59 | { | |
60 | serial_port_unix* sp = malloc(sizeof(serial_port_unix)); | |
13dbdd6b | 61 | strcpy(pcPort,pcPortName); |
62 | ||
5bcc76c4 | 63 | if (sp == 0) return INVALID_SERIAL_PORT; |
13dbdd6b | 64 | |
5bcc76c4 | 65 | sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); |
66 | if(sp->fd == -1) | |
67 | { | |
68 | uart_close(sp); | |
69 | return INVALID_SERIAL_PORT; | |
70 | } | |
13dbdd6b | 71 | |
5bcc76c4 | 72 | if(tcgetattr(sp->fd,&sp->tiOld) == -1) |
73 | { | |
74 | uart_close(sp); | |
75 | return INVALID_SERIAL_PORT; | |
76 | } | |
13dbdd6b | 77 | |
5bcc76c4 | 78 | // Make sure the port is not claimed already |
79 | if (sp->tiOld.c_iflag & CCLAIMED) | |
80 | { | |
81 | uart_close(sp); | |
82 | return CLAIMED_SERIAL_PORT; | |
83 | } | |
13dbdd6b | 84 | |
5bcc76c4 | 85 | // Copy the old terminal info struct |
86 | sp->tiNew = sp->tiOld; | |
13dbdd6b | 87 | |
5bcc76c4 | 88 | sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD; |
89 | sp->tiNew.c_iflag = CCLAIMED | IGNPAR; | |
90 | sp->tiNew.c_oflag = 0; | |
91 | sp->tiNew.c_lflag = 0; | |
0a24369c | 92 | |
93 | // Set serial port equivalent to raw mode | |
13dbdd6b | 94 | // sp->tiNew.c_iflag &= ~(CCLAIMED | IGNBRK | BRKINT | IGNPAR | ISTRIP | INLCR | IGNCR | ICRNL | IXON); |
0a24369c | 95 | // sp->tiNew.c_oflag &= ~OPOST; |
96 | // sp->tiNew.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); | |
13dbdd6b | 97 | // sp->tiNew.c_cflag &= ~(CSIZE | PARENB | CS8 | CLOCAL | CREAD); |
98 | ||
5bcc76c4 | 99 | sp->tiNew.c_cc[VMIN] = 0; // block until n bytes are received |
100 | sp->tiNew.c_cc[VTIME] = 0; // block until a timer expires (n * 100 mSec.) | |
13dbdd6b | 101 | |
5bcc76c4 | 102 | if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) |
103 | { | |
104 | uart_close(sp); | |
105 | return INVALID_SERIAL_PORT; | |
106 | } | |
13dbdd6b | 107 | |
0a24369c | 108 | tcflush(sp->fd, TCIOFLUSH); |
5bcc76c4 | 109 | return sp; |
110 | } | |
111 | ||
13dbdd6b | 112 | bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { |
5bcc76c4 | 113 | // Set port speed (Input and Output) |
5bcc76c4 | 114 | const serial_port_unix* spu = (serial_port_unix*)sp; |
13dbdd6b | 115 | cfsetispeed((struct termios*)&spu->tiNew, uiPortSpeed); |
116 | cfsetospeed((struct termios*)&spu->tiNew, uiPortSpeed); | |
117 | return (tcsetattr(spu->fd, TCSADRAIN, &spu->tiNew) != -1); | |
5bcc76c4 | 118 | } |
119 | ||
13dbdd6b | 120 | uint32_t uart_get_speed(const serial_port sp) { |
5bcc76c4 | 121 | uint32_t uiPortSpeed = 0; |
122 | const serial_port_unix* spu = (serial_port_unix*)sp; | |
13dbdd6b | 123 | uiPortSpeed = cfgetispeed(&spu->tiNew); |
5bcc76c4 | 124 | return uiPortSpeed; |
125 | } | |
126 | ||
13dbdd6b | 127 | void uart_close(const serial_port sp) { |
0a24369c | 128 | tcflush(((serial_port_unix*)sp)->fd,TCIOFLUSH); |
5bcc76c4 | 129 | tcsetattr(((serial_port_unix*)sp)->fd,TCSANOW,&((serial_port_unix*)sp)->tiOld); |
130 | close(((serial_port_unix*)sp)->fd); | |
131 | free(sp); | |
132 | } | |
133 | ||
13dbdd6b | 134 | bool uart_cts(const serial_port sp) { |
5bcc76c4 | 135 | char status; |
136 | if (ioctl(((serial_port_unix*)sp)->fd,TIOCMGET,&status) < 0) return false; | |
137 | return (status & TIOCM_CTS); | |
138 | } | |
139 | ||
13dbdd6b | 140 | bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) { |
5bcc76c4 | 141 | int res; |
142 | int byteCount; | |
143 | fd_set rfds; | |
144 | struct timeval tv; | |
13dbdd6b | 145 | |
146 | // Reset the output count | |
5bcc76c4 | 147 | *pszRxLen = 0; |
13dbdd6b | 148 | |
5bcc76c4 | 149 | do { |
150 | // Reset file descriptor | |
151 | FD_ZERO(&rfds); | |
152 | FD_SET(((serial_port_unix*)sp)->fd,&rfds); | |
153 | tv = timeout; | |
154 | res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv); | |
13dbdd6b | 155 | |
5bcc76c4 | 156 | // Read error |
157 | if (res < 0) { | |
5bcc76c4 | 158 | return false; |
159 | } | |
13dbdd6b | 160 | |
5bcc76c4 | 161 | // Read time-out |
162 | if (res == 0) { | |
163 | if (*pszRxLen == 0) { | |
164 | // Error, we received no data | |
5bcc76c4 | 165 | return false; |
166 | } else { | |
167 | // We received some data, but nothing more is available | |
168 | return true; | |
169 | } | |
170 | } | |
13dbdd6b | 171 | |
5bcc76c4 | 172 | // Retrieve the count of the incoming bytes |
173 | res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount); | |
174 | if (res < 0) return false; | |
13dbdd6b | 175 | |
5bcc76c4 | 176 | // There is something available, read the data |
177 | res = read(((serial_port_unix*)sp)->fd,pbtRx+(*pszRxLen),byteCount); | |
13dbdd6b | 178 | |
5bcc76c4 | 179 | // Stop if the OS has some troubles reading the data |
180 | if (res <= 0) return false; | |
13dbdd6b | 181 | |
5bcc76c4 | 182 | *pszRxLen += res; |
13dbdd6b | 183 | |
5bcc76c4 | 184 | } while (byteCount); |
13dbdd6b | 185 | |
5bcc76c4 | 186 | return true; |
187 | } | |
188 | ||
13dbdd6b | 189 | bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { |
5bcc76c4 | 190 | int32_t res; |
191 | size_t szPos = 0; | |
192 | fd_set rfds; | |
193 | struct timeval tv; | |
13dbdd6b | 194 | |
5bcc76c4 | 195 | while (szPos < szTxLen) |
196 | { | |
197 | // Reset file descriptor | |
198 | FD_ZERO(&rfds); | |
199 | FD_SET(((serial_port_unix*)sp)->fd,&rfds); | |
200 | tv = timeout; | |
201 | res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv); | |
13dbdd6b | 202 | |
5bcc76c4 | 203 | // Write error |
204 | if (res < 0) { | |
5bcc76c4 | 205 | return false; |
206 | } | |
13dbdd6b | 207 | |
5bcc76c4 | 208 | // Write time-out |
209 | if (res == 0) { | |
5bcc76c4 | 210 | return false; |
211 | } | |
13dbdd6b | 212 | |
5bcc76c4 | 213 | // Send away the bytes |
214 | res = write(((serial_port_unix*)sp)->fd,pbtTx+szPos,szTxLen-szPos); | |
215 | ||
216 | // Stop if the OS has some troubles sending the data | |
217 | if (res <= 0) return false; | |
13dbdd6b | 218 | |
5bcc76c4 | 219 | szPos += res; |
220 | } | |
221 | return true; | |
222 | } | |
223 | ||
224 | #else | |
225 | // The windows serial port implementation | |
226 | ||
13dbdd6b | 227 | typedef struct { |
5bcc76c4 | 228 | HANDLE hPort; // Serial port handle |
229 | DCB dcb; // Device control settings | |
230 | COMMTIMEOUTS ct; // Serial port time-out configuration | |
231 | } serial_port_windows; | |
232 | ||
a0bbdb76 | 233 | void upcase(char *p) { |
234 | while(*p != '\0') { | |
235 | if(*p >= 97 && *p <= 122) { | |
236 | *p -= 32; | |
237 | } | |
238 | ++p; | |
239 | } | |
240 | } | |
241 | ||
13dbdd6b | 242 | serial_port uart_open(const char* pcPortName) { |
5bcc76c4 | 243 | char acPortName[255]; |
244 | serial_port_windows* sp = malloc(sizeof(serial_port_windows)); | |
13dbdd6b | 245 | |
5bcc76c4 | 246 | // Copy the input "com?" to "\\.\COM?" format |
247 | sprintf(acPortName,"\\\\.\\%s",pcPortName); | |
a0bbdb76 | 248 | upcase(acPortName); |
13dbdd6b | 249 | |
5bcc76c4 | 250 | // Try to open the serial port |
251 | sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); | |
252 | if (sp->hPort == INVALID_HANDLE_VALUE) | |
253 | { | |
254 | uart_close(sp); | |
255 | return INVALID_SERIAL_PORT; | |
256 | } | |
13dbdd6b | 257 | |
5bcc76c4 | 258 | // Prepare the device control |
259 | memset(&sp->dcb, 0, sizeof(DCB)); | |
260 | sp->dcb.DCBlength = sizeof(DCB); | |
261 | if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) | |
262 | { | |
263 | uart_close(sp); | |
264 | return INVALID_SERIAL_PORT; | |
265 | } | |
13dbdd6b | 266 | |
5bcc76c4 | 267 | // Update the active serial port |
268 | if(!SetCommState(sp->hPort,&sp->dcb)) | |
269 | { | |
270 | uart_close(sp); | |
271 | return INVALID_SERIAL_PORT; | |
272 | } | |
13dbdd6b | 273 | |
5bcc76c4 | 274 | sp->ct.ReadIntervalTimeout = 0; |
275 | sp->ct.ReadTotalTimeoutMultiplier = 0; | |
276 | sp->ct.ReadTotalTimeoutConstant = 30; | |
277 | sp->ct.WriteTotalTimeoutMultiplier = 0; | |
278 | sp->ct.WriteTotalTimeoutConstant = 30; | |
13dbdd6b | 279 | |
5bcc76c4 | 280 | if(!SetCommTimeouts(sp->hPort,&sp->ct)) |
281 | { | |
282 | uart_close(sp); | |
283 | return INVALID_SERIAL_PORT; | |
284 | } | |
13dbdd6b | 285 | |
5bcc76c4 | 286 | PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR); |
13dbdd6b | 287 | |
5bcc76c4 | 288 | return sp; |
289 | } | |
290 | ||
13dbdd6b | 291 | void uart_close(const serial_port sp) { |
5bcc76c4 | 292 | CloseHandle(((serial_port_windows*)sp)->hPort); |
293 | free(sp); | |
294 | } | |
295 | ||
13dbdd6b | 296 | bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) { |
5bcc76c4 | 297 | serial_port_windows* spw; |
5bcc76c4 | 298 | spw = (serial_port_windows*)sp; |
299 | spw->dcb.BaudRate = uiPortSpeed; | |
13dbdd6b | 300 | return SetCommState(spw->hPort, &spw->dcb); |
5bcc76c4 | 301 | } |
302 | ||
13dbdd6b | 303 | uint32_t uart_get_speed(const serial_port sp) { |
5bcc76c4 | 304 | const serial_port_windows* spw = (serial_port_windows*)sp; |
13dbdd6b | 305 | if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) { |
5bcc76c4 | 306 | return spw->dcb.BaudRate; |
13dbdd6b | 307 | } |
5bcc76c4 | 308 | return 0; |
309 | } | |
310 | ||
13dbdd6b | 311 | bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) { |
5bcc76c4 | 312 | ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,*pszRxLen,(LPDWORD)pszRxLen,NULL); |
313 | return (*pszRxLen != 0); | |
314 | } | |
315 | ||
13dbdd6b | 316 | bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { |
5bcc76c4 | 317 | DWORD dwTxLen = 0; |
318 | return WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,szTxLen,&dwTxLen,NULL); | |
319 | return (dwTxLen != 0); | |
320 | } | |
321 | ||
322 | #endif |