blob: 03cd2484deed65037229d691c0c76690167f203f [file] [log] [blame]
Brian Silvermanf6b68842013-12-20 12:34:58 -08001#include "bbb/uart_reader.h"
Brian Silverman1662a0e2013-12-19 17:50:01 -08002
Brian Silverman0b6d9f42013-12-23 22:03:57 -08003#define OLD_CUSTOM_SPEED 0
4
Daniel Pettia4fe7bc2013-12-22 12:57:50 -08005#include <errno.h>
Daniel Pettib36f5b82013-12-15 08:55:04 -08006#include <fcntl.h>
7#include <linux/serial.h>
Daniel Pettib36f5b82013-12-15 08:55:04 -08008#include <unistd.h>
Daniel Petti059be422013-12-14 19:47:42 -08009
Brian Silverman1662a0e2013-12-19 17:50:01 -080010#include "aos/common/logging/logging.h"
Brian Silverman53f29182013-12-21 15:16:27 -080011
Daniel Petti059be422013-12-14 19:47:42 -080012// This is the code for receiving data from the cape via UART.
Brian Silverman1662a0e2013-12-19 17:50:01 -080013// NOTE: In order for this to work, you MUST HAVE
14// "capemgr.enable_partno=BB_UART1"
Daniel Petti059be422013-12-14 19:47:42 -080015// in your BBB's /media/BEAGLEBONE/uEnv.txt file!
Brian Silverman0b6d9f42013-12-23 22:03:57 -080016// `su -c "echo BB-UART1 > /sys/devices/bone_capemgr.*/slots"` works too, but
17// you have to do it every time.
Daniel Pettib36f5b82013-12-15 08:55:04 -080018
Daniel Petti059be422013-12-14 19:47:42 -080019namespace bbb {
Brian Silverman1662a0e2013-12-19 17:50:01 -080020namespace {
21// TODO(brians): Determine this in some way that allows easy switching for
22// testing with /dev/ttyUSB0 for example.
Brian Silverman0b6d9f42013-12-23 22:03:57 -080023const char *device = "/dev/ttyO1";
Brian Silverman1662a0e2013-12-19 17:50:01 -080024} // namespace
Daniel Petti059be422013-12-14 19:47:42 -080025
Brian Silverman0b6d9f42013-12-23 22:03:57 -080026extern "C" {
27
28// Sets all of the weird, annoying TTY flags on fd. In a separate file because
29// the structure it uses is so weird and annoying and carries so much baggage it
30// messes up all the #includes in whatever file it's used in.
Brian Silvermanc82c5122013-12-25 15:53:44 -080031// Defined in uart_reader_termios2.c.
Brian Silverman0b6d9f42013-12-23 22:03:57 -080032extern int aos_uart_reader_set_tty_options(int fd, int baud_rate);
33
34} // extern "C"
35
Brian Silvermanf6b68842013-12-20 12:34:58 -080036UartReader::UartReader(int32_t baud_rate)
Daniel Pettia4fe7bc2013-12-22 12:57:50 -080037 : fd_(open(device, O_RDWR | O_NOCTTY)) {
38
Brian Silverman1662a0e2013-12-19 17:50:01 -080039 if (fd_ < 0) {
40 LOG(FATAL, "open(%s, O_RDWR | O_NOCTTY) failed with %d: %s."
Brian Silvermanf6b68842013-12-20 12:34:58 -080041 " Did you read my note in bbb/uart_reader.cc?\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080042 device, errno, strerror(errno));
Daniel Petti059be422013-12-14 19:47:42 -080043 }
Brian Silvermaned030062013-12-20 21:03:47 -080044
Brian Silverman0b6d9f42013-12-23 22:03:57 -080045 if (aos_uart_reader_set_tty_options(fd_, baud_rate) != 0) {
46 LOG(FATAL, "aos_uart_reader_set_tty_options(%d) failed with %d: %s\n",
Brian Silverman1662a0e2013-12-19 17:50:01 -080047 fd_, errno, strerror(errno));
48 }
Daniel Petti059be422013-12-14 19:47:42 -080049}
50
Brian Silvermanf6b68842013-12-20 12:34:58 -080051UartReader::~UartReader() {
Brian Silverman1662a0e2013-12-19 17:50:01 -080052 if (fd_ > 0) close(fd_);
53}
Daniel Petti059be422013-12-14 19:47:42 -080054
Brian Silverman803b7a52014-01-01 13:21:18 -080055ssize_t UartReader::ReadBytes(AlignedChar *dest, size_t max_bytes) {
56 do {
57 ssize_t r = read(fd_, dest, max_bytes);
58 if (r != -1) return r;
59 } while (errno == EINTR);
60 return -1;
Brian Silverman1662a0e2013-12-19 17:50:01 -080061}
Daniel Petti059be422013-12-14 19:47:42 -080062
Brian Silvermanffeef3f2013-12-22 14:06:23 -080063} // namespace bbb