Example of redirecting stdout to an USBSerial stream

Dependencies:   max32630fthr USBDevice

Committer:
psionprime
Date:
Tue Apr 30 21:41:00 2019 +0000
Revision:
1:ac8ff4b8c33c
Parent:
0:4435b6bafeba
- updated USBDevice lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
psionprime 0:4435b6bafeba 1 /*
psionprime 0:4435b6bafeba 2 This is free and unencumbered software released into the public domain.
psionprime 0:4435b6bafeba 3
psionprime 0:4435b6bafeba 4 Anyone is free to copy, modify, publish, use, compile, sell, or
psionprime 0:4435b6bafeba 5 distribute this software, either in source code form or as a compiled
psionprime 0:4435b6bafeba 6 binary, for any purpose, commercial or non-commercial, and by any
psionprime 0:4435b6bafeba 7 means.
psionprime 0:4435b6bafeba 8
psionprime 0:4435b6bafeba 9 In jurisdictions that recognize copyright laws, the author or authors
psionprime 0:4435b6bafeba 10 of this software dedicate any and all copyright interest in the
psionprime 0:4435b6bafeba 11 software to the public domain. We make this dedication for the benefit
psionprime 0:4435b6bafeba 12 of the public at large and to the detriment of our heirs and
psionprime 0:4435b6bafeba 13 successors. We intend this dedication to be an overt act of
psionprime 0:4435b6bafeba 14 relinquishment in perpetuity of all present and future rights to this
psionprime 0:4435b6bafeba 15 software under copyright law.
psionprime 0:4435b6bafeba 16
psionprime 0:4435b6bafeba 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
psionprime 0:4435b6bafeba 18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
psionprime 0:4435b6bafeba 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
psionprime 0:4435b6bafeba 20 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
psionprime 0:4435b6bafeba 21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
psionprime 0:4435b6bafeba 22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
psionprime 0:4435b6bafeba 23 OTHER DEALINGS IN THE SOFTWARE.
psionprime 0:4435b6bafeba 24
psionprime 0:4435b6bafeba 25 For more information, please refer to <http://unlicense.org/>
psionprime 0:4435b6bafeba 26
psionprime 0:4435b6bafeba 27 Notes
psionprime 0:4435b6bafeba 28 2019-04-30
psionprime 0:4435b6bafeba 29 - no response from maxim requesting them to update their USB targets to
psionprime 0:4435b6bafeba 30 mbed 5.12
psionprime 0:4435b6bafeba 31 - making this example for 5.11.5
psionprime 0:4435b6bafeba 32
psionprime 0:4435b6bafeba 33
psionprime 0:4435b6bafeba 34 Ever wonder how to redirect standard in/out/error to a USBSerial interface ?
psionprime 0:4435b6bafeba 35 Using a *MAX32630FTHR board, here you go :)
psionprime 0:4435b6bafeba 36
psionprime 0:4435b6bafeba 37 Connect your favorite terminal, eg., CoolTerm, run the program, scan for any
psionprime 0:4435b6bafeba 38 new usb serial ports, connect. A USB serial port is not quite a UART so baud
psionprime 0:4435b6bafeba 39 doesn' matter much.
psionprime 0:4435b6bafeba 40
psionprime 0:4435b6bafeba 41
psionprime 0:4435b6bafeba 42 * After many many combinations of IDE's/boards, I picked the Max because I can
psionprime 0:4435b6bafeba 43 easily connect my Segger J-Link for much faster traditional debugging than
psionprime 0:4435b6bafeba 44 DAPLink working with VisualGDB extension to M$ Visual Studio 2017 Community
psionprime 0:4435b6bafeba 45 Edition.
psionprime 0:4435b6bafeba 46
psionprime 0:4435b6bafeba 47 */
psionprime 0:4435b6bafeba 48
psionprime 0:4435b6bafeba 49 #include "mbed.h"
psionprime 0:4435b6bafeba 50 #include "max32630fthr.h"
psionprime 0:4435b6bafeba 51 #include "USBSerial.h"
psionprime 0:4435b6bafeba 52
psionprime 0:4435b6bafeba 53 #include <stdio.h>
psionprime 0:4435b6bafeba 54 #include <errno.h>
psionprime 0:4435b6bafeba 55
psionprime 0:4435b6bafeba 56 // mbed-os USB virtual serial port globals
psionprime 0:4435b6bafeba 57 // The "usb" names a stream. I will make a pull request adding a single line
psionprime 0:4435b6bafeba 58 // change to USBSerial.h to allow for this.
psionprime 0:4435b6bafeba 59 USBSerial usbSerial("usb");
psionprime 0:4435b6bafeba 60
psionprime 0:4435b6bafeba 61 // platform globals
psionprime 0:4435b6bafeba 62 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
psionprime 0:4435b6bafeba 63 DigitalOut rLED(LED1);
psionprime 0:4435b6bafeba 64 DigitalOut gLED(LED2);
psionprime 0:4435b6bafeba 65 DigitalOut bLED(LED3);
psionprime 0:4435b6bafeba 66
psionprime 0:4435b6bafeba 67
psionprime 0:4435b6bafeba 68 int main() {
psionprime 0:4435b6bafeba 69 // redirect stdin, stdout, stderr to USBSerial object
psionprime 0:4435b6bafeba 70 freopen("/usb", "r", stdin);
psionprime 0:4435b6bafeba 71 freopen("/usb", "w", stdout);
psionprime 0:4435b6bafeba 72 freopen("/usb", "w", stderr);
psionprime 0:4435b6bafeba 73
psionprime 0:4435b6bafeba 74 rLED = LED_OFF;
psionprime 0:4435b6bafeba 75 gLED = LED_OFF;
psionprime 0:4435b6bafeba 76 bLED = LED_OFF;
psionprime 0:4435b6bafeba 77
psionprime 0:4435b6bafeba 78 // wait until user ready
psionprime 0:4435b6bafeba 79 while(!usbSerial.terminal_connected) {
psionprime 0:4435b6bafeba 80 ThisThread::yield();
psionprime 0:4435b6bafeba 81 }
psionprime 0:4435b6bafeba 82
psionprime 0:4435b6bafeba 83 printf("--- Maxim MAC32630FTHR mbed-os 5.11.5 stdout redirect ---\n");
psionprime 0:4435b6bafeba 84
psionprime 0:4435b6bafeba 85 // blink led indicating finished
psionprime 0:4435b6bafeba 86 while (true) {
psionprime 0:4435b6bafeba 87 bLED = LED_ON;
psionprime 0:4435b6bafeba 88 wait_ms(25);
psionprime 0:4435b6bafeba 89 bLED = LED_OFF;
psionprime 0:4435b6bafeba 90 wait_ms(2975);
psionprime 0:4435b6bafeba 91 }
psionprime 0:4435b6bafeba 92 }
psionprime 0:4435b6bafeba 93