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.

udi_msc.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * \file
  3. *
  4. * \brief USB Device Mass Storage Class (MSC) interface definitions.
  5. *
  6. * Copyright (c) 2009-2016 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 _UDI_MSC_H_
  47. #define _UDI_MSC_H_
  48. #include "conf_usb.h"
  49. #include "usb_protocol.h"
  50. #include "usb_protocol_msc.h"
  51. #include "udd.h"
  52. #include "udc_desc.h"
  53. #include "udi.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * \addtogroup udi_msc_group_udc
  59. * @{
  60. */
  61. //! Global structure which contains standard UDI interface for UDC
  62. extern UDC_DESC_STORAGE udi_api_t udi_api_msc;
  63. //@}
  64. /**
  65. * \ingroup udi_msc_group
  66. * \defgroup udi_msc_group USB interface descriptors
  67. *
  68. * The following structures provide predefined USB interface descriptors.
  69. * It must be used to define the final USB descriptors.
  70. */
  71. //@{
  72. //! Interface descriptor structure for MSC
  73. typedef struct {
  74. usb_iface_desc_t iface;
  75. usb_ep_desc_t ep_in;
  76. usb_ep_desc_t ep_out;
  77. } udi_msc_desc_t;
  78. //! By default no string associated to this interface
  79. #ifndef UDI_MSC_STRING_ID
  80. #define UDI_MSC_STRING_ID 0
  81. #endif
  82. //! MSC endpoints size for full speed
  83. #define UDI_MSC_EPS_SIZE_FS 64
  84. //! MSC endpoints size for high speed
  85. #define UDI_MSC_EPS_SIZE_HS 512
  86. //! Content of MSC interface descriptor for all speeds
  87. #define UDI_MSC_DESC \
  88. .iface.bLength = sizeof(usb_iface_desc_t),\
  89. .iface.bDescriptorType = USB_DT_INTERFACE,\
  90. .iface.bInterfaceNumber = UDI_MSC_IFACE_NUMBER,\
  91. .iface.bAlternateSetting = 0,\
  92. .iface.bNumEndpoints = 2,\
  93. .iface.bInterfaceClass = MSC_CLASS,\
  94. .iface.bInterfaceSubClass = MSC_SUBCLASS_TRANSPARENT,\
  95. .iface.bInterfaceProtocol = MSC_PROTOCOL_BULK,\
  96. .iface.iInterface = UDI_MSC_STRING_ID,\
  97. .ep_in.bLength = sizeof(usb_ep_desc_t),\
  98. .ep_in.bDescriptorType = USB_DT_ENDPOINT,\
  99. .ep_in.bEndpointAddress = UDI_MSC_EP_IN,\
  100. .ep_in.bmAttributes = USB_EP_TYPE_BULK,\
  101. .ep_in.bInterval = 0,\
  102. .ep_out.bLength = sizeof(usb_ep_desc_t),\
  103. .ep_out.bDescriptorType = USB_DT_ENDPOINT,\
  104. .ep_out.bEndpointAddress = UDI_MSC_EP_OUT,\
  105. .ep_out.bmAttributes = USB_EP_TYPE_BULK,\
  106. .ep_out.bInterval = 0,
  107. //! Content of MSC interface descriptor for full speed only
  108. #define UDI_MSC_DESC_FS {\
  109. UDI_MSC_DESC \
  110. .ep_in.wMaxPacketSize = LE16(UDI_MSC_EPS_SIZE_FS),\
  111. .ep_out.wMaxPacketSize = LE16(UDI_MSC_EPS_SIZE_FS),\
  112. }
  113. //! Content of MSC interface descriptor for high speed only
  114. #define UDI_MSC_DESC_HS {\
  115. UDI_MSC_DESC \
  116. .ep_in.wMaxPacketSize = LE16(UDI_MSC_EPS_SIZE_HS),\
  117. .ep_out.wMaxPacketSize = LE16(UDI_MSC_EPS_SIZE_HS),\
  118. }
  119. //@}
  120. /**
  121. * \ingroup udi_group
  122. * \defgroup udi_msc_group USB Device Interface (UDI) for Mass Storage Class (MSC)
  123. *
  124. * Common APIs used by high level application to use this USB class.
  125. *
  126. * These routines are used by memory to transfer its data
  127. * to/from USB MSC endpoints.
  128. *
  129. * See \ref udi_msc_quickstart.
  130. * @{
  131. */
  132. /**
  133. * \brief Process the background read/write commands
  134. *
  135. * Routine called by the main loop
  136. */
  137. bool udi_msc_process_trans(void);
  138. /**
  139. * \brief Transfers data to/from USB MSC endpoints
  140. *
  141. *
  142. * \param b_read Memory to USB, if true
  143. * \param block Buffer on Internal RAM to send or fill
  144. * \param block_size Buffer size to send or fill
  145. * \param callback Function to call at the end of transfer.
  146. * If NULL then the routine exit when transfer is finish.
  147. *
  148. * \return \c 1 if function was successfully done, otherwise \c 0.
  149. */
  150. bool udi_msc_trans_block(bool b_read, uint8_t * block, iram_size_t block_size,
  151. void (*callback) (udd_ep_status_t status, iram_size_t n, udd_ep_id_t ep));
  152. //@}
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. /**
  157. * \page udi_msc_quickstart Quick start guide for USB device Mass Storage module (UDI MSC)
  158. *
  159. * This is the quick start guide for the \ref udi_msc_group
  160. * "USB device interface MSC module (UDI MSC)" with step-by-step instructions on
  161. * how to configure and use the modules in a selection of use cases.
  162. *
  163. * The use cases contain several code fragments. The code fragments in the
  164. * steps for setup can be copied into a custom initialization function, while
  165. * the steps for usage can be copied into, e.g., the main application function.
  166. *
  167. * \section udi_msc_basic_use_case Basic use case
  168. * In this basic use case, the "USB MSC (Single Interface Device)" module is used.
  169. * The "USB MSC (Composite Device)" module usage is described in \ref udi_msc_use_cases
  170. * "Advanced use cases".
  171. *
  172. * \section udi_msc_basic_use_case_setup Setup steps
  173. * \subsection udi_msc_basic_use_case_setup_prereq Prerequisites
  174. * \copydetails udc_basic_use_case_setup_prereq
  175. * \subsection udi_msc_basic_use_case_setup_code Example code
  176. * \copydetails udc_basic_use_case_setup_code
  177. * \subsection udi_msc_basic_use_case_setup_flow Workflow
  178. * \copydetails udc_basic_use_case_setup_flow
  179. *
  180. * \section udi_msc_basic_use_case_usage Usage steps
  181. *
  182. * \subsection udi_msc_basic_use_case_usage_code Example code
  183. * Content of conf_usb.h:
  184. * \code
  185. #define USB_DEVICE_SERIAL_NAME "12...EF" // Disk SN for MSC
  186. #define UDI_MSC_GLOBAL_VENDOR_ID \
  187. 'A', 'T', 'M', 'E', 'L', ' ', ' ', ' '
  188. #define UDI_MSC_GLOBAL_PRODUCT_VERSION \
  189. '1', '.', '0', '0'
  190. #define UDI_MSC_ENABLE_EXT() my_callback_msc_enable()
  191. extern bool my_callback_msc_enable(void);
  192. #define UDI_MSC_DISABLE_EXT() my_callback_msc_disable()
  193. extern void my_callback_msc_disable(void);
  194. #include "udi_msc_conf.h" // At the end of conf_usb.h file
  195. \endcode
  196. *
  197. * Add to application C-file:
  198. * \code
  199. static bool my_flag_autorize_msc_transfert = false;
  200. bool my_callback_msc_enable(void)
  201. {
  202. my_flag_autorize_msc_transfert = true;
  203. return true;
  204. }
  205. void my_callback_msc_disable(void)
  206. {
  207. my_flag_autorize_msc_transfert = false;
  208. }
  209. void task(void)
  210. {
  211. udi_msc_process_trans();
  212. }
  213. \endcode
  214. *
  215. * \subsection udi_msc_basic_use_case_setup_flow Workflow
  216. * -# Ensure that conf_usb.h is available and contains the following configuration,
  217. * which is the USB device MSC configuration:
  218. * - \code #define USB_DEVICE_SERIAL_NAME "12...EF" // Disk SN for MSC \endcode
  219. * \note The USB serial number is mandatory when a MSC interface is used.
  220. * - \code //! Vendor name and Product version of MSC interface
  221. #define UDI_MSC_GLOBAL_VENDOR_ID \
  222. 'A', 'T', 'M', 'E', 'L', ' ', ' ', ' '
  223. #define UDI_MSC_GLOBAL_PRODUCT_VERSION \
  224. '1', '.', '0', '0' \endcode
  225. * \note The USB MSC interface requires a vendor ID (8 ASCII characters)
  226. * and a product version (4 ASCII characters).
  227. * - \code #define UDI_MSC_ENABLE_EXT() my_callback_msc_enable()
  228. extern bool my_callback_msc_enable(void); \endcode
  229. * \note After the device enumeration (detecting and identifying USB devices),
  230. * the USB host starts the device configuration. When the USB MSC interface
  231. * from the device is accepted by the host, the USB host enables this interface and the
  232. * UDI_MSC_ENABLE_EXT() callback function is called and return true.
  233. * Thus, when this event is received, the tasks which call
  234. * udi_msc_process_trans() must be enabled.
  235. * - \code #define UDI_MSC_DISABLE_EXT() my_callback_msc_disable()
  236. extern void my_callback_msc_disable(void); \endcode
  237. * \note When the USB device is unplugged or is reset by the USB host, the USB
  238. * interface is disabled and the UDI_MSC_DISABLE_EXT() callback function
  239. * is called. Thus, it is recommended to disable the task which is called udi_msc_process_trans().
  240. * -# The MSC is automatically linked with memory control access component
  241. * which provides the memories interfaces. However, the memory data transfers
  242. * must be done outside USB interrupt routine. This is done in the MSC process
  243. * ("udi_msc_process_trans()") called by main loop:
  244. * - \code * void task(void) {
  245. udi_msc_process_trans();
  246. } \endcode
  247. * -# The MSC speed depends on task periodicity. To get the best speed
  248. * the notification callback "UDI_MSC_NOTIFY_TRANS_EXT" can be used to wakeup
  249. * this task (Example, through a mutex):
  250. * - \code #define UDI_MSC_NOTIFY_TRANS_EXT() msc_notify_trans()
  251. void msc_notify_trans(void) {
  252. wakeup_my_task();
  253. } \endcode
  254. *
  255. * \section udi_msc_use_cases Advanced use cases
  256. * For more advanced use of the UDI MSC module, see the following use cases:
  257. * - \subpage udi_msc_use_case_composite
  258. * - \subpage udc_use_case_1
  259. * - \subpage udc_use_case_2
  260. * - \subpage udc_use_case_3
  261. * - \subpage udc_use_case_5
  262. * - \subpage udc_use_case_6
  263. */
  264. /**
  265. * \page udi_msc_use_case_composite MSC in a composite device
  266. *
  267. * A USB Composite Device is a USB Device which uses more than one USB class.
  268. * In this use case, the "USB MSC (Composite Device)" module is used to
  269. * create a USB composite device. Thus, this USB module can be associated with
  270. * another "Composite Device" module, like "USB HID Mouse (Composite Device)".
  271. *
  272. * Also, you can refer to application note
  273. * <A href="http://www.atmel.com/dyn/resources/prod_documents/doc8445.pdf">
  274. * AVR4902 ASF - USB Composite Device</A>.
  275. *
  276. * \section udi_msc_use_case_composite_setup Setup steps
  277. * For the setup code of this use case to work, the
  278. * \ref udi_msc_basic_use_case "basic use case" must be followed.
  279. *
  280. * \section udi_msc_use_case_composite_usage Usage steps
  281. *
  282. * \subsection udi_msc_use_case_composite_usage_code Example code
  283. * Content of conf_usb.h:
  284. * \code
  285. #define USB_DEVICE_EP_CTRL_SIZE 64
  286. #define USB_DEVICE_NB_INTERFACE (X+1)
  287. #define USB_DEVICE_MAX_EP (X+2)
  288. #define UDI_MSC_EP_IN (X | USB_EP_DIR_IN)
  289. #define UDI_MSC_EP_OUT (Y | USB_EP_DIR_OUT)
  290. #define UDI_MSC_IFACE_NUMBER X
  291. #define UDI_COMPOSITE_DESC_T \
  292. udi_msc_desc_t udi_msc; \
  293. ...
  294. #define UDI_COMPOSITE_DESC_FS \
  295. .udi_msc = UDI_MSC_DESC, \
  296. ...
  297. #define UDI_COMPOSITE_DESC_HS \
  298. .udi_msc = UDI_MSC_DESC, \
  299. ...
  300. #define UDI_COMPOSITE_API \
  301. &udi_api_msc, \
  302. ...
  303. \endcode
  304. *
  305. * \subsection udi_msc_use_case_composite_usage_flow Workflow
  306. * -# Ensure that conf_usb.h is available and contains the following parameters
  307. * required for a USB composite device configuration:
  308. * - \code // Endpoint control size, This must be:
  309. // - 8, 16, 32 or 64 for full speed device (8 is recommended to save RAM)
  310. // - 64 for a high speed device
  311. #define USB_DEVICE_EP_CTRL_SIZE 64
  312. // Total Number of interfaces on this USB device.
  313. // Add 1 for MSC.
  314. #define USB_DEVICE_NB_INTERFACE (X+1)
  315. // Total number of endpoints on this USB device.
  316. // This must include each endpoint for each interface.
  317. // Add 2 for MSC.
  318. #define USB_DEVICE_MAX_EP (X+2) \endcode
  319. * -# Ensure that conf_usb.h contains the description of
  320. * composite device:
  321. * - \code // The endpoint numbers chosen by you for the MSC.
  322. // The endpoint numbers starting from 1.
  323. #define UDI_MSC_EP_IN (X | USB_EP_DIR_IN)
  324. #define UDI_MSC_EP_OUT (Y | USB_EP_DIR_OUT)
  325. // The interface index of an interface starting from 0
  326. #define UDI_MSC_IFACE_NUMBER X \endcode
  327. * -# Ensure that conf_usb.h contains the following parameters
  328. * required for a USB composite device configuration:
  329. * - \code // USB Interfaces descriptor structure
  330. #define UDI_COMPOSITE_DESC_T \
  331. ...
  332. udi_msc_desc_t udi_msc; \
  333. ...
  334. // USB Interfaces descriptor value for Full Speed
  335. #define UDI_COMPOSITE_DESC_FS \
  336. ...
  337. .udi_msc = UDI_MSC_DESC_FS, \
  338. ...
  339. // USB Interfaces descriptor value for High Speed
  340. #define UDI_COMPOSITE_DESC_HS \
  341. ...
  342. .udi_msc = UDI_MSC_DESC_HS, \
  343. ...
  344. // USB Interface APIs
  345. #define UDI_COMPOSITE_API \
  346. ...
  347. &udi_api_msc, \
  348. ... \endcode
  349. * - \note The descriptors order given in the four lists above must be the
  350. * same as the order defined by all interface indexes. The interface index
  351. * orders are defined through UDI_X_IFACE_NUMBER defines.
  352. */
  353. #endif // _UDI_MSC_H_