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.");
110 static mraa_uart_context
111 mraa_uart_init_internal(mraa_adv_func_t* func_table)
113 mraa_uart_context dev = (mraa_uart_context) calloc(1, sizeof(struct _uart));
115 syslog(LOG_CRIT, "uart: Failed to allocate memory for context");
120 dev->advance_func = func_table;
126 mraa_uart_init(int index)
129 syslog(LOG_ERR, "uart: platform not initialised");
133 if (mraa_is_sub_platform_id(index)) {
134 syslog(LOG_NOTICE, "uart: Using sub platform is not supported");
138 if (plat->adv_func->uart_init_pre != NULL) {
139 if (plat->adv_func->uart_init_pre(index) != MRAA_SUCCESS) {
140 syslog(LOG_ERR, "uart: failure in pre-init platform hook");
145 if (plat->uart_dev_count == 0) {
146 syslog(LOG_ERR, "uart: platform has no UARTs defined");
150 if (plat->uart_dev_count <= index) {
151 syslog(LOG_ERR, "uart: platform has only %i", plat->uart_dev_count);
155 if (!plat->no_bus_mux) {
156 int pos = plat->uart_dev[index].rx;
158 if (plat->pins[pos].uart.mux_total > 0) {
159 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
160 syslog(LOG_ERR, "uart: failed to setup muxes for RX pin");
166 pos = plat->uart_dev[index].tx;
168 if (plat->pins[pos].uart.mux_total > 0) {
169 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
170 syslog(LOG_ERR, "uart: failed to setup muxes for TX pin");
177 mraa_uart_context dev = mraa_uart_init_raw((char*)plat->uart_dev[index].device_path);
181 dev->index = index; //Set the board Index.
183 if (IS_FUNC_DEFINED(dev, uart_init_post)) {
184 mraa_result_t ret = dev->advance_func->uart_init_post(dev);
185 if (ret != MRAA_SUCCESS) {
195 mraa_uart_init_raw(const char* path)
197 mraa_uart_context dev = mraa_uart_init_internal(plat == NULL ? NULL : plat->adv_func);
199 syslog(LOG_ERR, "uart: Failed to allocate memory for context");
205 syslog(LOG_ERR, "uart: device path undefined, open failed");
210 // now open the device
211 if ((dev->fd = open(dev->path, O_RDWR)) == -1) {
212 syslog(LOG_ERR, "uart: open() failed");
217 // now setup the tty and the selected baud rate
218 struct termios termio;
221 if (tcgetattr(dev->fd, &termio)) {
222 syslog(LOG_ERR, "uart: tcgetattr() failed");
228 // setup for a 'raw' mode. 8N1, no echo or special character
229 // handling, such as flow control or line editing semantics.
230 // cfmakeraw is not POSIX!
232 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
233 syslog(LOG_ERR, "uart: tcsetattr() failed after cfmakeraw()");
239 if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) {
249 mraa_uart_stop(mraa_uart_context dev)
252 syslog(LOG_ERR, "uart: stop: context is NULL");
253 return MRAA_ERROR_INVALID_HANDLE;
256 // just close the device and reset our fd.
267 mraa_uart_flush(mraa_uart_context dev)
270 syslog(LOG_ERR, "uart: stop: context is NULL");
271 return MRAA_ERROR_INVALID_HANDLE;
274 if (tcdrain(dev->fd) == -1) {
275 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
282 mraa_uart_set_baudrate(mraa_uart_context dev, unsigned int baud)
285 syslog(LOG_ERR, "uart: stop: context is NULL");
286 return MRAA_ERROR_INVALID_HANDLE;
289 struct termios termio;
290 if (tcgetattr(dev->fd, &termio)) {
291 syslog(LOG_ERR, "uart: tcgetattr() failed");
292 return MRAA_ERROR_INVALID_HANDLE;
295 // set our baud rates
296 speed_t speed = uint2speed(baud);
297 cfsetispeed(&termio, speed);
298 cfsetospeed(&termio, speed);
301 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
302 syslog(LOG_ERR, "uart: tcsetattr() failed");
303 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
309 mraa_uart_set_mode(mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits)
312 syslog(LOG_ERR, "uart: stop: context is NULL");
313 return MRAA_ERROR_INVALID_HANDLE;
316 struct termios termio;
317 if (tcgetattr(dev->fd, &termio)) {
318 syslog(LOG_ERR, "uart: tcgetattr() failed");
319 return MRAA_ERROR_INVALID_HANDLE;
322 termio.c_cflag &= ~CSIZE;
325 termio.c_cflag |= CS8;
328 termio.c_cflag |= CS7;
331 termio.c_cflag |= CS6;
334 termio.c_cflag |= CS5;
337 termio.c_cflag |= CS8;
341 // POSIX & linux doesn't support 1.5 and I've got bigger fish to fry
344 termio.c_cflag &= ~CSTOPB;
347 termio.c_cflag |= CSTOPB;
353 case MRAA_UART_PARITY_NONE:
354 termio.c_cflag &= ~(PARENB | PARODD);
356 case MRAA_UART_PARITY_EVEN:
357 termio.c_cflag |= PARENB;
358 termio.c_cflag &= ~PARODD;
360 case MRAA_UART_PARITY_ODD:
361 termio.c_cflag |= PARENB | PARODD;
363 case MRAA_UART_PARITY_MARK: // not POSIX
364 termio.c_cflag |= PARENB | CMSPAR | PARODD;
366 case MRAA_UART_PARITY_SPACE: // not POSIX
367 termio.c_cflag |= PARENB | CMSPAR;
368 termio.c_cflag &= ~PARODD;
372 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
373 syslog(LOG_ERR, "uart: tcsetattr() failed");
374 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
381 mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts)
384 syslog(LOG_ERR, "uart: stop: context is NULL");
385 return MRAA_ERROR_INVALID_HANDLE;
388 // hardware flow control
393 if (tcflow(dev->fd, action)) {
394 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
398 struct termios termio;
401 if (tcgetattr(dev->fd, &termio)) {
402 syslog(LOG_ERR, "uart: tcgetattr() failed");
403 return MRAA_ERROR_INVALID_HANDLE;
407 termio.c_cflag |= CRTSCTS;
409 termio.c_cflag &= ~CRTSCTS;
412 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
413 syslog(LOG_ERR, "uart: tcsetattr() failed");
414 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
421 mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar)
424 syslog(LOG_ERR, "uart: stop: context is NULL");
425 return MRAA_ERROR_INVALID_HANDLE;
428 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
432 mraa_uart_get_dev_path(mraa_uart_context dev)
435 syslog(LOG_ERR, "uart: get_device_path failed, context is NULL");
438 if (dev->path == NULL) {
439 syslog(LOG_ERR, "uart: device path undefined");
447 mraa_uart_read(mraa_uart_context dev, char* buf, size_t len)
450 syslog(LOG_ERR, "uart: read: context is NULL");
451 return MRAA_ERROR_INVALID_HANDLE;
455 syslog(LOG_ERR, "uart: port is not open");
456 return MRAA_ERROR_INVALID_RESOURCE;
459 return read(dev->fd, buf, len);
463 mraa_uart_write(mraa_uart_context dev, const char* buf, size_t len)
466 syslog(LOG_ERR, "uart: write: context is NULL");
467 return MRAA_ERROR_INVALID_HANDLE;
471 syslog(LOG_ERR, "uart: port is not open");
472 return MRAA_ERROR_INVALID_RESOURCE;
475 return write(dev->fd, buf, len);
479 mraa_uart_data_available(mraa_uart_context dev, unsigned int millis)
482 syslog(LOG_ERR, "uart: data_available: write context is NULL");
487 syslog(LOG_ERR, "uart: port is not open");
491 struct timeval timeout;
498 timeout.tv_sec = millis / 1000;
499 timeout.tv_usec = (millis % 1000) * 1000;
505 FD_SET(dev->fd, &readfds);
507 if (select(dev->fd + 1, &readfds, NULL, NULL, &timeout) > 0) {
508 return 1; // data is ready