main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // init device stack on configured roothub port
  52. tud_init(BOARD_TUD_RHPORT);
  53. while (1)
  54. {
  55. tud_task(); // tinyusb device task
  56. led_blinking_task();
  57. hid_task();
  58. }
  59. return 0;
  60. }
  61. //--------------------------------------------------------------------+
  62. // Device callbacks
  63. //--------------------------------------------------------------------+
  64. // Invoked when device is mounted
  65. void tud_mount_cb(void)
  66. {
  67. blink_interval_ms = BLINK_MOUNTED;
  68. }
  69. // Invoked when device is unmounted
  70. void tud_umount_cb(void)
  71. {
  72. blink_interval_ms = BLINK_NOT_MOUNTED;
  73. }
  74. // Invoked when usb bus is suspended
  75. // remote_wakeup_en : if host allow us to perform remote wakeup
  76. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  77. void tud_suspend_cb(bool remote_wakeup_en)
  78. {
  79. (void) remote_wakeup_en;
  80. blink_interval_ms = BLINK_SUSPENDED;
  81. }
  82. // Invoked when usb bus is resumed
  83. void tud_resume_cb(void)
  84. {
  85. blink_interval_ms = BLINK_MOUNTED;
  86. }
  87. //--------------------------------------------------------------------+
  88. // USB HID
  89. //--------------------------------------------------------------------+
  90. // Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
  91. // tud_hid_report_complete_cb() is used to send the next report after previous one is complete
  92. void hid_task(void)
  93. {
  94. // Poll every 10ms
  95. const uint32_t interval_ms = 10;
  96. static uint32_t start_ms = 0;
  97. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  98. start_ms += interval_ms;
  99. uint32_t const btn = board_button_read();
  100. if ( tud_suspended() && btn )
  101. {
  102. // Wake up host if we are in suspend mode
  103. // and REMOTE_WAKEUP feature is enabled by host
  104. tud_remote_wakeup();
  105. }
  106. else
  107. {
  108. // keyboard interface
  109. if ( tud_hid_n_ready(ITF_NUM_KEYBOARD) )
  110. {
  111. // used to avoid send multiple consecutive zero report for keyboard
  112. static bool has_keyboard_key = false;
  113. uint8_t const report_id = 0;
  114. uint8_t const modifier = 0;
  115. if ( btn )
  116. {
  117. uint8_t keycode[6] = { 0 };
  118. keycode[0] = HID_KEY_ARROW_RIGHT;
  119. tud_hid_n_keyboard_report(ITF_NUM_KEYBOARD, report_id, modifier, keycode);
  120. has_keyboard_key = true;
  121. }else
  122. {
  123. // send empty key report if previously has key pressed
  124. if (has_keyboard_key) tud_hid_n_keyboard_report(ITF_NUM_KEYBOARD, report_id, modifier, NULL);
  125. has_keyboard_key = false;
  126. }
  127. }
  128. // mouse interface
  129. if ( tud_hid_n_ready(ITF_NUM_MOUSE) )
  130. {
  131. if ( btn )
  132. {
  133. uint8_t const report_id = 0;
  134. uint8_t const button_mask = 0;
  135. uint8_t const vertical = 0;
  136. uint8_t const horizontal = 0;
  137. int8_t const delta = 5;
  138. tud_hid_n_mouse_report(ITF_NUM_MOUSE, report_id, button_mask, delta, delta, vertical, horizontal);
  139. }
  140. }
  141. }
  142. }
  143. // Invoked when received SET_PROTOCOL request
  144. // protocol is either HID_PROTOCOL_BOOT (0) or HID_PROTOCOL_REPORT (1)
  145. void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol)
  146. {
  147. (void) instance;
  148. (void) protocol;
  149. // nothing to do since we use the same compatible boot report for both Boot and Report mode.
  150. // TODO set a indicator for user
  151. }
  152. // Invoked when sent REPORT successfully to host
  153. // Application can use this to send the next report
  154. // Note: For composite reports, report[0] is report ID
  155. void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint16_t*/ uint8_t len)
  156. {
  157. (void) instance;
  158. (void) report;
  159. (void) len;
  160. // nothing to do
  161. }
  162. // Invoked when received GET_REPORT control request
  163. // Application must fill buffer report's content and return its length.
  164. // Return zero will cause the stack to STALL request
  165. 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)
  166. {
  167. // TODO not Implemented
  168. (void) instance;
  169. (void) report_id;
  170. (void) report_type;
  171. (void) buffer;
  172. (void) reqlen;
  173. return 0;
  174. }
  175. // Invoked when received SET_REPORT control request or
  176. // received data on OUT endpoint ( Report ID = 0, Type = 0 )
  177. 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)
  178. {
  179. (void) report_id;
  180. // keyboard interface
  181. if (instance == ITF_NUM_KEYBOARD)
  182. {
  183. // Set keyboard LED e.g Capslock, Numlock etc...
  184. if (report_type == HID_REPORT_TYPE_OUTPUT)
  185. {
  186. // bufsize should be (at least) 1
  187. if ( bufsize < 1 ) return;
  188. uint8_t const kbd_leds = buffer[0];
  189. if (kbd_leds & KEYBOARD_LED_CAPSLOCK)
  190. {
  191. // Capslock On: disable blink, turn led on
  192. blink_interval_ms = 0;
  193. board_led_write(true);
  194. }else
  195. {
  196. // Caplocks Off: back to normal blink
  197. board_led_write(false);
  198. blink_interval_ms = BLINK_MOUNTED;
  199. }
  200. }
  201. }
  202. }
  203. //--------------------------------------------------------------------+
  204. // BLINKING TASK
  205. //--------------------------------------------------------------------+
  206. void led_blinking_task(void)
  207. {
  208. static uint32_t start_ms = 0;
  209. static bool led_state = false;
  210. // blink is disabled
  211. if (!blink_interval_ms) return;
  212. // Blink every interval ms
  213. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  214. start_ms += blink_interval_ms;
  215. board_led_write(led_state);
  216. led_state = 1 - led_state; // toggle
  217. }