No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. };
  44. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  45. void led_blinking_task(void);
  46. void hid_task(void);
  47. /*------------- MAIN -------------*/
  48. int main(void)
  49. {
  50. board_init();
  51. tusb_init();
  52. while (1)
  53. {
  54. tud_task(); // tinyusb device task
  55. led_blinking_task();
  56. hid_task();
  57. }
  58. return 0;
  59. }
  60. //--------------------------------------------------------------------+
  61. // Device callbacks
  62. //--------------------------------------------------------------------+
  63. // Invoked when device is mounted
  64. void tud_mount_cb(void)
  65. {
  66. blink_interval_ms = BLINK_MOUNTED;
  67. }
  68. // Invoked when device is unmounted
  69. void tud_umount_cb(void)
  70. {
  71. blink_interval_ms = BLINK_NOT_MOUNTED;
  72. }
  73. // Invoked when usb bus is suspended
  74. // remote_wakeup_en : if host allow us to perform remote wakeup
  75. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  76. void tud_suspend_cb(bool remote_wakeup_en)
  77. {
  78. (void) remote_wakeup_en;
  79. blink_interval_ms = BLINK_SUSPENDED;
  80. }
  81. // Invoked when usb bus is resumed
  82. void tud_resume_cb(void)
  83. {
  84. blink_interval_ms = BLINK_MOUNTED;
  85. }
  86. //--------------------------------------------------------------------+
  87. // USB HID
  88. //--------------------------------------------------------------------+
  89. static void send_hid_report(uint8_t report_id, uint32_t btn)
  90. {
  91. // skip if hid is not ready yet
  92. if ( !tud_hid_ready() ) return;
  93. switch(report_id)
  94. {
  95. case REPORT_ID_KEYBOARD:
  96. {
  97. // use to avoid send multiple consecutive zero report for keyboard
  98. static bool has_keyboard_key = false;
  99. if ( btn )
  100. {
  101. uint8_t keycode[6] = { 0 };
  102. keycode[0] = HID_KEY_A;
  103. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
  104. has_keyboard_key = true;
  105. }else
  106. {
  107. // send empty key report if previously has key pressed
  108. if (has_keyboard_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
  109. has_keyboard_key = false;
  110. }
  111. }
  112. break;
  113. case REPORT_ID_MOUSE:
  114. {
  115. int8_t const delta = 5;
  116. // no button, right + down, no scroll, no pan
  117. tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
  118. }
  119. break;
  120. case REPORT_ID_CONSUMER_CONTROL:
  121. {
  122. // use to avoid send multiple consecutive zero report
  123. static bool has_consumer_key = false;
  124. if ( btn )
  125. {
  126. // volume down
  127. uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT;
  128. tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2);
  129. has_consumer_key = true;
  130. }else
  131. {
  132. // send empty key report (release key) if previously has key pressed
  133. uint16_t empty_key = 0;
  134. if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2);
  135. has_consumer_key = false;
  136. }
  137. }
  138. break;
  139. case REPORT_ID_GAMEPAD:
  140. {
  141. // use to avoid send multiple consecutive zero report for keyboard
  142. static bool has_gamepad_key = false;
  143. hid_gamepad_report_t report =
  144. {
  145. .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
  146. .hat = 0, .buttons = 0
  147. };
  148. if ( btn )
  149. {
  150. report.hat = GAMEPAD_HAT_UP;
  151. report.buttons = GAMEPAD_BUTTON_A;
  152. tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  153. has_gamepad_key = true;
  154. }else
  155. {
  156. report.hat = GAMEPAD_HAT_CENTERED;
  157. report.buttons = 0;
  158. if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  159. has_gamepad_key = false;
  160. }
  161. }
  162. break;
  163. default: break;
  164. }
  165. }
  166. // Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
  167. // tud_hid_report_complete_cb() is used to send the next report after previous one is complete
  168. void hid_task(void)
  169. {
  170. // Poll every 10ms
  171. const uint32_t interval_ms = 10;
  172. static uint32_t start_ms = 0;
  173. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  174. start_ms += interval_ms;
  175. uint32_t const btn = board_button_read();
  176. // Remote wakeup
  177. if ( tud_suspended() && btn )
  178. {
  179. // Wake up host if we are in suspend mode
  180. // and REMOTE_WAKEUP feature is enabled by host
  181. tud_remote_wakeup();
  182. }else
  183. {
  184. // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
  185. send_hid_report(REPORT_ID_KEYBOARD, btn);
  186. }
  187. }
  188. // Invoked when sent REPORT successfully to host
  189. // Application can use this to send the next report
  190. // Note: For composite reports, report[0] is report ID
  191. void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len)
  192. {
  193. (void) instance;
  194. (void) len;
  195. uint8_t next_report_id = report[0] + 1;
  196. if (next_report_id < REPORT_ID_COUNT)
  197. {
  198. send_hid_report(next_report_id, board_button_read());
  199. }
  200. }
  201. // Invoked when received GET_REPORT control request
  202. // Application must fill buffer report's content and return its length.
  203. // Return zero will cause the stack to STALL request
  204. 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)
  205. {
  206. // TODO not Implemented
  207. (void) instance;
  208. (void) report_id;
  209. (void) report_type;
  210. (void) buffer;
  211. (void) reqlen;
  212. return 0;
  213. }
  214. // Invoked when received SET_REPORT control request or
  215. // received data on OUT endpoint ( Report ID = 0, Type = 0 )
  216. 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)
  217. {
  218. (void) instance;
  219. if (report_type == HID_REPORT_TYPE_OUTPUT)
  220. {
  221. // Set keyboard LED e.g Capslock, Numlock etc...
  222. if (report_id == REPORT_ID_KEYBOARD)
  223. {
  224. // bufsize should be (at least) 1
  225. if ( bufsize < 1 ) return;
  226. uint8_t const kbd_leds = buffer[0];
  227. if (kbd_leds & KEYBOARD_LED_CAPSLOCK)
  228. {
  229. // Capslock On: disable blink, turn led on
  230. blink_interval_ms = 0;
  231. board_led_write(true);
  232. }else
  233. {
  234. // Caplocks Off: back to normal blink
  235. board_led_write(false);
  236. blink_interval_ms = BLINK_MOUNTED;
  237. }
  238. }
  239. }
  240. }
  241. //--------------------------------------------------------------------+
  242. // BLINKING TASK
  243. //--------------------------------------------------------------------+
  244. void led_blinking_task(void)
  245. {
  246. static uint32_t start_ms = 0;
  247. static bool led_state = false;
  248. // blink is disabled
  249. if (!blink_interval_ms) return;
  250. // Blink every interval ms
  251. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  252. start_ms += blink_interval_ms;
  253. board_led_write(led_state);
  254. led_state = 1 - led_state; // toggle
  255. }