This example allows you to connect your debug terminal to some other serial-connected device on the system. You can then type AT commands at the terminal and get responses back for the serially connected device. Should work with something like an ESP8266 or similar connected in to the Arduino header.

Files at this revision

API Documentation at this revision

Comitter:
AnnaBridge
Date:
Fri Nov 10 16:08:43 2017 +0000
Commit message:
UARTSerial example 3

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Fri Nov 10 16:08:43 2017 +0000
@@ -0,0 +1,4 @@
+.build
+.mbed
+projectfiles
+*.py*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 10 16:08:43 2017 +0000
@@ -0,0 +1,50 @@
+#include "mbed.h"
+
+UARTSerial pc(USBTX, USBRX);
+UARTSerial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX);
+
+static void copy_some(FileHandle *out, FileHandle *in) {
+    // To ensure performance, allow to read multiple bytes at once, although
+    // we don't expect to read many in practice.
+
+    // read() will return immediately, as we've already
+    // checked that `in` is ready with poll()
+    char buffer[32];
+    ssize_t read = in->read(buffer, sizeof buffer);
+    if (read <= 0) {
+        error("Input error");
+    }
+
+    // Then write them all out. Assuming output port is similar speed to input,
+    // this may block briefly, but not significantly.
+    ssize_t written = 0;
+    while (written < read) {
+        ssize_t w = out->write(buffer + written, read - written);
+        if (w <= 0) {
+            error("Output error");
+        }
+        written += w;
+    }
+}
+
+int main() {
+    char buffer[32];
+    pollfh fds[2];
+
+    fds[0].fh = &pc;
+    fds[0].events = POLLIN;
+    fds[1].fh = &device;
+    fds[1].events = POLLIN;
+
+    while (1) {
+        // Block until either of the 2 ports is readable (or has an error)
+        poll(fds, 2, -1);
+
+        if (fds[0].revents) {
+            copy_some(fds[1].fh, fds[0].fh);
+        }
+        if (fds[1].revents) {
+            copy_some(fds[0].fh, fds[1].fh);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Nov 10 16:08:43 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e62a1b9236b44e70ae3b0902dc538481c04d455b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Fri Nov 10 16:08:43 2017 +0000
@@ -0,0 +1,18 @@
+{
+    "config": {
+        "uart1_tx": {
+            "help": "UART1 TX pin",
+            "value": "NC"
+        },
+        "uart1_rx": {
+            "help": "UART1 RX pin",
+            "value": "NC"
+        }
+    },
+    "target_overrides": {
+        "K64F": {
+            "uart1_tx": "D1",
+            "uart1_rx": "D0"
+        }
+    }
+}