main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "bsp/board.h"
  29. #include "tusb.h"
  30. #include "usb_descriptors.h"
  31. //--------------------------------------------------------------------+
  32. // MACRO CONSTANT TYPEDEF PROTYPES
  33. //--------------------------------------------------------------------+
  34. /* Blink pattern
  35. * - 250 ms : device not mounted
  36. * - 1000 ms : device mounted
  37. * - 2500 ms : device is suspended
  38. */
  39. enum {
  40. BLINK_NOT_MOUNTED = 250,
  41. BLINK_MOUNTED = 1000,
  42. BLINK_SUSPENDED = 2500,
  43. BLINK_ALWAYS_ON = UINT32_MAX,
  44. BLINK_ALWAYS_OFF = 0
  45. };
  46. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  47. #define URL "www.tinyusb.org/examples/webusb-serial"
  48. const tusb_desc_webusb_url_t desc_url =
  49. {
  50. .bLength = 3 + sizeof(URL) - 1,
  51. .bDescriptorType = 3, // WEBUSB URL type
  52. .bScheme = 1, // 0: http, 1: https
  53. .url = URL
  54. };
  55. static bool web_serial_connected = false;
  56. //------------- prototypes -------------//
  57. void led_blinking_task(void);
  58. void cdc_task(void);
  59. void webserial_task(void);
  60. /*------------- MAIN -------------*/
  61. int main(void)
  62. {
  63. board_init();
  64. tusb_init();
  65. while (1)
  66. {
  67. tud_task(); // tinyusb device task
  68. cdc_task();
  69. webserial_task();
  70. led_blinking_task();
  71. }
  72. return 0;
  73. }
  74. // send characters to both CDC and WebUSB
  75. void echo_all(uint8_t buf[], uint32_t count)
  76. {
  77. // echo to web serial
  78. if ( web_serial_connected )
  79. {
  80. tud_vendor_write(buf, count);
  81. }
  82. // echo to cdc
  83. if ( tud_cdc_connected() )
  84. {
  85. for(uint32_t i=0; i<count; i++)
  86. {
  87. tud_cdc_write_char(buf[i]);
  88. if ( buf[i] == '\r' ) tud_cdc_write_char('\n');
  89. }
  90. tud_cdc_write_flush();
  91. }
  92. }
  93. //--------------------------------------------------------------------+
  94. // Device callbacks
  95. //--------------------------------------------------------------------+
  96. // Invoked when device is mounted
  97. void tud_mount_cb(void)
  98. {
  99. blink_interval_ms = BLINK_MOUNTED;
  100. }
  101. // Invoked when device is unmounted
  102. void tud_umount_cb(void)
  103. {
  104. blink_interval_ms = BLINK_NOT_MOUNTED;
  105. }
  106. // Invoked when usb bus is suspended
  107. // remote_wakeup_en : if host allow us to perform remote wakeup
  108. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  109. void tud_suspend_cb(bool remote_wakeup_en)
  110. {
  111. (void) remote_wakeup_en;
  112. blink_interval_ms = BLINK_SUSPENDED;
  113. }
  114. // Invoked when usb bus is resumed
  115. void tud_resume_cb(void)
  116. {
  117. blink_interval_ms = BLINK_MOUNTED;
  118. }
  119. //--------------------------------------------------------------------+
  120. // WebUSB use vendor class
  121. //--------------------------------------------------------------------+
  122. // Invoked when received VENDOR control request
  123. bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request)
  124. {
  125. switch (request->bRequest)
  126. {
  127. case VENDOR_REQUEST_WEBUSB:
  128. // match vendor request in BOS descriptor
  129. // Get landing page url
  130. return tud_control_xfer(rhport, request, (void*) &desc_url, desc_url.bLength);
  131. case VENDOR_REQUEST_MICROSOFT:
  132. if ( request->wIndex == 7 )
  133. {
  134. // Get Microsoft OS 2.0 compatible descriptor
  135. uint16_t total_len;
  136. memcpy(&total_len, desc_ms_os_20+8, 2);
  137. return tud_control_xfer(rhport, request, (void*) desc_ms_os_20, total_len);
  138. }else
  139. {
  140. return false;
  141. }
  142. case 0x22:
  143. // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to
  144. // connect and disconnect.
  145. web_serial_connected = (request->wValue != 0);
  146. // Always lit LED if connected
  147. if ( web_serial_connected )
  148. {
  149. board_led_write(true);
  150. blink_interval_ms = BLINK_ALWAYS_ON;
  151. tud_vendor_write_str("\r\nTinyUSB WebUSB device example\r\n");
  152. }else
  153. {
  154. blink_interval_ms = BLINK_MOUNTED;
  155. }
  156. // response with status OK
  157. return tud_control_status(rhport, request);
  158. default:
  159. // stall unknown request
  160. return false;
  161. }
  162. return true;
  163. }
  164. // Invoked when DATA Stage of VENDOR's request is complete
  165. bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request)
  166. {
  167. (void) rhport;
  168. (void) request;
  169. // nothing to do
  170. return true;
  171. }
  172. void webserial_task(void)
  173. {
  174. if ( web_serial_connected )
  175. {
  176. if ( tud_vendor_available() )
  177. {
  178. uint8_t buf[64];
  179. uint32_t count = tud_vendor_read(buf, sizeof(buf));
  180. // echo back to both web serial and cdc
  181. echo_all(buf, count);
  182. }
  183. }
  184. }
  185. //--------------------------------------------------------------------+
  186. // USB CDC
  187. //--------------------------------------------------------------------+
  188. void cdc_task(void)
  189. {
  190. if ( tud_cdc_connected() )
  191. {
  192. // connected and there are data available
  193. if ( tud_cdc_available() )
  194. {
  195. uint8_t buf[64];
  196. uint32_t count = tud_cdc_read(buf, sizeof(buf));
  197. // echo back to both web serial and cdc
  198. echo_all(buf, count);
  199. }
  200. }
  201. }
  202. // Invoked when cdc when line state changed e.g connected/disconnected
  203. void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
  204. {
  205. (void) itf;
  206. // connected
  207. if ( dtr && rts )
  208. {
  209. // print initial message when connected
  210. tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n");
  211. }
  212. }
  213. // Invoked when CDC interface received data from host
  214. void tud_cdc_rx_cb(uint8_t itf)
  215. {
  216. (void) itf;
  217. }
  218. //--------------------------------------------------------------------+
  219. // BLINKING TASK
  220. //--------------------------------------------------------------------+
  221. void led_blinking_task(void)
  222. {
  223. static uint32_t start_ms = 0;
  224. static bool led_state = false;
  225. // Blink every interval ms
  226. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  227. start_ms += blink_interval_ms;
  228. board_led_write(led_state);
  229. led_state = 1 - led_state; // toggle
  230. }