2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Contributions: Jon Trulson <jtrulson@ics.com>
4 * Brendan le Foll <brendan.le.foll@intel.com>
5 * Copyright (c) 2014 - 2015 Intel Corporation.
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include "mraa_internal.h"
36 // This function takes an unsigned int and converts it to a B* speed_t
37 // that can be used with linux/posix termios
39 uint2speed(unsigned int speed)
43 return B0; // hangup, not too useful otherwise
103 // if we are here, then an unsupported baudrate was selected.
104 // Report it via syslog and return B9600, a common default.
105 syslog(LOG_ERR, "uart: unsupported baud rate, defaulting to 9600.");
111 mraa_uart_init(int index)
114 syslog(LOG_ERR, "uart: platform not initialised");
118 if (advance_func->uart_init_pre != NULL) {
119 if (advance_func->uart_init_pre(index) != MRAA_SUCCESS) {
120 syslog(LOG_ERR, "uart: failure in pre-init platform hook");
125 if (plat->uart_dev_count == 0) {
126 syslog(LOG_ERR, "uart: platform has no UARTs defined");
130 if (plat->uart_dev_count <= index) {
131 syslog(LOG_ERR, "uart: platform has only %i", plat->uart_dev_count);
135 int pos = plat->uart_dev[index].rx;
137 if (plat->pins[pos].uart.mux_total > 0) {
138 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
139 syslog(LOG_ERR, "uart: failed to setup muxes for RX pin");
146 pos = plat->uart_dev[index].tx;
147 if (plat->pins[pos].uart.mux_total > 0) {
148 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
149 syslog(LOG_ERR, "uart: failed to setup muxes for TX pin");
155 mraa_uart_context dev = mraa_uart_init_raw((char*)plat->uart_dev[index].device_path);
159 dev->index = index; //Set the board Index.
161 if (advance_func->uart_init_post != NULL) {
162 mraa_result_t ret = advance_func->uart_init_post(dev);
163 if (ret != MRAA_SUCCESS) {
173 mraa_uart_init_raw(char* path)
175 mraa_uart_context dev = (mraa_uart_context) malloc(sizeof(struct _uart));
177 syslog(LOG_CRIT, "uart: Failed to allocate memory for context");
180 memset(dev, 0, sizeof(struct _uart));
187 syslog(LOG_ERR, "uart: device path undefined, open failed");
192 // now open the device
193 if ((dev->fd = open(dev->path, O_RDWR)) == -1) {
194 syslog(LOG_ERR, "uart: open() failed");
199 // now setup the tty and the selected baud rate
200 struct termios termio;
203 if (tcgetattr(dev->fd, &termio)) {
204 syslog(LOG_ERR, "uart: tcgetattr() failed");
210 // setup for a 'raw' mode. 8N1, no echo or special character
211 // handling, such as flow control or line editing semantics.
212 // cfmakeraw is not POSIX!
215 if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) {
225 mraa_uart_stop(mraa_uart_context dev)
228 syslog(LOG_ERR, "uart: stop: context is NULL");
229 return MRAA_ERROR_INVALID_HANDLE;
232 // just close the device and reset our fd.
243 mraa_uart_flush(mraa_uart_context dev)
246 syslog(LOG_ERR, "uart: stop: context is NULL");
247 return MRAA_ERROR_INVALID_HANDLE;
250 if (!tcdrain(dev->fd)) {
251 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
258 mraa_uart_set_baudrate(mraa_uart_context dev, unsigned int baud)
261 syslog(LOG_ERR, "uart: stop: context is NULL");
262 return MRAA_ERROR_INVALID_HANDLE;
265 struct termios termio;
266 if (tcgetattr(dev->fd, &termio)) {
267 syslog(LOG_ERR, "uart: tcgetattr() failed");
268 return MRAA_ERROR_INVALID_HANDLE;
271 // set our baud rates
272 speed_t speed = uint2speed(baud);
273 cfsetispeed(&termio, speed);
274 cfsetospeed(&termio, speed);
277 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
278 syslog(LOG_ERR, "uart: tcsetattr() failed");
279 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
285 mraa_uart_set_mode(mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits)
288 syslog(LOG_ERR, "uart: stop: context is NULL");
289 return MRAA_ERROR_INVALID_HANDLE;
292 struct termios termio;
293 if (!tcgetattr(dev->fd, &termio)) {
294 syslog(LOG_ERR, "uart: tcgetattr() failed");
295 return MRAA_ERROR_INVALID_HANDLE;
298 termio.c_cflag &= ~CSIZE;
301 termio.c_cflag |= CS8;
304 termio.c_cflag |= CS7;
307 termio.c_cflag |= CS6;
310 termio.c_cflag |= CS5;
313 termio.c_cflag |= CS8;
317 // POSIX & linux doesn't support 1.5 and I've got bigger fish to fry
320 termio.c_cflag &= CSTOPB;
323 termio.c_cflag |= CSTOPB;
329 case MRAA_UART_PARITY_NONE:
330 termio.c_cflag &= ~(PARENB | PARODD);
332 case MRAA_UART_PARITY_EVEN:
333 termio.c_cflag &= ~PARODD;
334 termio.c_cflag |= PARODD;
336 case MRAA_UART_PARITY_ODD:
337 termio.c_cflag |= PARENB | PARODD;
339 case MRAA_UART_PARITY_MARK: // not POSIX
340 termio.c_cflag |= PARENB | CMSPAR | PARODD;
342 case MRAA_UART_PARITY_SPACE: // not POSIX
343 termio.c_cflag |= PARENB | CMSPAR;
344 termio.c_cflag &= ~PARODD;
348 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
349 syslog(LOG_ERR, "uart: tcsetattr() failed");
350 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
357 mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts)
360 syslog(LOG_ERR, "uart: stop: context is NULL");
361 return MRAA_ERROR_INVALID_HANDLE;
364 // hardware flow control
369 if (!tcflow(dev->fd, action)) {
370 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
374 struct termios termio;
377 if (!tcgetattr(dev->fd, &termio)) {
378 syslog(LOG_ERR, "uart: tcgetattr() failed");
379 return MRAA_ERROR_INVALID_HANDLE;
383 termio.c_cflag |= CRTSCTS;
385 termio.c_cflag &= ~CRTSCTS;
388 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
389 syslog(LOG_ERR, "uart: tcsetattr() failed");
390 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
397 mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar)
400 syslog(LOG_ERR, "uart: stop: context is NULL");
401 return MRAA_ERROR_INVALID_HANDLE;
404 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
408 mraa_uart_get_dev_path(mraa_uart_context dev)
411 syslog(LOG_ERR, "uart: get_device_path failed, context is NULL");
414 if (dev->path == NULL) {
415 syslog(LOG_ERR, "uart: device path undefined");
423 mraa_uart_read(mraa_uart_context dev, char* buf, size_t len)
426 syslog(LOG_ERR, "uart: read: context is NULL");
427 return MRAA_ERROR_INVALID_HANDLE;
431 syslog(LOG_ERR, "uart: port is not open");
432 return MRAA_ERROR_INVALID_RESOURCE;
435 return read(dev->fd, buf, len);
439 mraa_uart_write(mraa_uart_context dev, char* buf, size_t len)
442 syslog(LOG_ERR, "uart: write: context is NULL");
443 return MRAA_ERROR_INVALID_HANDLE;
447 syslog(LOG_ERR, "uart: port is not open");
448 return MRAA_ERROR_INVALID_RESOURCE;
451 return write(dev->fd, buf, len);
455 mraa_uart_data_available(mraa_uart_context dev, unsigned int millis)
458 syslog(LOG_ERR, "uart: data_available: write context is NULL");
463 syslog(LOG_ERR, "uart: port is not open");
467 struct timeval timeout;
474 timeout.tv_sec = millis / 1000;
475 timeout.tv_usec = (millis % 1000) * 1000;
481 FD_SET(dev->fd, &readfds);
483 if (select(dev->fd + 1, &readfds, NULL, NULL, &timeout) > 0) {
484 return 1; // data is ready