drv_usbd.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-02-01 Urey first
  9. * 2018-12-20 heyuanjie fix bugs
  10. * 2018-12-25 Zhou Yanjie modify the coding style
  11. */
  12. #include <rtthread.h>
  13. #include <drivers/usb_device.h>
  14. #include "x1000.h"
  15. #include "x1000_dwc.h"
  16. static struct udcd __x1000_usbd;
  17. static dwc_handle __dwc_hdl;
  18. //#define USBD_DEBUG
  19. #ifdef USBD_DEBUG
  20. #define USBD_DBG(fmt, args...) rt_kprintf(fmt ,##args)
  21. #else
  22. #define USBD_DBG(fmt, args...)
  23. #endif
  24. static void __delay(void)
  25. {
  26. int i;
  27. for (i = 0; i < 1000; i++);
  28. }
  29. static struct ep_id __ep_pool[] =
  30. {
  31. {0x00, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED},
  32. #if DWC_FORCE_SPEED_FULL
  33. {0x01, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  34. {0x02, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  35. {0x02, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  36. {0x04, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  37. {0x04, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  38. #else
  39. {0x01, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  40. {0x01, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  41. {0x02, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  42. {0x02, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  43. {0x04, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  44. {0x04, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  45. {0x06, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  46. {0x06, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  47. #endif
  48. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED}
  49. };
  50. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  51. static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
  52. {
  53. unsigned char *buf = (unsigned char*)ptr;
  54. int i, j;
  55. for (i=0; i<buflen; i+=16)
  56. {
  57. rt_kprintf("%06x: ", i);
  58. for (j=0; j<16; j++)
  59. if (i+j < buflen)
  60. rt_kprintf("%02x ", buf[i+j]);
  61. else
  62. rt_kprintf(" ");
  63. rt_kprintf(" ");
  64. for (j=0; j<16; j++)
  65. if (i+j < buflen)
  66. rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.');
  67. rt_kprintf("\n");
  68. }
  69. }
  70. static rt_err_t __ep_set_stall(rt_uint8_t address)
  71. {
  72. // RT_ASSERT(address != 0);
  73. RT_DEBUG_LOG(RT_DEBUG_USB, ("ep set_stall, address 0x%x\n", address));
  74. dwc_set_ep_stall(&__dwc_hdl, address);
  75. return RT_EOK;
  76. }
  77. static rt_err_t __ep_clear_stall(rt_uint8_t address)
  78. {
  79. // RT_ASSERT(address != 0);
  80. RT_DEBUG_LOG(RT_DEBUG_USB, ("ep clear_stall, address 0x%x\n", address));
  81. dwc_clr_ep_stall(&__dwc_hdl, address);
  82. return RT_EOK;
  83. }
  84. static rt_err_t __set_address(rt_uint8_t address)
  85. {
  86. RT_DEBUG_LOG(RT_DEBUG_USB,("set address, 0x%x\n", address));
  87. dwc_set_address(&__dwc_hdl,address);
  88. return RT_EOK;
  89. }
  90. static rt_err_t __set_config(rt_uint8_t address)
  91. {
  92. RT_DEBUG_LOG(RT_DEBUG_USB,("%s, 0x%x\n", __func__,address));
  93. //init EP0
  94. __dwc_hdl.status.b.state = USB_CONFIGURED;
  95. return RT_EOK;
  96. }
  97. static rt_err_t __ep_enable(uep_t ep)
  98. {
  99. dwc_ep* dwc_ep;
  100. RT_ASSERT(ep != RT_NULL);
  101. RT_ASSERT(ep->ep_desc != RT_NULL);
  102. RT_DEBUG_LOG(RT_DEBUG_USB,("%s ,address = %02x\n", __func__,EP_ADDRESS(ep)));
  103. if(ep->id->dir == USB_DIR_IN)
  104. dwc_enable_in_ep(&__dwc_hdl,ep->id->addr);
  105. else
  106. dwc_enable_out_ep(&__dwc_hdl,ep->id->addr);
  107. return RT_EOK;
  108. }
  109. static rt_err_t __ep_disable(uep_t ep)
  110. {
  111. RT_ASSERT(ep != RT_NULL);
  112. RT_ASSERT(ep->ep_desc != RT_NULL);
  113. RT_DEBUG_LOG(RT_DEBUG_USB,("%s\n", __func__));
  114. // USB_DisableEP(EP_ADDRESS(ep));
  115. return RT_EOK;
  116. }
  117. static rt_size_t __ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  118. {
  119. dwc_ep *pep ;
  120. RT_DEBUG_LOG(RT_DEBUG_USB,("%s address = %02x,size = %d\n", __func__,address,size));
  121. pep = __dwc_hdl.dep[(address & 0x0F) + DWC_EP_OUT_OFS];
  122. pep->ep_state = EP_DATA;
  123. pep->xfer_len = size;
  124. // pep->xfer_buff = buffer;
  125. pep->xfer_count = 0;
  126. dwc_handle_ep_data_out_phase(&__dwc_hdl, address);
  127. return size;
  128. }
  129. static rt_size_t __ep_read(rt_uint8_t address, void *buffer)
  130. {
  131. rt_size_t size = 0;
  132. RT_ASSERT(buffer != RT_NULL);
  133. RT_DEBUG_LOG(RT_DEBUG_USB,("%s\n", __func__));
  134. size = HW_GetPKT(&__dwc_hdl,address,(uint8_t *)buffer,0);
  135. return size;
  136. }
  137. static rt_size_t __ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  138. {
  139. RT_DEBUG_LOG(RT_DEBUG_USB,("%s address = %02x,buffer = %08x ,size = %d\n", __func__,address,(uint32_t)buffer,size));
  140. size = HW_SendPKT(&__dwc_hdl,address,(const uint8_t *)buffer,size);
  141. return size;
  142. }
  143. static rt_err_t __ep0_send_status(void)
  144. {
  145. RT_DEBUG_LOG(RT_DEBUG_USB,("%s\n", __func__));
  146. HW_SendPKT(&__dwc_hdl,0,0,0);
  147. return RT_EOK;
  148. }
  149. static rt_err_t __suspend(void)
  150. {
  151. RT_DEBUG_LOG(RT_DEBUG_USB,("%s\n", __func__));
  152. return RT_EOK;
  153. }
  154. static rt_err_t __wakeup(void)
  155. {
  156. RT_DEBUG_LOG(RT_DEBUG_USB,("%s\n", __func__));
  157. return RT_EOK;
  158. }
  159. static rt_err_t __init(rt_device_t device)
  160. {
  161. int epidx = 0, epnum = 0;
  162. __dwc_hdl.status.b.state = USB_CABLE_DISCONNECT;
  163. /* clear all dep */
  164. for (epidx = 0; epidx < 32; epidx++)
  165. {
  166. __dwc_hdl.dep[epidx] = RT_NULL;
  167. }
  168. for (epidx = 0; __ep_pool[epidx].addr != 0xFF; ++epidx)
  169. {
  170. dwc_ep *pep = RT_NULL;
  171. rt_uint8_t *pXfer = RT_NULL;
  172. if(epidx == 0)
  173. { /* EP0 is IN-OUT */
  174. pep = (dwc_ep *) rt_malloc(sizeof(dwc_ep));
  175. if (!pep)
  176. {
  177. rt_kprintf("ERROR: no memory for pep\n");
  178. while (1) ;
  179. }
  180. /* malloc memory for EP */
  181. pXfer = rt_malloc_align(__ep_pool[epidx].maxpacket * 2, 32);
  182. if (!pXfer)
  183. {
  184. rt_kprintf("ERROR: no memory for pXfer\n");
  185. while (1) ;
  186. }
  187. /* init pep */
  188. {
  189. pep->num = 0;
  190. pep->ep_state = EP_SETUP;
  191. pep->is_in = 0;
  192. pep->active = 0;
  193. pep->type = DWC_OTG_EP_TYPE_CONTROL;
  194. pep->maxpacket = __ep_pool[epidx].maxpacket;
  195. pep->xfer_buff = (void *)UNCACHED(pXfer);
  196. pep->xfer_len = 0;
  197. pep->xfer_count = 0;
  198. }
  199. __dwc_hdl.dep[0 + DWC_EP_IN_OFS] = pep;
  200. __dwc_hdl.dep[0 + DWC_EP_OUT_OFS] = pep;
  201. }
  202. else
  203. {
  204. pep = (dwc_ep *) rt_malloc(sizeof(dwc_ep));
  205. if (!pep)
  206. {
  207. rt_kprintf("ERROR: no memory for pep\n");
  208. while (1) ;
  209. }
  210. /* malloc memory for EP */
  211. pXfer = rt_malloc_align(__ep_pool[epidx].maxpacket * 2, 32);
  212. if (!pXfer)
  213. {
  214. rt_kprintf("ERROR: no memory for pXfer\n");
  215. while (1) ;
  216. }
  217. /* init pep */
  218. {
  219. pep->num = __ep_pool[epidx].addr;
  220. pep->ep_state = EP_IDLE;
  221. pep->is_in = (__ep_pool[epidx].dir == USB_DIR_IN) ? 1 : 0;
  222. pep->active = 0;
  223. pep->type = __ep_pool[epidx].type;
  224. pep->maxpacket = __ep_pool[epidx].maxpacket;
  225. pep->xfer_buff = (void *)UNCACHED(pXfer);
  226. pep->xfer_len = 0;
  227. pep->xfer_count = 0;
  228. }
  229. if(__ep_pool[epidx].dir == USB_DIR_OUT)
  230. epnum = __ep_pool[epidx].addr + DWC_EP_OUT_OFS;
  231. else
  232. epnum = __ep_pool[epidx].addr + DWC_EP_IN_OFS;
  233. __dwc_hdl.dep[epnum] = pep;
  234. }
  235. }
  236. x1000_usbd_init(&__dwc_hdl);
  237. {
  238. dwc_ep *pep = __dwc_hdl.dep[18];
  239. // rt_kprintf("18 pep->is_in = %d\n",pep->is_in);
  240. // rt_kprintf("18 xfer_buff = %08x\n",(uint32_t)pep->xfer_buff);
  241. }
  242. return RT_EOK;
  243. }
  244. static struct udcd_ops __x1000_usbd_ops =
  245. {
  246. __set_address,
  247. __set_config,
  248. __ep_set_stall,
  249. __ep_clear_stall,
  250. __ep_enable,
  251. __ep_disable,
  252. __ep_read_prepare,
  253. __ep_read,
  254. __ep_write,
  255. __ep0_send_status,
  256. __suspend,
  257. __wakeup,
  258. };
  259. void x1000_usbd_event_cb(uint8_t address,uint32_t event,void *arg)
  260. {
  261. switch (event)
  262. {
  263. case USB_EVT_SETUP:
  264. USBD_DBG("USB_EVT_SETUP\n");
  265. if(address == 0)
  266. {
  267. rt_usbd_ep0_setup_handler(&__x1000_usbd, (struct urequest*)arg);
  268. }
  269. break;
  270. case USB_EVT_OUT:
  271. USBD_DBG("USB_EVT_OUT\n");
  272. if(address == 0)
  273. rt_usbd_ep0_out_handler(&__x1000_usbd, (rt_size_t)arg);
  274. else
  275. rt_usbd_ep_out_handler(&__x1000_usbd, USB_DIR_OUT | address, 0);
  276. break;
  277. case USB_EVT_IN:
  278. USBD_DBG("USB_EVT_IN\n");
  279. if(address == 0)
  280. rt_usbd_ep0_in_handler(&__x1000_usbd);
  281. else
  282. rt_usbd_ep_in_handler(&__x1000_usbd, USB_DIR_IN | address,__dwc_hdl.dep[DWC_EP_IN_OFS + address]->xfer_count);
  283. break;
  284. case USB_EVT_SOF:
  285. rt_usbd_sof_handler(&__x1000_usbd);
  286. break;
  287. default:
  288. break;
  289. }
  290. }
  291. int x1000_usbd_register(void)
  292. {
  293. rt_memset((void *)&__x1000_usbd, 0, sizeof(struct udcd));
  294. __x1000_usbd.parent.type = RT_Device_Class_USBDevice;
  295. __x1000_usbd.parent.init = __init;
  296. __x1000_usbd.ops = &__x1000_usbd_ops;
  297. /* Register endpoint infomation */
  298. __x1000_usbd.ep_pool = __ep_pool;
  299. __x1000_usbd.ep0.id = &__ep_pool[0];
  300. rt_device_register(&__x1000_usbd.parent, "usbd", 0);
  301. rt_usb_device_init();
  302. return RT_EOK;
  303. }
  304. INIT_ENV_EXPORT(x1000_usbd_register);