main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. void hid_task(void)
  90. {
  91. //Set up interfaces
  92. const uint8_t keyboard_interface = 0;
  93. const uint8_t mouse_interface = 1;
  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. // Remote wakeup
  101. if ( tud_suspended() && btn )
  102. {
  103. // Wake up host if we are in suspend mode
  104. // and REMOTE_WAKEUP feature is enabled by host
  105. tud_remote_wakeup();
  106. }
  107. /*------------- Keyboard -------------*/
  108. if ( tud_hid_n_ready(keyboard_interface) )
  109. {
  110. // use to avoid send multiple consecutive zero report for keyboard
  111. static bool has_key = false;
  112. if ( btn )
  113. {
  114. uint8_t keycode[6] = { 0 };
  115. keycode[0] = HID_KEY_A;
  116. tud_hid_n_keyboard_report(keyboard_interface, REPORT_ID_KEYBOARD, 0, keycode);
  117. has_key = true;
  118. }else
  119. {
  120. // send empty key report if previously has key pressed
  121. if (has_key) tud_hid_n_keyboard_report(keyboard_interface, REPORT_ID_KEYBOARD, 0, NULL);
  122. has_key = false;
  123. }
  124. }
  125. /*------------- Mouse -------------*/
  126. if ( tud_hid_n_ready(mouse_interface) )
  127. {
  128. if ( btn )
  129. {
  130. int8_t const delta = 5;
  131. // no button, right + down, no scroll pan
  132. tud_hid_n_mouse_report(mouse_interface, REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
  133. // delay a bit before attempt to send keyboard report
  134. board_delay(10);
  135. }
  136. }
  137. }
  138. // Invoked when received GET_REPORT control request
  139. // Application must fill buffer report's content and return its length.
  140. // Return zero will cause the stack to STALL request
  141. uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
  142. {
  143. // TODO not Implemented
  144. (void) report_id;
  145. (void) report_type;
  146. (void) buffer;
  147. (void) reqlen;
  148. return 0;
  149. }
  150. // Invoked when received SET_REPORT control request or
  151. // received data on OUT endpoint ( Report ID = 0, Type = 0 )
  152. void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
  153. {
  154. // TODO set LED based on CAPLOCK, NUMLOCK etc...
  155. (void) report_id;
  156. (void) report_type;
  157. (void) buffer;
  158. (void) bufsize;
  159. }
  160. //--------------------------------------------------------------------+
  161. // BLINKING TASK
  162. //--------------------------------------------------------------------+
  163. void led_blinking_task(void)
  164. {
  165. static uint32_t start_ms = 0;
  166. static bool led_state = false;
  167. // Blink every interval ms
  168. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  169. start_ms += blink_interval_ms;
  170. board_led_write(led_state);
  171. led_state = 1 - led_state; // toggle
  172. }