Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 1 | #include "bbb/uart_reader.h" |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 2 | |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 3 | #define OLD_CUSTOM_SPEED 0 |
| 4 | |
Daniel Petti | a4fe7bc | 2013-12-22 12:57:50 -0800 | [diff] [blame] | 5 | #include <errno.h> |
Daniel Petti | b36f5b8 | 2013-12-15 08:55:04 -0800 | [diff] [blame] | 6 | #include <fcntl.h> |
| 7 | #include <linux/serial.h> |
Daniel Petti | b36f5b8 | 2013-12-15 08:55:04 -0800 | [diff] [blame] | 8 | #include <unistd.h> |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 9 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 10 | #include "aos/common/logging/logging.h" |
Brian Silverman | 53f2918 | 2013-12-21 15:16:27 -0800 | [diff] [blame] | 11 | |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 12 | // This is the code for receiving data from the cape via UART. |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 13 | // NOTE: In order for this to work, you MUST HAVE |
| 14 | // "capemgr.enable_partno=BB_UART1" |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 15 | // in your BBB's /media/BEAGLEBONE/uEnv.txt file! |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 16 | // `su -c "echo BB-UART1 > /sys/devices/bone_capemgr.*/slots"` works too, but |
| 17 | // you have to do it every time. |
Daniel Petti | b36f5b8 | 2013-12-15 08:55:04 -0800 | [diff] [blame] | 18 | |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 19 | namespace bbb { |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 20 | namespace { |
| 21 | // TODO(brians): Determine this in some way that allows easy switching for |
| 22 | // testing with /dev/ttyUSB0 for example. |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 23 | const char *device = "/dev/ttyO1"; |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 24 | } // namespace |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 25 | |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 26 | extern "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. |
| 31 | extern int aos_uart_reader_set_tty_options(int fd, int baud_rate); |
| 32 | |
| 33 | } // extern "C" |
| 34 | |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 35 | UartReader::UartReader(int32_t baud_rate) |
Daniel Petti | a4fe7bc | 2013-12-22 12:57:50 -0800 | [diff] [blame] | 36 | : fd_(open(device, O_RDWR | O_NOCTTY)) { |
| 37 | |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 38 | if (fd_ < 0) { |
| 39 | LOG(FATAL, "open(%s, O_RDWR | O_NOCTTY) failed with %d: %s." |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 40 | " Did you read my note in bbb/uart_reader.cc?\n", |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 41 | device, errno, strerror(errno)); |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 42 | } |
Brian Silverman | ed03006 | 2013-12-20 21:03:47 -0800 | [diff] [blame] | 43 | |
Brian Silverman | 0b6d9f4 | 2013-12-23 22:03:57 -0800 | [diff] [blame] | 44 | if (aos_uart_reader_set_tty_options(fd_, baud_rate) != 0) { |
| 45 | LOG(FATAL, "aos_uart_reader_set_tty_options(%d) failed with %d: %s\n", |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 46 | fd_, errno, strerror(errno)); |
| 47 | } |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Brian Silverman | f6b6884 | 2013-12-20 12:34:58 -0800 | [diff] [blame] | 50 | UartReader::~UartReader() { |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 51 | if (fd_ > 0) close(fd_); |
| 52 | } |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 53 | |
Daniel Petti | a4fe7bc | 2013-12-22 12:57:50 -0800 | [diff] [blame] | 54 | int UartReader::ReadBytes(AlignedChar *dest, size_t max_bytes) { |
| 55 | return read(fd_, dest, max_bytes); |
Brian Silverman | 1662a0e | 2013-12-19 17:50:01 -0800 | [diff] [blame] | 56 | } |
Daniel Petti | 059be42 | 2013-12-14 19:47:42 -0800 | [diff] [blame] | 57 | |
Brian Silverman | ffeef3f | 2013-12-22 14:06:23 -0800 | [diff] [blame] | 58 | } // namespace bbb |