drv_usbd.c 9.5 KB

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