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.

usb_task.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * \file
  3. *
  4. * \brief Main functions for USB composite example
  5. *
  6. * Copyright (c) 2011-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. // Support and FAQ: visit <a href="https://www.atmel.com/design-support/">Atmel Support</a>
  44. #ifdef ARDUINO_ARCH_SAM
  45. #include <Arduino.h>
  46. #include <Reset.h>
  47. #include "conf_usb.h"
  48. #include "udc.h"
  49. #if ENABLED(SDSUPPORT)
  50. static volatile bool main_b_msc_enable = false;
  51. #endif
  52. static volatile bool main_b_cdc_enable = false;
  53. static volatile bool main_b_dtr_active = false;
  54. void usb_task_idle(void) {
  55. #if ENABLED(SDSUPPORT)
  56. // Attend SD card access from the USB MSD -- Prioritize access to improve speed
  57. int delay = 2;
  58. while (main_b_msc_enable && --delay > 0) {
  59. if (udi_msc_process_trans()) delay = 10000;
  60. // Reset the watchdog, just to be sure
  61. REG_WDT_CR = WDT_CR_WDRSTT | WDT_CR_KEY(0xA5);
  62. }
  63. #endif
  64. }
  65. #if ENABLED(SDSUPPORT)
  66. bool usb_task_msc_enable(void) { return ((main_b_msc_enable = true)); }
  67. void usb_task_msc_disable(void) { main_b_msc_enable = false; }
  68. bool usb_task_msc_isenabled(void) { return main_b_msc_enable; }
  69. #endif
  70. bool usb_task_cdc_enable(const uint8_t port) { UNUSED(port); return ((main_b_cdc_enable = true)); }
  71. void usb_task_cdc_disable(const uint8_t port) { UNUSED(port); main_b_cdc_enable = false; main_b_dtr_active = false; }
  72. bool usb_task_cdc_isenabled(void) { return main_b_cdc_enable; }
  73. /*! \brief Called by CDC interface
  74. * Callback running when CDC device have received data
  75. */
  76. void usb_task_cdc_rx_notify(const uint8_t port) { UNUSED(port); }
  77. /*! \brief Configures communication line
  78. *
  79. * \param cfg line configuration
  80. */
  81. static uint16_t dwDTERate = 0;
  82. void usb_task_cdc_config(const uint8_t port, usb_cdc_line_coding_t *cfg) {
  83. UNUSED(port);
  84. // Store last DTE rate
  85. dwDTERate = cfg->dwDTERate;
  86. }
  87. void usb_task_cdc_set_dtr(const uint8_t port, const bool b_enable) {
  88. UNUSED(port);
  89. // Keep DTR status
  90. main_b_dtr_active = b_enable;
  91. // Implement Arduino-Compatible kludge to enter programming mode from
  92. // the native port:
  93. // "Auto-reset into the bootloader is triggered when the port, already
  94. // open at 1200 bps, is closed."
  95. if (1200 == dwDTERate) {
  96. // We check DTR state to determine if host port is open (bit 0 of lineState).
  97. if (!b_enable) {
  98. // Set RST pin to go low for 65535 clock cycles on reset
  99. // This helps restarting when firmware flash ends
  100. RSTC->RSTC_MR = 0xA5000F01;
  101. // Schedule delayed reset
  102. initiateReset(250);
  103. }
  104. else
  105. cancelReset();
  106. }
  107. }
  108. bool usb_task_cdc_dtr_active(void) { return main_b_dtr_active; }
  109. /// Microsoft WCID descriptor
  110. typedef struct USB_MicrosoftCompatibleDescriptor_Interface {
  111. uint8_t bFirstInterfaceNumber;
  112. uint8_t reserved1;
  113. uint8_t compatibleID[8];
  114. uint8_t subCompatibleID[8];
  115. uint8_t reserved2[6];
  116. } __attribute__((packed)) USB_MicrosoftCompatibleDescriptor_Interface;
  117. typedef struct USB_MicrosoftCompatibleDescriptor {
  118. uint32_t dwLength;
  119. uint16_t bcdVersion;
  120. uint16_t wIndex;
  121. uint8_t bCount;
  122. uint8_t reserved[7];
  123. USB_MicrosoftCompatibleDescriptor_Interface interfaces[];
  124. } __attribute__((packed)) USB_MicrosoftCompatibleDescriptor;
  125. // 3D Printer compatible descriptor
  126. static USB_MicrosoftCompatibleDescriptor microsoft_compatible_id_descriptor = {
  127. .dwLength = sizeof(USB_MicrosoftCompatibleDescriptor) +
  128. 1*sizeof(USB_MicrosoftCompatibleDescriptor_Interface),
  129. .bcdVersion = 0x0100,
  130. .wIndex = 0x0004,
  131. .bCount = 1,
  132. .reserved = {0, 0, 0, 0, 0, 0, 0},
  133. .interfaces = {
  134. {
  135. .bFirstInterfaceNumber = 0,
  136. .reserved1 = 1,
  137. .compatibleID = "3DPRINT",
  138. .subCompatibleID = {0, 0, 0, 0, 0, 0, 0, 0},
  139. .reserved2 = {0, 0, 0, 0, 0, 0},
  140. }
  141. }
  142. };
  143. #define xstr(s) str(s)
  144. #define str(s) #s
  145. #define MS3DPRINT_CONFIG u"MS3DPrintConfig"
  146. #define MS3DPRINT_CONFIG_DATA \
  147. u"Base=SD\0"\
  148. u"Job3DOutputAreaWidth=" xstr(X_BED_SIZE) "000\0"\
  149. u"Job3DOutputAreaDepth=" xstr(Y_BED_SIZE) "000\0"\
  150. u"Job3DOutputAreaHeight=" xstr(Z_MAX_POS) "000\0"\
  151. u"filamentdiameter=1750\0"
  152. typedef struct USB_MicrosoftExtendedPropertiesDescriptor {
  153. uint32_t dwLength;
  154. uint16_t bcdVersion;
  155. uint16_t wIndex;
  156. uint16_t bCount;
  157. uint32_t dwPropertySize;
  158. uint32_t dwPropertyDataType;
  159. uint16_t wPropertyNameLength;
  160. uint16_t PropertyName[sizeof(MS3DPRINT_CONFIG)/sizeof(uint16_t)];
  161. uint32_t dwPropertyDataLength;
  162. uint16_t PropertyData[sizeof(MS3DPRINT_CONFIG_DATA)/sizeof(uint16_t)];
  163. } __attribute__((packed)) USB_MicrosoftExtendedPropertiesDescriptor;
  164. static USB_MicrosoftExtendedPropertiesDescriptor microsoft_extended_properties_descriptor = {
  165. .dwLength = sizeof(USB_MicrosoftExtendedPropertiesDescriptor),
  166. .bcdVersion = 0x0100,
  167. .wIndex = 0x0005,
  168. .bCount = 1,
  169. .dwPropertySize = 4 + 4 + 2 + 4 + sizeof(MS3DPRINT_CONFIG) + sizeof(MS3DPRINT_CONFIG_DATA),
  170. .dwPropertyDataType = 7, // (1=REG_SZ, 4=REG_DWORD, 7=REG_MULTI_SZ)
  171. .wPropertyNameLength = sizeof(MS3DPRINT_CONFIG),
  172. .PropertyName = MS3DPRINT_CONFIG,
  173. .dwPropertyDataLength = sizeof(MS3DPRINT_CONFIG_DATA),
  174. .PropertyData = MS3DPRINT_CONFIG_DATA
  175. };
  176. /**************************************************************************************************
  177. ** WCID configuration information
  178. ** Hooked into UDC via UDC_GET_EXTRA_STRING #define.
  179. */
  180. bool usb_task_extra_string(void) {
  181. static uint8_t udi_msft_magic[] = "MSFT100\xEE";
  182. static uint8_t udi_cdc_name[] = "CDC interface";
  183. #if ENABLED(SDSUPPORT)
  184. static uint8_t udi_msc_name[] = "MSC interface";
  185. #endif
  186. struct extra_strings_desc_t {
  187. usb_str_desc_t header;
  188. #if ENABLED(SDSUPPORT)
  189. le16_t string[Max(Max(sizeof(udi_cdc_name) - 1, sizeof(udi_msc_name) - 1), sizeof(udi_msft_magic) - 1)];
  190. #else
  191. le16_t string[Max(sizeof(udi_cdc_name) - 1, sizeof(udi_msft_magic) - 1)];
  192. #endif
  193. };
  194. static UDC_DESC_STORAGE struct extra_strings_desc_t extra_strings_desc = {
  195. .header.bDescriptorType = USB_DT_STRING
  196. };
  197. uint8_t *str;
  198. uint8_t str_lgt = 0;
  199. // Link payload pointer to the string corresponding at request
  200. switch (udd_g_ctrlreq.req.wValue & 0xFF) {
  201. case UDI_CDC_IAD_STRING_ID:
  202. str_lgt = sizeof(udi_cdc_name) - 1;
  203. str = udi_cdc_name;
  204. break;
  205. #if ENABLED(SDSUPPORT)
  206. case UDI_MSC_STRING_ID:
  207. str_lgt = sizeof(udi_msc_name) - 1;
  208. str = udi_msc_name;
  209. break;
  210. #endif
  211. case 0xEE:
  212. str_lgt = sizeof(udi_msft_magic) - 1;
  213. str = udi_msft_magic;
  214. break;
  215. default:
  216. return false;
  217. }
  218. for (uint8_t i = 0; i < str_lgt; i++)
  219. extra_strings_desc.string[i] = cpu_to_le16((le16_t)str[i]);
  220. extra_strings_desc.header.bLength = 2 + str_lgt * 2;
  221. udd_g_ctrlreq.payload_size = extra_strings_desc.header.bLength;
  222. udd_g_ctrlreq.payload = (uint8_t*)&extra_strings_desc;
  223. // if the string is larger than request length, then cut it
  224. if (udd_g_ctrlreq.payload_size > udd_g_ctrlreq.req.wLength) {
  225. udd_g_ctrlreq.payload_size = udd_g_ctrlreq.req.wLength;
  226. }
  227. return true;
  228. }
  229. /**************************************************************************************************
  230. ** Handle device requests that the ASF stack doesn't
  231. */
  232. bool usb_task_other_requests(void) {
  233. uint8_t* ptr = 0;
  234. uint16_t size = 0;
  235. if (Udd_setup_type() == USB_REQ_TYPE_VENDOR) {
  236. //if (udd_g_ctrlreq.req.bRequest == 0x30)
  237. if (1) {
  238. if (udd_g_ctrlreq.req.wIndex == 0x04) {
  239. ptr = (uint8_t*)&microsoft_compatible_id_descriptor;
  240. size = (udd_g_ctrlreq.req.wLength);
  241. if (size > microsoft_compatible_id_descriptor.dwLength)
  242. size = microsoft_compatible_id_descriptor.dwLength;
  243. }
  244. else if (udd_g_ctrlreq.req.wIndex == 0x05) {
  245. ptr = (uint8_t*)&microsoft_extended_properties_descriptor;
  246. size = (udd_g_ctrlreq.req.wLength);
  247. if (size > microsoft_extended_properties_descriptor.dwLength)
  248. size = microsoft_extended_properties_descriptor.dwLength;
  249. }
  250. else
  251. return false;
  252. }
  253. }
  254. udd_g_ctrlreq.payload_size = size;
  255. if (size == 0) {
  256. udd_g_ctrlreq.callback = 0;
  257. udd_g_ctrlreq.over_under_run = 0;
  258. }
  259. else
  260. udd_g_ctrlreq.payload = ptr;
  261. return true;
  262. }
  263. void usb_task_init(void) {
  264. uint16_t *ptr;
  265. // Disable USB peripheral so we start clean and avoid lockups
  266. otg_disable();
  267. udd_disable();
  268. // Set the USB interrupt to our stack
  269. UDD_SetStack(&USBD_ISR);
  270. // Start USB stack to authorize VBus monitoring
  271. udc_start();
  272. // Patch in filament diameter - Be careful: String is in UNICODE (2bytes per char)
  273. ptr = &microsoft_extended_properties_descriptor.PropertyData[0];
  274. while (ptr[0] || ptr[1]) { // Double 0 flags end of resource
  275. // Found the filamentdiameter= unicode string
  276. if (ptr[0] == 'r' && ptr[1] == '=') {
  277. char diam[16];
  278. char *sptr;
  279. // Patch in the filament diameter
  280. sprintf_P(diam, PSTR("%d"), (int)((DEFAULT_NOMINAL_FILAMENT_DIA) * 1000.0));
  281. // And copy it to the proper place, expanding it to unicode
  282. sptr = &diam[0];
  283. ptr += 2;
  284. while (*sptr) *ptr++ = *sptr++;
  285. // Done!
  286. break;
  287. }
  288. // Go to the next character
  289. ptr++;
  290. }
  291. }
  292. #endif // ARDUINO_ARCH_SAM