hal_usb.dox 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @defgroup USB USB Driver
  15. * @brief Generic USB Driver.
  16. * @details This module implements a generic USB (Universal Serial Bus) driver
  17. * supporting device-mode operations.
  18. * @pre In order to use the USB driver the @p HAL_USE_USB option
  19. * must be enabled in @p halconf.h.
  20. *
  21. * @section usb_1 Driver State Machine
  22. * The driver implements a state machine internally, not all the driver
  23. * functionalities can be used in any moment, any transition not explicitly
  24. * shown in the following diagram has to be considered an error and shall
  25. * be captured by an assertion (if enabled).
  26. * @if LATEX_PDF
  27. * @dot
  28. digraph example {
  29. size="5, 7";
  30. rankdir="LR";
  31. node [shape=circle, fontname=Helvetica, fontsize=8, fixedsize="true",
  32. width="0.9", height="0.9"];
  33. edge [fontname=Helvetica, fontsize=8];
  34. stop [label="USB_STOP\nLow Power"];
  35. uninit [label="USB_UNINIT", style="bold"];
  36. ready [label="USB_READY\nClock Enabled"];
  37. selected [label="\nUSB_SELECTED\naddress\nassigned"];
  38. active [label="\nUSB_ACTIVE\nconfiguration\nselected"];
  39. uninit -> stop [label=" usbInit()", constraint=false];
  40. stop -> stop [label="\nusbStop()"];
  41. stop -> ready [label="\nusbStart()"];
  42. ready -> stop [label="\nusbStop()"];
  43. ready -> ready [label="\n\nusbStart()"];
  44. ready -> ready [label="\nSUSPEND/WAKEUP\n>event_cb<"];
  45. ready -> selected [label="\nSET_ADDRESS\n>event_cb<"];
  46. selected -> stop [label="\nusbStop()"];
  47. selected -> ready [label="\nUSB RESET\n>event_cb<"];
  48. selected -> selected [label="\nSUSPEND/WAKEUP\n>event_cb<\n\nValid EP0 Message\n>requests_hook_cb<\n\nGET DESCRIPTOR\n>get_descriptor_cb<"];
  49. selected -> active [label="\nSET_CONF(n)\n>event_cb<"];
  50. active -> stop [label="\nusbStop()"];
  51. active -> selected [label="\nSET_CONF(0)\n>event_cb<"];
  52. active -> active [label="\nSUSPEND/WAKEUP\n>event_cb<\n\nValid EP0 Message\n>requests_hook_cb<\n\nGET DESCRIPTOR\n>get_descriptor_cb<\n\nEndpoints Activity\n >in_cb< or >out_cb<"];
  53. active -> ready [label="\nUSB RESET\n>event_cb<"];
  54. }
  55. * @enddot
  56. * @else
  57. * @dot
  58. digraph example {
  59. rankdir="LR";
  60. node [shape=circle, fontname=Helvetica, fontsize=8, fixedsize="true",
  61. width="0.9", height="0.9"];
  62. edge [fontname=Helvetica, fontsize=8];
  63. stop [label="USB_STOP\nLow Power"];
  64. uninit [label="USB_UNINIT", style="bold"];
  65. ready [label="USB_READY\nClock Enabled"];
  66. selected [label="\nUSB_SELECTED\naddress\nassigned"];
  67. active [label="\nUSB_ACTIVE\nconfiguration\nselected"];
  68. uninit -> stop [label=" usbInit()", constraint=false];
  69. stop -> stop [label="\nusbStop()"];
  70. stop -> ready [label="\nusbStart()"];
  71. ready -> stop [label="\nusbStop()"];
  72. ready -> ready [label="\n\nusbStart()"];
  73. ready -> ready [label="\nSUSPEND/WAKEUP\n>event_cb<"];
  74. ready -> selected [label="\nSET_ADDRESS\n>event_cb<"];
  75. selected -> stop [label="\nusbStop()"];
  76. selected -> ready [label="\nUSB RESET\n>event_cb<"];
  77. selected -> selected [label="\nSUSPEND/WAKEUP\n>event_cb<\n\nValid EP0 Message\n>requests_hook_cb<\n\nGET DESCRIPTOR\n>get_descriptor_cb<"];
  78. selected -> active [label="\nSET_CONF(n)\n>event_cb<"];
  79. active -> stop [label="\nusbStop()"];
  80. active -> selected [label="\nSET_CONF(0)\n>event_cb<"];
  81. active -> active [label="\nSUSPEND/WAKEUP\n>event_cb<\n\nValid EP0 Message\n>requests_hook_cb<\n\nGET DESCRIPTOR\n>get_descriptor_cb<\n\nEndpoints Activity\n >in_cb< or >out_cb<"];
  82. active -> ready [label="\nUSB RESET\n>event_cb<"];
  83. }
  84. * @enddot
  85. * @endif
  86. *
  87. * @section usb_2 USB Operations
  88. * The USB driver is quite complex and USB is complex in itself, it is
  89. * recommended to study the USB specification before trying to use the
  90. * driver.
  91. *
  92. * @subsection usb_2_1 USB Implementation
  93. * The USB driver abstracts the inner details of the underlying USB hardware.
  94. * The driver works asynchronously and communicates with the application
  95. * using callbacks. The application is responsible of the descriptors and
  96. * strings required by the USB device class to be implemented and of the
  97. * handling of the specific messages sent over the endpoint zero. Standard
  98. * messages are handled internally to the driver. The application can use
  99. * hooks in order to handle custom messages or override the handling of the
  100. * default handling of standard messages.
  101. *
  102. * @subsection usb_2_2 USB Endpoints
  103. * USB endpoints are the objects that the application uses to exchange
  104. * data with the host. There are two kind of endpoints:
  105. * - <b>IN</b> endpoints are used by the application to transmit data to
  106. * the host.<br>
  107. * - <b>OUT</b> endpoints are used by the application to receive data from
  108. * the host.
  109. * .
  110. * The driver invokes a callback after finishing an IN or OUT transaction.
  111. * States diagram for OUT endpoints in transaction mode:
  112. * @dot
  113. digraph example {
  114. rankdir="LR";
  115. node [shape=circle, fontname=Helvetica, fontsize=8, fixedsize="true",
  116. width="0.9", height="0.9"];
  117. edge [fontname=Helvetica, fontsize=8];
  118. disabled [label="EP_DISABLED\nDisabled", style="bold"];
  119. receiving [label="EP_BUSY\nReceiving"];
  120. idle [label="EP_IDLE\nReady"];
  121. disabled -> idle [label="\nusbInitEndpointI()"];
  122. idle -> receiving [label="\nusbPrepareReceive()\nusbStartReceiveI()"];
  123. receiving -> receiving [label="\nmore packets"];
  124. receiving -> idle [label="\nreception end\n>out_cb<"];
  125. receiving -> disabled [label="\nUSB RESET\nusbDisableEndpointsI()"];
  126. idle -> disabled [label="\nUSB RESET\nusbDisableEndpointsI()"];
  127. }
  128. * @enddot
  129. * <br><br>
  130. * States diagram for IN endpoints in transaction mode:
  131. * @dot
  132. digraph example {
  133. rankdir="LR";
  134. node [shape=circle, fontname=Helvetica, fontsize=8, fixedsize="true",
  135. width="0.9", height="0.9"];
  136. edge [fontname=Helvetica, fontsize=8];
  137. disabled [label="EP_DISABLED\nDisabled", style="bold"];
  138. transmitting [label="EP_BUSY\nTransmitting"];
  139. idle [label="EP_IDLE\nReady"];
  140. disabled -> idle [label="\usbInitEndpointI()"];
  141. idle -> transmitting [label="\nusbPrepareTransmit()\nusbStartTransmitI()"];
  142. transmitting -> transmitting [label="\nmore packets"];
  143. transmitting -> idle [label="\ntransmission end\n>in_cb<"];
  144. transmitting -> disabled [label="\nUSB RESET\nusbDisableEndpointsI()"];
  145. idle -> disabled [label="\nUSB RESET\nusbDisableEndpointsI()"];
  146. }
  147. * @enddot
  148. * <br><br>
  149. *
  150. * @subsection usb_2_4 USB Callbacks
  151. * The USB driver uses callbacks in order to interact with the application.
  152. * There are several kinds of callbacks to be handled:
  153. * - Driver events callback. As example errors, suspend event, reset event
  154. * etc.
  155. * - Messages Hook callback. This hook allows the application to implement
  156. * handling of custom messages or to override the default handling of
  157. * standard messages on endpoint zero.
  158. * - Descriptor Requested callback. When the driver endpoint zero handler
  159. * receives a GET DESCRIPTOR message and needs to send a descriptor to
  160. * the host it queries the application using this callback.
  161. * - Start of Frame callback. This callback is invoked each time a SOF
  162. * packet is received.
  163. * - Endpoint callbacks. Each endpoint informs the application about I/O
  164. * conditions using those callbacks.
  165. * .
  166. *
  167. * @ingroup HAL_NORMAL_DRIVERS
  168. */