My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

udd.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * \file
  3. *
  4. * \brief Common API for USB Device Drivers (UDD)
  5. *
  6. * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef _UDD_H_
  47. #define _UDD_H_
  48. #include "usb_protocol.h"
  49. #include "udc_desc.h"
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /**
  54. * \ingroup usb_device_group
  55. * \defgroup udd_group USB Device Driver (UDD)
  56. *
  57. * The UDD driver provides a low-level abstraction of the device
  58. * controller hardware. Most events coming from the hardware such as
  59. * interrupts, which may cause the UDD to call into the UDC and UDI.
  60. *
  61. * @{
  62. */
  63. //! \brief Endpoint identifier
  64. typedef uint8_t udd_ep_id_t;
  65. //! \brief Endpoint transfer status
  66. //! Returned in parameters of callback register via udd_ep_run routine.
  67. typedef enum {
  68. UDD_EP_TRANSFER_OK = 0,
  69. UDD_EP_TRANSFER_ABORT = 1,
  70. } udd_ep_status_t;
  71. /**
  72. * \brief Global variable to give and record information of the setup request management
  73. *
  74. * This global variable allows to decode and response a setup request.
  75. * It can be updated by udc_process_setup() from UDC or *setup() from UDIs.
  76. */
  77. typedef struct {
  78. //! Data received in USB SETUP packet
  79. //! Note: The swap of "req.wValues" from uin16_t to le16_t is done by UDD.
  80. usb_setup_req_t req;
  81. //! Point to buffer to send or fill with data following SETUP packet
  82. //! This buffer must be word align for DATA IN phase (use prefix COMPILER_WORD_ALIGNED for buffer)
  83. uint8_t *payload;
  84. //! Size of buffer to send or fill, and content the number of byte transfered
  85. uint16_t payload_size;
  86. //! Callback called after reception of ZLP from setup request
  87. void (*callback)(void);
  88. //! Callback called when the buffer given (.payload) is full or empty.
  89. //! This one return false to abort data transfer, or true with a new buffer in .payload.
  90. bool (*over_under_run)(void);
  91. } udd_ctrl_request_t;
  92. extern udd_ctrl_request_t udd_g_ctrlreq;
  93. //! Return true if the setup request \a udd_g_ctrlreq indicates IN data transfer
  94. #define Udd_setup_is_in() \
  95. (USB_REQ_DIR_IN == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK))
  96. //! Return true if the setup request \a udd_g_ctrlreq indicates OUT data transfer
  97. #define Udd_setup_is_out() \
  98. (USB_REQ_DIR_OUT == (udd_g_ctrlreq.req.bmRequestType & USB_REQ_DIR_MASK))
  99. //! Return the type of the SETUP request \a udd_g_ctrlreq. \see usb_reqtype.
  100. #define Udd_setup_type() \
  101. (udd_g_ctrlreq.req.bmRequestType & USB_REQ_TYPE_MASK)
  102. //! Return the recipient of the SETUP request \a udd_g_ctrlreq. \see usb_recipient
  103. #define Udd_setup_recipient() \
  104. (udd_g_ctrlreq.req.bmRequestType & USB_REQ_RECIP_MASK)
  105. /**
  106. * \brief End of halt callback function type.
  107. * Registered by routine udd_ep_wait_stall_clear()
  108. * Callback called when endpoint stall is cleared.
  109. */
  110. typedef void (*udd_callback_halt_cleared_t)(void);
  111. /**
  112. * \brief End of transfer callback function type.
  113. * Registered by routine udd_ep_run()
  114. * Callback called by USB interrupt after data transfer or abort (reset,...).
  115. *
  116. * \param status UDD_EP_TRANSFER_OK, if transfer is complete
  117. * \param status UDD_EP_TRANSFER_ABORT, if transfer is aborted
  118. * \param n number of data transfered
  119. */
  120. typedef void (*udd_callback_trans_t) (udd_ep_status_t status,
  121. iram_size_t nb_transfered, udd_ep_id_t ep);
  122. /**
  123. * \brief Authorizes the VBUS event
  124. *
  125. * \return true, if the VBUS monitoring is possible.
  126. */
  127. bool udd_include_vbus_monitoring(void);
  128. /**
  129. * \brief Enables the USB Device mode
  130. */
  131. void udd_enable(void);
  132. /**
  133. * \brief Disables the USB Device mode
  134. */
  135. void udd_disable(void);
  136. /**
  137. * \brief Attach device to the bus when possible
  138. *
  139. * \warning If a VBus control is included in driver,
  140. * then it will attach device when an acceptable Vbus
  141. * level from the host is detected.
  142. */
  143. void udd_attach(void);
  144. /**
  145. * \brief Detaches the device from the bus
  146. *
  147. * The driver must remove pull-up on USB line D- or D+.
  148. */
  149. void udd_detach(void);
  150. /**
  151. * \brief Test whether the USB Device Controller is running at high
  152. * speed or not.
  153. *
  154. * \return \c true if the Device is running at high speed mode, otherwise \c false.
  155. */
  156. bool udd_is_high_speed(void);
  157. /**
  158. * \brief Changes the USB address of device
  159. *
  160. * \param address New USB address
  161. */
  162. void udd_set_address(uint8_t address);
  163. /**
  164. * \brief Returns the USB address of device
  165. *
  166. * \return USB address
  167. */
  168. uint8_t udd_getaddress(void);
  169. /**
  170. * \brief Returns the current start of frame number
  171. *
  172. * \return current start of frame number.
  173. */
  174. uint16_t udd_get_frame_number(void);
  175. /**
  176. * \brief Returns the current micro start of frame number
  177. *
  178. * \return current micro start of frame number required in high speed mode.
  179. */
  180. uint16_t udd_get_micro_frame_number(void);
  181. /*! \brief The USB driver sends a resume signal called Upstream Resume
  182. */
  183. void udd_send_remotewakeup(void);
  184. /**
  185. * \brief Load setup payload
  186. *
  187. * \param payload Pointer on payload
  188. * \param payload_size Size of payload
  189. */
  190. void udd_set_setup_payload( uint8_t *payload, uint16_t payload_size );
  191. /**
  192. * \name Endpoint Management
  193. *
  194. * The following functions allow drivers to create and remove
  195. * endpoints, as well as set, clear and query their "halted" and
  196. * "wedged" states.
  197. */
  198. //@{
  199. #if (USB_DEVICE_MAX_EP != 0)
  200. /**
  201. * \brief Configures and enables an endpoint
  202. *
  203. * \param ep Endpoint number including direction (USB_EP_DIR_IN/USB_EP_DIR_OUT).
  204. * \param bmAttributes Attributes of endpoint declared in the descriptor.
  205. * \param MaxEndpointSize Endpoint maximum size
  206. *
  207. * \return \c 1 if the endpoint is enabled, otherwise \c 0.
  208. */
  209. bool udd_ep_alloc(udd_ep_id_t ep, uint8_t bmAttributes,
  210. uint16_t MaxEndpointSize);
  211. /**
  212. * \brief Disables an endpoint
  213. *
  214. * \param ep Endpoint number including direction (USB_EP_DIR_IN/USB_EP_DIR_OUT).
  215. */
  216. void udd_ep_free(udd_ep_id_t ep);
  217. /**
  218. * \brief Check if the endpoint \a ep is halted.
  219. *
  220. * \param ep The ID of the endpoint to check.
  221. *
  222. * \return \c 1 if \a ep is halted, otherwise \c 0.
  223. */
  224. bool udd_ep_is_halted(udd_ep_id_t ep);
  225. /**
  226. * \brief Set the halted state of the endpoint \a ep
  227. *
  228. * After calling this function, any transaction on \a ep will result
  229. * in a STALL handshake being sent. Any pending transactions will be
  230. * performed first, however.
  231. *
  232. * \param ep The ID of the endpoint to be halted
  233. *
  234. * \return \c 1 if \a ep is halted, otherwise \c 0.
  235. */
  236. bool udd_ep_set_halt(udd_ep_id_t ep);
  237. /**
  238. * \brief Clear the halted state of the endpoint \a ep
  239. *
  240. * After calling this function, any transaction on \a ep will
  241. * be handled normally, i.e. a STALL handshake will not be sent, and
  242. * the data toggle sequence will start at DATA0.
  243. *
  244. * \param ep The ID of the endpoint to be un-halted
  245. *
  246. * \return \c 1 if function was successfully done, otherwise \c 0.
  247. */
  248. bool udd_ep_clear_halt(udd_ep_id_t ep);
  249. /**
  250. * \brief Registers a callback to call when endpoint halt is cleared
  251. *
  252. * \param ep The ID of the endpoint to use
  253. * \param callback NULL or function to call when endpoint halt is cleared
  254. *
  255. * \warning if the endpoint is not halted then the \a callback is called immediately.
  256. *
  257. * \return \c 1 if the register is accepted, otherwise \c 0.
  258. */
  259. bool udd_ep_wait_stall_clear(udd_ep_id_t ep,
  260. udd_callback_halt_cleared_t callback);
  261. /**
  262. * \brief Allows to receive or send data on an endpoint
  263. *
  264. * The driver uses a specific DMA USB to transfer data
  265. * from internal RAM to endpoint, if this one is available.
  266. * When the transfer is finished or aborted (stall, reset, ...), the \a callback is called.
  267. * The \a callback returns the transfer status and eventually the number of byte transfered.
  268. * Note: The control endpoint is not authorized.
  269. *
  270. * \param ep The ID of the endpoint to use
  271. * \param b_shortpacket Enabled automatic short packet
  272. * \param buf Buffer on Internal RAM to send or fill.
  273. * It must be align, then use COMPILER_WORD_ALIGNED.
  274. * \param buf_size Buffer size to send or fill
  275. * \param callback NULL or function to call at the end of transfer
  276. *
  277. * \warning About \a b_shortpacket, for IN endpoint it means that a short packet
  278. * (or a Zero Length Packet) will be sent to the USB line to properly close the usb
  279. * transfer at the end of the data transfer.
  280. * For Bulk and Interrupt OUT endpoint, it will automatically stop the transfer
  281. * at the end of the data transfer (received short packet).
  282. *
  283. * \return \c 1 if function was successfully done, otherwise \c 0.
  284. */
  285. bool udd_ep_run(udd_ep_id_t ep, bool b_shortpacket,
  286. uint8_t * buf, iram_size_t buf_size,
  287. udd_callback_trans_t callback);
  288. /**
  289. * \brief Aborts transfer on going on endpoint
  290. *
  291. * If a transfer is on going, then it is stopped and
  292. * the callback registered is called to signal the end of transfer.
  293. * Note: The control endpoint is not authorized.
  294. *
  295. * \param ep Endpoint to abort
  296. */
  297. void udd_ep_abort(udd_ep_id_t ep);
  298. #endif
  299. //@}
  300. /**
  301. * \name High speed test mode management
  302. *
  303. * The following functions allow the device to jump to a specific test mode required in high speed mode.
  304. */
  305. //@{
  306. void udd_test_mode_j(void);
  307. void udd_test_mode_k(void);
  308. void udd_test_mode_se0_nak(void);
  309. void udd_test_mode_packet(void);
  310. //@}
  311. /**
  312. * \name UDC callbacks to provide for UDD
  313. *
  314. * The following callbacks are used by UDD.
  315. */
  316. //@{
  317. /**
  318. * \brief Decodes and manages a setup request
  319. *
  320. * The driver call it when a SETUP packet is received.
  321. * The \c udd_g_ctrlreq contains the data of SETUP packet.
  322. * If this callback accepts the setup request then it must
  323. * return \c 1 and eventually update \c udd_g_ctrlreq to send or receive data.
  324. *
  325. * \return \c 1 if the request is accepted, otherwise \c 0.
  326. */
  327. extern bool udc_process_setup(void);
  328. /**
  329. * \brief Reset the UDC
  330. *
  331. * The UDC must reset all configuration.
  332. */
  333. extern void udc_reset(void);
  334. /**
  335. * \brief To signal that a SOF is occurred
  336. *
  337. * The UDC must send the signal to all UDIs enabled
  338. */
  339. extern void udc_sof_notify(void);
  340. //@}
  341. //@}
  342. #ifdef __cplusplus
  343. }
  344. #endif
  345. #endif // _UDD_H_