hal_usb_lld.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. * @file hal_usb_lld.h
  15. * @brief PLATFORM USB subsystem low level driver header.
  16. *
  17. * @addtogroup USB
  18. * @{
  19. */
  20. #ifndef HAL_USB_LLD_H
  21. #define HAL_USB_LLD_H
  22. #if (HAL_USE_USB == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @brief Maximum endpoint address.
  28. */
  29. #define USB_MAX_ENDPOINTS 4
  30. /**
  31. * @brief Status stage handling method.
  32. */
  33. #define USB_EP0_STATUS_STAGE USB_EP0_STATUS_STAGE_SW
  34. /**
  35. * @brief The address can be changed immediately upon packet reception.
  36. */
  37. #define USB_SET_ADDRESS_MODE USB_LATE_SET_ADDRESS
  38. /**
  39. * @brief Method for set address acknowledge.
  40. */
  41. #define USB_SET_ADDRESS_ACK_HANDLING USB_SET_ADDRESS_ACK_SW
  42. /*===========================================================================*/
  43. /* Driver pre-compile time settings. */
  44. /*===========================================================================*/
  45. /**
  46. * @name PLATFORM configuration options
  47. * @{
  48. */
  49. /**
  50. * @brief USB driver enable switch.
  51. * @details If set to @p TRUE the support for USB1 is included.
  52. * @note The default is @p FALSE.
  53. */
  54. #if !defined(PLATFORM_USB_USE_USB1) || defined(__DOXYGEN__)
  55. #define PLATFORM_USB_USE_USB1 FALSE
  56. #endif
  57. /** @} */
  58. /*===========================================================================*/
  59. /* Derived constants and error checks. */
  60. /*===========================================================================*/
  61. /*===========================================================================*/
  62. /* Driver data structures and types. */
  63. /*===========================================================================*/
  64. /**
  65. * @brief Type of an IN endpoint state structure.
  66. */
  67. typedef struct {
  68. /**
  69. * @brief Requested transmit transfer size.
  70. */
  71. size_t txsize;
  72. /**
  73. * @brief Transmitted bytes so far.
  74. */
  75. size_t txcnt;
  76. /**
  77. * @brief Pointer to the transmission linear buffer.
  78. */
  79. const uint8_t *txbuf;
  80. #if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  81. /**
  82. * @brief Waiting thread.
  83. */
  84. thread_reference_t thread;
  85. #endif
  86. /* End of the mandatory fields.*/
  87. } USBInEndpointState;
  88. /**
  89. * @brief Type of an OUT endpoint state structure.
  90. */
  91. typedef struct {
  92. /**
  93. * @brief Requested receive transfer size.
  94. */
  95. size_t rxsize;
  96. /**
  97. * @brief Received bytes so far.
  98. */
  99. size_t rxcnt;
  100. /**
  101. * @brief Pointer to the receive linear buffer.
  102. */
  103. uint8_t *rxbuf;
  104. #if (USB_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  105. /**
  106. * @brief Waiting thread.
  107. */
  108. thread_reference_t thread;
  109. #endif
  110. /* End of the mandatory fields.*/
  111. } USBOutEndpointState;
  112. /**
  113. * @brief Type of an USB endpoint configuration structure.
  114. * @note Platform specific restrictions may apply to endpoints.
  115. */
  116. typedef struct {
  117. /**
  118. * @brief Type and mode of the endpoint.
  119. */
  120. uint32_t ep_mode;
  121. /**
  122. * @brief Setup packet notification callback.
  123. * @details This callback is invoked when a setup packet has been
  124. * received.
  125. * @post The application must immediately call @p usbReadPacket() in
  126. * order to access the received packet.
  127. * @note This field is only valid for @p USB_EP_MODE_TYPE_CTRL
  128. * endpoints, it should be set to @p NULL for other endpoint
  129. * types.
  130. */
  131. usbepcallback_t setup_cb;
  132. /**
  133. * @brief IN endpoint notification callback.
  134. * @details This field must be set to @p NULL if the IN endpoint is not
  135. * used.
  136. */
  137. usbepcallback_t in_cb;
  138. /**
  139. * @brief OUT endpoint notification callback.
  140. * @details This field must be set to @p NULL if the OUT endpoint is not
  141. * used.
  142. */
  143. usbepcallback_t out_cb;
  144. /**
  145. * @brief IN endpoint maximum packet size.
  146. * @details This field must be set to zero if the IN endpoint is not
  147. * used.
  148. */
  149. uint16_t in_maxsize;
  150. /**
  151. * @brief OUT endpoint maximum packet size.
  152. * @details This field must be set to zero if the OUT endpoint is not
  153. * used.
  154. */
  155. uint16_t out_maxsize;
  156. /**
  157. * @brief @p USBEndpointState associated to the IN endpoint.
  158. * @details This structure maintains the state of the IN endpoint.
  159. */
  160. USBInEndpointState *in_state;
  161. /**
  162. * @brief @p USBEndpointState associated to the OUT endpoint.
  163. * @details This structure maintains the state of the OUT endpoint.
  164. */
  165. USBOutEndpointState *out_state;
  166. /* End of the mandatory fields.*/
  167. } USBEndpointConfig;
  168. /**
  169. * @brief Type of an USB driver configuration structure.
  170. */
  171. typedef struct {
  172. /**
  173. * @brief USB events callback.
  174. * @details This callback is invoked when an USB driver event is registered.
  175. */
  176. usbeventcb_t event_cb;
  177. /**
  178. * @brief Device GET_DESCRIPTOR request callback.
  179. * @note This callback is mandatory and cannot be set to @p NULL.
  180. */
  181. usbgetdescriptor_t get_descriptor_cb;
  182. /**
  183. * @brief Requests hook callback.
  184. * @details This hook allows to be notified of standard requests or to
  185. * handle non standard requests.
  186. */
  187. usbreqhandler_t requests_hook_cb;
  188. /**
  189. * @brief Start Of Frame callback.
  190. */
  191. usbcallback_t sof_cb;
  192. /* End of the mandatory fields.*/
  193. } USBConfig;
  194. /**
  195. * @brief Structure representing an USB driver.
  196. */
  197. struct USBDriver {
  198. /**
  199. * @brief Driver state.
  200. */
  201. usbstate_t state;
  202. /**
  203. * @brief Current configuration data.
  204. */
  205. const USBConfig *config;
  206. /**
  207. * @brief Bit map of the transmitting IN endpoints.
  208. */
  209. uint16_t transmitting;
  210. /**
  211. * @brief Bit map of the receiving OUT endpoints.
  212. */
  213. uint16_t receiving;
  214. /**
  215. * @brief Active endpoints configurations.
  216. */
  217. const USBEndpointConfig *epc[USB_MAX_ENDPOINTS + 1];
  218. /**
  219. * @brief Fields available to user, it can be used to associate an
  220. * application-defined handler to an IN endpoint.
  221. * @note The base index is one, the endpoint zero does not have a
  222. * reserved element in this array.
  223. */
  224. void *in_params[USB_MAX_ENDPOINTS];
  225. /**
  226. * @brief Fields available to user, it can be used to associate an
  227. * application-defined handler to an OUT endpoint.
  228. * @note The base index is one, the endpoint zero does not have a
  229. * reserved element in this array.
  230. */
  231. void *out_params[USB_MAX_ENDPOINTS];
  232. /**
  233. * @brief Endpoint 0 state.
  234. */
  235. usbep0state_t ep0state;
  236. /**
  237. * @brief Next position in the buffer to be transferred through endpoint 0.
  238. */
  239. uint8_t *ep0next;
  240. /**
  241. * @brief Number of bytes yet to be transferred through endpoint 0.
  242. */
  243. size_t ep0n;
  244. /**
  245. * @brief Endpoint 0 end transaction callback.
  246. */
  247. usbcallback_t ep0endcb;
  248. /**
  249. * @brief Setup packet buffer.
  250. */
  251. uint8_t setup[8];
  252. /**
  253. * @brief Current USB device status.
  254. */
  255. uint16_t status;
  256. /**
  257. * @brief Assigned USB address.
  258. */
  259. uint8_t address;
  260. /**
  261. * @brief Current USB device configuration.
  262. */
  263. uint8_t configuration;
  264. /**
  265. * @brief State of the driver when a suspend happened.
  266. */
  267. usbstate_t saved_state;
  268. #if defined(USB_DRIVER_EXT_FIELDS)
  269. USB_DRIVER_EXT_FIELDS
  270. #endif
  271. /* End of the mandatory fields.*/
  272. };
  273. /*===========================================================================*/
  274. /* Driver macros. */
  275. /*===========================================================================*/
  276. /**
  277. * @brief Returns the current frame number.
  278. *
  279. * @param[in] usbp pointer to the @p USBDriver object
  280. * @return The current frame number.
  281. *
  282. * @notapi
  283. */
  284. #define usb_lld_get_frame_number(usbp) 0
  285. /**
  286. * @brief Returns the exact size of a receive transaction.
  287. * @details The received size can be different from the size specified in
  288. * @p usbStartReceiveI() because the last packet could have a size
  289. * different from the expected one.
  290. * @pre The OUT endpoint must have been configured in transaction mode
  291. * in order to use this function.
  292. *
  293. * @param[in] usbp pointer to the @p USBDriver object
  294. * @param[in] ep endpoint number
  295. * @return Received data size.
  296. *
  297. * @notapi
  298. */
  299. #define usb_lld_get_transaction_size(usbp, ep) \
  300. ((usbp)->epc[ep]->out_state->rxcnt)
  301. /**
  302. * @brief Connects the USB device.
  303. *
  304. * @api
  305. */
  306. #define usb_lld_connect_bus(usbp)
  307. /**
  308. * @brief Disconnect the USB device.
  309. *
  310. * @api
  311. */
  312. #define usb_lld_disconnect_bus(usbp)
  313. /**
  314. * @brief Start of host wake-up procedure.
  315. *
  316. * @notapi
  317. */
  318. #define usb_lld_wakeup_host(usbp)
  319. /*===========================================================================*/
  320. /* External declarations. */
  321. /*===========================================================================*/
  322. #if (PLATFORM_USB_USE_USB1 == TRUE) && !defined(__DOXYGEN__)
  323. extern USBDriver USBD1;
  324. #endif
  325. #ifdef __cplusplus
  326. extern "C" {
  327. #endif
  328. void usb_lld_init(void);
  329. void usb_lld_start(USBDriver *usbp);
  330. void usb_lld_stop(USBDriver *usbp);
  331. void usb_lld_reset(USBDriver *usbp);
  332. void usb_lld_set_address(USBDriver *usbp);
  333. void usb_lld_init_endpoint(USBDriver *usbp, usbep_t ep);
  334. void usb_lld_disable_endpoints(USBDriver *usbp);
  335. usbepstatus_t usb_lld_get_status_in(USBDriver *usbp, usbep_t ep);
  336. usbepstatus_t usb_lld_get_status_out(USBDriver *usbp, usbep_t ep);
  337. void usb_lld_read_setup(USBDriver *usbp, usbep_t ep, uint8_t *buf);
  338. void usb_lld_prepare_receive(USBDriver *usbp, usbep_t ep);
  339. void usb_lld_prepare_transmit(USBDriver *usbp, usbep_t ep);
  340. void usb_lld_start_out(USBDriver *usbp, usbep_t ep);
  341. void usb_lld_start_in(USBDriver *usbp, usbep_t ep);
  342. void usb_lld_stall_out(USBDriver *usbp, usbep_t ep);
  343. void usb_lld_stall_in(USBDriver *usbp, usbep_t ep);
  344. void usb_lld_clear_out(USBDriver *usbp, usbep_t ep);
  345. void usb_lld_clear_in(USBDriver *usbp, usbep_t ep);
  346. #ifdef __cplusplus
  347. }
  348. #endif
  349. #endif /* HAL_USE_USB == TRUE */
  350. #endif /* HAL_USB_LLD_H */
  351. /** @} */