drv_usbh.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-05-25 CDT first version
  9. */
  10. /*******************************************************************************
  11. * Include files
  12. ******************************************************************************/
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #if defined(BSP_USING_USBH)
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.usbh"
  18. #include <drv_log.h>
  19. #include "board_config.h"
  20. #include "irq_config.h"
  21. #include "drv_usbh.h"
  22. #if defined(HC32F472)
  23. #define USBFS_DRVVBUS_PIN (rt_base_t)(((rt_uint16_t)USBF_DRVVBUS_PORT * 16) + __CLZ(__RBIT(USBF_DRVVBUS_PIN)))
  24. #endif
  25. #if !defined(BSP_USING_USBH_HS)
  26. extern rt_err_t rt_hw_usbfs_board_init(void);
  27. #else
  28. extern rt_err_t rt_hw_usbhs_board_init(void);
  29. #endif
  30. extern void rt_hw_us_delay(rt_uint32_t us);
  31. static usb_core_instance _hc32_usbh;
  32. static volatile rt_bool_t connect_status = RT_FALSE;
  33. static struct rt_completion urb_completion;
  34. void usb_udelay(const uint32_t usec)
  35. {
  36. rt_hw_us_delay(usec);
  37. }
  38. void usb_mdelay(const uint32_t msec)
  39. {
  40. rt_thread_mdelay(msec);
  41. }
  42. void usb_bsp_cfgvbus(usb_core_instance *pdev)
  43. {
  44. #if defined(HC32F472)
  45. rt_pin_mode(USBFS_DRVVBUS_PIN, PIN_MODE_OUTPUT);
  46. #endif
  47. }
  48. void usb_bsp_drivevbus(usb_core_instance *pdev, uint8_t state)
  49. {
  50. #if defined(HC32F472)
  51. if (0x00U == state)
  52. {
  53. rt_pin_write(USBFS_DRVVBUS_PIN, PIN_LOW);
  54. }
  55. else
  56. {
  57. rt_pin_write(USBFS_DRVVBUS_PIN, PIN_HIGH);
  58. }
  59. #endif
  60. }
  61. static void usb_device_connect_callback(usb_core_instance *pdev)
  62. {
  63. uhcd_t hcd = (uhcd_t)pdev->pData;
  64. if (!connect_status)
  65. {
  66. connect_status = RT_TRUE;
  67. LOG_D("usb connected");
  68. rt_usbh_root_hub_connect_handler(hcd, USB_FS_PORT, RT_FALSE);
  69. }
  70. }
  71. static void usb_device_disconnect_callback(usb_core_instance *pdev)
  72. {
  73. uhcd_t hcd = (uhcd_t)pdev->pData;
  74. if (connect_status)
  75. {
  76. connect_status = RT_FALSE;
  77. LOG_D("usb disconnnect");
  78. rt_usbh_root_hub_disconnect_handler(hcd, USB_FS_PORT);
  79. }
  80. }
  81. static void usb_host_notify_urbchange_Callback(usb_core_instance *pdev, uint8_t chnum, HOST_CH_XFER_STATE urb_state)
  82. {
  83. rt_completion_done(&urb_completion);
  84. }
  85. static void usb_host_chx_out_isr(usb_core_instance *pdev, uint8_t chnum)
  86. {
  87. uint32_t u32hcchar;
  88. uint32_t u32hcint;
  89. uint32_t u32hcintmsk;
  90. u32hcchar = READ_REG32(pdev->regs.HC_REGS[chnum]->HCCHAR);
  91. u32hcint = READ_REG32(pdev->regs.HC_REGS[chnum]->HCINT);
  92. u32hcintmsk = READ_REG32(pdev->regs.HC_REGS[chnum]->HCINTMSK);
  93. u32hcint = u32hcint & u32hcintmsk;
  94. if (0UL != (u32hcint & USBFS_HCINT_ACK))
  95. {
  96. usb_host_clrint(pdev, chnum, USBFS_HCINT_ACK);
  97. }
  98. #if defined (HC32F4A0) || defined (HC32F472) || defined (HC32F4A8)
  99. else if (0UL != (u32hcint & USBFS_HCINT_AHBERR))
  100. {
  101. usb_host_clrint(pdev, chnum, USBFS_HCINT_AHBERR);
  102. usb_host_int_unmskchhltd(pdev, chnum);
  103. }
  104. #endif
  105. else if (0UL != (u32hcint & USBFS_HCINT_FRMOR))
  106. {
  107. usb_host_int_unmskchhltd(pdev, chnum);
  108. usb_hchstop(&pdev->regs, chnum);
  109. usb_host_clrint(pdev, chnum, USBFS_HCINT_FRMOR);
  110. }
  111. else if (0UL != (u32hcint & USBFS_HCINT_XFRC))
  112. {
  113. pdev->host.ErrCnt[chnum] = 0U;
  114. usb_host_int_unmskchhltd(pdev, chnum);
  115. usb_hchstop(&pdev->regs, chnum);
  116. usb_host_clrint(pdev, chnum, USBFS_HCINT_XFRC);
  117. pdev->host.HC_Status[chnum] = HOST_CH_XFERCOMPL;
  118. }
  119. else if (0UL != (u32hcint & USBFS_HCINT_STALL))
  120. {
  121. usb_host_clrint(pdev, chnum, USBFS_HCINT_STALL);
  122. usb_host_int_unmskchhltd(pdev, chnum);
  123. usb_hchstop(&pdev->regs, chnum);
  124. pdev->host.HC_Status[chnum] = HOST_CH_STALL;
  125. }
  126. else if (0UL != (u32hcint & USBFS_HCINT_NAK))
  127. {
  128. pdev->host.ErrCnt[chnum] = 0U;
  129. usb_host_int_unmskchhltd(pdev, chnum);
  130. usb_hchstop(&pdev->regs, chnum);
  131. usb_host_clrint(pdev, chnum, USBFS_HCINT_NAK);
  132. pdev->host.HC_Status[chnum] = HOST_CH_NAK;
  133. }
  134. else if (0UL != (u32hcint & USBFS_HCINT_TXERR))
  135. {
  136. usb_host_int_unmskchhltd(pdev, chnum);
  137. usb_hchstop(&pdev->regs, chnum);
  138. pdev->host.ErrCnt[chnum] ++;
  139. pdev->host.HC_Status[chnum] = HOST_CH_XACTERR;
  140. usb_host_clrint(pdev, chnum, USBFS_HCINT_TXERR);
  141. }
  142. else if (0UL != (u32hcint & HCINT_NYET))
  143. {
  144. pdev->host.ErrCnt[chnum] = 0U;
  145. usb_host_int_unmskchhltd(pdev, chnum);
  146. usb_hchstop(&pdev->regs, chnum);
  147. usb_host_clrint(pdev, chnum, HCINT_NYET);
  148. pdev->host.HC_Status[chnum] = HOST_CH_NYET;
  149. }
  150. else if (0UL != (u32hcint & USBFS_HCINT_DTERR))
  151. {
  152. usb_host_int_unmskchhltd(pdev, chnum);
  153. usb_hchstop(&pdev->regs, chnum);
  154. usb_host_clrint(pdev, chnum, USBFS_HCINT_NAK);
  155. pdev->host.HC_Status[chnum] = HOST_CH_DATATGLERR;
  156. usb_host_clrint(pdev, chnum, USBFS_HCINT_DTERR);
  157. }
  158. else if (0UL != (u32hcint & USBFS_HCINT_CHH))
  159. {
  160. usb_host_int_mskchhltd(pdev, chnum);
  161. if (pdev->host.HC_Status[chnum] == HOST_CH_XFERCOMPL)
  162. {
  163. pdev->host.URB_State[chnum] = HOST_CH_XFER_DONE;
  164. if (((u32hcchar & USBFS_HCCHAR_EPTYP) >> USBFS_HCCHAR_EPTYP_POS) == EP_TYPE_BULK)
  165. {
  166. pdev->host.hc[chnum].out_toggle ^= 1U;
  167. }
  168. }
  169. else if (pdev->host.HC_Status[chnum] == HOST_CH_NAK)
  170. {
  171. pdev->host.URB_State[chnum] = HOST_CH_XFER_UNREADY;
  172. }
  173. else if (pdev->host.HC_Status[chnum] == HOST_CH_NYET)
  174. {
  175. if (pdev->host.hc[chnum].do_ping == 1U)
  176. {
  177. usb_pingtokenissue(&pdev->regs, chnum);
  178. }
  179. pdev->host.URB_State[chnum] = HOST_CH_XFER_UNREADY;
  180. }
  181. else if (pdev->host.HC_Status[chnum] == HOST_CH_STALL)
  182. {
  183. pdev->host.URB_State[chnum] = HOST_CH_XFER_STALL;
  184. }
  185. else if (pdev->host.HC_Status[chnum] == HOST_CH_XACTERR)
  186. {
  187. pdev->host.URB_State[chnum] = HOST_CH_XFER_ERROR;
  188. pdev->host.ErrCnt[chnum] = 0UL;
  189. }
  190. else
  191. {
  192. ;
  193. }
  194. usb_host_clrint(pdev, chnum, USBFS_HCINT_CHH);
  195. usb_host_notify_urbchange_Callback(pdev, chnum, pdev->host.URB_State[chnum]);
  196. }
  197. else
  198. {
  199. ;
  200. }
  201. }
  202. static void usb_host_chx_in_isr(usb_core_instance *pdev, uint8_t chnum)
  203. {
  204. uint32_t u32hcchar;
  205. uint32_t u32hctsiz;
  206. uint32_t u32eptypetmp;
  207. uint32_t u32hcint;
  208. uint32_t u32hcintmsk;
  209. u32hcchar = READ_REG32(pdev->regs.HC_REGS[chnum]->HCCHAR);
  210. u32hcint = READ_REG32(pdev->regs.HC_REGS[chnum]->HCINT);
  211. u32hcintmsk = READ_REG32(pdev->regs.HC_REGS[chnum]->HCINTMSK);
  212. u32hcint = u32hcint & u32hcintmsk;
  213. u32eptypetmp = (u32hcchar & USBFS_HCCHAR_EPTYP) >> USBFS_HCCHAR_EPTYP_POS;
  214. if (0UL != (u32hcint & USBFS_HCINT_ACK))
  215. {
  216. usb_host_clrint(pdev, chnum, USBFS_HCINT_ACK);
  217. }
  218. #if defined (HC32F4A0) || defined (HC32F472) || defined (HC32F4A8)
  219. else if (0UL != (u32hcint & USBFS_HCINT_AHBERR))
  220. {
  221. usb_host_clrint(pdev, chnum, USBFS_HCINT_AHBERR);
  222. usb_host_int_unmskchhltd(pdev, chnum);
  223. }
  224. #endif
  225. else if (0UL != (u32hcint & USBFS_HCINT_BBERR))
  226. {
  227. usb_host_clrint(pdev, chnum, USBFS_HCINT_BBERR);
  228. pdev->host.HC_Status[chnum] = HOST_CH_BBLERR;
  229. usb_host_int_unmskchhltd(pdev, chnum);
  230. usb_hchstop(&pdev->regs, chnum);
  231. }
  232. else if (0UL != (u32hcint & USBFS_HCINT_STALL))
  233. {
  234. usb_host_int_unmskchhltd(pdev, chnum);
  235. pdev->host.HC_Status[chnum] = HOST_CH_STALL;
  236. usb_host_clrint(pdev, chnum, USBFS_HCINT_NAK);
  237. usb_host_clrint(pdev, chnum, USBFS_HCINT_STALL);
  238. usb_hchstop(&pdev->regs, chnum);
  239. }
  240. else if (0UL != (u32hcint & USBFS_HCINT_DTERR))
  241. {
  242. usb_host_int_unmskchhltd(pdev, chnum);
  243. usb_hchstop(&pdev->regs, chnum);
  244. usb_host_clrint(pdev, chnum, USBFS_HCINT_NAK);
  245. pdev->host.HC_Status[chnum] = HOST_CH_DATATGLERR;
  246. usb_host_clrint(pdev, chnum, USBFS_HCINT_DTERR);
  247. }
  248. else if (0UL != (u32hcint & USBFS_HCINT_FRMOR))
  249. {
  250. usb_host_int_unmskchhltd(pdev, chnum);
  251. usb_hchstop(&pdev->regs, chnum);
  252. usb_host_clrint(pdev, chnum, USBFS_HCINT_FRMOR);
  253. }
  254. else if (0UL != (u32hcint & USBFS_HCINT_XFRC))
  255. {
  256. if (pdev->basic_cfgs.dmaen == 1U)
  257. {
  258. u32hctsiz = READ_REG32(pdev->regs.HC_REGS[chnum]->HCTSIZ);
  259. pdev->host.XferCnt[chnum] = pdev->host.hc[chnum].xfer_len - (u32hctsiz & USBFS_HCTSIZ_XFRSIZ);
  260. pdev->host.hc[chnum].xfer_count += pdev->host.XferCnt[chnum];
  261. }
  262. pdev->host.HC_Status[chnum] = HOST_CH_XFERCOMPL;
  263. pdev->host.ErrCnt [chnum] = 0U;
  264. usb_host_clrint(pdev, chnum, USBFS_HCINT_XFRC);
  265. switch (u32eptypetmp)
  266. {
  267. case EP_TYPE_CTRL:
  268. case EP_TYPE_BULK:
  269. usb_host_int_unmskchhltd(pdev, chnum);
  270. usb_hchstop(&pdev->regs, chnum);
  271. usb_host_clrint(pdev, chnum, USBFS_HCINT_NAK);
  272. pdev->host.hc[chnum].in_toggle ^= (uint8_t)1;
  273. break;
  274. case EP_TYPE_INTR:
  275. u32hcchar |= USBFS_HCCHAR_ODDFRM;
  276. WRITE_REG32(pdev->regs.HC_REGS[chnum]->HCCHAR, u32hcchar);
  277. pdev->host.URB_State[chnum] = HOST_CH_XFER_DONE;
  278. usb_host_notify_urbchange_Callback(pdev, chnum, pdev->host.URB_State[chnum]);
  279. break;
  280. case EP_TYPE_ISOC:
  281. if (pdev->host.HC_Status[chnum] == HOST_CH_XFERCOMPL)
  282. {
  283. pdev->host.URB_State[chnum] = HOST_CH_XFER_DONE;
  284. usb_host_notify_urbchange_Callback(pdev, chnum, pdev->host.URB_State[chnum]);
  285. }
  286. break;
  287. default:
  288. break;
  289. }
  290. }
  291. else if (0UL != (u32hcint & USBFS_HCINT_CHH))
  292. {
  293. usb_host_int_mskchhltd(pdev, chnum);
  294. if (pdev->host.HC_Status[chnum] == HOST_CH_XFERCOMPL)
  295. {
  296. pdev->host.URB_State[chnum] = HOST_CH_XFER_DONE;
  297. }
  298. else if (pdev->host.HC_Status[chnum] == HOST_CH_STALL)
  299. {
  300. pdev->host.URB_State[chnum] = HOST_CH_XFER_STALL;
  301. }
  302. else if (pdev->host.HC_Status[chnum] == HOST_CH_XACTERR)
  303. {
  304. pdev->host.ErrCnt[chnum] = 0U;
  305. pdev->host.URB_State[chnum] = HOST_CH_XFER_ERROR;
  306. }
  307. else if (pdev->host.HC_Status[chnum] == HOST_CH_DATATGLERR)
  308. {
  309. pdev->host.ErrCnt[chnum] = 0U;
  310. pdev->host.URB_State[chnum] = HOST_CH_XFER_ERROR;
  311. }
  312. else if (pdev->host.HC_Status[chnum] == HOST_CH_BBLERR)
  313. {
  314. pdev->host.ErrCnt[chnum] ++;
  315. pdev->host.URB_State[chnum] = HOST_CH_XFER_ERROR;
  316. }
  317. else if (u32eptypetmp == EP_TYPE_INTR)
  318. {
  319. pdev->host.hc[chnum].in_toggle ^= (uint8_t)1;
  320. }
  321. else
  322. {
  323. ;
  324. }
  325. usb_host_clrint(pdev, chnum, USBFS_HCINT_CHH);
  326. usb_host_notify_urbchange_Callback(pdev, chnum, pdev->host.URB_State[chnum]);
  327. }
  328. else if (0UL != (u32hcint & USBFS_HCINT_TXERR))
  329. {
  330. usb_host_int_unmskchhltd(pdev, chnum);
  331. pdev->host.ErrCnt[chnum] ++;
  332. pdev->host.HC_Status[chnum] = HOST_CH_XACTERR;
  333. usb_hchstop(&pdev->regs, chnum);
  334. usb_host_clrint(pdev, chnum, USBFS_HCINT_TXERR);
  335. }
  336. else if (0UL != (u32hcint & USBFS_HCINT_NAK))
  337. {
  338. if (u32eptypetmp == EP_TYPE_INTR)
  339. {
  340. usb_host_int_unmskchhltd(pdev, chnum);
  341. usb_hchstop(&pdev->regs, chnum);
  342. }
  343. else if (u32eptypetmp == EP_TYPE_CTRL)
  344. {
  345. u32hcchar |= USBFS_HCCHAR_CHENA;
  346. u32hcchar &= ~USBFS_HCCHAR_CHDIS;
  347. WRITE_REG32(pdev->regs.HC_REGS[chnum]->HCCHAR, u32hcchar);
  348. }
  349. else if (u32eptypetmp == EP_TYPE_BULK)
  350. {
  351. usb_host_int_unmskchhltd(pdev, chnum);
  352. usb_hchstop(&pdev->regs, chnum);/* stop hc avoid block */
  353. }
  354. else
  355. {
  356. ;
  357. }
  358. pdev->host.HC_Status[chnum] = HOST_CH_NAK;
  359. usb_host_clrint(pdev, chnum, USBFS_HCINT_NAK);
  360. }
  361. else
  362. {
  363. ;
  364. }
  365. }
  366. static void usb_host_hc_isr(usb_core_instance *pdev)
  367. {
  368. uint32_t u32hcchar;
  369. uint8_t u8Cnt;
  370. uint32_t u32haint;
  371. u32haint = READ_REG32(pdev->regs.HREGS->HAINT);
  372. for (u8Cnt = 0U; u8Cnt < pdev->basic_cfgs.host_chnum; u8Cnt++)
  373. {
  374. if (0UL != (u32haint & (1UL << u8Cnt)))
  375. {
  376. u32hcchar = READ_REG32(pdev->regs.HC_REGS[u8Cnt]->HCCHAR);
  377. if (0UL != ((u32hcchar & USBFS_HCCHAR_EPDIR) >> USBFS_HCCHAR_EPDIR_POS))
  378. {
  379. usb_host_chx_in_isr(pdev, u8Cnt);
  380. }
  381. else
  382. {
  383. usb_host_chx_out_isr(pdev, u8Cnt);
  384. }
  385. }
  386. }
  387. }
  388. static void usb_host_sof_isr(usb_core_instance *pdev)
  389. {
  390. WRITE_REG32(pdev->regs.GREGS->GINTSTS, USBFS_GINTSTS_SOF);
  391. }
  392. static void usb_host_disconn_isr(usb_core_instance *pdev)
  393. {
  394. WRITE_REG32(pdev->regs.GREGS->GINTSTS, USBFS_GINTSTS_DISCINT);
  395. /* flush all the txFIFOs and the whole rxFIFO */
  396. usb_rxfifoflush(&pdev->regs);
  397. usb_txfifoflush(&pdev->regs, 0x10UL);
  398. pdev->host.is_dev_connect = 0U;
  399. usb_device_disconnect_callback(pdev);
  400. }
  401. #define USBFS_HNPTXSTS_NPTXQTOP_CHEPNUM_POS (27U)
  402. #define USBFS_HNPTXSTS_NPTXQTOP_CHEPNUM (0x78000000UL)
  403. static void usb_host_nptxfifoempty_isr(usb_core_instance *pdev)
  404. {
  405. uint32_t u32hnptxsts;
  406. uint16_t u16LenWord;
  407. uint16_t u16Len;
  408. uint8_t u8ChNum;
  409. u32hnptxsts = READ_REG32(pdev->regs.GREGS->HNPTXSTS);
  410. u8ChNum = (uint8_t)((u32hnptxsts & USBFS_HNPTXSTS_NPTXQTOP_CHEPNUM) >> USBFS_HNPTXSTS_NPTXQTOP_CHEPNUM_POS) % USB_MAX_TX_FIFOS;
  411. u16LenWord = (uint16_t)((pdev->host.hc[u8ChNum].xfer_len + 3UL) / 4UL);
  412. while (((u32hnptxsts & USBFS_HNPTXSTS_NPTXFSAV) > u16LenWord) && (pdev->host.hc[u8ChNum].xfer_len != 0U))
  413. {
  414. u16Len = (uint16_t)((u32hnptxsts & USBFS_HNPTXSTS_NPTXFSAV) * 4UL);
  415. if (u16Len > pdev->host.hc[u8ChNum].xfer_len)
  416. {
  417. u16Len = (uint16_t)pdev->host.hc[u8ChNum].xfer_len;
  418. CLR_REG32_BIT(pdev->regs.GREGS->GINTMSK, USBFS_GINTSTS_NPTXFE);
  419. }
  420. u16LenWord = (uint16_t)((pdev->host.hc[u8ChNum].xfer_len + 3UL) / 4UL);
  421. usb_wrpkt(&pdev->regs, pdev->host.hc[u8ChNum].xfer_buff, u8ChNum, u16Len, pdev->basic_cfgs.dmaen);
  422. pdev->host.hc[u8ChNum].xfer_buff += u16Len;
  423. pdev->host.hc[u8ChNum].xfer_len -= u16Len;
  424. pdev->host.hc[u8ChNum].xfer_count += u16Len;
  425. u32hnptxsts = READ_REG32(pdev->regs.GREGS->HNPTXSTS);
  426. }
  427. }
  428. #define USBFS_HPTXSTS_PTXQTOP_CHNUM_POS (27U)
  429. #define USBFS_HPTXSTS_PTXQTOP_CHNUM (0x78000000UL)
  430. static void usb_host_ptxfifoempty_isr(usb_core_instance *pdev)
  431. {
  432. uint32_t u32hptxsts;
  433. uint16_t u16LenWord;
  434. uint16_t u16Len;
  435. uint8_t u8ChNum;
  436. u32hptxsts = READ_REG32(pdev->regs.HREGS->HPTXSTS);
  437. u8ChNum = (uint8_t)((u32hptxsts & USBFS_HPTXSTS_PTXQTOP_CHNUM) >> USBFS_HPTXSTS_PTXQTOP_CHNUM_POS) % USB_MAX_TX_FIFOS;
  438. u16LenWord = (uint16_t)((pdev->host.hc[u8ChNum].xfer_len + 3UL) / 4UL);
  439. while ((((u32hptxsts & USBFS_HPTXSTS_PTXFSAVL)) > u16LenWord) && (pdev->host.hc[u8ChNum].xfer_len != 0U))
  440. {
  441. u16Len = (uint16_t)((u32hptxsts & USBFS_HPTXSTS_PTXFSAVL) * 4UL);
  442. if (u16Len > pdev->host.hc[u8ChNum].xfer_len)
  443. {
  444. u16Len = (uint16_t)pdev->host.hc[u8ChNum].xfer_len;
  445. CLR_REG32_BIT(pdev->regs.GREGS->GINTMSK, USBFS_GINTMSK_PTXFEM);
  446. }
  447. u16LenWord = (uint16_t)((pdev->host.hc[u8ChNum].xfer_len + 3UL) / 4UL);
  448. usb_wrpkt(&pdev->regs, pdev->host.hc[u8ChNum].xfer_buff, u8ChNum, u16Len, pdev->basic_cfgs.dmaen);
  449. pdev->host.hc[u8ChNum].xfer_buff += u16Len;
  450. pdev->host.hc[u8ChNum].xfer_len -= u16Len;
  451. pdev->host.hc[u8ChNum].xfer_count += u16Len;
  452. u32hptxsts = READ_REG32(pdev->regs.HREGS->HPTXSTS);
  453. }
  454. }
  455. static uint32_t host_driver_getvbusdrivestate(usb_core_instance *pdev)
  456. {
  457. uint32_t u32hppt;
  458. u32hppt = READ_REG32(*pdev->regs.HPRT);
  459. return ((u32hppt & USBFS_HPRT_PWPR) >> USBFS_HPRT_PWPR_POS);
  460. }
  461. static void usb_host_port_isr(usb_core_instance *pdev)
  462. {
  463. uint32_t u32hprt;
  464. uint32_t u32hprt_bk;
  465. uint8_t u8fslspclksel;
  466. uint32_t do_reset = 0UL;
  467. uint8_t u8PortSpeed;
  468. u32hprt = READ_REG32(*pdev->regs.HPRT);
  469. u32hprt_bk = u32hprt;
  470. /* Clear the interrupt bits in GINTSTS */
  471. u32hprt_bk &= ~(USBFS_HPRT_PENA | USBFS_HPRT_PCDET | USBFS_HPRT_PENCHNG);
  472. /* check if a port connect have been detected */
  473. if ((u32hprt & USBFS_HPRT_PCDET) != 0UL)
  474. {
  475. u32hprt_bk |= USBFS_HPRT_PCDET;
  476. if (host_driver_getvbusdrivestate(pdev) != 0UL)
  477. {
  478. pdev->host.is_dev_connect = 1U;
  479. usb_device_connect_callback(pdev);
  480. }
  481. }
  482. /* check if port enable or disable change */
  483. if ((u32hprt & USBFS_HPRT_PENCHNG) != 0UL)
  484. {
  485. u32hprt_bk |= USBFS_HPRT_PENCHNG;
  486. if ((u32hprt & USBFS_HPRT_PENA) != 0UL)
  487. {
  488. u8PortSpeed = (uint8_t)((u32hprt & USBFS_HPRT_PSPD) >> USBFS_HPRT_PSPD_POS);
  489. pdev->host.devspeed = u8PortSpeed;
  490. if ((u8PortSpeed == PRTSPD_LOW_SPEED) || (u8PortSpeed == PRTSPD_FULL_SPEED))
  491. {
  492. u8fslspclksel = (uint8_t)(READ_REG32(pdev->regs.HREGS->HCFG) & USBFS_HCFG_FSLSPCS);
  493. if (u8PortSpeed == PRTSPD_LOW_SPEED)
  494. {
  495. if (u8fslspclksel != HCFG_6_MHZ)
  496. {
  497. do_reset = 1U;
  498. }
  499. }
  500. else
  501. {
  502. /* 1ms*(PHY clock frequency for FS/LS)-1 */
  503. WRITE_REG32(pdev->regs.HREGS->HFIR, 48000UL);
  504. if (u8fslspclksel != HCFG_48_MHZ)
  505. {
  506. usb_fslspclkselset(&pdev->regs, HCFG_48_MHZ);
  507. do_reset = 1U;
  508. }
  509. }
  510. }
  511. else
  512. {
  513. do_reset = 1U;
  514. }
  515. }
  516. }
  517. if (0UL != do_reset)
  518. {
  519. /* RTT Will Call Reset */
  520. }
  521. WRITE_REG32(*pdev->regs.HPRT, u32hprt_bk);
  522. }
  523. static void usb_host_rxflvl_isr(usb_core_instance *pdev)
  524. {
  525. uint32_t u32grxsts;
  526. uint32_t u32hctsiz;
  527. uint32_t u32hcchar;
  528. uint8_t u8chnum;
  529. uint8_t *pu8Tmp;
  530. uint16_t u16bcnt;
  531. CLR_REG32_BIT(pdev->regs.GREGS->GINTMSK, USBFS_GINTSTS_RXFNE);
  532. u32grxsts = READ_REG32(pdev->regs.GREGS->GRXSTSP);
  533. u8chnum = (uint8_t)(u32grxsts & USBFS_GRXSTSP_CHNUM_EPNUM) % USB_MAX_TX_FIFOS;
  534. u16bcnt = (uint16_t)((u32grxsts & USBFS_GRXSTSP_BCNT) >> USBFS_GRXSTSP_BCNT_POS);
  535. u32hcchar = READ_REG32(pdev->regs.HC_REGS[u8chnum]->HCCHAR);
  536. switch ((u32grxsts & USBFS_GRXSTSP_PKTSTS) >> USBFS_GRXSTSP_PKTSTS_POS)
  537. {
  538. case 2: /* IN dat packet received */
  539. pu8Tmp = pdev->host.hc[u8chnum].xfer_buff;
  540. if ((u16bcnt > 0U) && (pu8Tmp != (void *)0U))
  541. {
  542. usb_rdpkt(&pdev->regs, pdev->host.hc[u8chnum].xfer_buff, u16bcnt);
  543. pdev->host.hc[u8chnum].xfer_buff += u16bcnt;
  544. pdev->host.hc[u8chnum].xfer_count += u16bcnt;
  545. pdev->host.XferCnt[u8chnum] = pdev->host.hc[u8chnum].xfer_count;
  546. u32hctsiz = READ_REG32(pdev->regs.HC_REGS[u8chnum]->HCTSIZ);
  547. if (((u32hctsiz & USBFS_HCTSIZ_PKTCNT) >> USBFS_HCTSIZ_PKTCNT_POS) > 0U)
  548. {
  549. u32hcchar |= USBFS_HCCHAR_CHENA;
  550. u32hcchar &= ~USBFS_HCCHAR_CHDIS;
  551. WRITE_REG32(pdev->regs.HC_REGS[u8chnum]->HCCHAR, u32hcchar);
  552. }
  553. }
  554. break;
  555. case 3: /* IN transfer completed(trigger an interrupt) */
  556. break;
  557. case 5: /* Daat toggle error(trigger an interrupt) */
  558. break;
  559. case 7: /* Channel halted(trigger an interrupt) */
  560. break;
  561. default:
  562. break;
  563. }
  564. SET_REG32_BIT(pdev->regs.GREGS->GINTMSK, USBFS_GINTSTS_RXFNE);
  565. }
  566. static void usb_host_incomplisoout_isr(usb_core_instance *pdev)
  567. {
  568. SET_REG32_BIT(pdev->regs.HC_REGS[0]->HCCHAR, USBFS_HCCHAR_CHENA | USBFS_HCCHAR_CHDIS);
  569. WRITE_REG32(pdev->regs.GREGS->GINTSTS, USBFS_GINTSTS_IPXFR_INCOMPISOOUT);
  570. }
  571. static void usb_host_wkupint_isr(usb_core_instance *pdev)
  572. {
  573. uint32_t u32hprt;
  574. u32hprt = usb_rdhprt(&pdev->regs);
  575. u32hprt &= ~USBFS_HPRT_PRES;
  576. WRITE_REG32(*pdev->regs.HPRT, u32hprt);
  577. }
  578. /**
  579. * @brief This function process all interrupt of USB in host mode
  580. * @param [in] pdev device instance
  581. * @retval None
  582. */
  583. static void usb_host_isr(usb_core_instance *pdev)
  584. {
  585. uint32_t gintstsval;
  586. if (0U != usb_getcurmod(&pdev->regs))
  587. {
  588. gintstsval = usb_getcoreintr(&pdev->regs);
  589. if (0UL != (gintstsval & USBFS_GINTSTS_SOF))
  590. {
  591. usb_host_sof_isr(pdev);
  592. }
  593. if (0UL != (gintstsval & USBFS_GINTSTS_RXFNE))
  594. {
  595. usb_host_rxflvl_isr(pdev);
  596. }
  597. if (0UL != (gintstsval & USBFS_GINTSTS_NPTXFE))
  598. {
  599. usb_host_nptxfifoempty_isr(pdev);
  600. }
  601. if (0UL != (gintstsval & USBFS_GINTSTS_PTXFE))
  602. {
  603. usb_host_ptxfifoempty_isr(pdev);
  604. }
  605. if (0UL != (gintstsval & USBFS_GINTSTS_HCINT))
  606. {
  607. usb_host_hc_isr(pdev);
  608. }
  609. if (0UL != (gintstsval & USBFS_GINTSTS_HPRTINT))
  610. {
  611. usb_host_port_isr(pdev);
  612. }
  613. if (0UL != (gintstsval & USBFS_GINTSTS_DISCINT))
  614. {
  615. usb_host_disconn_isr(pdev);
  616. }
  617. if (0UL != (gintstsval & USBFS_GINTSTS_IPXFR_INCOMPISOOUT))
  618. {
  619. usb_host_incomplisoout_isr(pdev);
  620. }
  621. if (0UL != (gintstsval & USBFS_GINTSTS_WKUINT))
  622. {
  623. usb_host_wkupint_isr(pdev);
  624. }
  625. }
  626. }
  627. static void usbh_irq_handler(void)
  628. {
  629. rt_interrupt_enter();
  630. usb_host_isr(&_hc32_usbh);
  631. rt_interrupt_leave();
  632. }
  633. #if defined(HC32F472)
  634. void USBFS_Handler(void)
  635. {
  636. usbh_irq_handler();
  637. }
  638. #endif
  639. static void usb_host_chopen(usb_core_instance *pdev,
  640. uint8_t hc_num,
  641. uint8_t epnum,
  642. uint8_t dev_address,
  643. uint8_t speed,
  644. uint8_t ep_type,
  645. uint16_t mps)
  646. {
  647. pdev->host.channel[hc_num] = epnum; /* assign channel here */
  648. pdev->host.hc[hc_num].ep_idx = (uint8_t) pdev->host.channel[hc_num] & 0x7FU;
  649. pdev->host.hc[hc_num].is_epin = (uint8_t)((pdev->host.channel[hc_num] & 0x80U) == 0x80U);
  650. pdev->host.hc[hc_num].dev_addr = dev_address;
  651. pdev->host.hc[hc_num].ep_type = ep_type;
  652. pdev->host.hc[hc_num].max_packet = mps;
  653. pdev->host.hc[hc_num].ch_speed = speed;
  654. pdev->host.hc[hc_num].in_toggle = 0U;
  655. pdev->host.hc[hc_num].out_toggle = 0U;
  656. (void)usb_inithch(&pdev->regs, hc_num, &pdev->host.hc[hc_num], pdev->basic_cfgs.dmaen);
  657. }
  658. static void usb_host_ch_init(usb_core_instance *pdev,
  659. uint8_t hc_num,
  660. uint8_t epnum,
  661. uint8_t dev_address,
  662. uint8_t speed,
  663. uint8_t ep_type,
  664. uint16_t mps)
  665. {
  666. pdev->host.channel[hc_num] = epnum; /* assign channel here */
  667. pdev->host.hc[hc_num].ep_idx = (uint8_t) pdev->host.channel[hc_num] & 0x7FU;
  668. pdev->host.hc[hc_num].is_epin = (uint8_t)((pdev->host.channel[hc_num] & 0x80U) == 0x80U);
  669. pdev->host.hc[hc_num].dev_addr = dev_address;
  670. pdev->host.hc[hc_num].ep_type = ep_type;
  671. pdev->host.hc[hc_num].max_packet = mps;
  672. pdev->host.hc[hc_num].ch_speed = speed;
  673. (void)usb_inithch(&pdev->regs, hc_num, &pdev->host.hc[hc_num], pdev->basic_cfgs.dmaen);
  674. }
  675. static uint32_t usb_host_submitrequest(usb_core_instance *pdev,
  676. uint8_t ch_num,
  677. uint8_t direction,
  678. uint8_t ep_type,
  679. uint8_t token,
  680. uint8_t *pbuff,
  681. uint16_t length,
  682. uint8_t do_ping)
  683. {
  684. pdev->host.hc[ch_num].is_epin = direction;
  685. pdev->host.hc[ch_num].ep_type = ep_type;
  686. if (token == 0U)
  687. {
  688. pdev->host.hc[ch_num].pid_type = PID_SETUP;
  689. pdev->host.hc[ch_num].do_ping = do_ping;
  690. }
  691. else
  692. {
  693. pdev->host.hc[ch_num].pid_type = PID_DATA1;
  694. }
  695. /* Manage Data Toggle */
  696. switch (ep_type)
  697. {
  698. case EP_TYPE_CTRL:
  699. if ((token == 1U) && (direction == 0U)) /*send data */
  700. {
  701. if (length == 0U)
  702. {
  703. /* For Status OUT stage, Length==0, Status Out PID = 1 */
  704. pdev->host.hc[ch_num].out_toggle = 1U;
  705. }
  706. /* Set the Data Toggle bit as per the Flag */
  707. if (pdev->host.hc[ch_num].out_toggle == 0U)
  708. {
  709. /* Put the PID 0 */
  710. pdev->host.hc[ch_num].pid_type = PID_DATA0;
  711. }
  712. else
  713. {
  714. /* Put the PID 1 */
  715. pdev->host.hc[ch_num].pid_type = PID_DATA1;
  716. }
  717. }
  718. break;
  719. case EP_TYPE_BULK:
  720. if (direction == 0U)
  721. {
  722. /* Set the Data Toggle bit as per the Flag */
  723. if (pdev->host.hc[ch_num].out_toggle == 0U)
  724. {
  725. /* Put the PID 0 */
  726. pdev->host.hc[ch_num].pid_type = PID_DATA0;
  727. }
  728. else
  729. {
  730. /* Put the PID 1 */
  731. pdev->host.hc[ch_num].pid_type = PID_DATA1;
  732. }
  733. }
  734. else
  735. {
  736. if (pdev->host.hc[ch_num].in_toggle == 0U)
  737. {
  738. pdev->host.hc[ch_num].pid_type = PID_DATA0;
  739. }
  740. else
  741. {
  742. pdev->host.hc[ch_num].pid_type = PID_DATA1;
  743. }
  744. }
  745. break;
  746. case EP_TYPE_INTR:
  747. if (direction == 0U)
  748. {
  749. /* Set the Data Toggle bit as per the Flag */
  750. if (pdev->host.hc[ch_num].out_toggle == 0U)
  751. {
  752. /* Put the PID 0 */
  753. pdev->host.hc[ch_num].pid_type = PID_DATA0;
  754. }
  755. else
  756. {
  757. /* Put the PID 1 */
  758. pdev->host.hc[ch_num].pid_type = PID_DATA1;
  759. }
  760. }
  761. else
  762. {
  763. if (pdev->host.hc[ch_num].in_toggle == 0U)
  764. {
  765. pdev->host.hc[ch_num].pid_type = PID_DATA0;
  766. }
  767. else
  768. {
  769. pdev->host.hc[ch_num].pid_type = PID_DATA1;
  770. }
  771. }
  772. break;
  773. case EP_TYPE_ISOC:
  774. pdev->host.hc[ch_num].pid_type = PID_DATA0;
  775. break;
  776. default:
  777. break;
  778. }
  779. pdev->host.hc[ch_num].xfer_buff = pbuff;
  780. pdev->host.hc[ch_num].xfer_len = length;
  781. pdev->host.hc[ch_num].xfer_count = 0U;
  782. pdev->host.HC_Status[ch_num] = HOST_CH_IDLE; /* state */
  783. pdev->host.URB_State[ch_num] = HOST_CH_XFER_IDLE; /* urb state */
  784. return usb_hchtransbegin(&pdev->regs, ch_num, &pdev->host.hc[ch_num], pdev->basic_cfgs.dmaen);
  785. }
  786. static HOST_CH_XFER_STATE usb_hsot_get_ch_urbstate(usb_core_instance *pdev, uint8_t chnum)
  787. {
  788. return pdev->host.URB_State[chnum];
  789. }
  790. static HOST_CH_STATUS usb_hsot_get_ch_state(usb_core_instance *pdev, uint8_t chnum)
  791. {
  792. return pdev->host.HC_Status[chnum];
  793. }
  794. static uint32_t usb_hsot_get_ch_xfercount(usb_core_instance *pdev, uint8_t chnum)
  795. {
  796. return pdev->host.hc[chnum].xfer_count;
  797. }
  798. static rt_err_t _usbh_reset_port(rt_uint8_t port)
  799. {
  800. LOG_D("reset port");
  801. usb_hprtrst(&_hc32_usbh.regs);
  802. return RT_EOK;
  803. }
  804. static int _usbh_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeouts)
  805. {
  806. int timeout = timeouts;
  807. uint8_t devspeed;
  808. uint32_t u32NakCnt = 0;
  809. if (pipe->pipe_index >= USB_MAX_CH_NUM)
  810. {
  811. rt_kprintf("Error: pipe_index %d, Exceeded the max number of host channels\r\n", pipe->pipe_index);
  812. return -1;
  813. }
  814. while (1)
  815. {
  816. if (!connect_status)
  817. {
  818. return -1;
  819. }
  820. rt_completion_init(&urb_completion);
  821. usb_host_submitrequest(&_hc32_usbh,
  822. pipe->pipe_index,
  823. (pipe->ep.bEndpointAddress & 0x80) >> 7,
  824. pipe->ep.bmAttributes,
  825. token,
  826. buffer,
  827. nbytes,
  828. 0);
  829. if ((pipe->ep.bEndpointAddress & 0x80))
  830. {
  831. LOG_D("IN");
  832. }
  833. else
  834. {
  835. LOG_D("OUT");
  836. }
  837. rt_completion_wait(&urb_completion, timeout);
  838. rt_thread_mdelay(1);
  839. if (usb_hsot_get_ch_state(&_hc32_usbh, pipe->pipe_index) == HOST_CH_NAK)
  840. {
  841. LOG_D("nak");
  842. #define MAX_NAK_CNT (5U)
  843. u32NakCnt ++;
  844. if (u32NakCnt > MAX_NAK_CNT)
  845. {
  846. #if defined(RT_USBH_MSTORAGE) || defined(RT_USBH_HID)
  847. /* Do not limit the number of Naks */
  848. #else
  849. return -1;
  850. #endif
  851. }
  852. if (pipe->ep.bmAttributes == USB_EP_ATTR_INT)
  853. {
  854. rt_thread_delay((pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) > 0 ? (pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) : 1);
  855. }
  856. /* Get the speed of the connected device */
  857. devspeed = _hc32_usbh.host.devspeed;
  858. usb_hchstop(&_hc32_usbh.regs, pipe->pipe_index);
  859. usb_host_ch_init(&_hc32_usbh,
  860. pipe->pipe_index,
  861. pipe->ep.bEndpointAddress,
  862. pipe->inst->address,
  863. devspeed,
  864. pipe->ep.bmAttributes,
  865. pipe->ep.wMaxPacketSize);
  866. continue;
  867. }
  868. else if (usb_hsot_get_ch_state(&_hc32_usbh, pipe->pipe_index) == HOST_CH_STALL)
  869. {
  870. LOG_D("stall");
  871. pipe->status = UPIPE_STATUS_STALL;
  872. if (pipe->callback != RT_NULL)
  873. {
  874. pipe->callback(pipe);
  875. }
  876. return -1;
  877. }
  878. else if (usb_hsot_get_ch_urbstate(&_hc32_usbh, pipe->pipe_index) == HOST_CH_XFER_ERROR)
  879. {
  880. LOG_D("error");
  881. pipe->status = UPIPE_STATUS_ERROR;
  882. if (pipe->callback != RT_NULL)
  883. {
  884. pipe->callback(pipe);
  885. }
  886. return -1;
  887. }
  888. else if (HOST_CH_XFER_DONE == usb_hsot_get_ch_urbstate(&_hc32_usbh, pipe->pipe_index))
  889. {
  890. LOG_D("done");
  891. pipe->status = UPIPE_STATUS_OK;
  892. if (pipe->callback != RT_NULL)
  893. {
  894. pipe->callback(pipe);
  895. }
  896. size_t size = usb_hsot_get_ch_xfercount(&_hc32_usbh, pipe->pipe_index);
  897. if (pipe->ep.bEndpointAddress & 0x80)
  898. {
  899. return size;
  900. }
  901. return nbytes;
  902. }
  903. else
  904. {
  905. LOG_D("other");
  906. }
  907. continue;
  908. }
  909. }
  910. static rt_uint16_t pipe_index = 0;
  911. static rt_uint8_t _usbh_get_free_pipe_index(void)
  912. {
  913. rt_uint8_t idx;
  914. for (idx = 0; idx < USB_MAX_CH_NUM; idx++)
  915. {
  916. if (!(pipe_index & (0x01 << idx)))
  917. {
  918. pipe_index |= (0x01 << idx);
  919. return idx;
  920. }
  921. }
  922. rt_kprintf("Error: Exceeded the max number of host channels\r\n");
  923. return USB_MAX_CH_NUM;
  924. }
  925. static void _usbh_free_pipe_index(rt_uint8_t index)
  926. {
  927. pipe_index &= ~(0x01 << index);
  928. }
  929. static rt_err_t _usbh_open_pipe(upipe_t pipe)
  930. {
  931. uint8_t devspeed;
  932. pipe->pipe_index = _usbh_get_free_pipe_index();
  933. if (pipe->pipe_index >= USB_MAX_CH_NUM)
  934. {
  935. rt_kprintf("Error: pipe_index %d, Exceeded the max number of host channels\r\n", pipe->pipe_index);
  936. return -RT_ERROR;
  937. }
  938. /* Get the speed of the connected device */
  939. devspeed = _hc32_usbh.host.devspeed;
  940. usb_host_chopen(&_hc32_usbh,
  941. pipe->pipe_index,
  942. pipe->ep.bEndpointAddress,
  943. pipe->inst->address,
  944. devspeed,
  945. pipe->ep.bmAttributes,
  946. pipe->ep.wMaxPacketSize);
  947. /* Set DATA0 PID token*/
  948. if (_hc32_usbh.host.hc[pipe->pipe_index].is_epin)
  949. {
  950. _hc32_usbh.host.hc[pipe->pipe_index].in_toggle = 0;
  951. }
  952. else
  953. {
  954. _hc32_usbh.host.hc[pipe->pipe_index].out_toggle = 0;
  955. }
  956. return RT_EOK;
  957. }
  958. static rt_err_t _usbh_close_pipe(upipe_t pipe)
  959. {
  960. if (pipe->pipe_index >= USB_MAX_CH_NUM)
  961. {
  962. rt_kprintf("Error: pipe_index %d, Exceeded the max number of host channels\r\n", pipe->pipe_index);
  963. return -RT_ERROR;
  964. }
  965. usb_hchstop(&_hc32_usbh.regs, pipe->pipe_index);
  966. _usbh_free_pipe_index(pipe->pipe_index);
  967. return RT_EOK;
  968. }
  969. static struct uhcd_ops _uhcd_ops =
  970. {
  971. _usbh_reset_port,
  972. _usbh_pipe_xfer,
  973. _usbh_open_pipe,
  974. _usbh_close_pipe,
  975. };
  976. static void _usbh_driver_init(usb_core_instance *pdev, stc_usb_port_identify *pstcPortIdentify)
  977. {
  978. uint8_t i;
  979. pdev->host.is_dev_connect = 0U;
  980. for (i = 0U; i < USB_MAX_TX_FIFOS; i++)
  981. {
  982. pdev->host.ErrCnt[i] = 0U;
  983. pdev->host.XferCnt[i] = 0U;
  984. pdev->host.HC_Status[i] = HOST_CH_IDLE;
  985. }
  986. pdev->host.hc[0].max_packet = 8U;
  987. usb_setregaddr(&pdev->regs, pstcPortIdentify, &pdev->basic_cfgs);
  988. usb_gintdis(&pdev->regs);
  989. usb_initusbcore(&pdev->regs, &pdev->basic_cfgs);
  990. /* force to work in host mode*/
  991. usb_modeset(&pdev->regs, HOST_MODE);
  992. /* configure charge pump IO */
  993. usb_bsp_cfgvbus(pdev);
  994. /* enable or disable the external charge pump */
  995. usb_bsp_drivevbus(pdev, 1U);
  996. usb_vbusctrl(&pdev->regs, 1U);
  997. /* Wait some time for power stable */
  998. usb_mdelay(100UL);
  999. usb_hostmodeinit(&pdev->regs, &pdev->basic_cfgs);
  1000. usb_ginten(&pdev->regs);
  1001. }
  1002. static rt_err_t _usbh_init(rt_device_t device)
  1003. {
  1004. stc_usb_port_identify stcPortIdentify;
  1005. struct hc32_irq_config irq_config;
  1006. usb_core_instance *hhcd = (usb_core_instance *)device->user_data;
  1007. /* Fcg config */
  1008. #if !defined(BSP_USING_USBH_HS)
  1009. FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_USBFS, ENABLE);
  1010. #else
  1011. FCG_Fcg1PeriphClockCmd(FCG1_PERIPH_USBHS, ENABLE);
  1012. #endif
  1013. /* Parameters */
  1014. #if !defined(BSP_USING_USBH_HS)
  1015. stcPortIdentify.u8CoreID = USBFS_CORE_ID;
  1016. #else
  1017. stcPortIdentify.u8CoreID = USBHS_CORE_ID;
  1018. #endif
  1019. #if defined (HC32F4A0) || defined (HC32F4A8)
  1020. #if !defined(BSP_USING_USBHS_PHY_EXTERN)
  1021. stcPortIdentify.u8PhyType = USBHS_PHY_EMBED;
  1022. #else
  1023. stcPortIdentify.u8PhyType = USBHS_PHY_EXT;
  1024. #endif
  1025. #endif
  1026. /* BSP Config */
  1027. #if !defined(BSP_USING_USBH_HS)
  1028. rt_hw_usbfs_board_init();
  1029. #else
  1030. rt_hw_usbhs_board_init();
  1031. #endif
  1032. /* Host driver init */
  1033. _usbh_driver_init(hhcd, &stcPortIdentify);
  1034. /* NVIC config */
  1035. #if !defined(BSP_USING_USBH_HS)
  1036. irq_config.irq_num = BSP_USBFS_GLB_IRQ_NUM;
  1037. irq_config.int_src = INT_SRC_USBFS_GLB;
  1038. irq_config.irq_prio = BSP_USBFS_GLB_IRQ_PRIO;
  1039. #else
  1040. irq_config.irq_num = BSP_USBHS_GLB_IRQ_NUM;
  1041. irq_config.int_src = INT_SRC_USBHS_GLB;
  1042. irq_config.irq_prio = BSP_USBHS_GLB_IRQ_PRIO;
  1043. #endif
  1044. /* register interrupt */
  1045. hc32_install_irq_handler(&irq_config,
  1046. usbh_irq_handler,
  1047. RT_TRUE);
  1048. return RT_EOK;
  1049. }
  1050. int rt_hw_usbh_init(void)
  1051. {
  1052. rt_err_t res = -RT_ERROR;
  1053. uhcd_t uhcd = (uhcd_t)rt_malloc(sizeof(struct uhcd));
  1054. if (uhcd == RT_NULL)
  1055. {
  1056. rt_kprintf("uhcd malloc failed\r\n");
  1057. return -RT_ERROR;
  1058. }
  1059. rt_memset((void *)uhcd, 0, sizeof(struct uhcd));
  1060. uhcd->parent.type = RT_Device_Class_USBHost;
  1061. uhcd->parent.init = _usbh_init;
  1062. uhcd->parent.user_data = &_hc32_usbh;
  1063. uhcd->ops = &_uhcd_ops;
  1064. uhcd->num_ports = USB_FS_PORT;
  1065. _hc32_usbh.pData = uhcd;
  1066. res = rt_device_register(&uhcd->parent, "usbh", RT_DEVICE_FLAG_DEACTIVATE);
  1067. if (res != RT_EOK)
  1068. {
  1069. rt_kprintf("register usb host failed res = %d\r\n", res);
  1070. return -RT_ERROR;
  1071. }
  1072. rt_usb_host_init("usbh");
  1073. return RT_EOK;
  1074. }
  1075. INIT_DEVICE_EXPORT(rt_hw_usbh_init);
  1076. #endif /* BSP_USING_USBH */