From 6d269eeabd97e6649ba0b51ac7b19c86e1ad04b3 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Wed, 29 Apr 2015 16:03:53 -0600 Subject: [PATCH] uart: add uart device open and close functionality Signed-off-by: Jon Trulson Signed-off-by: Brendan Le Foll --- api/mraa/uart.h | 30 ++++++++++++++++++++++ api/mraa/uart.hpp | 29 ++++++++++++++++++++++ src/uart/uart.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) diff --git a/api/mraa/uart.h b/api/mraa/uart.h index 351a30b..79aa732 100644 --- a/api/mraa/uart.h +++ b/api/mraa/uart.h @@ -1,5 +1,6 @@ /* * Author: Thomas Ingleby + * Contributions: Jon Trulson * Copyright (c) 2014 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -41,6 +42,15 @@ extern "C" { #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "common.h" @@ -63,6 +73,26 @@ mraa_uart_context mraa_uart_init(int uart); */ char* mraa_uart_get_dev_path(mraa_uart_context dev); +/** + * Open the TTY device associated with a UART context, and set up the + * terminal modes and baud rate. The TTY is setup for a 'raw' + * mode. 81N, no echo or special character handling, such as flow + * control or line editing semantics. + * + * @param dev uart context + * @param baud desired baud rate + * @return mraa_result_t + */ +mraa_result_t mraa_uart_open_dev(mraa_uart_context dev, speed_t baud); + +/** + * Close a device previously opened with mraa_uart_open_dev(). + * + * @param dev uart context + * @return mraa_result_t + */ +mraa_result_t mraa_uart_close_dev(mraa_uart_context dev); + #ifdef __cplusplus } #endif diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index b2e1bf7..e976c87 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -1,5 +1,6 @@ /* * Author: Brendan Le Foll + * Contributions: Jon Trulson * Copyright (c) 2014 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -57,6 +58,7 @@ class Uart */ ~Uart() { + closeDevice(); return; } @@ -73,6 +75,33 @@ class Uart return ret_val; } + /** + * Open the TTY device associated with a UART context, and set up the + * terminal modes and baud rate. The TTY is setup for a 'raw' + * mode. 81N, no echo or special character handling, such as flow + * control or line editing semantics. + * + * @param baud desired baud rate + * @return mraa_result_t + */ + mraa_result_t + openDevice(speed_t baud) + { + return mraa_uart_open_dev(m_uart, baud); + } + + /** + * Close a device previously opened with mraa_uart_open_dev(). + * + * @param dev uart context + * @return mraa_result_t + */ + mraa_result_t + closeDevice() + { + return mraa_uart_close_dev(m_uart); + } + private: mraa_uart_context m_uart; }; diff --git a/src/uart/uart.c b/src/uart/uart.c index aba7b66..d933f0a 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -1,5 +1,6 @@ /* * Author: Thomas Ingleby + * Contributions: Jon Trulson * Copyright (c) 2014 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -109,3 +110,65 @@ mraa_uart_get_dev_path(mraa_uart_context dev) } return dev->path; } + +mraa_result_t +mraa_uart_open_dev(mraa_uart_context dev, speed_t baud) +{ + if (!dev) { + syslog(LOG_ERR, "uart: open: context is NULL"); + return MRAA_ERROR_INVALID_HANDLE; + } + + char *devPath = mraa_uart_get_dev_path(dev); + + if (!devPath) { + syslog(LOG_ERR, "uart: device path undefined, open failed"); + return MRAA_ERROR_UNSPECIFIED; + } + + // now open the device + if ( (dev->fd = open(devPath, O_RDWR)) == -1) { + syslog(LOG_ERR, "uart: open() failed"); + return MRAA_ERROR_UNSPECIFIED; + } + + // now setup the tty and the selected baud rate + + struct termios termio; + + // get current modes + tcgetattr(dev->fd, &termio); + + // setup for a 'raw' mode. 81N, no echo or special character + // handling, such as flow control or line editing semantics. + cfmakeraw(&termio); + + // set our baud rates + cfsetispeed(&termio, baud); + cfsetospeed(&termio, baud); + + // make it so + if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) { + syslog(LOG_ERR, "uart: tcsetattr() failed"); + return MRAA_ERROR_UNSPECIFIED; + } + + return MRAA_SUCCESS; +} + +mraa_result_t +mraa_uart_close_dev(mraa_uart_context dev) +{ + if (!dev) { + syslog(LOG_ERR, "uart: close: context is NULL"); + return MRAA_ERROR_INVALID_HANDLE; + } + + // just close the device and reset our fd. + if (dev->fd >= 0) { + close(dev->fd); + } + + dev->fd = -1; + return MRAA_SUCCESS; +}