From 88f3052d99f3a599adeff4430f752fde4320ff64 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 30 Apr 2015 14:12:45 -0600 Subject: [PATCH] uart: use an unsigned int for baud rate rather than speed_t uart.c contains a static conversion function that will translate known baud rates into their speed_t counter parts. If an unsupported baud rate is selected, a diagnostic will be emitted via syslog and a default of B9600 will be chosen. Signed-off-by: Jon Trulson Signed-off-by: Brendan Le Foll --- api/mraa/uart.h | 2 +- api/mraa/uart.hpp | 2 +- src/uart/uart.c | 50 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/api/mraa/uart.h b/api/mraa/uart.h index 468778a..298329c 100644 --- a/api/mraa/uart.h +++ b/api/mraa/uart.h @@ -83,7 +83,7 @@ char* mraa_uart_get_dev_path(mraa_uart_context dev); * @param baud desired baud rate * @return mraa_result_t */ -mraa_result_t mraa_uart_open_dev(mraa_uart_context dev, speed_t baud); +mraa_result_t mraa_uart_open_dev(mraa_uart_context dev, unsigned int baud); /** * Close a device previously opened with mraa_uart_open_dev(). diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index 82296b8..5642190 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -85,7 +85,7 @@ class Uart * @return mraa_result_t */ mraa_result_t - openDevice(speed_t baud) + openDevice(unsigned int baud) { return mraa_uart_open_dev(m_uart, baud); } diff --git a/src/uart/uart.c b/src/uart/uart.c index b8d8ec5..bc04ef8 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -31,6 +31,49 @@ #include "uart.h" #include "mraa_internal.h" +// This function takes an unsigned int and converts it to a B* speed_t +// that can be used with linux termios. +static speed_t uint2speed(unsigned int speed) +{ + switch (speed) { + case 0: return B0; // hangup, not too useful otherwise + case 50: return B50; + case 75: return B75; + case 110: return B110; + case 150: return B150; + case 200: return B200; + case 300: return B300; + case 600: return B600; + case 1200: return B1200; + case 1800: return B1800; + case 2400: return B2400; + case 4800: return B4800; + case 9600: return B9600; + case 19200: return B19200; + case 38400: return B38400; + case 57600: return B57600; + case 115200: return B115200; + case 230400: return B230400; + case 460800: return B460800; + case 500000: return B500000; + case 576000: return B576000; + case 921600: return B921600; + case 1000000: return B1000000; + case 1152000: return B1152000; + case 1500000: return B1500000; + case 2000000: return B2000000; + case 2500000: return B2500000; + case 3000000: return B3000000; + case 3500000: return B3500000; + case 4000000: return B4000000; + } + + // if we are here, then an unsupported baudrate was selected. + // Report it via syslog and return B9600, a common default. + syslog(LOG_ERR, "uart: unsupported baud rate, defaulting to 9600."); + return B9600; +} + mraa_uart_context mraa_uart_init(int index) { @@ -112,7 +155,7 @@ mraa_uart_get_dev_path(mraa_uart_context dev) } mraa_result_t -mraa_uart_open_dev(mraa_uart_context dev, speed_t baud) +mraa_uart_open_dev(mraa_uart_context dev, unsigned int baud) { if (!dev) { syslog(LOG_ERR, "uart: open: context is NULL"); @@ -144,8 +187,9 @@ mraa_uart_open_dev(mraa_uart_context dev, speed_t baud) cfmakeraw(&termio); // set our baud rates - cfsetispeed(&termio, baud); - cfsetospeed(&termio, baud); + speed_t speed = uint2speed(baud); + cfsetispeed(&termio, speed); + cfsetospeed(&termio, speed); // make it so if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {