udc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (c) 2016, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*- Includes ----------------------------------------------------------------*/
  29. #include <stdbool.h>
  30. #include <string.h>
  31. #include "samd21.h"
  32. #include "hal_gpio.h"
  33. #include "udc.h"
  34. #include "usb.h"
  35. #include "utils.h"
  36. #include "nvm_data.h"
  37. #include "usb_descriptors.h"
  38. /*- Definitions -------------------------------------------------------------*/
  39. HAL_GPIO_PIN(USB_DM, A, 24);
  40. HAL_GPIO_PIN(USB_DP, A, 25);
  41. enum
  42. {
  43. USB_DEVICE_EPCFG_EPTYPE_DISABLED = 0,
  44. USB_DEVICE_EPCFG_EPTYPE_CONTROL = 1,
  45. USB_DEVICE_EPCFG_EPTYPE_ISOCHRONOUS = 2,
  46. USB_DEVICE_EPCFG_EPTYPE_BULK = 3,
  47. USB_DEVICE_EPCFG_EPTYPE_INTERRUPT = 4,
  48. USB_DEVICE_EPCFG_EPTYPE_DUAL_BANK = 5,
  49. };
  50. enum
  51. {
  52. USB_DEVICE_PCKSIZE_SIZE_8 = 0,
  53. USB_DEVICE_PCKSIZE_SIZE_16 = 1,
  54. USB_DEVICE_PCKSIZE_SIZE_32 = 2,
  55. USB_DEVICE_PCKSIZE_SIZE_64 = 3,
  56. USB_DEVICE_PCKSIZE_SIZE_128 = 4,
  57. USB_DEVICE_PCKSIZE_SIZE_256 = 5,
  58. USB_DEVICE_PCKSIZE_SIZE_512 = 6,
  59. USB_DEVICE_PCKSIZE_SIZE_1023 = 7,
  60. };
  61. /*- Types -------------------------------------------------------------------*/
  62. typedef union
  63. {
  64. UsbDeviceDescBank bank[2];
  65. struct
  66. {
  67. UsbDeviceDescBank out;
  68. UsbDeviceDescBank in;
  69. };
  70. } udc_mem_t;
  71. /*- Variables ---------------------------------------------------------------*/
  72. static udc_mem_t udc_mem[USB_EPT_NUM];
  73. static uint32_t udc_ctrl_in_buf[16];
  74. static uint32_t udc_ctrl_out_buf[16];
  75. /*- Implementations ---------------------------------------------------------*/
  76. //-----------------------------------------------------------------------------
  77. void udc_init(void)
  78. {
  79. HAL_GPIO_USB_DM_pmuxen(PORT_PMUX_PMUXE_G_Val);
  80. HAL_GPIO_USB_DP_pmuxen(PORT_PMUX_PMUXE_G_Val);
  81. PM->APBBMASK.reg |= PM_APBBMASK_USB;
  82. GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_ID(USB_GCLK_ID) |
  83. GCLK_CLKCTRL_GEN(0);
  84. USB->DEVICE.CTRLA.bit.SWRST = 1;
  85. while (USB->DEVICE.SYNCBUSY.bit.SWRST);
  86. USB->DEVICE.PADCAL.bit.TRANSN = NVM_READ_CAL(NVM_USB_TRANSN);
  87. USB->DEVICE.PADCAL.bit.TRANSP = NVM_READ_CAL(NVM_USB_TRANSP);
  88. USB->DEVICE.PADCAL.bit.TRIM = NVM_READ_CAL(NVM_USB_TRIM);
  89. memset((uint8_t *)udc_mem, 0, sizeof(udc_mem));
  90. USB->DEVICE.DESCADD.reg = (uint32_t)udc_mem;
  91. USB->DEVICE.CTRLA.bit.MODE = USB_CTRLA_MODE_DEVICE_Val;
  92. USB->DEVICE.CTRLA.bit.RUNSTDBY = 1;
  93. USB->DEVICE.CTRLB.bit.SPDCONF = USB_DEVICE_CTRLB_SPDCONF_FS_Val;
  94. USB->DEVICE.CTRLB.bit.DETACH = 0;
  95. USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_EORST;
  96. USB->DEVICE.DeviceEndpoint[0].EPINTENSET.bit.RXSTP = 1;
  97. USB->DEVICE.CTRLA.reg |= USB_CTRLA_ENABLE;
  98. for (int i = 0; i < USB_EPT_NUM; i++)
  99. {
  100. udc_reset_endpoint(i, USB_IN_ENDPOINT);
  101. udc_reset_endpoint(i, USB_OUT_ENDPOINT);
  102. }
  103. NVIC_EnableIRQ(USB_IRQn);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void udc_attach(void)
  107. {
  108. USB->DEVICE.CTRLB.bit.DETACH = 0;
  109. }
  110. //-----------------------------------------------------------------------------
  111. void udc_detach(void)
  112. {
  113. USB->DEVICE.CTRLB.bit.DETACH = 1;
  114. }
  115. //-----------------------------------------------------------------------------
  116. void udc_reset_endpoint(int ep, int dir)
  117. {
  118. if (USB_IN_ENDPOINT == dir)
  119. USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE1 = USB_DEVICE_EPCFG_EPTYPE_DISABLED;
  120. else
  121. USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE0 = USB_DEVICE_EPCFG_EPTYPE_DISABLED;
  122. }
  123. //-----------------------------------------------------------------------------
  124. void udc_configure_endpoint(usb_endpoint_descriptor_t *desc)
  125. {
  126. int ep, dir, type, size;
  127. ep = desc->bEndpointAddress & USB_INDEX_MASK;
  128. dir = desc->bEndpointAddress & USB_DIRECTION_MASK;
  129. type = desc->bmAttributes & 0x03;
  130. size = desc->wMaxPacketSize & 0x3ff;
  131. udc_reset_endpoint(ep, dir);
  132. if (size <= 8)
  133. size = USB_DEVICE_PCKSIZE_SIZE_8;
  134. else if (size <= 16)
  135. size = USB_DEVICE_PCKSIZE_SIZE_16;
  136. else if (size <= 32)
  137. size = USB_DEVICE_PCKSIZE_SIZE_32;
  138. else if (size <= 64)
  139. size = USB_DEVICE_PCKSIZE_SIZE_64;
  140. else if (size <= 128)
  141. size = USB_DEVICE_PCKSIZE_SIZE_128;
  142. else if (size <= 256)
  143. size = USB_DEVICE_PCKSIZE_SIZE_256;
  144. else if (size <= 512)
  145. size = USB_DEVICE_PCKSIZE_SIZE_512;
  146. else if (size <= 1023)
  147. size = USB_DEVICE_PCKSIZE_SIZE_1023;
  148. else
  149. while (1);
  150. if (USB_CONTROL_ENDPOINT == type)
  151. type = USB_DEVICE_EPCFG_EPTYPE_CONTROL;
  152. else if (USB_ISOCHRONOUS_ENDPOINT == type)
  153. type = USB_DEVICE_EPCFG_EPTYPE_ISOCHRONOUS;
  154. else if (USB_BULK_ENDPOINT == type)
  155. type = USB_DEVICE_EPCFG_EPTYPE_BULK;
  156. else
  157. type = USB_DEVICE_EPCFG_EPTYPE_INTERRUPT;
  158. if (USB_IN_ENDPOINT == dir)
  159. {
  160. USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE1 = type;
  161. USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.bit.TRCPT1 = 1;
  162. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLIN = 1;
  163. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.BK1RDY = 1;
  164. udc_mem[ep].in.PCKSIZE.bit.SIZE = size;
  165. }
  166. else
  167. {
  168. USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE0 = type;
  169. USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.bit.TRCPT0 = 1;
  170. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLOUT = 1;
  171. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.BK0RDY = 1;
  172. udc_mem[ep].out.PCKSIZE.bit.SIZE = size;
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. bool udc_endpoint_configured(int ep, int dir)
  177. {
  178. if (USB_IN_ENDPOINT == dir)
  179. return (USB_DEVICE_EPCFG_EPTYPE_DISABLED != USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE1);
  180. else
  181. return (USB_DEVICE_EPCFG_EPTYPE_DISABLED != USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE0);
  182. }
  183. //-----------------------------------------------------------------------------
  184. int udc_endpoint_get_status(int ep, int dir)
  185. {
  186. if (USB_IN_ENDPOINT == dir)
  187. return USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ1;
  188. else
  189. return USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ0;
  190. }
  191. //-----------------------------------------------------------------------------
  192. void udc_endpoint_set_feature(int ep, int dir)
  193. {
  194. if (USB_IN_ENDPOINT == dir)
  195. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.STALLRQ1 = 1;
  196. else
  197. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.STALLRQ0 = 1;
  198. }
  199. //-----------------------------------------------------------------------------
  200. void udc_endpoint_clear_feature(int ep, int dir)
  201. {
  202. if (USB_IN_ENDPOINT == dir)
  203. {
  204. if (USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ1)
  205. {
  206. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.STALLRQ1 = 1;
  207. if (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.STALL1)
  208. {
  209. USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.STALL1 = 1;
  210. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLIN = 1;
  211. }
  212. }
  213. }
  214. else
  215. {
  216. if (USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ0)
  217. {
  218. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.STALLRQ0 = 1;
  219. if (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.STALL0)
  220. {
  221. USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.STALL0 = 1;
  222. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLOUT = 1;
  223. }
  224. }
  225. }
  226. }
  227. //-----------------------------------------------------------------------------
  228. void udc_set_address(int address)
  229. {
  230. USB->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | USB_DEVICE_DADD_DADD(address);
  231. }
  232. //-----------------------------------------------------------------------------
  233. void udc_send(int ep, uint8_t *data, int size)
  234. {
  235. udc_mem[ep].in.ADDR.reg = (uint32_t)data;
  236. udc_mem[ep].in.PCKSIZE.bit.BYTE_COUNT = size;
  237. udc_mem[ep].in.PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
  238. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.BK1RDY = 1;
  239. }
  240. //-----------------------------------------------------------------------------
  241. void udc_recv(int ep, uint8_t *data, int size)
  242. {
  243. udc_mem[ep].out.ADDR.reg = (uint32_t)data;
  244. udc_mem[ep].out.PCKSIZE.bit.MULTI_PACKET_SIZE = size;
  245. udc_mem[ep].out.PCKSIZE.bit.BYTE_COUNT = 0;
  246. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.BK0RDY = 1;
  247. }
  248. //-----------------------------------------------------------------------------
  249. void udc_control_send_zlp(void)
  250. {
  251. udc_mem[0].in.PCKSIZE.bit.BYTE_COUNT = 0;
  252. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT1 = 1;
  253. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK1RDY = 1;
  254. while (0 == USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT1);
  255. }
  256. //-----------------------------------------------------------------------------
  257. void udc_control_stall(void)
  258. {
  259. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.STALLRQ1 = 1;
  260. }
  261. //-----------------------------------------------------------------------------
  262. void udc_control_send(uint8_t *data, int size)
  263. {
  264. if (size <= usb_device_descriptor.bMaxPacketSize0)
  265. {
  266. // Small payloads may be unaligned, so copy them into an aligned buffer
  267. memcpy(udc_ctrl_in_buf, data, size);
  268. udc_mem[0].in.ADDR.reg = (uint32_t)udc_ctrl_in_buf;
  269. }
  270. else
  271. {
  272. // Large payloads must be aligned
  273. udc_mem[0].in.ADDR.reg = (uint32_t)data;
  274. }
  275. udc_mem[0].in.PCKSIZE.bit.BYTE_COUNT = size;
  276. udc_mem[0].in.PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
  277. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT1 = 1;
  278. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK1RDY = 1;
  279. while (0 == USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT1);
  280. }
  281. //-----------------------------------------------------------------------------
  282. void irq_handler_usb(void)
  283. {
  284. int epint, flags;
  285. if (USB->DEVICE.INTFLAG.bit.EORST)
  286. {
  287. USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_EORST;
  288. USB->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN;
  289. for (int i = 0; i < USB_EPT_NUM; i++)
  290. {
  291. udc_reset_endpoint(i, USB_IN_ENDPOINT);
  292. udc_reset_endpoint(i, USB_OUT_ENDPOINT);
  293. }
  294. USB->DEVICE.DeviceEndpoint[0].EPCFG.reg =
  295. USB_DEVICE_EPCFG_EPTYPE0(USB_DEVICE_EPCFG_EPTYPE_CONTROL) |
  296. USB_DEVICE_EPCFG_EPTYPE1(USB_DEVICE_EPCFG_EPTYPE_CONTROL);
  297. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK0RDY = 1;
  298. USB->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.bit.BK1RDY = 1;
  299. udc_mem[0].in.ADDR.reg = (uint32_t)udc_ctrl_in_buf;
  300. udc_mem[0].in.PCKSIZE.bit.SIZE = USB_DEVICE_PCKSIZE_SIZE_64;
  301. udc_mem[0].in.PCKSIZE.bit.BYTE_COUNT = 0;
  302. udc_mem[0].in.PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
  303. udc_mem[0].out.ADDR.reg = (uint32_t)udc_ctrl_out_buf;
  304. udc_mem[0].out.PCKSIZE.bit.SIZE = USB_DEVICE_PCKSIZE_SIZE_64;
  305. udc_mem[0].out.PCKSIZE.bit.MULTI_PACKET_SIZE = 8;
  306. udc_mem[0].out.PCKSIZE.bit.BYTE_COUNT = 0;
  307. USB->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.bit.BK0RDY = 1;
  308. USB->DEVICE.DeviceEndpoint[0].EPINTENSET.bit.RXSTP = 1;
  309. }
  310. if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP)
  311. {
  312. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP = 1;
  313. USB->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.bit.BK0RDY = 1;
  314. usb_handle_standard_request((usb_request_t *)udc_ctrl_out_buf);
  315. }
  316. epint = USB->DEVICE.EPINTSMRY.reg;
  317. for (int i = 0; epint && i < USB_EPT_NUM; i++)
  318. {
  319. if (0 == (epint & (1 << i)))
  320. continue;
  321. epint &= ~(1 << i);
  322. flags = USB->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg;
  323. if (flags & USB_DEVICE_EPINTFLAG_TRCPT0)
  324. {
  325. USB->DEVICE.DeviceEndpoint[i].EPINTFLAG.bit.TRCPT0 = 1;
  326. USB->DEVICE.DeviceEndpoint[i].EPSTATUSSET.bit.BK0RDY = 1;
  327. udc_recv_callback(i);
  328. }
  329. if (flags & USB_DEVICE_EPINTFLAG_TRCPT1)
  330. {
  331. USB->DEVICE.DeviceEndpoint[i].EPINTFLAG.bit.TRCPT1 = 1;
  332. USB->DEVICE.DeviceEndpoint[i].EPSTATUSCLR.bit.BK1RDY = 1;
  333. udc_send_callback(i);
  334. }
  335. }
  336. }