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 int pos = plat->uart_dev[index].rx;
157 if (plat->pins[pos].uart.mux_total > 0) {
158 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
159 syslog(LOG_ERR, "uart: failed to setup muxes for RX pin");
165 pos = plat->uart_dev[index].tx;
167 if (plat->pins[pos].uart.mux_total > 0) {
168 if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
169 syslog(LOG_ERR, "uart: failed to setup muxes for TX pin");
175 mraa_uart_context dev = mraa_uart_init_raw((char*)plat->uart_dev[index].device_path);
179 dev->index = index; //Set the board Index.
181 if (IS_FUNC_DEFINED(dev, uart_init_post)) {
182 mraa_result_t ret = dev->advance_func->uart_init_post(dev);
183 if (ret != MRAA_SUCCESS) {
193 mraa_uart_init_raw(const char* path)
195 mraa_uart_context dev = mraa_uart_init_internal(plat == NULL ? NULL : plat->adv_func);
197 syslog(LOG_ERR, "uart: Failed to allocate memory for context");
203 syslog(LOG_ERR, "uart: device path undefined, open failed");
208 // now open the device
209 if ((dev->fd = open(dev->path, O_RDWR)) == -1) {
210 syslog(LOG_ERR, "uart: open() failed");
215 // now setup the tty and the selected baud rate
216 struct termios termio;
219 if (tcgetattr(dev->fd, &termio)) {
220 syslog(LOG_ERR, "uart: tcgetattr() failed");
226 // setup for a 'raw' mode. 8N1, no echo or special character
227 // handling, such as flow control or line editing semantics.
228 // cfmakeraw is not POSIX!
230 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
231 syslog(LOG_ERR, "uart: tcsetattr() failed after cfmakeraw()");
237 if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) {
247 mraa_uart_stop(mraa_uart_context dev)
250 syslog(LOG_ERR, "uart: stop: context is NULL");
251 return MRAA_ERROR_INVALID_HANDLE;
254 // just close the device and reset our fd.
265 mraa_uart_flush(mraa_uart_context dev)
268 syslog(LOG_ERR, "uart: stop: context is NULL");
269 return MRAA_ERROR_INVALID_HANDLE;
272 if (tcdrain(dev->fd) == -1) {
273 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
280 mraa_uart_set_baudrate(mraa_uart_context dev, unsigned int baud)
283 syslog(LOG_ERR, "uart: stop: context is NULL");
284 return MRAA_ERROR_INVALID_HANDLE;
287 struct termios termio;
288 if (tcgetattr(dev->fd, &termio)) {
289 syslog(LOG_ERR, "uart: tcgetattr() failed");
290 return MRAA_ERROR_INVALID_HANDLE;
293 // set our baud rates
294 speed_t speed = uint2speed(baud);
295 cfsetispeed(&termio, speed);
296 cfsetospeed(&termio, speed);
299 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
300 syslog(LOG_ERR, "uart: tcsetattr() failed");
301 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
307 mraa_uart_set_mode(mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits)
310 syslog(LOG_ERR, "uart: stop: context is NULL");
311 return MRAA_ERROR_INVALID_HANDLE;
314 struct termios termio;
315 if (tcgetattr(dev->fd, &termio)) {
316 syslog(LOG_ERR, "uart: tcgetattr() failed");
317 return MRAA_ERROR_INVALID_HANDLE;
320 termio.c_cflag &= ~CSIZE;
323 termio.c_cflag |= CS8;
326 termio.c_cflag |= CS7;
329 termio.c_cflag |= CS6;
332 termio.c_cflag |= CS5;
335 termio.c_cflag |= CS8;
339 // POSIX & linux doesn't support 1.5 and I've got bigger fish to fry
342 termio.c_cflag &= ~CSTOPB;
345 termio.c_cflag |= CSTOPB;
351 case MRAA_UART_PARITY_NONE:
352 termio.c_cflag &= ~(PARENB | PARODD);
354 case MRAA_UART_PARITY_EVEN:
355 termio.c_cflag |= PARENB;
356 termio.c_cflag &= ~PARODD;
358 case MRAA_UART_PARITY_ODD:
359 termio.c_cflag |= PARENB | PARODD;
361 case MRAA_UART_PARITY_MARK: // not POSIX
362 termio.c_cflag |= PARENB | CMSPAR | PARODD;
364 case MRAA_UART_PARITY_SPACE: // not POSIX
365 termio.c_cflag |= PARENB | CMSPAR;
366 termio.c_cflag &= ~PARODD;
370 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
371 syslog(LOG_ERR, "uart: tcsetattr() failed");
372 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
379 mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts)
382 syslog(LOG_ERR, "uart: stop: context is NULL");
383 return MRAA_ERROR_INVALID_HANDLE;
386 // hardware flow control
391 if (tcflow(dev->fd, action)) {
392 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
396 struct termios termio;
399 if (tcgetattr(dev->fd, &termio)) {
400 syslog(LOG_ERR, "uart: tcgetattr() failed");
401 return MRAA_ERROR_INVALID_HANDLE;
405 termio.c_cflag |= CRTSCTS;
407 termio.c_cflag &= ~CRTSCTS;
410 if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
411 syslog(LOG_ERR, "uart: tcsetattr() failed");
412 return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
419 mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar)
422 syslog(LOG_ERR, "uart: stop: context is NULL");
423 return MRAA_ERROR_INVALID_HANDLE;
426 return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
430 mraa_uart_get_dev_path(mraa_uart_context dev)
433 syslog(LOG_ERR, "uart: get_device_path failed, context is NULL");
436 if (dev->path == NULL) {
437 syslog(LOG_ERR, "uart: device path undefined");
445 mraa_uart_read(mraa_uart_context dev, char* buf, size_t len)
448 syslog(LOG_ERR, "uart: read: context is NULL");
449 return MRAA_ERROR_INVALID_HANDLE;
453 syslog(LOG_ERR, "uart: port is not open");
454 return MRAA_ERROR_INVALID_RESOURCE;
457 return read(dev->fd, buf, len);
461 mraa_uart_write(mraa_uart_context dev, const char* buf, size_t len)
464 syslog(LOG_ERR, "uart: write: context is NULL");
465 return MRAA_ERROR_INVALID_HANDLE;
469 syslog(LOG_ERR, "uart: port is not open");
470 return MRAA_ERROR_INVALID_RESOURCE;
473 return write(dev->fd, buf, len);
477 mraa_uart_data_available(mraa_uart_context dev, unsigned int millis)
480 syslog(LOG_ERR, "uart: data_available: write context is NULL");
485 syslog(LOG_ERR, "uart: port is not open");
489 struct timeval timeout;
496 timeout.tv_sec = millis / 1000;
497 timeout.tv_usec = (millis % 1000) * 1000;
503 FD_SET(dev->fd, &readfds);
505 if (select(dev->fd + 1, &readfds, NULL, NULL, &timeout) > 0) {
506 return 1; // data is ready