usb_m484.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2022, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. /*- Includes ----------------------------------------------------------------*/
  4. #include "M480.h"
  5. #include "usb.h"
  6. #include "usb_std.h"
  7. #include "usb_descriptors.h"
  8. /*- Definitions -------------------------------------------------------------*/
  9. #define USB_EP_NUM 12
  10. #define USB_MEM_SIZE 4096
  11. /*- Variables ---------------------------------------------------------------*/
  12. static uint8_t usb_ctrl_out_buf[USB_CTRL_EP_SIZE];
  13. static void (*usb_control_recv_callback)(uint8_t *data, int size);
  14. static uint8_t *usb_ep_data[USB_EP_NUM];
  15. static int usb_ep_size[USB_EP_NUM];
  16. static int usb_setup_length;
  17. static int usb_ep_mem_ptr;
  18. /*- Prototypes --------------------------------------------------------------*/
  19. static void usb_reset_endpoints(void);
  20. /*- Implementations ---------------------------------------------------------*/
  21. //-----------------------------------------------------------------------------
  22. void usb_hw_init(void)
  23. {
  24. SYS->USBPHY = SYS_USBPHY_HSUSBEN_Msk | SYS_USBPHY_SBO_Msk;
  25. for (int i = 0; i < 2000; i++) // Delay more than 10 us
  26. asm("nop");
  27. SYS->USBPHY |= SYS_USBPHY_HSUSBACT_Msk;
  28. CLK->AHBCLK |= CLK_AHBCLK_HSUSBDCKEN_Msk;
  29. HSUSBD->PHYCTL = HSUSBD_PHYCTL_PHYEN_Msk;
  30. while (0 == HSUSBD->EP[0].EPMPS)
  31. HSUSBD->EP[0].EPMPS = 1;
  32. HSUSBD->FADDR = 0;
  33. HSUSBD->OPER = HSUSBD_OPER_HISPDEN_Msk;
  34. usb_setup_length = -1;
  35. usb_reset_endpoints();
  36. }
  37. //-----------------------------------------------------------------------------
  38. void usb_attach(void)
  39. {
  40. HSUSBD->PHYCTL |= HSUSBD_PHYCTL_DPPUEN_Msk;
  41. }
  42. //-----------------------------------------------------------------------------
  43. void usb_detach(void)
  44. {
  45. HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk;
  46. }
  47. //-----------------------------------------------------------------------------
  48. static void usb_reset_endpoints(void)
  49. {
  50. for (int i = 0; i < USB_EP_NUM; i++)
  51. {
  52. usb_ep_size[i] = 0;
  53. usb_ep_data[i] = NULL;
  54. HSUSBD->EP[i].EPRSPCTL = HSUSBD_EPRSPCTL_FLUSH_Msk;
  55. HSUSBD->EP[i].EPCFG = 0;
  56. HSUSBD->EP[i].EPMPS = 0;
  57. HSUSBD->EP[i].EPBUFST = 0;
  58. HSUSBD->EP[i].EPBUFEND = 0;
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. void usb_configure_endpoint(usb_endpoint_descriptor_t *desc)
  63. {
  64. int ep, dir, type, size;
  65. ep = desc->bEndpointAddress & USB_INDEX_MASK;
  66. dir = desc->bEndpointAddress & USB_DIRECTION_MASK;
  67. type = desc->bmAttributes & 0x03;
  68. size = desc->wMaxPacketSize;
  69. if (USB_CONTROL_ENDPOINT == type)
  70. while (1);
  71. else if (USB_ISOCHRONOUS_ENDPOINT == type)
  72. type = (3/*Isochronous*/ << HSUSBD_EPCFG_EPTYPE_Pos);
  73. else if (USB_BULK_ENDPOINT == type)
  74. type = (1/*Bulk*/ << HSUSBD_EPCFG_EPTYPE_Pos);
  75. else
  76. type = (2/*Interrupt*/ << HSUSBD_EPCFG_EPTYPE_Pos);
  77. if (USB_IN_ENDPOINT == dir)
  78. dir = HSUSBD_EPCFG_EPDIR_Msk;
  79. else
  80. dir = 0;
  81. HSUSBD->EP[ep-1].EPMPS = size;
  82. HSUSBD->EP[ep-1].EPBUFST = usb_ep_mem_ptr;
  83. HSUSBD->EP[ep-1].EPBUFEND = usb_ep_mem_ptr + size - 1;
  84. usb_ep_mem_ptr += size;
  85. while (usb_ep_mem_ptr > USB_MEM_SIZE);
  86. HSUSBD->EP[ep-1].EPRSPCTL = HSUSBD_EPRSPCTL_FLUSH_Msk | (1/*Manual*/ << HSUSBD_EPRSPCTL_MODE_Pos);
  87. HSUSBD->EP[ep-1].EPCFG = HSUSBD_EPCFG_EPEN_Msk | type | dir | (ep << HSUSBD_EPCFG_EPNUM_Pos);
  88. }
  89. //-----------------------------------------------------------------------------
  90. bool usb_endpoint_configured(int ep, int dir)
  91. {
  92. return (0 != (HSUSBD->EP[ep-1].EPCFG & HSUSBD_EPCFG_EPTYPE_Msk));
  93. (void)dir;
  94. }
  95. //-----------------------------------------------------------------------------
  96. int usb_endpoint_get_status(int ep, int dir)
  97. {
  98. return (HSUSBD->EP[ep-1].EPRSPCTL & HSUSBD_EPRSPCTL_HALT_Msk);
  99. (void)dir;
  100. }
  101. //-----------------------------------------------------------------------------
  102. void usb_endpoint_set_feature(int ep, int dir)
  103. {
  104. HSUSBD->EP[ep-1].EPRSPCTL |= HSUSBD_EPRSPCTL_HALT_Msk;
  105. (void)dir;
  106. }
  107. //-----------------------------------------------------------------------------
  108. void usb_endpoint_clear_feature(int ep, int dir)
  109. {
  110. HSUSBD->EP[ep-1].EPRSPCTL = (1/*Manual*/ << HSUSBD_EPRSPCTL_MODE_Pos) | HSUSBD_EPRSPCTL_TOGGLE_Msk;
  111. (void)dir;
  112. }
  113. //-----------------------------------------------------------------------------
  114. void usb_set_address(int address)
  115. {
  116. HSUSBD->FADDR = address;
  117. }
  118. //-----------------------------------------------------------------------------
  119. void usb_send(int ep, uint8_t *data, int size)
  120. {
  121. HSUSBD->DMACNT = size;
  122. HSUSBD->DMAADDR = (uint32_t)data;
  123. HSUSBD->DMACTL = (ep << HSUSBD_DMACTL_EPNUM_Pos) | HSUSBD_DMACTL_DMARD_Msk |
  124. HSUSBD_DMACTL_SVINEP_Msk | HSUSBD_DMACTL_DMAEN_Msk;
  125. while (0 == (HSUSBD->BUSINTSTS & HSUSBD_BUSINTSTS_DMADONEIF_Msk));
  126. HSUSBD->BUSINTSTS = HSUSBD_BUSINTSTS_DMADONEIF_Msk;
  127. HSUSBD->EP[ep-1].EPTXCNT = size;
  128. }
  129. //-----------------------------------------------------------------------------
  130. void usb_recv(int ep, uint8_t *data, int size)
  131. {
  132. usb_ep_size[ep-1] = size;
  133. usb_ep_data[ep-1] = data;
  134. }
  135. //-----------------------------------------------------------------------------
  136. void usb_control_send_zlp(void)
  137. {
  138. HSUSBD->CEPINTSTS = HSUSBD_CEPINTSTS_STSDONEIF_Msk;
  139. HSUSBD->CEPCTL = 0; // Clear NAK. USB controller will send ZLP automatically.
  140. while (0 == (HSUSBD->CEPINTSTS & HSUSBD_CEPINTSTS_STSDONEIF_Msk));
  141. }
  142. //-----------------------------------------------------------------------------
  143. void usb_control_stall(void)
  144. {
  145. HSUSBD->CEPCTL = HSUSBD_CEPCTL_STALLEN_Msk;
  146. }
  147. //-----------------------------------------------------------------------------
  148. void usb_control_send(uint8_t *data, int size)
  149. {
  150. bool need_zlp = (size < usb_setup_length) &&
  151. ((size & (usb_device_descriptor.bMaxPacketSize0-1)) == 0);
  152. HSUSBD->CEPCTL = 0; // Clear NAK
  153. while (size)
  154. {
  155. int transfer_size = USB_LIMIT(size, usb_device_descriptor.bMaxPacketSize0);
  156. for (int i = 0; i < transfer_size; i++)
  157. HSUSBD->CEPDAT_BYTE = data[i];
  158. HSUSBD->CEPTXCNT = transfer_size;
  159. while (0 == (HSUSBD->CEPINTSTS & HSUSBD_CEPINTSTS_TXPKIF_Msk));
  160. HSUSBD->CEPINTSTS = HSUSBD_CEPINTSTS_TXPKIF_Msk;
  161. size -= transfer_size;
  162. data += transfer_size;
  163. }
  164. if (need_zlp)
  165. {
  166. HSUSBD->CEPTXCNT = 0;
  167. while (0 == (HSUSBD->CEPINTSTS & HSUSBD_CEPINTSTS_TXPKIF_Msk));
  168. HSUSBD->CEPINTSTS = HSUSBD_CEPINTSTS_TXPKIF_Msk;
  169. }
  170. }
  171. //-----------------------------------------------------------------------------
  172. void usb_control_recv(void (*callback)(uint8_t *data, int size))
  173. {
  174. usb_control_recv_callback = callback;
  175. }
  176. //-----------------------------------------------------------------------------
  177. void usb_task(void)
  178. {
  179. int busint = HSUSBD->BUSINTSTS;
  180. int cepint = HSUSBD->CEPINTSTS;
  181. int epint;
  182. if (busint & HSUSBD_BUSINTSTS_VBUSDETIF_Msk)
  183. {
  184. HSUSBD->BUSINTSTS = HSUSBD_BUSINTSTS_VBUSDETIF_Msk;
  185. if (HSUSBD->PHYCTL & HSUSBD_PHYCTL_VBUSDET_Msk)
  186. usb_attach();
  187. else
  188. usb_detach();
  189. }
  190. if (busint & HSUSBD_BUSINTSTS_RSTIF_Msk)
  191. {
  192. HSUSBD->BUSINTSTS = HSUSBD_BUSINTSTS_RSTIF_Msk;
  193. HSUSBD->FADDR = 0;
  194. HSUSBD->DMACNT = 0;
  195. HSUSBD->DMACTL = HSUSBD_DMACTL_DMARST_Msk;
  196. HSUSBD->DMACTL = 0;
  197. usb_reset_endpoints();
  198. HSUSBD->CEPBUFST = 0;
  199. HSUSBD->CEPBUFEND = usb_device_descriptor.bMaxPacketSize0-1;
  200. usb_ep_mem_ptr = usb_device_descriptor.bMaxPacketSize0;
  201. while (0 == (HSUSBD->OPER & HSUSBD_OPER_CURSPD_Msk)); // Block if FS
  202. }
  203. if (cepint & HSUSBD_CEPINTSTS_SETUPPKIF_Msk)
  204. {
  205. usb_request_t request;
  206. HSUSBD->CEPINTSTS = HSUSBD_CEPINTSTS_SETUPPKIF_Msk;
  207. request.bmRequestType = HSUSBD->SETUP1_0;
  208. request.bRequest = HSUSBD->SETUP1_0 >> 8;
  209. request.wValue = HSUSBD->SETUP3_2;
  210. request.wIndex = HSUSBD->SETUP5_4;
  211. request.wLength = HSUSBD->SETUP7_6;
  212. usb_setup_length = request.wLength;
  213. if (!usb_handle_standard_request(&request))
  214. usb_control_stall();
  215. usb_setup_length = -1;
  216. }
  217. if (cepint & HSUSBD_CEPINTSTS_RXPKIF_Msk)
  218. {
  219. if (usb_control_recv_callback)
  220. {
  221. int size = HSUSBD->CEPRXCNT;
  222. for (int i = 0; i < size; i++)
  223. usb_ctrl_out_buf[i] = HSUSBD->CEPDAT_BYTE;
  224. usb_control_recv_callback(usb_ctrl_out_buf, size);
  225. usb_control_recv_callback = NULL;
  226. usb_control_send_zlp();
  227. }
  228. else
  229. {
  230. HSUSBD->CEPCTL = 0; // Clear NAK
  231. }
  232. HSUSBD->CEPINTSTS = HSUSBD_CEPINTSTS_RXPKIF_Msk;
  233. }
  234. for (int ep = 0; ep < USB_EP_NUM; ep++)
  235. {
  236. epint = HSUSBD->EP[ep].EPINTSTS;
  237. if (epint & HSUSBD_EPINTSTS_RXPKIF_Msk)
  238. {
  239. int size = HSUSBD->EP[ep].EPDATCNT & HSUSBD_EPDATCNT_DATCNT_Msk;
  240. if (NULL == usb_ep_data[ep])
  241. continue;
  242. HSUSBD->EP[ep].EPINTSTS = HSUSBD_EPINTSTS_RXPKIF_Msk;
  243. HSUSBD->DMACNT = size; // Note: currently the buffer must be at least endpoint size
  244. HSUSBD->DMAADDR = (uint32_t)usb_ep_data[ep];
  245. HSUSBD->DMACTL = ((ep+1) << HSUSBD_DMACTL_EPNUM_Pos) | HSUSBD_DMACTL_DMAEN_Msk;
  246. while (0 == (HSUSBD->BUSINTSTS & HSUSBD_BUSINTSTS_DMADONEIF_Msk));
  247. HSUSBD->BUSINTSTS = HSUSBD_BUSINTSTS_DMADONEIF_Msk;
  248. usb_ep_data[ep] = NULL;
  249. usb_recv_callback(ep+1, size);
  250. }
  251. if (epint & HSUSBD_EPINTSTS_TXPKIF_Msk)
  252. {
  253. HSUSBD->EP[ep].EPINTSTS = HSUSBD_EPINTSTS_TXPKIF_Msk;
  254. usb_send_callback(ep+1);
  255. }
  256. }
  257. }