main.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
  32. // ESP-IDF need "freertos/" prefix in include path.
  33. // CFG_TUSB_OS_INC_PATH should be defined accordingly.
  34. #include "freertos/FreeRTOS.h"
  35. #include "freertos/semphr.h"
  36. #include "freertos/queue.h"
  37. #include "freertos/task.h"
  38. #include "freertos/timers.h"
  39. #define USBD_STACK_SIZE 4096
  40. #else
  41. #include "FreeRTOS.h"
  42. #include "semphr.h"
  43. #include "queue.h"
  44. #include "task.h"
  45. #include "timers.h"
  46. // Increase stack size when debug log is enabled
  47. #define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
  48. #endif
  49. //--------------------------------------------------------------------+
  50. // MACRO CONSTANT TYPEDEF PROTYPES
  51. //--------------------------------------------------------------------+
  52. /* Blink pattern
  53. * - 250 ms : device not mounted
  54. * - 1000 ms : device mounted
  55. * - 2500 ms : device is suspended
  56. */
  57. enum {
  58. BLINK_NOT_MOUNTED = 250,
  59. BLINK_MOUNTED = 1000,
  60. BLINK_SUSPENDED = 2500,
  61. };
  62. // static timer
  63. StaticTimer_t blinky_tmdef;
  64. TimerHandle_t blinky_tm;
  65. // static task
  66. StackType_t usb_device_stack[USBD_STACK_SIZE];
  67. StaticTask_t usb_device_taskdef;
  68. // static task for hid
  69. #define HID_STACK_SZIE configMINIMAL_STACK_SIZE
  70. StackType_t hid_stack[HID_STACK_SZIE];
  71. StaticTask_t hid_taskdef;
  72. void led_blinky_cb(TimerHandle_t xTimer);
  73. void usb_device_task(void* param);
  74. void hid_task(void* params);
  75. //--------------------------------------------------------------------+
  76. // Main
  77. //--------------------------------------------------------------------+
  78. int main(void)
  79. {
  80. board_init();
  81. // soft timer for blinky
  82. blinky_tm = xTimerCreateStatic(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb, &blinky_tmdef);
  83. xTimerStart(blinky_tm, 0);
  84. // Create a task for tinyusb device stack
  85. (void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
  86. // Create HID task
  87. (void) xTaskCreateStatic( hid_task, "hid", HID_STACK_SZIE, NULL, configMAX_PRIORITIES-2, hid_stack, &hid_taskdef);
  88. // skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
  89. #if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
  90. vTaskStartScheduler();
  91. #endif
  92. return 0;
  93. }
  94. #if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
  95. void app_main(void)
  96. {
  97. main();
  98. }
  99. #endif
  100. // USB Device Driver task
  101. // This top level thread process all usb events and invoke callbacks
  102. void usb_device_task(void* param)
  103. {
  104. (void) param;
  105. // This should be called after scheduler/kernel is started.
  106. // Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
  107. tusb_init();
  108. // RTOS forever loop
  109. while (1)
  110. {
  111. // put this thread to waiting state until there is new events
  112. tud_task();
  113. // following code only run if tud_task() process at least 1 event
  114. }
  115. }
  116. //--------------------------------------------------------------------+
  117. // Device callbacks
  118. //--------------------------------------------------------------------+
  119. // Invoked when device is mounted
  120. void tud_mount_cb(void)
  121. {
  122. xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_MOUNTED), 0);
  123. }
  124. // Invoked when device is unmounted
  125. void tud_umount_cb(void)
  126. {
  127. xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), 0);
  128. }
  129. // Invoked when usb bus is suspended
  130. // remote_wakeup_en : if host allow us to perform remote wakeup
  131. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  132. void tud_suspend_cb(bool remote_wakeup_en)
  133. {
  134. (void) remote_wakeup_en;
  135. xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_SUSPENDED), 0);
  136. }
  137. // Invoked when usb bus is resumed
  138. void tud_resume_cb(void)
  139. {
  140. xTimerChangePeriod(blinky_tm, pdMS_TO_TICKS(BLINK_MOUNTED), 0);
  141. }
  142. //--------------------------------------------------------------------+
  143. // USB HID
  144. //--------------------------------------------------------------------+
  145. static void send_hid_report(uint8_t report_id, uint32_t btn)
  146. {
  147. // skip if hid is not ready yet
  148. if ( !tud_hid_ready() ) return;
  149. switch(report_id)
  150. {
  151. case REPORT_ID_KEYBOARD:
  152. {
  153. // use to avoid send multiple consecutive zero report for keyboard
  154. static bool has_keyboard_key = false;
  155. if ( btn )
  156. {
  157. uint8_t keycode[6] = { 0 };
  158. keycode[0] = HID_KEY_A;
  159. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
  160. has_keyboard_key = true;
  161. }else
  162. {
  163. // send empty key report if previously has key pressed
  164. if (has_keyboard_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
  165. has_keyboard_key = false;
  166. }
  167. }
  168. break;
  169. case REPORT_ID_MOUSE:
  170. {
  171. int8_t const delta = 5;
  172. // no button, right + down, no scroll, no pan
  173. tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
  174. }
  175. break;
  176. case REPORT_ID_CONSUMER_CONTROL:
  177. {
  178. // use to avoid send multiple consecutive zero report
  179. static bool has_consumer_key = false;
  180. if ( btn )
  181. {
  182. // volume down
  183. uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT;
  184. tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2);
  185. has_consumer_key = true;
  186. }else
  187. {
  188. // send empty key report (release key) if previously has key pressed
  189. uint16_t empty_key = 0;
  190. if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2);
  191. has_consumer_key = false;
  192. }
  193. }
  194. break;
  195. case REPORT_ID_GAMEPAD:
  196. {
  197. // use to avoid send multiple consecutive zero report for keyboard
  198. static bool has_gamepad_key = false;
  199. hid_gamepad_report_t report =
  200. {
  201. .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
  202. .hat = 0, .buttons = 0
  203. };
  204. if ( btn )
  205. {
  206. report.hat = GAMEPAD_HAT_UP;
  207. report.buttons = GAMEPAD_BUTTON_A;
  208. tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  209. has_gamepad_key = true;
  210. }else
  211. {
  212. report.hat = GAMEPAD_HAT_CENTERED;
  213. report.buttons = 0;
  214. if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  215. has_gamepad_key = false;
  216. }
  217. }
  218. break;
  219. default: break;
  220. }
  221. }
  222. void hid_task(void* param)
  223. {
  224. (void) param;
  225. while(1)
  226. {
  227. // Poll every 10ms
  228. vTaskDelay(pdMS_TO_TICKS(10));
  229. uint32_t const btn = board_button_read();
  230. // Remote wakeup
  231. if ( tud_suspended() && btn )
  232. {
  233. // Wake up host if we are in suspend mode
  234. // and REMOTE_WAKEUP feature is enabled by host
  235. tud_remote_wakeup();
  236. }
  237. else
  238. {
  239. // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
  240. send_hid_report(REPORT_ID_KEYBOARD, btn);
  241. }
  242. }
  243. }
  244. // Invoked when sent REPORT successfully to host
  245. // Application can use this to send the next report
  246. // Note: For composite reports, report[0] is report ID
  247. void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len)
  248. {
  249. (void) instance;
  250. (void) len;
  251. uint8_t next_report_id = report[0] + 1;
  252. if (next_report_id < REPORT_ID_COUNT)
  253. {
  254. send_hid_report(next_report_id, board_button_read());
  255. }
  256. }
  257. // Invoked when received GET_REPORT control request
  258. // Application must fill buffer report's content and return its length.
  259. // Return zero will cause the stack to STALL request
  260. uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
  261. {
  262. // TODO not Implemented
  263. (void) instance;
  264. (void) report_id;
  265. (void) report_type;
  266. (void) buffer;
  267. (void) reqlen;
  268. return 0;
  269. }
  270. // Invoked when received SET_REPORT control request or
  271. // received data on OUT endpoint ( Report ID = 0, Type = 0 )
  272. void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
  273. {
  274. (void) instance;
  275. if (report_type == HID_REPORT_TYPE_OUTPUT)
  276. {
  277. // Set keyboard LED e.g Capslock, Numlock etc...
  278. if (report_id == REPORT_ID_KEYBOARD)
  279. {
  280. // bufsize should be (at least) 1
  281. if ( bufsize < 1 ) return;
  282. uint8_t const kbd_leds = buffer[0];
  283. if (kbd_leds & KEYBOARD_LED_CAPSLOCK)
  284. {
  285. // Capslock On: disable blink, turn led on
  286. xTimerStop(blinky_tm, portMAX_DELAY);
  287. board_led_write(true);
  288. }else
  289. {
  290. // Caplocks Off: back to normal blink
  291. board_led_write(false);
  292. xTimerStart(blinky_tm, portMAX_DELAY);
  293. }
  294. }
  295. }
  296. }
  297. //--------------------------------------------------------------------+
  298. // BLINKING TASK
  299. //--------------------------------------------------------------------+
  300. void led_blinky_cb(TimerHandle_t xTimer)
  301. {
  302. (void) xTimer;
  303. static bool led_state = false;
  304. board_led_write(led_state);
  305. led_state = 1 - led_state; // toggle
  306. }