hcd.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /** \ingroup group_usbh
  27. * \defgroup Group_HCD Host Controller Driver (HCD)
  28. * @{ */
  29. #ifndef _TUSB_HCD_H_
  30. #define _TUSB_HCD_H_
  31. #include <common/tusb_common.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. //--------------------------------------------------------------------+
  36. // MACRO CONSTANT TYPEDEF
  37. //--------------------------------------------------------------------+
  38. typedef enum
  39. {
  40. HCD_EVENT_DEVICE_ATTACH,
  41. HCD_EVENT_DEVICE_REMOVE,
  42. HCD_EVENT_XFER_COMPLETE,
  43. // Not an HCD event, just a convenient way to defer ISR function
  44. USBH_EVENT_FUNC_CALL,
  45. HCD_EVENT_COUNT
  46. } hcd_eventid_t;
  47. typedef struct
  48. {
  49. uint8_t rhport;
  50. uint8_t event_id;
  51. uint8_t dev_addr;
  52. union
  53. {
  54. // Attach, Remove
  55. struct {
  56. uint8_t hub_addr;
  57. uint8_t hub_port;
  58. } connection;
  59. // XFER_COMPLETE
  60. struct {
  61. uint8_t ep_addr;
  62. uint8_t result;
  63. uint32_t len;
  64. } xfer_complete;
  65. // FUNC_CALL
  66. struct {
  67. void (*func) (void*);
  68. void* param;
  69. }func_call;
  70. };
  71. } hcd_event_t;
  72. #if TUSB_OPT_HOST_ENABLED
  73. // Max number of endpoints per device
  74. enum {
  75. HCD_MAX_ENDPOINT = CFG_TUSB_HOST_DEVICE_MAX*(CFG_TUH_HUB + CFG_TUH_HID_KEYBOARD + CFG_TUH_HID_MOUSE + CFG_TUSB_HOST_HID_GENERIC +
  76. CFG_TUH_MSC*2 + CFG_TUH_CDC*3),
  77. HCD_MAX_XFER = HCD_MAX_ENDPOINT*2,
  78. };
  79. //#define HCD_MAX_ENDPOINT 16
  80. //#define HCD_MAX_XFER 16
  81. #endif
  82. //--------------------------------------------------------------------+
  83. // Controller & Port API
  84. //--------------------------------------------------------------------+
  85. bool hcd_init(uint8_t rhport);
  86. void hcd_int_handler(uint8_t rhport);
  87. void hcd_int_enable (uint8_t rhport);
  88. void hcd_int_disable(uint8_t rhport);
  89. // Get micro frame number (125 us)
  90. uint32_t hcd_uframe_number(uint8_t rhport);
  91. // Get frame number (1ms)
  92. static inline uint32_t hcd_frame_number(uint8_t rhport)
  93. {
  94. return hcd_uframe_number(rhport) >> 3;
  95. }
  96. /// return the current connect status of roothub port
  97. bool hcd_port_connect_status(uint8_t hostid);
  98. void hcd_port_reset(uint8_t hostid);
  99. void hcd_port_reset_end(uint8_t rhport);
  100. tusb_speed_t hcd_port_speed_get(uint8_t hostid);
  101. // HCD closes all opened endpoints belong to this device
  102. void hcd_device_close(uint8_t rhport, uint8_t dev_addr);
  103. //--------------------------------------------------------------------+
  104. // Endpoints API
  105. //--------------------------------------------------------------------+
  106. bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]);
  107. bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc);
  108. bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr);
  109. bool hcd_edpt_stalled(uint8_t dev_addr, uint8_t ep_addr);
  110. bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr);
  111. // TODO merge with pipe_xfer
  112. bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen);
  113. //--------------------------------------------------------------------+
  114. // PIPE API
  115. //--------------------------------------------------------------------+
  116. // TODO control xfer should be used via usbh layer
  117. bool hcd_pipe_queue_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes); // only queue, not transferring yet
  118. bool hcd_pipe_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete);
  119. // tusb_error_t hcd_pipe_cancel();
  120. //--------------------------------------------------------------------+
  121. // Event API (implemented by stack)
  122. //--------------------------------------------------------------------+
  123. // Called by HCD to notify stack
  124. extern void hcd_event_handler(hcd_event_t const* event, bool in_isr);
  125. // Helper to send device attach event
  126. extern void hcd_event_device_attach(uint8_t rhport, bool in_isr);
  127. // Helper to send device removal event
  128. extern void hcd_event_device_remove(uint8_t rhport, bool in_isr);
  129. // Helper to send USB transfer event
  130. extern void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr);
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif /* _TUSB_HCD_H_ */
  135. /// @}