usbh.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * This file is part of the TinyUSB stack.
  25. */
  26. #ifndef _TUSB_USBH_H_
  27. #define _TUSB_USBH_H_
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "common/tusb_common.h"
  32. #include "hcd.h"
  33. //--------------------------------------------------------------------+
  34. // MACRO CONSTANT TYPEDEF
  35. //--------------------------------------------------------------------+
  36. // forward declaration
  37. struct tuh_xfer_s;
  38. typedef struct tuh_xfer_s tuh_xfer_t;
  39. typedef void (*tuh_xfer_cb_t)(tuh_xfer_t* xfer);
  40. // Note1: layout and order of this will be changed in near future
  41. // it is advised to initialize it using member name
  42. // Note2: not all field is available/meaningful in callback, some info is not saved by
  43. // usbh to save SRAM
  44. struct tuh_xfer_s
  45. {
  46. uint8_t daddr;
  47. uint8_t ep_addr;
  48. xfer_result_t result;
  49. uint32_t actual_len; // excluding setup packet
  50. union
  51. {
  52. tusb_control_request_t const* setup; // setup packet pointer if control transfer
  53. uint32_t buflen; // expected length if not control transfer (not available in callback)
  54. };
  55. uint8_t* buffer; // not available in callback if not control transfer
  56. tuh_xfer_cb_t complete_cb;
  57. uintptr_t user_data;
  58. // uint32_t timeout_ms; // place holder, not supported yet
  59. };
  60. //--------------------------------------------------------------------+
  61. // APPLICATION CALLBACK
  62. //--------------------------------------------------------------------+
  63. //TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device);
  64. // Invoked when device is mounted (configured)
  65. TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr);
  66. /// Invoked when device is unmounted (bus reset/unplugged)
  67. TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
  68. //--------------------------------------------------------------------+
  69. // APPLICATION API
  70. //--------------------------------------------------------------------+
  71. // Init host stack
  72. bool tuh_init(uint8_t rhport);
  73. // Check if host stack is already initialized
  74. bool tuh_inited(void);
  75. // Task function should be called in main/rtos loop, extended version of tuh_task()
  76. // - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever
  77. // - in_isr: if function is called in ISR
  78. void tuh_task_ext(uint32_t timeout_ms, bool in_isr);
  79. // Task function should be called in main/rtos loop
  80. TU_ATTR_ALWAYS_INLINE static inline
  81. void tuh_task(void)
  82. {
  83. tuh_task_ext(UINT32_MAX, false);
  84. }
  85. // Interrupt handler, name alias to HCD
  86. extern void hcd_int_handler(uint8_t rhport);
  87. #define tuh_int_handler hcd_int_handler
  88. bool tuh_vid_pid_get(uint8_t daddr, uint16_t* vid, uint16_t* pid);
  89. tusb_speed_t tuh_speed_get(uint8_t daddr);
  90. // Check if device is connected and configured
  91. bool tuh_mounted(uint8_t daddr);
  92. // Check if device is suspended
  93. TU_ATTR_ALWAYS_INLINE static inline
  94. bool tuh_suspended(uint8_t daddr)
  95. {
  96. // TODO implement suspend & resume on host
  97. (void) daddr;
  98. return false;
  99. }
  100. // Check if device is ready to communicate with
  101. TU_ATTR_ALWAYS_INLINE static inline
  102. bool tuh_ready(uint8_t daddr)
  103. {
  104. return tuh_mounted(daddr) && !tuh_suspended(daddr);
  105. }
  106. //--------------------------------------------------------------------+
  107. // Transfer API
  108. //--------------------------------------------------------------------+
  109. // Submit a control transfer
  110. // - async: complete callback invoked when finished.
  111. // - sync : blocking if complete callback is NULL.
  112. bool tuh_control_xfer(tuh_xfer_t* xfer);
  113. // Submit a bulk/interrupt transfer
  114. // - async: complete callback invoked when finished.
  115. // - sync : blocking if complete callback is NULL.
  116. bool tuh_edpt_xfer(tuh_xfer_t* xfer);
  117. // Open an non-control endpoint
  118. bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep);
  119. // Set Configuration (control transfer)
  120. // config_num = 0 will un-configure device. Note: config_num = config_descriptor_index + 1
  121. // true on success, false if there is on-going control transfer or incorrect parameters
  122. bool tuh_configuration_set(uint8_t daddr, uint8_t config_num,
  123. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  124. //--------------------------------------------------------------------+
  125. // Descriptors Asynchronous (non-blocking)
  126. //--------------------------------------------------------------------+
  127. // Get an descriptor (control transfer)
  128. // true on success, false if there is on-going control transfer or incorrect parameters
  129. bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len,
  130. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  131. // Get device descriptor (control transfer)
  132. // true on success, false if there is on-going control transfer or incorrect parameters
  133. bool tuh_descriptor_get_device(uint8_t daddr, void* buffer, uint16_t len,
  134. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  135. // Get configuration descriptor (control transfer)
  136. // true on success, false if there is on-going control transfer or incorrect parameters
  137. bool tuh_descriptor_get_configuration(uint8_t daddr, uint8_t index, void* buffer, uint16_t len,
  138. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  139. // Get HID report descriptor (control transfer)
  140. // true on success, false if there is on-going control transfer or incorrect parameters
  141. bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len,
  142. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  143. // Get string descriptor (control transfer)
  144. // true on success, false if there is on-going control transfer or incorrect parameters
  145. // Blocking if complete callback is NULL, in this case 'user_data' must contain xfer_result_t variable
  146. bool tuh_descriptor_get_string(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len,
  147. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  148. // Get manufacturer string descriptor (control transfer)
  149. // true on success, false if there is on-going control transfer or incorrect parameters
  150. bool tuh_descriptor_get_manufacturer_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
  151. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  152. // Get product string descriptor (control transfer)
  153. // true on success, false if there is on-going control transfer or incorrect parameters
  154. bool tuh_descriptor_get_product_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
  155. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  156. // Get serial string descriptor (control transfer)
  157. // true on success, false if there is on-going control transfer or incorrect parameters
  158. bool tuh_descriptor_get_serial_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
  159. tuh_xfer_cb_t complete_cb, uintptr_t user_data);
  160. //--------------------------------------------------------------------+
  161. // Descriptors Synchronous (blocking)
  162. //--------------------------------------------------------------------+
  163. // Sync (blocking) version of tuh_descriptor_get()
  164. // return transfer result
  165. uint8_t tuh_descriptor_get_sync(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len);
  166. // Sync (blocking) version of tuh_descriptor_get_device()
  167. // return transfer result
  168. uint8_t tuh_descriptor_get_device_sync(uint8_t daddr, void* buffer, uint16_t len);
  169. // Sync (blocking) version of tuh_descriptor_get_configuration()
  170. // return transfer result
  171. uint8_t tuh_descriptor_get_configuration_sync(uint8_t daddr, uint8_t index, void* buffer, uint16_t len);
  172. // Sync (blocking) version of tuh_descriptor_get_hid_report()
  173. // return transfer result
  174. uint8_t tuh_descriptor_get_hid_report_sync(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len);
  175. // Sync (blocking) version of tuh_descriptor_get_string()
  176. // return transfer result
  177. uint8_t tuh_descriptor_get_string_sync(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len);
  178. // Sync (blocking) version of tuh_descriptor_get_manufacturer_string()
  179. // return transfer result
  180. uint8_t tuh_descriptor_get_manufacturer_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
  181. // Sync (blocking) version of tuh_descriptor_get_product_string()
  182. // return transfer result
  183. uint8_t tuh_descriptor_get_product_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
  184. // Sync (blocking) version of tuh_descriptor_get_serial_string()
  185. // return transfer result
  186. uint8_t tuh_descriptor_get_serial_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif