Private
Public Access
2
0

uart: add uart device open and close functionality

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Jon Trulson
2015-04-29 16:03:53 -06:00
committed by Thomas Ingleby
parent bdadbb8b01
commit 6d269eeabd
3 changed files with 122 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Contributions: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@@ -41,6 +42,15 @@ extern "C" {
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#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

View File

@@ -1,5 +1,6 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Contributions: Jon Trulson <jtrulson@ics.com>
* 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;
};

View File

@@ -1,5 +1,6 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Contributions: Jon Trulson <jtrulson@ics.com>
* 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;
}