1
0

usbd_core.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. .*/
  15. #ifndef _USBD_CORE_H_
  16. #define _USBD_CORE_H_
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. /**\addtogroup USBD_HW_CORE USB Device HW driver and core API
  21. * @{ */
  22. #if defined(__DOXYGEN__)
  23. /**\name Compile-time control macros
  24. * @{ */
  25. #define USBD_SOF_DISABLED /**<\brief Disables SOF handling.*/
  26. #define USBD_VBUS_DETECT /**<\brief Enables Vbus detection for V2 driver.*/
  27. #define USBD_DP_PORT /**<\brief DP pullup port for F103/F303 driver.*/
  28. #define USBD_DP_PIN /**<\brief DP pullup pin for F103/F303 driver.*/
  29. /** @} */
  30. #endif
  31. /**\addtogroup USBD_HW USB hardware driver
  32. * \brief Contains HW driver API
  33. * @{ */
  34. /**\anchor USB_EVENTS
  35. * \name USB device events
  36. * @{ */
  37. #define usbd_evt_reset 0 /**<\brief Reset.*/
  38. #define usbd_evt_sof 1 /**<\brief Start of frame.*/
  39. #define usbd_evt_susp 2 /**<\brief Suspend.*/
  40. #define usbd_evt_wkup 3 /**<\brief Wakeup.*/
  41. #define usbd_evt_eptx 4 /**<\brief Data packet transmitted*/
  42. #define usbd_evt_eprx 5 /**<\brief Data packet received.*/
  43. #define usbd_evt_epsetup 6 /**<\brief Setup packet received.*/
  44. #define usbd_evt_error 7 /**<\brief Data error.*/
  45. #define usbd_evt_count 8
  46. /** @} */
  47. /**\anchor USB_LANES_STATUS
  48. * \name USB lanes connection states
  49. * @{ */
  50. #define usbd_lane_unk 0 /**<\brief Unknown or proprietary charger.*/
  51. #define usbd_lane_dsc 1 /**<\brief Lanes disconnected.*/
  52. #define usbd_lane_sdp 2 /**<\brief Lanes connected to standard downstream port.*/
  53. #define usbd_lane_cdp 3 /**<\brief Lanes connected to charging downstream port.*/
  54. #define usbd_lane_dcp 4 /**<\brief Lanes connected to dedicated charging port.*/
  55. /** @} */
  56. /** \name USB HW capabilities
  57. * @{ */
  58. #define USBD_HW_ADDRFST (1 << 0) /**<\brief Set address before STATUS_OUT.*/
  59. #define USBD_HW_BC (1 << 1) /**<\brief Battery charging detection supported.*/
  60. /** @} */
  61. /** @} */
  62. /**\addtogroup USBD_CORE USB device core
  63. * \brief Contains core API
  64. * @{ */
  65. #define USB_EPTYPE_DBLBUF 0x04 /**<\brief Doublebuffered endpoint (bulk endpoint only).*/
  66. /**\name bmRequestType bitmapped field
  67. * @{ */
  68. #define USB_REQ_DIRECTION (1 << 7) /**<\brief Request direction mask.*/
  69. #define USB_REQ_HOSTTODEV (0 << 7) /**<\brief Request direction is HOST to DEVICE.*/
  70. #define USB_REQ_DEVTOHOST (1 << 7) /**<\brief Request direction is DEVICE to HOST.*/
  71. #define USB_REQ_TYPE (3 << 5) /**<\brief Request type mask.*/
  72. #define USB_REQ_STANDARD (0 << 5) /**<\brief Standard request.*/
  73. #define USB_REQ_CLASS (1 << 5) /**<\brief Class specified request.*/
  74. #define USB_REQ_VENDOR (2 << 5) /**<\brief Vendor specified request.*/
  75. #define USB_REQ_RECIPIENT (3 << 0) /**<\brief Request recipient mask.*/
  76. #define USB_REQ_DEVICE (0 << 0) /**<\brief Request to device.*/
  77. #define USB_REQ_INTERFACE (1 << 0) /**<\brief Request to interface.*/
  78. #define USB_REQ_ENDPOINT (2 << 0) /**<\brief Request to endpoint.*/
  79. #define USB_REQ_OTHER (3 << 0) /**<\brief Other request.*/
  80. /** @} */
  81. #if !defined(__ASSEMBLER__)
  82. /**\brief USB device machine states.*/
  83. enum usbd_machine_state {
  84. usbd_state_disabled,
  85. usbd_state_disconnected,
  86. usbd_state_default, /**< Default.*/
  87. usbd_state_addressed, /**< Addressed.*/
  88. usbd_state_configured, /**< Configured.*/
  89. };
  90. /**\brief USB device control endpoint machine state.*/
  91. enum usbd_ctl_state {
  92. usbd_ctl_idle, /**<\brief Idle stage. Awaiting for SETUP packet.*/
  93. usbd_ctl_rxdata, /**<\brief RX stage. Receiving DATA-OUT payload.*/
  94. usbd_ctl_txdata, /**<\brief TX stage. Transmitting DATA-IN payload.*/
  95. usbd_ctl_ztxdata, /**<\brief TX stage. Transmitting DATA-IN payload. Zero length
  96. * packet maybe required..*/
  97. usbd_ctl_lastdata, /**<\brief TX stage. Last DATA-IN packed passed to buffer. Awaiting
  98. * for the TX completion.*/
  99. usbd_ctl_statusin, /**<\brief STATUS-IN stage.*/
  100. usbd_ctl_statusout, /**<\brief STATUS-OUT stage.*/
  101. };
  102. /**\brief Asynchronous device control commands.*/
  103. enum usbd_commands {
  104. usbd_cmd_enable, /**<\brief Enables device.*/
  105. usbd_cmd_disable, /**<\brief Disables device.*/
  106. usbd_cmd_connect, /**<\brief Connects device to host.*/
  107. usbd_cmd_disconnect, /**<\brief Disconnects device from host.*/
  108. usbd_cmd_reset, /**<\brief Resets device.*/
  109. };
  110. /**\brief Reporting status results.*/
  111. typedef enum _usbd_respond {
  112. usbd_fail, /**<\brief Function has an error, STALLPID will be issued.*/
  113. usbd_ack, /**<\brief Function completes request accepted ZLP or data will be send.*/
  114. usbd_nak, /**<\brief Function is busy. NAK handshake.*/
  115. } usbd_respond;
  116. typedef struct _usbd_device usbd_device;
  117. /**\brief Represents generic USB control request.*/
  118. typedef struct {
  119. uint8_t bmRequestType; /**<\brief This bitmapped field identifies the characteristics of
  120. * the specific request.*/
  121. uint8_t bRequest; /**<\brief This field specifies the particular request.*/
  122. uint16_t wValue; /**<\brief It is used to pass a parameter to the device, specific to
  123. * the request.*/
  124. uint16_t wIndex; /**<\brief It is used to pass a parameter to the device, specific to
  125. * the request.*/
  126. uint16_t wLength; /**<\brief This field specifies the length of the data transferred
  127. * during the second phase of the control transfer.*/
  128. uint8_t data[]; /**<\brief Data payload.*/
  129. } usbd_ctlreq;
  130. /** USB device status data.*/
  131. typedef struct {
  132. void *data_buf; /**<\brief Pointer to data buffer used for control requests.*/
  133. void *data_ptr; /**<\brief Pointer to current data for control request.*/
  134. uint16_t data_count; /**<\brief Count remained data for control request.*/
  135. uint16_t data_maxsize; /**<\brief Size of the data buffer for control requests.*/
  136. uint8_t ep0size; /**<\brief Size of the control endpoint.*/
  137. uint8_t device_cfg; /**<\brief Current device configuration number.*/
  138. uint8_t device_state; /**<\brief Current \ref usbd_machine_state.*/
  139. uint8_t control_state; /**<\brief Current \ref usbd_ctl_state.*/
  140. } usbd_status;
  141. /**\brief Generic USB device event callback for events and endpoints processing
  142. * \param[in] dev pointer to USB device
  143. * \param event \ref USB_EVENTS "USB event"
  144. * \param ep active endpoint number
  145. * \note endpoints with same indexes i.e. 0x01 and 0x81 shares same callback.
  146. */
  147. typedef void (*usbd_evt_callback)(usbd_device *dev, uint8_t event, uint8_t ep);
  148. /**\brief USB control transfer completed callback function.
  149. * \param[in] dev pointer to USB device
  150. * \param[in] req pointer to usb request structure
  151. * \note usbd_device->complete_callback will be set to NULL after this callback completion.
  152. */
  153. typedef void (*usbd_rqc_callback)(usbd_device *dev, usbd_ctlreq *req);
  154. /**\brief USB control callback function.
  155. * \details Uses for the control request processing.
  156. * Some requests will be handled by core if callback don't process it (returns FALSE).
  157. * If request was not processed STALL PID will be issued.
  158. * - GET_CONFIGURATION
  159. * - SET_CONFIGURATION (passes to \ref usbd_cfg_callback)
  160. * - GET_DESCRIPTOR (passes to \ref usbd_dsc_callback)
  161. * - GET_STATUS
  162. * - SET_FEATURE, CLEAR_FEATURE (endpoints only)
  163. * - SET_ADDRESS
  164. * \param[in] dev points to USB device
  165. * \param[in] req points to usb control request
  166. * \param[out] *callback USB control transfer completion callback, default is NULL (no callback)
  167. * \return usbd_respond status.
  168. */
  169. typedef usbd_respond (*usbd_ctl_callback)(usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback);
  170. /**\brief USB get descriptor callback function
  171. * \details Called when GET_DESCRIPTOR request issued
  172. * \param[in] req pointer to usb control request structure
  173. * \param[in,out] address pointer to the descriptor in memory. Points to req->data by default. You
  174. * can use this buffer.
  175. * \param[in,out] dsize descriptor size. maximum buffer size by default.
  176. * \return usbd_ack if you passed the correct descriptor, usbd_fail otherwise.
  177. */
  178. typedef usbd_respond (*usbd_dsc_callback)(usbd_ctlreq *req, void **address, uint16_t *dsize);
  179. /**\brief USB set configuration callback function
  180. * \details called when SET_CONFIGURATION request issued
  181. * \param[in] dev pointer to USB device
  182. * \param[in] cfg configuration number.
  183. * \note if config is 0 device endpoints should be de-configured
  184. * \return TRUE if success
  185. */
  186. typedef usbd_respond (*usbd_cfg_callback)(usbd_device *dev, uint8_t cfg);
  187. /** @} */
  188. /**\addtogroup USBD_HW
  189. * @{ */
  190. /**\brief Enables or disables USB hardware
  191. * \param enable Enables USB when TRUE disables otherwise.
  192. */
  193. typedef void (*usbd_hw_enable)(bool enable);
  194. /**\brief Resets USB hardware.*/
  195. typedef void (*usbd_hw_reset)(void);
  196. /** Connects or disconnects USB hardware to/from usb host
  197. * \param connect Connects USB to host if TRUE, disconnects otherwise
  198. * \return lanes connection status.
  199. */
  200. typedef uint8_t (*usbd_hw_connect)(bool connect);
  201. /**\brief Sets USB hardware address
  202. * \param address USB address
  203. */
  204. typedef void (*usbd_hw_setaddr)(uint8_t address);
  205. /**\brief Configures endpoint
  206. * \param ep endpoint address. Use USB_EPDIR_ macros to set endpoint direction
  207. * \param eptype endpoint type. Use USB_EPTYPE_* macros.
  208. * \param epsize endpoint size in bytes
  209. * \return TRUE if success
  210. */
  211. typedef bool (*usbd_hw_ep_config)(uint8_t ep, uint8_t eptype, uint16_t epsize);
  212. /**\brief De-configures, cleans and disables endpoint
  213. * \param ep endpoint index
  214. * \note if you have two one-direction single-buffered endpoints with same index (i.e. 0x02 and 0x82)
  215. * both will be deconfigured.
  216. */
  217. typedef void (*usbd_hw_ep_deconfig)(uint8_t ep);
  218. /**\brief Reads data from OUT or control endpoint
  219. * \param ep endpoint index, should belong to OUT or CONTROL endpoint.
  220. * \param buf pointer to read buffer
  221. * \param blen size of the read buffer in bytes
  222. * \return size of the actually received data, -1 on error.
  223. */
  224. typedef int32_t (*usbd_hw_ep_read)(uint8_t ep, void *buf, uint16_t blen);
  225. /**\brief Writes data to IN or control endpoint
  226. * \param ep endpoint index, hould belong to IN or CONTROL endpoint
  227. * \param buf pointer to data buffer
  228. * \param blen size of data will be written
  229. * \return number of written bytes
  230. */
  231. typedef int32_t (*usbd_hw_ep_write)(uint8_t ep, void *buf, uint16_t blen);
  232. /** Stalls and unstalls endpoint
  233. * \param ep endpoint address
  234. * \param stall endpoint will be stalled if TRUE and unstalled otherwise.
  235. * \note Has no effect on inactive endpoints.
  236. */
  237. typedef void (*usbd_hw_ep_setstall)(uint8_t ep, bool stall);
  238. /**\brief Checks endpoint for stalled state
  239. * \param ep endpoint address
  240. * \return TRUE if endpoint is stalled
  241. */
  242. typedef bool (*usbd_hw_ep_isstalled)(uint8_t ep);
  243. /**\brief Polls USB hardware for the events
  244. * \param[in] dev pointer to usb device structure
  245. * \param callback callback to event processing subroutine
  246. */
  247. typedef void (*usbd_hw_poll)(usbd_device *dev, usbd_evt_callback callback);
  248. /**\brief Gets frame number from usb hardware.*/
  249. typedef uint16_t (*usbd_hw_get_frameno)(void);
  250. /**\brief Makes a string descriptor contains unique serial number from hardware ID's
  251. * \param[in] buffer pointer to buffer for the descriptor
  252. * \return of the descriptor in bytes
  253. */
  254. typedef uint16_t (*usbd_hw_get_serialno)(void *buffer);
  255. /**\brief Represents a hardware USB driver call table.*/
  256. struct usbd_driver {
  257. uint32_t caps; /**<\brief HW capabilities */
  258. usbd_hw_enable enable; /**<\copybrief usbd_hw_enable */
  259. usbd_hw_reset reset; /**<\copybrief usbd_hw_reset */
  260. usbd_hw_connect connect; /**<\copybrief usbd_hw_connect */
  261. usbd_hw_setaddr setaddr; /**<\copybrief usbd_hw_setaddr */
  262. usbd_hw_ep_config ep_config; /**<\copybrief usbd_hw_ep_config */
  263. usbd_hw_ep_deconfig ep_deconfig; /**<\copybrief usbd_hw_ep_deconfig */
  264. usbd_hw_ep_read ep_read; /**<\copybrief usbd_hw_ep_read */
  265. usbd_hw_ep_write ep_write; /**<\copybrief usbd_hw_ep_write */
  266. usbd_hw_ep_setstall ep_setstall; /**<\copybrief usbd_hw_ep_setstall */
  267. usbd_hw_ep_isstalled ep_isstalled; /**<\copybrief usbd_hw_ep_isstalled */
  268. usbd_hw_poll poll; /**<\copybrief usbd_hw_poll */
  269. usbd_hw_get_frameno frame_no; /**<\copybrief usbd_hw_get_frameno */
  270. usbd_hw_get_serialno get_serialno_desc; /**<\copybrief usbd_hw_get_serialno */
  271. };
  272. /** @} */
  273. /**\addtogroup USBD_CORE
  274. * @{ */
  275. /**\brief Represents a USB device data.*/
  276. struct _usbd_device {
  277. const struct usbd_driver *driver; /**<\copybrief usbd_driver */
  278. usbd_ctl_callback control_callback; /**<\copybrief usbd_ctl_callback */
  279. usbd_rqc_callback complete_callback; /**<\copybrief usbd_rqc_callback */
  280. usbd_cfg_callback config_callback; /**<\copybrief usbd_cfg_callback */
  281. usbd_dsc_callback descriptor_callback; /**<\copybrief usbd_dsc_callback */
  282. usbd_evt_callback events[usbd_evt_count]; /**<\brief array of the event callbacks.*/
  283. usbd_evt_callback endpoint[8]; /**<\brief array of the endpoint callbacks.*/
  284. usbd_status status; /**<\copybrief usbd_status */
  285. };
  286. /**\brief Initializes device structure
  287. * \param dev USB device that will be initialized
  288. * \param drv Pointer to hardware driver
  289. * \param ep0size Control endpoint 0 size
  290. * \param buffer Pointer to control request data buffer (32-bit aligned)
  291. * \param bsize Size of the data buffer
  292. */
  293. inline static void usbd_init(usbd_device *dev, const struct usbd_driver *drv,
  294. const uint8_t ep0size, uint32_t *buffer, const uint16_t bsize) {
  295. dev->driver = drv;
  296. dev->status.ep0size = ep0size;
  297. dev->status.data_ptr = buffer;
  298. dev->status.data_buf = buffer;
  299. dev->status.data_maxsize = bsize - __builtin_offsetof(usbd_ctlreq, data);
  300. }
  301. /**\brief Polls USB for events
  302. * \param dev Pointer to device structure
  303. * \note can be called as from main routine as from USB interrupt
  304. */
  305. void usbd_poll(usbd_device *dev);
  306. /**\brief Asynchronous device control
  307. * \param dev dev usb device \ref _usbd_device
  308. * \param cmd Asynchronous control command
  309. */
  310. void usbd_control(usbd_device *dev, enum usbd_commands cmd) __attribute__((deprecated));
  311. /**\brief Register callback for all control requests
  312. * \param dev usb device \ref _usbd_device
  313. * \param callback user control callback \ref usbd_ctl_callback
  314. */
  315. inline static void usbd_reg_control(usbd_device *dev, usbd_ctl_callback callback) {
  316. dev->control_callback = callback;
  317. }
  318. /**\brief Register callback for SET_CONFIG control request
  319. * \param dev dev usb device \ref _usbd_device
  320. * \param callback pointer to user \ref usbd_cfg_callback
  321. */
  322. inline static void usbd_reg_config(usbd_device *dev, usbd_cfg_callback callback) {
  323. dev->config_callback = callback;
  324. }
  325. /**\brief Register callback for GET_DESCRIPTOR control request
  326. * \param dev dev usb device \ref _usbd_device
  327. * \param callback pointer to user \ref usbd_ctl_callback
  328. */
  329. inline static void usbd_reg_descr(usbd_device *dev, usbd_dsc_callback callback) {
  330. dev->descriptor_callback = callback;
  331. }
  332. /**\brief Configure endpoint
  333. * \param dev dev usb device \ref _usbd_device
  334. * \copydetails usbd_hw_ep_config
  335. */
  336. inline static bool usbd_ep_config(usbd_device *dev, uint8_t ep, uint8_t eptype, uint16_t epsize) {
  337. return dev->driver->ep_config(ep, eptype, epsize);
  338. }
  339. /**\brief Deconfigure endpoint
  340. * \param dev dev usb device \ref _usbd_device
  341. * \copydetails usbd_hw_ep_deconfig
  342. */
  343. inline static void usbd_ep_deconfig(usbd_device *dev, uint8_t ep) {
  344. dev->driver->ep_deconfig(ep);
  345. }
  346. /**\brief Register endpoint callback
  347. * \param dev dev usb device \ref _usbd_device
  348. * \param ep endpoint index
  349. * \param callback pointer to user \ref usbd_evt_callback callback for endpoint events
  350. */
  351. inline static void usbd_reg_endpoint(usbd_device *dev, uint8_t ep, usbd_evt_callback callback) {
  352. dev->endpoint[ep & 0x07] = callback;
  353. }
  354. /**\brief Registers event callback
  355. * \param dev dev usb device \ref _usbd_device
  356. * \param evt device \ref USB_EVENTS "event" wants to be registered
  357. * \param callback pointer to user \ref usbd_evt_callback for this event
  358. */
  359. inline static void usbd_reg_event(usbd_device *dev, uint8_t evt, usbd_evt_callback callback) {
  360. dev->events[evt] = callback;
  361. }
  362. /**\brief Write data to endpoint
  363. * \param dev dev usb device \ref _usbd_device
  364. * \copydetails usbd_hw_ep_write
  365. */
  366. inline static int32_t usbd_ep_write(usbd_device *dev, uint8_t ep, void *buf, uint16_t blen) {
  367. return dev->driver->ep_write(ep, buf, blen);
  368. }
  369. /**\brief Read data from endpoint
  370. * \param dev dev usb device \ref _usbd_device
  371. * \copydetails usbd_hw_ep_read
  372. */
  373. inline static int32_t usbd_ep_read(usbd_device *dev, uint8_t ep, void *buf, uint16_t blen) {
  374. return dev->driver->ep_read(ep, buf, blen);
  375. }
  376. /**\brief Stall endpoint
  377. * \param dev dev usb device \ref _usbd_device
  378. * \param ep endpoint address
  379. */
  380. inline static void usbd_ep_stall(usbd_device *dev, uint8_t ep) {
  381. dev->driver->ep_setstall(ep, 1);
  382. }
  383. /**\brief Unstall endpoint
  384. * \param dev dev usb device \ref _usbd_device
  385. * \param ep endpoint address
  386. */
  387. inline static void usbd_ep_unstall(usbd_device *dev, uint8_t ep) {
  388. dev->driver->ep_setstall(ep, 0);
  389. }
  390. /**\brief Enables or disables USB hardware
  391. * \param dev dev usb device \ref _usbd_device
  392. * \param enable Enables USB when TRUE disables otherwise
  393. */
  394. inline static void usbd_enable(usbd_device *dev, bool enable) {
  395. dev->driver->enable(enable);
  396. }
  397. /**\brief Connects or disconnects USB hardware to/from usb host
  398. * \param dev dev usb device \ref _usbd_device
  399. * \param connect Connects USB to host if TRUE, disconnects otherwise
  400. * \return lanes connection status. \ref USB_LANES_STATUS
  401. */
  402. inline static uint8_t usbd_connect(usbd_device *dev, bool connect) {
  403. return dev->driver->connect(connect);
  404. }
  405. #endif //(__ASSEMBLER__)
  406. /** @} */
  407. /** @} */
  408. #if defined(__cplusplus)
  409. }
  410. #endif
  411. #endif //_USBD_STD_H_