usbd_core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* This file is the part of the LUS32 project
  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. /** \file usbd_core.h
  16. * \brief Core and hardware driver framework.
  17. * \author Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  18. * \version 1.0
  19. * \copyright Apache License, Version 2.0
  20. */
  21. #ifndef _USBD_CORE_H_
  22. #define _USBD_CORE_H_
  23. #if defined(__cplusplus)
  24. extern "C" {
  25. #endif
  26. /** \addtogroup USBD_CORE USB device core
  27. * \brief Contains core and hardware driver framework definitions
  28. * @{ */
  29. #define USB_EPTYPE_DBLBUF 0x04 /**< indicates a doublebuffered endpoint (bulk endpoint only) */
  30. #define USB_EPDIR_IN 0x00 /**< indicates host-to-device endpoint direction */
  31. #define USB_EPDIR_OUT 0x80 /**< indicates device-to-host endpoint direction */
  32. /** \name bmRequestType bitmapped field
  33. * @{ */
  34. #define USB_REQ_DIRECTION (1 << 7)
  35. #define USB_REQ_HOSTTODEV (0 << 7)
  36. #define USB_REQ_DEVTOHOST (1 << 7)
  37. #define USB_REQ_TYPE (3 << 5)
  38. #define USB_REQ_STANDARD (0 << 5)
  39. #define USB_REQ_CLASS (1 << 5)
  40. #define USB_REQ_VENDOR (2 << 5)
  41. #define USB_REQ_RECIPIENT (3 << 0)
  42. #define USB_REQ_DEVICE (0 << 0)
  43. #define USB_REQ_INTERFACE (1 << 0)
  44. #define USB_REQ_ENDPOINT (2 << 0)
  45. #define USB_REQ_OTHER (3 << 0)
  46. /** @} */
  47. #if defined(__ASSEMBLER__)
  48. #define usbd_evt_reset 0
  49. #define usbd_evt_sof 1
  50. #define usbd_evt_susp 2
  51. #define usbd_evt_wkup 3
  52. #define usbd_evt_eptx 4
  53. #define usbd_evt_eprx 5
  54. #define usbd_evt_epsetup 6
  55. #define usbd_evt_error 7
  56. #define usbd_evt_esof 8
  57. #else
  58. /** USB device events */
  59. enum usbd_evt {
  60. usbd_evt_reset, /**< Reset */
  61. usbd_evt_sof, /**< Start Of Frame */
  62. usbd_evt_susp, /**< Suspend */
  63. usbd_evt_wkup, /**< Wakeup */
  64. usbd_evt_eptx, /**< Transmit completed */
  65. usbd_evt_eprx, /**< Data packet received */
  66. usbd_evt_epsetup, /**< Setup packet received */
  67. usbd_evt_error, /**< Data error */
  68. usbd_evt_esof, /**< Missed SOF */
  69. usbd_evt_count, /* this is trick to count qty */
  70. };
  71. /** USB device machine state */
  72. enum usbd_machine_state {
  73. usbd_state_disabled,
  74. usbd_state_disconnected,
  75. usbd_state_default, /**< Default */
  76. usbd_state_addressed, /**< Addressed */
  77. usbd_state_configured, /**< Configured */
  78. };
  79. /** USB device control endpoint machine state */
  80. enum usbd_ctl_state {
  81. usbd_ctl_idle, /**< Idle. Awaiting for SETUP packet */
  82. usbd_ctl_rxdata, /**< RX. Receiving DATA-OUT payload */
  83. usbd_ctl_txdata, /**< TX. Transmitting DATA-IN payload */
  84. usbd_ctl_ztxdata, /**< TX. Transmitting DATA-IN payload. Zero length packet maybe required. */
  85. usbd_ctl_lastdata, /**< TX. Last DATA-IN packed passed to buffer. Awaiting for the TX completion */
  86. usbd_ctl_statusin, /**< STATUS-IN stage */
  87. usbd_ctl_statusout, /**< STATUS-OUT stage */
  88. };
  89. /** Asynchronous device control commands */
  90. enum usbd_commands {
  91. usbd_cmd_enable, /**< Enables device */
  92. usbd_cmd_disable, /**< Disables device */
  93. usbd_cmd_connect, /**< Connects device to host */
  94. usbd_cmd_disconnect, /**< Disconnects device from host */
  95. usbd_cmd_reset, /**< Resets device */
  96. };
  97. /* Reporting status results */
  98. typedef enum _usbd_respond {
  99. usbd_fail, /**< Function has an error, STALLPID will be issued */
  100. usbd_ack, /**< Function completes request accepted ZLP or data will be send */
  101. usbd_nak, /**< Function is busy. NAK handshake */
  102. } usbd_respond;
  103. typedef struct _usbd_device usbd_device;
  104. typedef struct _usbd_ctlreq usbd_ctlreq;
  105. typedef struct _usbd_status usbd_status;
  106. /**\name USB Device user callbacks function prototypes
  107. * @{ */
  108. /** Generic USB device event callback for events and endpoints processing
  109. * \param[in] dev pointer to USB device
  110. * \param event \ref usbd_evt "USB event"
  111. * \param ep active endpoint number
  112. * \note endpoints with same indexes i.e. 0x01 and 0x81 shares same callback.
  113. */
  114. typedef void (*usbd_evt_callback)(usbd_device *dev, uint8_t event, uint8_t ep);
  115. /** USB control transfer completed callback.
  116. * \param[in] dev pointer to USB device
  117. * \param[in] req pointer to usb request structure
  118. * \note When this callback will be completed usbd_device#complete_callback will be reseted to NULL
  119. */
  120. typedef void (*usbd_ctl_complete)(usbd_device *dev, usbd_ctlreq *req);
  121. /** USB control callback.
  122. * \details Uses for the control request processing.
  123. * Some requests will be handled by core if callback don't process it (returns FALSE). If request was not processed STALL PID will be issued.
  124. * - GET_CONFIGURATION
  125. * - SET_CONFIGURATION (passes to \ref usbd_cfg_callback)
  126. * - GET_DESCRIPTOR (passes to \ref usbd_dsc_callback)
  127. * - GET_STATUS
  128. * - SET_FEATURE, CLEAR_FEATURE (endpoints only)
  129. * - SET_ADDRESS
  130. * \param[in] dev points to USB device
  131. * \param[in] req points to usb control request
  132. * \param[out] *callback \ref usbd_ctl_complete "pointer to USB control transfer completed callback", default is NULL (no callback)
  133. * \return usbd_respond status.
  134. */
  135. typedef usbd_respond (*usbd_ctl_callback)(usbd_device *dev, usbd_ctlreq *req, usbd_ctl_complete *callback);
  136. /** USB get descriptor callback
  137. * \details Called when GET_DESCRIPTOR request issued
  138. * \param[in] req pointer to usb control request structure
  139. * \param[in,out] address pointer to the descriptor in memory. Points to req->data by default. You can use this buffer.
  140. * \param[in,out] dsize descriptor size. req->data buffer size by default.
  141. * \return TRUE if you passed the correct descriptor, FALSE otherwise.
  142. */
  143. typedef bool (*usbd_dsc_callback)(usbd_ctlreq *req, void **address, uint16_t *dsize);
  144. /** USB set configuration callback
  145. * \details called when SET_CONFIGURATION request issued
  146. * \param[in] dev pointer to USB device
  147. * \param[in] cfg configuration number.
  148. * \note if config is 0 device endpoints should be de-configured
  149. * \return TRUE if success
  150. */
  151. typedef bool (*usbd_cfg_callback)(usbd_device *dev, uint8_t cfg);
  152. /** @} */
  153. /**\name USB Hardware driver API function prototypes
  154. * @{ */
  155. /** Enables or disables USB hardware
  156. * \param enable Enables USB when TRUE disables otherwise
  157. */
  158. typedef void (*usbd_hw_enable)(bool enable);
  159. /** Resets USB hardware */
  160. typedef void (*usbd_hw_reset)(void);
  161. /** Connects or disconnects USB hardware to/from usb host
  162. * \param connect Connects USB to host if TRUE, disconnects otherwise
  163. */
  164. typedef void (*usbd_hw_connect)(bool connect);
  165. /** Sets USB hardware address
  166. * \param address USB address
  167. */
  168. typedef void (*usbd_hw_setaddr)(uint8_t address);
  169. /** Configures endpoint
  170. * \param ep endpoint address. Use USB_EPDIR_ macros to set endpoint direction
  171. * \param eptype endpoint type. Use USB_EPTYPE_* macros.
  172. * \param epsize endpoint size in bytes
  173. * \return TRUE if success
  174. */
  175. typedef bool (*usbd_hw_ep_config)(uint8_t ep, uint8_t eptype, uint16_t epsize);
  176. /** De-configures, cleans and disables endpoint
  177. * \param ep endpoint index
  178. * \note if you have two one-direction single-buffered endpoints with same index (i.e. 0x02 and 0x82) both will be deconfigured.
  179. */
  180. typedef void (*usbd_hw_ep_deconfig)(uint8_t ep);
  181. /** Reads data from OUT or control endpoint
  182. * \param ep endpoint index, should belong to OUT or CONTROL endpoint.
  183. * \param buf pointer to read buffer
  184. * \param blen size of the read buffer in bytes
  185. * \return size of the actually received data
  186. */
  187. typedef uint16_t (*usbd_hw_ep_read)(uint8_t ep, void *buf, uint16_t blen);
  188. /** Writes data to IN or control endpoint
  189. * \param ep endpoint index, hould belong to IN or CONTROL endpoint
  190. * \param buf pointer to data buffer
  191. * \param blen size of data will be written
  192. * \return number of written bytes
  193. */
  194. typedef uint16_t (*usbd_hw_ep_write)(uint8_t ep, void *buf, uint16_t blen);
  195. /** Stalls and unstalls endpoint
  196. * \param ep endpoint address
  197. * \param stall endpoint will be stalled if TRUE and unstalled otherwise.
  198. * \note Has no effect on inactive endpoints.
  199. */
  200. typedef void (*usbd_hw_ep_setstall)(uint8_t ep, bool stall);
  201. /** Checks endpoint for stalled state
  202. * \param ep endpoint address
  203. * \return TRUE if endpoint is stalled
  204. */
  205. typedef bool (*usbd_hw_ep_isstalled)(uint8_t ep);
  206. /** Polls USB hardware for the events
  207. * \param[in] dev pointer to usb device structure
  208. * \param drv_callback callback to event processing subroutine
  209. */
  210. typedef void (*usbd_hw_poll)(usbd_device *dev, usbd_evt_callback drv_callback);
  211. /** Gets frame number from usb hardware
  212. */
  213. typedef uint16_t (*usbd_hw_get_frameno)(void);
  214. /** Makes a string descriptor contains unique serial number from hardware ID's
  215. * \param[in] buffer pointer to buffer for the descriptor
  216. * \return of the descriptor in bytes
  217. */
  218. typedef uint16_t (*usbd_hw_get_serialno)(void *buffer);
  219. /** @} */
  220. /** Represents generic USB control request */
  221. struct _usbd_ctlreq {
  222. uint8_t bmRequestType; /**< This bitmapped field identifies the characteristics of the specific request. */
  223. uint8_t bRequest; /**< This field specifies the particular request. */
  224. uint16_t wValue; /**< It is used to pass a parameter to the device, specific to the request. */
  225. uint16_t wIndex; /**< It is used to pass a parameter to the device, specific to the request. */
  226. uint16_t wLength; /**< This field specifies the length of the data transferred during the second phase of the control transfer */
  227. uint8_t data[]; /**< Request data payload */
  228. };
  229. /** USB device status data for control endpoint */
  230. struct _usbd_status {
  231. void *data_buf;
  232. void *data_ptr; /**< Pointer to control endpoint current data buffer */
  233. uint16_t data_count; /**< Control endpoint data counter */
  234. uint16_t data_maxsize; /**< Size of the data buffer for control endpoint */
  235. uint8_t ep0size; /**< Size of the control endpoint */
  236. uint8_t device_cfg; /**< Current device configuration ID */
  237. uint8_t device_state; /**< Current \ref usbd_machine_state */
  238. uint8_t control_state; /**< Current \ref usbd_ctl_state */
  239. };
  240. /** Structure represents a hardware USB driver call table */
  241. struct usbd_driver {
  242. usbd_hw_enable enable;
  243. usbd_hw_reset reset;
  244. usbd_hw_connect connect;
  245. usbd_hw_setaddr setaddr;
  246. usbd_hw_ep_config ep_config;
  247. usbd_hw_ep_deconfig ep_deconfig;
  248. usbd_hw_ep_read ep_read;
  249. usbd_hw_ep_write ep_write;
  250. usbd_hw_ep_setstall ep_setstall;
  251. usbd_hw_ep_isstalled ep_isstalled;
  252. usbd_hw_poll poll;
  253. usbd_hw_get_frameno frame_no;
  254. usbd_hw_get_serialno get_serialno_desc;
  255. };
  256. /** Structure represents a USB device data. No other data used by USB core and driver
  257. * \note use helper \ref usbd_device typedef
  258. * \note structure must be aligned
  259. */
  260. struct _usbd_device {
  261. const struct usbd_driver *driver;
  262. usbd_ctl_callback control_callback;
  263. usbd_ctl_complete complete_callback;
  264. usbd_cfg_callback config_callback;
  265. usbd_dsc_callback descriptor_callback;
  266. usbd_evt_callback events[usbd_evt_count]; /**< events callbacks array */
  267. usbd_evt_callback endpoint[8]; /**< endpoint callbacks array for tx, rx and setup events */
  268. usbd_status status;
  269. };
  270. /** Initializes device structure
  271. * \param dev USB device that will be initialized
  272. * \param drv Pointer to hardware driver
  273. * \param ep0size Control endpoint 0 size
  274. * \param buffer Pointer to control request data buffer (32-bit aligned)
  275. * \param bsize Size of the data buffer
  276. */
  277. void usbd_init(usbd_device *dev, const struct usbd_driver *drv, const uint8_t ep0size, uint32_t *buffer, const uint16_t bsize);
  278. /** Polls USB for events
  279. * \param dev Pointer to device structure
  280. * \note can be called as from main routine as from USB interrupt
  281. */
  282. void usbd_poll(usbd_device *dev);
  283. /** Asynchronous device control
  284. * \param dev USB device
  285. * \param cmd control command
  286. */
  287. void usbd_control(usbd_device *dev, enum usbd_commands cmd);
  288. /** \name API macro functions
  289. * @{ */
  290. /** Macro to register control callback
  291. * \param dev pointer to \ref usbd_device structure
  292. * \param cb pointer to user \ref usbd_ctl_callback
  293. */
  294. #define usbd_reg_control(dev, cb) (dev)->control_callback = (cb)
  295. /** Macro to register set configuration callback
  296. * \param dev pointer to \ref usbd_device structire
  297. * \param cb pointer to user \ref usbd_ctl_callback
  298. */
  299. #define usbd_reg_config(dev, cb) (dev)->config_callback = (cb)
  300. /** Macro to register GET_DESCRIPTOR callback
  301. * \param dev USB device
  302. * \param cb callback
  303. */
  304. #define usbd_reg_descr(dev, cb) (dev)->descriptor_callback = (cb)
  305. /** Macro to register endpoint callback
  306. * \param dev pointer to \ref usbd_device structure
  307. * \param ep endpoint index
  308. * \param cb pointer to user \ref usbd_evt_callback callback for endpoint events
  309. */
  310. #define usbd_reg_endpoint(dev, ep, cb) (dev)->endpoint[(ep) & 0x07] = (cb)
  311. /** Macro to registers event callback
  312. * \param dev pointer to \ref usbd_device structure
  313. * \param evt device \ref usbd_evt "event" wants to be registered
  314. * \param cb pointer to user \ref usbd_evt_callback for this event
  315. */
  316. #define usbd_reg_event(dev, evt, cb) (dev)->events[(evt)] = (cb)
  317. /** Macro to configure endpoint
  318. * \param dev pointer to \ref usbd_device structure
  319. * \copydetails usbd_hw_ep_config
  320. */
  321. #define usbd_ep_config(dev, ep, eptype, epsize) (dev)->driver->ep_config((ep), (eptype), (epsize))
  322. /** Macro to deconfigure endpoint
  323. * \param dev pointer to \ref usbd_device structure
  324. * \copydetails usbd_hw_ep_deconfig
  325. */
  326. #define usbd_ep_deconfig(dev, ep) (dev)->driver->ep_deconfig(ep)
  327. /** Macro to write data to endpoint
  328. * \param dev pointer to \ref usbd_device structure
  329. * \copydetails usbd_hw_ep_write
  330. */
  331. #define usbd_ep_write(dev, ep, buf, blen) (dev)->driver->ep_write((ep), (buf), (blen))
  332. /** Macro to read data from endpoint
  333. * \param dev pointer to \ref usbd_device structure
  334. * \copydetails usbd_hw_ep_read
  335. */
  336. #define usbd_ep_read(dev, ep, buffer, length) (dev)->driver->ep_read((ep), (buf), (blen))
  337. /** Macro to stall endpoint
  338. * \param dev pointer to \ref usbd_device structure
  339. * \param ep endpoint address
  340. */
  341. #define usbd_ep_stall(dev, ep) (dev)->driver->ep_setstall((ep), 1)
  342. /** Macro to unstall endpoint
  343. * \param dev pointer to \ref usbd_device structure
  344. * \param ep endpoint address
  345. */
  346. #define usbd_ep_unstall(dev, ep) (dev)->driver->ep_setstall((ep), 0)
  347. /** @} */
  348. #endif //(__ASSEMBLER__)
  349. /**@} */
  350. #if defined(__cplusplus)
  351. }
  352. #endif
  353. #endif //_USBD_STD_H_