tfx2001 4 лет назад
Родитель
Сommit
4ba0e97dae
2 измененных файлов с 44 добавлено и 0 удалено
  1. 3 0
      rt-thread/SConscript
  2. 41 0
      rt-thread/example/cdc.c

+ 3 - 0
rt-thread/SConscript

@@ -28,6 +28,9 @@ if GetDepend(["PKG_TINYUSB_DEVICE_CDC"]):
 
 if GetDepend(["PKG_TINYUSB_DEVICE_MSC"]):
     src += ["../src/class/msc/msc_device.c", "port/msc_device.c"]
+
+if GetDepend(["PKG_TINYUSB_DEVICE_EXAMPLE_CDC"]):
+    src += ["example/cdc.c"]
   
 LOCAL_CCFLAGS = ''
 

+ 41 - 0
rt-thread/example/cdc.c

@@ -0,0 +1,41 @@
+#include <stdint.h>
+#include <rtthread.h>
+#include <tusb.h>
+
+static void cdc_example(void)
+{
+    uint8_t buffer[32];
+    char ch;
+
+    if (tud_cdc_connected())
+    {
+        rt_memset(buffer, 0, 32);
+
+        tud_cdc_write_str("please enter something: ");
+        tud_cdc_write_flush();
+
+        tud_cdc_read_flush();
+        for (int i = 0; i < 32; i++)
+        {
+            while (!tud_cdc_available()) { rt_thread_mdelay(10); }
+            // read char
+            ch = tud_cdc_read_char();
+            *(buffer + i) = ch;
+            // echo
+            tud_cdc_write_char(ch);
+            tud_cdc_write_flush();
+            // end with CR
+            if (ch == '\r') { break; }
+        }
+        tud_cdc_write_str("\r\nwhat you enter: ");
+        tud_cdc_write((const char *) buffer, 32);
+        tud_cdc_write_str("\r\n");
+        tud_cdc_write_flush();
+    }
+    else
+    {
+        rt_kprintf("please open port and make sure DTR=1\n");
+    }
+}
+
+MSH_CMD_EXPORT(cdc_example, TinyUSB cdc example)