usb_samd11.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2016-2022, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. /*- Includes ----------------------------------------------------------------*/
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <stdalign.h>
  7. #include "samd11.h"
  8. #include "hal_gpio.h"
  9. #include "nvm_data.h"
  10. #include "usb.h"
  11. #include "usb_std.h"
  12. #include "usb_descriptors.h"
  13. /*- Definitions -------------------------------------------------------------*/
  14. #define USB_EP_NUM 8
  15. HAL_GPIO_PIN(USB_DM, A, 24);
  16. HAL_GPIO_PIN(USB_DP, A, 25);
  17. enum
  18. {
  19. USB_DEVICE_EPCFG_EPTYPE_DISABLED = 0,
  20. USB_DEVICE_EPCFG_EPTYPE_CONTROL = 1,
  21. USB_DEVICE_EPCFG_EPTYPE_ISOCHRONOUS = 2,
  22. USB_DEVICE_EPCFG_EPTYPE_BULK = 3,
  23. USB_DEVICE_EPCFG_EPTYPE_INTERRUPT = 4,
  24. USB_DEVICE_EPCFG_EPTYPE_DUAL_BANK = 5,
  25. };
  26. enum
  27. {
  28. USB_DEVICE_PCKSIZE_SIZE_8 = 0,
  29. USB_DEVICE_PCKSIZE_SIZE_16 = 1,
  30. USB_DEVICE_PCKSIZE_SIZE_32 = 2,
  31. USB_DEVICE_PCKSIZE_SIZE_64 = 3,
  32. USB_DEVICE_PCKSIZE_SIZE_128 = 4,
  33. USB_DEVICE_PCKSIZE_SIZE_256 = 5,
  34. USB_DEVICE_PCKSIZE_SIZE_512 = 6,
  35. USB_DEVICE_PCKSIZE_SIZE_1023 = 7,
  36. };
  37. /*- Types -------------------------------------------------------------------*/
  38. typedef union
  39. {
  40. UsbDeviceDescBank bank[2];
  41. struct
  42. {
  43. UsbDeviceDescBank out;
  44. UsbDeviceDescBank in;
  45. };
  46. } udc_mem_t;
  47. /*- Variables ---------------------------------------------------------------*/
  48. static alignas(4) udc_mem_t udc_mem[USB_EP_NUM];
  49. static alignas(4) uint8_t usb_ctrl_in_buf[64];
  50. static alignas(4) uint8_t usb_ctrl_out_buf[64];
  51. static void (*usb_control_recv_callback)(uint8_t *data, int size);
  52. static int usb_setup_length;
  53. /*- Prototypes --------------------------------------------------------------*/
  54. static void usb_reset_endpoints(void);
  55. /*- Implementations ---------------------------------------------------------*/
  56. //-----------------------------------------------------------------------------
  57. void usb_hw_init(void)
  58. {
  59. HAL_GPIO_USB_DM_pmuxen(PORT_PMUX_PMUXE_G_Val);
  60. HAL_GPIO_USB_DP_pmuxen(PORT_PMUX_PMUXE_G_Val);
  61. PM->APBBMASK.reg |= PM_APBBMASK_USB;
  62. GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_ID(USB_GCLK_ID) |
  63. GCLK_CLKCTRL_GEN(0);
  64. USB->DEVICE.CTRLA.bit.SWRST = 1;
  65. while (USB->DEVICE.SYNCBUSY.bit.SWRST);
  66. USB->DEVICE.PADCAL.bit.TRANSN = NVM_READ_CAL(NVM_USB_TRANSN);
  67. USB->DEVICE.PADCAL.bit.TRANSP = NVM_READ_CAL(NVM_USB_TRANSP);
  68. USB->DEVICE.PADCAL.bit.TRIM = NVM_READ_CAL(NVM_USB_TRIM);
  69. usb_setup_length = -1;
  70. for (int i = 0; i < (int)sizeof(udc_mem); i++)
  71. ((uint8_t *)udc_mem)[i] = 0;
  72. USB->DEVICE.DESCADD.reg = (uint32_t)udc_mem;
  73. USB->DEVICE.CTRLA.bit.MODE = USB_CTRLA_MODE_DEVICE_Val;
  74. USB->DEVICE.CTRLA.bit.RUNSTDBY = 1;
  75. USB->DEVICE.CTRLB.bit.SPDCONF = USB_DEVICE_CTRLB_SPDCONF_FS_Val;
  76. USB->DEVICE.CTRLB.bit.DETACH = 0;
  77. USB->DEVICE.INTENSET.reg = USB_DEVICE_INTENSET_EORST;
  78. USB->DEVICE.DeviceEndpoint[0].EPINTENSET.bit.RXSTP = 1;
  79. USB->DEVICE.CTRLA.reg |= USB_CTRLA_ENABLE;
  80. usb_reset_endpoints();
  81. }
  82. //-----------------------------------------------------------------------------
  83. void usb_attach(void)
  84. {
  85. USB->DEVICE.CTRLB.bit.DETACH = 0;
  86. }
  87. //-----------------------------------------------------------------------------
  88. void usb_detach(void)
  89. {
  90. USB->DEVICE.CTRLB.bit.DETACH = 1;
  91. }
  92. //-----------------------------------------------------------------------------
  93. static void usb_reset_endpoints(void)
  94. {
  95. for (int i = 0; i < USB_EP_NUM; i++)
  96. USB->DEVICE.DeviceEndpoint[i].EPCFG.reg = 0;
  97. }
  98. //-----------------------------------------------------------------------------
  99. void usb_configure_endpoint(usb_endpoint_descriptor_t *desc)
  100. {
  101. int ep, dir, type, size;
  102. ep = desc->bEndpointAddress & USB_INDEX_MASK;
  103. dir = desc->bEndpointAddress & USB_DIRECTION_MASK;
  104. type = desc->bmAttributes & 0x03;
  105. size = desc->wMaxPacketSize & 0x3ff;
  106. if (size <= 8)
  107. size = USB_DEVICE_PCKSIZE_SIZE_8;
  108. else if (size <= 16)
  109. size = USB_DEVICE_PCKSIZE_SIZE_16;
  110. else if (size <= 32)
  111. size = USB_DEVICE_PCKSIZE_SIZE_32;
  112. else if (size <= 64)
  113. size = USB_DEVICE_PCKSIZE_SIZE_64;
  114. else if (size <= 128)
  115. size = USB_DEVICE_PCKSIZE_SIZE_128;
  116. else if (size <= 256)
  117. size = USB_DEVICE_PCKSIZE_SIZE_256;
  118. else if (size <= 512)
  119. size = USB_DEVICE_PCKSIZE_SIZE_512;
  120. else if (size <= 1023)
  121. size = USB_DEVICE_PCKSIZE_SIZE_1023;
  122. else
  123. while (1);
  124. if (USB_CONTROL_ENDPOINT == type)
  125. type = USB_DEVICE_EPCFG_EPTYPE_CONTROL;
  126. else if (USB_ISOCHRONOUS_ENDPOINT == type)
  127. type = USB_DEVICE_EPCFG_EPTYPE_ISOCHRONOUS;
  128. else if (USB_BULK_ENDPOINT == type)
  129. type = USB_DEVICE_EPCFG_EPTYPE_BULK;
  130. else
  131. type = USB_DEVICE_EPCFG_EPTYPE_INTERRUPT;
  132. if (USB_IN_ENDPOINT == dir)
  133. {
  134. USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE1 = type;
  135. USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.bit.TRCPT1 = 1;
  136. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLIN = 1;
  137. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.BK1RDY = 1;
  138. udc_mem[ep].in.PCKSIZE.bit.SIZE = size;
  139. }
  140. else
  141. {
  142. USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE0 = type;
  143. USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.bit.TRCPT0 = 1;
  144. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLOUT = 1;
  145. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.BK0RDY = 1;
  146. udc_mem[ep].out.PCKSIZE.bit.SIZE = size;
  147. }
  148. }
  149. //-----------------------------------------------------------------------------
  150. bool usb_endpoint_configured(int ep, int dir)
  151. {
  152. if (USB_IN_ENDPOINT == dir)
  153. return (USB_DEVICE_EPCFG_EPTYPE_DISABLED != USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE1);
  154. else
  155. return (USB_DEVICE_EPCFG_EPTYPE_DISABLED != USB->DEVICE.DeviceEndpoint[ep].EPCFG.bit.EPTYPE0);
  156. }
  157. //-----------------------------------------------------------------------------
  158. int usb_endpoint_get_status(int ep, int dir)
  159. {
  160. if (USB_IN_ENDPOINT == dir)
  161. return USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ1;
  162. else
  163. return USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ0;
  164. }
  165. //-----------------------------------------------------------------------------
  166. void usb_endpoint_set_feature(int ep, int dir)
  167. {
  168. if (USB_IN_ENDPOINT == dir)
  169. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.STALLRQ1 = 1;
  170. else
  171. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.STALLRQ0 = 1;
  172. }
  173. //-----------------------------------------------------------------------------
  174. void usb_endpoint_clear_feature(int ep, int dir)
  175. {
  176. if (USB_IN_ENDPOINT == dir)
  177. {
  178. if (USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ1)
  179. {
  180. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.STALLRQ1 = 1;
  181. if (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.STALL1)
  182. {
  183. USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL1;
  184. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLIN = 1;
  185. }
  186. }
  187. }
  188. else
  189. {
  190. if (USB->DEVICE.DeviceEndpoint[ep].EPSTATUS.bit.STALLRQ0)
  191. {
  192. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.STALLRQ0 = 1;
  193. if (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.STALL0)
  194. {
  195. USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL0;
  196. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.DTGLOUT = 1;
  197. }
  198. }
  199. }
  200. }
  201. //-----------------------------------------------------------------------------
  202. void usb_set_address(int address)
  203. {
  204. USB->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | USB_DEVICE_DADD_DADD(address);
  205. }
  206. //-----------------------------------------------------------------------------
  207. void usb_send(int ep, uint8_t *data, int size)
  208. {
  209. udc_mem[ep].in.ADDR.reg = (uint32_t)data;
  210. udc_mem[ep].in.PCKSIZE.bit.BYTE_COUNT = size;
  211. udc_mem[ep].in.PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
  212. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.BK1RDY = 1;
  213. }
  214. //-----------------------------------------------------------------------------
  215. void usb_recv(int ep, uint8_t *data, int size)
  216. {
  217. udc_mem[ep].out.ADDR.reg = (uint32_t)data;
  218. udc_mem[ep].out.PCKSIZE.bit.MULTI_PACKET_SIZE = size;
  219. udc_mem[ep].out.PCKSIZE.bit.BYTE_COUNT = 0;
  220. USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.bit.BK0RDY = 1;
  221. }
  222. //-----------------------------------------------------------------------------
  223. void usb_control_send_zlp(void)
  224. {
  225. udc_mem[0].in.PCKSIZE.bit.BYTE_COUNT = 0;
  226. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
  227. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK1RDY = 1;
  228. while (0 == USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT1);
  229. }
  230. //-----------------------------------------------------------------------------
  231. void usb_control_stall(void)
  232. {
  233. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.STALLRQ1 = 1;
  234. }
  235. //-----------------------------------------------------------------------------
  236. void usb_control_send(uint8_t *data, int size)
  237. {
  238. bool need_zlp = (size < usb_setup_length) &&
  239. ((size & (usb_device_descriptor.bMaxPacketSize0-1)) == 0);
  240. // USB controller does not have access to the flash memory, so here we do
  241. // a manual multi-packet transfer. This way data can be located in in
  242. // the flash memory (big constant descriptors).
  243. while (size)
  244. {
  245. int transfer_size = USB_LIMIT(size, usb_device_descriptor.bMaxPacketSize0);
  246. for (int i = 0; i < transfer_size; i++)
  247. usb_ctrl_in_buf[i] = data[i];
  248. udc_mem[0].in.ADDR.reg = (uint32_t)usb_ctrl_in_buf;
  249. udc_mem[0].in.PCKSIZE.bit.BYTE_COUNT = transfer_size;
  250. udc_mem[0].in.PCKSIZE.bit.MULTI_PACKET_SIZE = 0;
  251. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
  252. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK1RDY = 1;
  253. while (0 == USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT1);
  254. size -= transfer_size;
  255. data += transfer_size;
  256. }
  257. if (need_zlp)
  258. usb_control_send_zlp();
  259. }
  260. //-----------------------------------------------------------------------------
  261. void usb_control_recv(void (*callback)(uint8_t *data, int size))
  262. {
  263. usb_control_recv_callback = callback;
  264. }
  265. //-----------------------------------------------------------------------------
  266. void usb_task(void)
  267. {
  268. int flags, epints;
  269. if (USB->DEVICE.INTFLAG.bit.EORST)
  270. {
  271. USB->DEVICE.INTFLAG.reg = USB_DEVICE_INTFLAG_EORST;
  272. USB->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN;
  273. usb_reset_endpoints();
  274. USB->DEVICE.DeviceEndpoint[0].EPCFG.reg =
  275. USB_DEVICE_EPCFG_EPTYPE0(USB_DEVICE_EPCFG_EPTYPE_CONTROL) |
  276. USB_DEVICE_EPCFG_EPTYPE1(USB_DEVICE_EPCFG_EPTYPE_CONTROL);
  277. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK0RDY = 1;
  278. USB->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.bit.BK1RDY = 1;
  279. udc_mem[0].in.PCKSIZE.bit.SIZE = USB_DEVICE_PCKSIZE_SIZE_64;
  280. udc_mem[0].out.ADDR.reg = (uint32_t)usb_ctrl_out_buf;
  281. udc_mem[0].out.PCKSIZE.bit.SIZE = USB_DEVICE_PCKSIZE_SIZE_64;
  282. udc_mem[0].out.PCKSIZE.bit.MULTI_PACKET_SIZE = 64;
  283. udc_mem[0].out.PCKSIZE.bit.BYTE_COUNT = 0;
  284. USB->DEVICE.DeviceEndpoint[0].EPINTENSET.bit.RXSTP = 1;
  285. USB->DEVICE.DeviceEndpoint[0].EPINTENSET.bit.TRCPT0 = 1;
  286. }
  287. if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP)
  288. {
  289. usb_request_t *request = (usb_request_t *)usb_ctrl_out_buf;
  290. usb_setup_length = request->wLength;
  291. if (sizeof(usb_request_t) == udc_mem[0].out.PCKSIZE.bit.BYTE_COUNT)
  292. {
  293. if (usb_handle_standard_request(request))
  294. {
  295. udc_mem[0].out.PCKSIZE.bit.BYTE_COUNT = 0;
  296. USB->DEVICE.DeviceEndpoint[0].EPSTATUSCLR.bit.BK0RDY = 1;
  297. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
  298. }
  299. else
  300. {
  301. usb_control_stall();
  302. }
  303. }
  304. else
  305. {
  306. usb_control_stall();
  307. }
  308. usb_setup_length = -1;
  309. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_RXSTP;
  310. }
  311. else if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT0)
  312. {
  313. if (usb_control_recv_callback)
  314. {
  315. usb_control_recv_callback(usb_ctrl_out_buf, udc_mem[0].out.PCKSIZE.bit.BYTE_COUNT);
  316. usb_control_recv_callback = NULL;
  317. usb_control_send_zlp();
  318. }
  319. USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK0RDY = 1;
  320. USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
  321. }
  322. epints = USB->DEVICE.EPINTSMRY.reg;
  323. for (int i = 1; i < USB_EP_NUM && epints > 0; i++)
  324. {
  325. flags = USB->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg;
  326. epints &= ~(1 << i);
  327. if (flags & USB_DEVICE_EPINTFLAG_TRCPT0)
  328. {
  329. USB->DEVICE.DeviceEndpoint[i].EPSTATUSSET.bit.BK0RDY = 1;
  330. USB->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
  331. usb_recv_callback(i, udc_mem[i].out.PCKSIZE.bit.BYTE_COUNT);
  332. }
  333. if (flags & USB_DEVICE_EPINTFLAG_TRCPT1)
  334. {
  335. USB->DEVICE.DeviceEndpoint[i].EPSTATUSCLR.bit.BK1RDY = 1;
  336. USB->DEVICE.DeviceEndpoint[i].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
  337. usb_send_callback(i);
  338. }
  339. }
  340. }