diff --git a/api/mraa/uart.h b/api/mraa/uart.h index 8e7a069..afdeeaa 100644 --- a/api/mraa/uart.h +++ b/api/mraa/uart.h @@ -55,16 +55,67 @@ typedef struct _uart* mraa_uart_context; */ mraa_uart_context mraa_uart_init(int uart); -mraa_uart_context mraa_uart_init_raw(int uart); +/** + * Initialise a raw uart_context. No board setup. + * + * @param path for example "/dev/ttyS0" + * @return uart context or NULL + */ +mraa_uart_context mraa_uart_init_raw(char* path); -mraa_result_t mraa_uart_flush(mraa_uart_context); +/** + * Flush the outbound data. + * Blocks until complete. + * + * @param dev The UART context + * @return Result of operation + */ +mraa_result_t mraa_uart_flush(mraa_uart_context dev); +/** + * Set the baudrate. + * Takes an int and will attempt to decide what baudrate is + * to be used on the UART hardware. + * + * @param dev The UART context + * @param baud unsigned int of baudrate i.e. 9600 + * @return Result of operation + */ mraa_result_t mraa_uart_set_baudrate(mraa_uart_context dev, unsigned int baud); +/** + * Set the transfer mode + * For example setting the mode to 8N1 would be + * "mraa_uart_set_mode(dev, 8,MRAA_UART_PARITY_NONE , 1)" + * + * @param dev The UART context + * @param bytesize data bits + * @param parity Parity bit setting + * @param stopbits stop bits + * @return Result of operation + */ mraa_result_t mraa_uart_set_mode(mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits); +/** + * Set the flowcontrol + * + * @param dev The UART context + * @param xonxoff XON/XOFF Software flow control. + * @param rtscts RTS/CTS out of band hardware flow control + * @return Result of operation + */ mraa_result_t mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts); +/** + * Set the timeout for read and write operations + * <= 0 will disable that timeout + * + * @param dev The UART context + * @param read read timeout + * @param write write timeout + * @param interchar inbetween char timeout + * @return Result of operation + */ mraa_result_t mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar); /** @@ -89,20 +140,20 @@ mraa_result_t mraa_uart_stop(mraa_uart_context dev); * * @param dev uart context * @param buf buffer pointer - * @param len maximum size of buffer + * @param length maximum size of buffer * @return the number of bytes read, or -1 if an error occurred */ -int mraa_uart_read(mraa_uart_context dev, char* buf, size_t len); +int mraa_uart_read(mraa_uart_context dev, char* buf, size_t length); /** * Write bytes in buffer to a device * * @param dev uart context * @param buf buffer pointer - * @param len maximum size of buffer + * @param length maximum size of buffer * @return the number of bytes written, or -1 if an error occurred */ -int mraa_uart_write(mraa_uart_context dev, char* buf, size_t len); +int mraa_uart_write(mraa_uart_context dev, char* buf, size_t length); /** * Check to see if data is available on the device for reading diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index 482deac..675170a 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -1,6 +1,7 @@ /* * Author: Brendan Le Foll * Contributions: Jon Trulson + * Contributions: Thomas Ingleby * Copyright (c) 2014 - 2015 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -27,6 +28,7 @@ #include "uart.h" #include +#include namespace mraa { @@ -53,6 +55,24 @@ class Uart throw std::invalid_argument("Error initialising UART"); } } + + /** + * Uart Constructor, takes a string to the path of the serial + * interface that is needed. + * + * @param uart the index of the uart set to use + */ + Uart(std::string path) + { + char *p = new char[path.length() + 1]; + std::strcpy(p, path.c_str()); + m_uart = mraa_uart_init_raw(p); + + if (m_uart == NULL) { + throw std::invalid_argument("Error initialising UART"); + } + } + /** * Uart destructor */ diff --git a/src/uart/uart.c b/src/uart/uart.c index 71bcefa..c14eb44 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -152,7 +152,11 @@ mraa_uart_init(int index) } } - mraa_uart_context dev = mraa_uart_init_raw(index); + mraa_uart_context dev = mraa_uart_init_raw((char*)plat->uart_dev[index].device_path); + if (dev == NULL) { + return NULL; + } + dev->index = index; //Set the board Index. if (advance_func->uart_init_post != NULL) { mraa_result_t ret = advance_func->uart_init_post(dev); @@ -166,7 +170,7 @@ mraa_uart_init(int index) } mraa_uart_context -mraa_uart_init_raw(int index) +mraa_uart_init_raw(char* path) { mraa_uart_context dev = (mraa_uart_context) malloc(sizeof(struct _uart)); if (dev == NULL) { @@ -175,20 +179,18 @@ mraa_uart_init_raw(int index) } memset(dev, 0, sizeof(struct _uart)); - dev->index = index; + dev->index = -1; dev->fd = -1; - dev->path = (char*) plat->uart_dev[index].device_path; + dev->path = path; - char* devPath = mraa_uart_get_dev_path(dev); - - if (!devPath) { + if (!dev->path) { syslog(LOG_ERR, "uart: device path undefined, open failed"); free(dev); return NULL; } // now open the device - if ((dev->fd = open(devPath, O_RDWR)) == -1) { + if ((dev->fd = open(dev->path, O_RDWR)) == -1) { syslog(LOG_ERR, "uart: open() failed"); free(dev); return NULL;