Several examples run on only mbed-os5.13.0 (not 5.14.0)

Dependencies:   BD_SD_DISCO_F769NI BSP_DISCO_F769NI LCD_DISCO_F769NI TS_DISCO_F769NI USBHost_F769NI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers 6_USBHost_serial.cpp Source File

6_USBHost_serial.cpp

00001 // Original
00002 //      https://os.mbed.com/handbook/USBHostSerial
00003 //
00004 // Modified by K.Arai
00005 //      October   14th, 2019
00006 //
00007 
00008 #include "select_program.h"
00009 //#define EXAMPLE_6_USBHOST_SERIAL
00010 #ifdef EXAMPLE_6_USBHOST_SERIAL
00011 
00012 //  Include --------------------------------------------------------------------
00013 #include "mbed.h"
00014 #include "USBHostSerial.h"
00015 
00016 //  Definition -----------------------------------------------------------------
00017 
00018 //  Constructor ----------------------------------------------------------------
00019 DigitalOut led(LED1);
00020 Serial pc(USBTX, USBRX, 115200);
00021 
00022 //  RAM ------------------------------------------------------------------------
00023 
00024 //  ROM / Constant data --------------------------------------------------------
00025 
00026 //  Function prototypes --------------------------------------------------------
00027 static void serial_task(void const *args);
00028 
00029 //------------------------------------------------------------------------------
00030 //  Control Program
00031 //------------------------------------------------------------------------------
00032 osThreadDef(serial_task, osPriorityNormal, 4096);
00033 
00034 int main()
00035 {
00036     pc.printf("\x1b[2J\x1b[H %s\r\n %s %s (UTC)\r\n",
00037               __FILE__, __DATE__, __TIME__);
00038     printf(" USB HOST serial for DISCO-F769NI:\r\n");   
00039     osThreadCreate(osThread(serial_task), NULL);
00040     while(true) {
00041         led=!led;
00042         ThisThread::sleep_for(1000);
00043     }
00044 }
00045 
00046 void serial_task(void const*)
00047 {
00048     USBHostSerial usb_ser;
00049     wait(0.5f);
00050     while(true) {
00051         // try to connect a USB serial device
00052         while(!usb_ser.connect()) {
00053             ThisThread::sleep_for(500);
00054         }
00055         // in a loop, print all characters received
00056         // if the device is disconnected, we try to connect it again
00057         while (true) {
00058             // if device disconnected, try to connect it again
00059             if (!usb_ser.connected()) {
00060                 break;
00061             }
00062             // print characters received
00063             while (usb_ser.available()) {
00064                 pc.printf("%c", usb_ser.getc());
00065             }
00066         }
00067     }
00068 }
00069 
00070 #endif
00071 
00072 #if 0
00073 // USBDevice example
00074 #include "mbed.h"
00075 #include "USBSerial.h"
00076 //Virtual serial port over USB
00077 USBSerial serial;
00078 Serial pc(USBTX, USBRX, 115200);
00079 int main(void)
00080 {
00081     uint32_t n = 0;
00082     while(1) {
00083         serial.printf("I am a virtual serial port\r\n");
00084         pc.printf("I am also a virtual serial port. loop count = %d\r\n", n++);
00085     }
00086 }
00087 #endif