dcd.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_usbd
  27. * \defgroup group_dcd Device Controller Driver (DCD)
  28. * @{ */
  29. #ifndef _TUSB_DCD_H_
  30. #define _TUSB_DCD_H_
  31. #include "common/tusb_common.h"
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. typedef enum
  36. {
  37. DCD_EVENT_INVALID = 0,
  38. DCD_EVENT_BUS_RESET,
  39. DCD_EVENT_UNPLUGGED,
  40. DCD_EVENT_SOF,
  41. DCD_EVENT_SUSPEND, // TODO LPM Sleep L1 support
  42. DCD_EVENT_RESUME,
  43. DCD_EVENT_SETUP_RECEIVED,
  44. DCD_EVENT_XFER_COMPLETE,
  45. // Not an DCD event, just a convenient way to defer ISR function
  46. USBD_EVENT_FUNC_CALL,
  47. DCD_EVENT_COUNT
  48. } dcd_eventid_t;
  49. typedef struct TU_ATTR_ALIGNED(4)
  50. {
  51. uint8_t rhport;
  52. uint8_t event_id;
  53. union {
  54. // USBD_EVT_SETUP_RECEIVED
  55. tusb_control_request_t setup_received;
  56. // USBD_EVT_XFER_COMPLETE
  57. struct {
  58. uint8_t ep_addr;
  59. uint8_t result;
  60. uint32_t len;
  61. }xfer_complete;
  62. // USBD_EVENT_FUNC_CALL
  63. struct {
  64. void (*func) (void*);
  65. void* param;
  66. }func_call;
  67. };
  68. } dcd_event_t;
  69. //TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct");
  70. /*------------------------------------------------------------------*/
  71. /* Device API
  72. *------------------------------------------------------------------*/
  73. // Initialize controller to device mode
  74. void dcd_init (uint8_t rhport);
  75. // Enable device interrupt
  76. void dcd_int_enable (uint8_t rhport);
  77. // Disable device interrupt
  78. void dcd_int_disable(uint8_t rhport);
  79. // Receive Set Address request, mcu port must also include status IN response
  80. void dcd_set_address(uint8_t rhport, uint8_t dev_addr);
  81. // Receive Set Configure request
  82. void dcd_set_config (uint8_t rhport, uint8_t config_num);
  83. // Wake up host
  84. void dcd_remote_wakeup(uint8_t rhport);
  85. //--------------------------------------------------------------------+
  86. // Endpoint API
  87. //--------------------------------------------------------------------+
  88. // Configure endpoint's registers according to descriptor
  89. bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
  90. // Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack
  91. bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
  92. // Stall endpoint
  93. void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
  94. // clear stall, data toggle is also reset to DATA0
  95. void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
  96. //--------------------------------------------------------------------+
  97. // Event API (Implemented by device stack)
  98. //--------------------------------------------------------------------+
  99. // Called by DCD to notify device stack
  100. extern void dcd_event_handler(dcd_event_t const * event, bool in_isr);
  101. // helper to send bus signal event
  102. extern void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr);
  103. // helper to send setup received
  104. extern void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr);
  105. // helper to send transfer complete event
  106. extern void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr);
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif /* _TUSB_DCD_H_ */
  111. /// @}