Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:0f1c0f6579ab, committed 2017-11-10
- Comitter:
- AnnaBridge
- Date:
- Fri Nov 10 16:08:43 2017 +0000
- Commit message:
- UARTSerial example 3
Changed in this revision
--- /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"
+ }
+ }
+}