taught the flasher how to export the UART
diff --git a/bbb_cape/src/bbb/bbb.gyp b/bbb_cape/src/bbb/bbb.gyp
index 1da4732..5f5b590 100644
--- a/bbb_cape/src/bbb/bbb.gyp
+++ b/bbb_cape/src/bbb/bbb.gyp
@@ -46,6 +46,17 @@
],
},
{
+ 'target_name': 'export_uart',
+ 'type': 'static_library',
+ 'sources': [
+ 'export_uart.cc',
+ ],
+ 'dependencies': [
+ '<(AOS)/build/aos.gyp:logging',
+ '<(AOS)/common/common.gyp:time',
+ ],
+ },
+ {
'target_name': 'uart_reader',
'type': 'static_library',
'sources': [
@@ -56,6 +67,7 @@
'<(AOS)/build/aos.gyp:logging',
'<(AOS)/common/common.gyp:time',
'byte_io',
+ 'export_uart',
],
'export_dependent_settings': [
'<(AOS)/common/common.gyp:time',
diff --git a/bbb_cape/src/bbb/export_uart.cc b/bbb_cape/src/bbb/export_uart.cc
new file mode 100644
index 0000000..ccf751c
--- /dev/null
+++ b/bbb_cape/src/bbb/export_uart.cc
@@ -0,0 +1,59 @@
+#include "bbb/export_uart.h"
+
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+#include "aos/common/logging/logging.h"
+#include "aos/common/time.h"
+
+namespace bbb {
+namespace {
+
+// TODO(brians): Determine this in some way that allows easy switching for
+// testing with /dev/ttyUSB0 for example.
+const char *device = "/dev/ttyO1";
+
+bool easy_access(const char *path) {
+ if (access(path, R_OK | W_OK) == 0) return true;
+ if (errno == EACCES || errno == ENOENT) return false;
+ LOG(FATAL, "access(%s, F_OK) failed with %d: %s\n", path, errno,
+ strerror(errno));
+}
+
+} // namespace
+
+const char *UartDevice() { return device; }
+
+void ExportUart() {
+ if (easy_access(device)) {
+ LOG(INFO, "unexporting BB-UART1\n");
+ if (system("bash -c 'echo -$(cat /sys/devices/bone_capemgr.*/slots"
+ " | fgrep BB-UART1"
+ " | cut -d : -f 1 | tr -d \" \")"
+ " > /sys/devices/bone_capemgr.*/slots'") == -1) {
+ LOG(FATAL, "system([disable OMAP UART]) failed with %d: %s\n", errno,
+ strerror(errno));
+ }
+ while (easy_access(device)) {
+ LOG(DEBUG, "waiting for BB-UART1 to be unexported\n");
+ ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
+ }
+ }
+
+ LOG(INFO, "exporting BB-UART1\n");
+ // 2 strings to work around a VIM bug where the indenter locks up when they're
+ // combined as 1...
+ if (system("bash -c 'echo BB-UART1 > /sys/devices/bone_capemgr.*"
+ "/slots'") ==
+ -1) {
+ LOG(FATAL, "system([enable OMAP UART]) failed with %d: %s\n", errno,
+ strerror(errno));
+ }
+ while (!easy_access(device)) {
+ LOG(DEBUG, "waiting for BB-UART1 to be exported\n");
+ ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
+ }
+}
+
+} // namespace bbb
diff --git a/bbb_cape/src/bbb/export_uart.h b/bbb_cape/src/bbb/export_uart.h
new file mode 100644
index 0000000..4f91a57
--- /dev/null
+++ b/bbb_cape/src/bbb/export_uart.h
@@ -0,0 +1,15 @@
+#ifndef BBB_EXPORT_UART_H_
+#define BBB_EXPORT_UART_H_
+
+namespace bbb {
+
+// Returns a pointer to static memory with the name of the device file for the
+// UART.
+const char *UartDevice();
+
+// Unexports (if already exported) and then reexports the UART cape slot.
+void ExportUart();
+
+} // namespace bbb
+
+#endif // BBB_EXPORT_UART_H_
diff --git a/bbb_cape/src/bbb/uart_reader.cc b/bbb_cape/src/bbb/uart_reader.cc
index 9abb284..7f88d51 100644
--- a/bbb_cape/src/bbb/uart_reader.cc
+++ b/bbb_cape/src/bbb/uart_reader.cc
@@ -7,6 +7,8 @@
#include "aos/common/logging/logging.h"
+#include "bbb/export_uart.h"
+
// This is the code for receiving data from the cape via UART.
// fragment active.
// `su -c "echo BB-UART1 > /sys/devices/bone_capemgr.*/slots"` works, but
@@ -16,48 +18,10 @@
namespace bbb {
namespace {
-// TODO(brians): Determine this in some way that allows easy switching for
-// testing with /dev/ttyUSB0 for example.
-const char *device = "/dev/ttyO1";
-
-bool easy_access(const char *path) {
- if (access(path, R_OK | W_OK) == 0) return true;
- if (errno == EACCES || errno == ENOENT) return false;
- LOG(FATAL, "access(%s, F_OK) failed with %d: %s\n", path, errno,
- strerror(errno));
-}
-
int open_device() {
- if (easy_access(device)) {
- LOG(INFO, "unexporting BB-UART1\n");
- if (system("bash -c 'echo -$(cat /sys/devices/bone_capemgr.*/slots"
- " | fgrep BB-UART1"
- " | cut -d : -f 1 | tr -d \" \")"
- " > /sys/devices/bone_capemgr.*/slots'") == -1) {
- LOG(FATAL, "system([disable OMAP UART]) failed with %d: %s\n", errno,
- strerror(errno));
- }
- while (easy_access(device)) {
- LOG(DEBUG, "waiting for BB-UART1 to be unexported\n");
- ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
- }
- }
+ ExportUart();
- LOG(INFO, "exporting BB-UART1\n");
- // 2 strings to work around a VIM bug where the indenter locks up when they're
- // combined as 1...
- if (system("bash -c 'echo BB-UART1 > /sys/devices/bone_capemgr.*"
- "/slots'") ==
- -1) {
- LOG(FATAL, "system([enable OMAP UART]) failed with %d: %s\n", errno,
- strerror(errno));
- }
- while (!easy_access(device)) {
- LOG(DEBUG, "waiting for BB-UART1 to be exported\n");
- ::aos::time::SleepFor(::aos::time::Time::InSeconds(0.1));
- }
-
- return open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
+ return open(UartDevice(), O_RDWR | O_NOCTTY | O_NONBLOCK);
}
} // namespace
@@ -77,7 +41,7 @@
if (fd_ < 0) {
LOG(FATAL, "open(%s, O_RDWR | O_NOCTTY | O_NOBLOCK) failed with %d: %s\n",
- device, errno, strerror(errno));
+ UartDevice(), errno, strerror(errno));
}
if (aos_uart_reader_set_tty_options(fd_, baud_rate) != 0) {
diff --git a/bbb_cape/src/flasher/flasher.gyp b/bbb_cape/src/flasher/flasher.gyp
index 2d506b8..fba1477 100644
--- a/bbb_cape/src/flasher/flasher.gyp
+++ b/bbb_cape/src/flasher/flasher.gyp
@@ -17,6 +17,7 @@
'<(EXTERNALS):stm32flash',
'<(AOS)/build/aos.gyp:logging',
'<(DEPTH)/bbb_cape/src/bbb/bbb.gyp:gpios',
+ '<(DEPTH)/bbb_cape/src/bbb/bbb.gyp:export_uart',
'<(AOS)/common/common.gyp:time',
],
},
diff --git a/bbb_cape/src/flasher/stm32_flasher.cc b/bbb_cape/src/flasher/stm32_flasher.cc
index 48887d2..a48abc4 100644
--- a/bbb_cape/src/flasher/stm32_flasher.cc
+++ b/bbb_cape/src/flasher/stm32_flasher.cc
@@ -19,6 +19,7 @@
}
#include "bbb/gpo.h"
+#include "bbb/export_uart.h"
namespace {
@@ -48,9 +49,6 @@
}
::std::string target = argv[1];
- //::std::string device = "/dev/ttyUSB0";
- // TODO(brians): Figure out an intelligent way to set the device to use.
- ::std::string device = "/dev/ttyO1";
serial_baud_t baud_rate = SERIAL_BAUD_57600;
::std::string filename =
@@ -112,17 +110,21 @@
LOG(FATAL, "opening file %s failed\n", filename.c_str());
}
- serial_t *serial = serial_open(device.c_str());
+ ::bbb::ExportUart();
+
+ const char *device = ::bbb::UartDevice();
+
+ serial_t *serial = serial_open(device);
if (serial == NULL) {
LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
- device.c_str(), errno, strerror(errno));
+ device, errno, strerror(errno));
}
if (serial_setup(serial, baud_rate,
SERIAL_BITS_8,
SERIAL_PARITY_EVEN,
SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
- device.c_str(), errno, strerror(errno));
+ device, errno, strerror(errno));
}
LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));