usbh_ctrl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*!
  2. \file usbh_ctrl.c
  3. \brief this file implements the functions for the control transmit process
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "usbh_core.h"
  33. #include "usbh_std.h"
  34. #include "usbh_ctrl.h"
  35. uint8_t ctrl_polling_handle_flag = 0U;
  36. uint8_t ctrl_setup_wait_flag = 0U;
  37. uint8_t ctrl_data_wait_flag = 0U;
  38. uint8_t ctrl_status_wait_flag = 0U;
  39. static uint16_t timeout = 0U;
  40. static void ctrl_idle_handle (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
  41. static void ctrl_setup_handle (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
  42. static void ctrl_data_handle (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
  43. static void ctrl_status_handle (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
  44. static void ctrl_error_handle (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
  45. static void ctrl_stalled_handle (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
  46. static void ctrl_complete_handle (usb_core_handle_struct *pudev, usbh_host_struct *puhost, usbh_state_handle_struct *pustate);
  47. /* the ctrl state handle function array */
  48. void (*ctrl_state_handle[]) (usb_core_handle_struct *pudev,
  49. usbh_host_struct *puhost,
  50. usbh_state_handle_struct *pustate) =
  51. {
  52. ctrl_idle_handle,
  53. ctrl_setup_handle,
  54. ctrl_data_handle,
  55. ctrl_status_handle,
  56. ctrl_error_handle,
  57. ctrl_stalled_handle,
  58. ctrl_complete_handle,
  59. };
  60. /* the ctrl state handle table */
  61. state_table_struct ctrl_handle_table[CTRL_HANDLE_TABLE_SIZE] =
  62. {
  63. /* the current state the current event the next state the event function */
  64. {CTRL_IDLE, CTRL_EVENT_SETUP, CTRL_SETUP, only_state_move },
  65. {CTRL_SETUP, CTRL_EVENT_DATA, CTRL_DATA, only_state_move },
  66. {CTRL_SETUP, CTRL_EVENT_STATUS, CTRL_STATUS, only_state_move },
  67. {CTRL_SETUP, CTRL_EVENT_ERROR, CTRL_ERROR, only_state_move },
  68. {CTRL_DATA, CTRL_EVENT_STATUS, CTRL_STATUS, only_state_move },
  69. {CTRL_DATA, CTRL_EVENT_ERROR, CTRL_ERROR, only_state_move },
  70. {CTRL_DATA, CTRL_EVENT_STALLED, CTRL_STALLED, only_state_move },
  71. {CTRL_STATUS, CTRL_EVENT_COMPLETE, CTRL_COMPLETE, only_state_move },
  72. {CTRL_STATUS, CTRL_EVENT_ERROR, CTRL_ERROR, only_state_move },
  73. {CTRL_STATUS, CTRL_EVENT_STALLED, CTRL_STALLED, only_state_move },
  74. {CTRL_ERROR, GO_TO_UP_STATE_EVENT, UP_STATE, goto_up_state_fun },
  75. {CTRL_STALLED, GO_TO_UP_STATE_EVENT, UP_STATE, goto_up_state_fun },
  76. {CTRL_COMPLETE, GO_TO_UP_STATE_EVENT, UP_STATE, goto_up_state_fun },
  77. };
  78. /*!
  79. \brief the polling function of CTRL state
  80. \param[in] pudev: pointer to usb device
  81. \param[in] puhost: pointer to usb host
  82. \param[in] pustate: pointer to usb state driver
  83. \param[out] none
  84. \retval none
  85. */
  86. usbh_status_enum ctrl_state_polling_fun (usb_core_handle_struct *pudev,
  87. usbh_host_struct *puhost,
  88. void *pustate)
  89. {
  90. usbh_status_enum exe_state = USBH_BUSY;
  91. usbh_state_handle_struct *p_state;
  92. p_state = (usbh_state_handle_struct *)pustate;
  93. /* if first enter this function, begin the ctrl state */
  94. if (0U == ctrl_polling_handle_flag) {
  95. ctrl_polling_handle_flag = 1U;
  96. scd_table_push(p_state);
  97. scd_state_move(p_state, CTRL_IDLE);
  98. }
  99. /* base on the current state to handle the ctrl state */
  100. scd_begin(p_state, CTRL_FSM_ID);
  101. ctrl_state_handle[p_state->usbh_current_state](pudev, puhost, p_state);
  102. /* determine the control transfer whether to complete */
  103. switch (puhost->usbh_backup_state.ctrl_backup_state) {
  104. case CTRL_COMPLETE:
  105. ctrl_polling_handle_flag = 0U;
  106. puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
  107. exe_state = USBH_OK;
  108. break;
  109. case CTRL_STALLED:
  110. ctrl_polling_handle_flag = 0U;
  111. puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
  112. exe_state = USBH_NOT_SUPPORTED;
  113. break;
  114. case CTRL_ERROR:
  115. ctrl_polling_handle_flag = 0U;
  116. puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
  117. exe_state = USBH_FAIL;
  118. break;
  119. default:
  120. exe_state = USBH_BUSY;
  121. break;
  122. }
  123. return exe_state;
  124. }
  125. /*!
  126. \brief the handle function of CTRL_IDLE state
  127. \param[in] pudev: pointer to usb device
  128. \param[in] puhost: pointer to usb host
  129. \param[in] pustate: pointer to usb state driver
  130. \param[out] none
  131. \retval none
  132. */
  133. static void ctrl_idle_handle (usb_core_handle_struct *pudev,
  134. usbh_host_struct *puhost,
  135. usbh_state_handle_struct *pustate)
  136. {
  137. puhost->usbh_backup_state.ctrl_backup_state = CTRL_IDLE;
  138. scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_SETUP, pustate->usbh_current_state);
  139. }
  140. /*!
  141. \brief the handle function of CTRL_SETUP state
  142. \param[in] pudev: pointer to usb device
  143. \param[in] puhost: pointer to usb host
  144. \param[in] pustate: pointer to usb state driver
  145. \param[out] none
  146. \retval none
  147. */
  148. static void ctrl_setup_handle (usb_core_handle_struct *pudev,
  149. usbh_host_struct *puhost,
  150. usbh_state_handle_struct *pustate)
  151. {
  152. urb_state_enum urb_status = URB_IDLE;
  153. puhost->usbh_backup_state.ctrl_backup_state = CTRL_SETUP;
  154. if (0U == ctrl_setup_wait_flag) {
  155. ctrl_setup_wait_flag = 1U;
  156. /* send a setup packet */
  157. usbh_ctltx_setup (pudev,
  158. puhost->control.setup.data,
  159. puhost->control.hc_out_num);
  160. } else {
  161. urb_status = hcd_urb_state_get(pudev, puhost->control.hc_out_num);
  162. /* case setup packet sent successfully */
  163. if (URB_DONE == urb_status) {
  164. /* check if there is a data stage */
  165. if (0U != puhost->control.setup.b.wLength) {
  166. ctrl_setup_wait_flag = 0U;
  167. timeout = DATA_STAGE_TIMEOUT;
  168. scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_DATA, pustate->usbh_current_state);
  169. /* no data stage */
  170. } else {
  171. timeout = NODATA_STAGE_TIMEOUT;
  172. ctrl_setup_wait_flag = 0U;
  173. scd_event_handle(pudev,
  174. puhost,
  175. pustate,
  176. CTRL_EVENT_STATUS,
  177. pustate->usbh_current_state);
  178. }
  179. /* set the delay timer to enable timeout for data stage completion */
  180. puhost->control.timer = (uint16_t)USB_CURRENT_FRAME_GET();
  181. } else if (URB_ERROR == urb_status) {
  182. ctrl_setup_wait_flag = 0U;
  183. scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_ERROR, pustate->usbh_current_state);
  184. } else {
  185. /* no operation */
  186. }
  187. }
  188. }
  189. /*!
  190. \brief the handle function of CTRL_DATA state
  191. \param[in] pudev: pointer to usb device
  192. \param[in] puhost: pointer to usb host
  193. \param[in] pustate: pointer to usb state driver
  194. \param[out] none
  195. \retval none
  196. */
  197. static void ctrl_data_handle (usb_core_handle_struct *pudev,
  198. usbh_host_struct *puhost,
  199. usbh_state_handle_struct *pustate)
  200. {
  201. uint8_t direction;
  202. urb_state_enum urb_status = URB_IDLE;
  203. puhost->usbh_backup_state.ctrl_backup_state = CTRL_DATA;
  204. direction = (puhost->control.setup.b.bmRequestType & USB_DIR_MASK);
  205. if (USB_DIR_IN == direction) {
  206. if (0U == ctrl_data_wait_flag) {
  207. ctrl_data_wait_flag = 1U;
  208. /* issue an IN token */
  209. usbh_xfer(pudev,
  210. puhost->control.buff,
  211. puhost->control.hc_in_num,
  212. puhost->control.length);
  213. } else {
  214. urb_status = hcd_urb_state_get(pudev, puhost->control.hc_in_num);
  215. /* check is data packet transfered successfully */
  216. switch (urb_status) {
  217. case URB_DONE:
  218. ctrl_data_wait_flag = 0U;
  219. scd_event_handle(pudev,
  220. puhost,
  221. pustate,
  222. CTRL_EVENT_STATUS,
  223. pustate->usbh_current_state);
  224. break;
  225. case URB_STALL:
  226. ctrl_data_wait_flag = 0U;
  227. scd_event_handle(pudev,
  228. puhost,
  229. pustate,
  230. CTRL_EVENT_STALLED,
  231. pustate->usbh_current_state);
  232. break;
  233. case URB_ERROR:
  234. ctrl_data_wait_flag = 0U;
  235. /* device error */
  236. scd_event_handle(pudev,
  237. puhost,
  238. pustate,
  239. CTRL_EVENT_ERROR,
  240. pustate->usbh_current_state);
  241. break;
  242. default:
  243. if (((uint16_t)USB_CURRENT_FRAME_GET() - puhost->control.timer) > timeout) {
  244. ctrl_data_wait_flag = 0U;
  245. /* timeout for in transfer */
  246. scd_event_handle(pudev,
  247. puhost,
  248. pustate,
  249. CTRL_EVENT_ERROR,
  250. pustate->usbh_current_state);
  251. }
  252. break;
  253. }
  254. }
  255. } else {
  256. if (0U == ctrl_data_wait_flag) {
  257. ctrl_data_wait_flag = 1U;
  258. /* start data out transfer (only one data packet)*/
  259. pudev->host.host_channel[puhost->control.hc_out_num].data_tg_out = 1U;
  260. usbh_xfer(pudev,
  261. puhost->control.buff,
  262. puhost->control.hc_out_num,
  263. puhost->control.length);
  264. } else {
  265. urb_status = hcd_urb_state_get(pudev, puhost->control.hc_out_num);
  266. switch (urb_status) {
  267. case URB_DONE:
  268. ctrl_data_wait_flag = 0U;
  269. /* if the setup pkt is sent successful, then change the state */
  270. scd_event_handle(pudev,
  271. puhost,
  272. pustate,
  273. CTRL_EVENT_STATUS,
  274. pustate->usbh_current_state);
  275. break;
  276. case URB_STALL:
  277. ctrl_data_wait_flag = 0U;
  278. scd_event_handle(pudev,
  279. puhost,
  280. pustate,
  281. CTRL_EVENT_STALLED,
  282. pustate->usbh_current_state);
  283. break;
  284. case URB_NOTREADY:
  285. /* nack received from device */
  286. ctrl_data_wait_flag = 0U;
  287. break;
  288. case URB_ERROR:
  289. ctrl_data_wait_flag = 0U;
  290. /* device error */
  291. scd_event_handle(pudev,
  292. puhost,
  293. pustate,
  294. CTRL_EVENT_ERROR,
  295. pustate->usbh_current_state);
  296. break;
  297. default:
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. /*!
  304. \brief the handle function of CTRL_STATUS state
  305. \param[in] pudev: pointer to usb device
  306. \param[in] puhost: pointer to usb host
  307. \param[in] pustate: pointer to usb state driver
  308. \param[out] none
  309. \retval none
  310. */
  311. static void ctrl_status_handle (usb_core_handle_struct *pudev,
  312. usbh_host_struct *puhost,
  313. usbh_state_handle_struct *pustate)
  314. {
  315. uint8_t direction;
  316. urb_state_enum urb_status = URB_IDLE;
  317. puhost->usbh_backup_state.ctrl_backup_state = CTRL_STATUS;
  318. /* get the transfer direction in the data state, but the transfer direction in the status state is opposite */
  319. direction = (puhost->control.setup.b.bmRequestType & USB_DIR_MASK);
  320. if (USB_DIR_OUT == direction) {
  321. /* handle status in */
  322. if (0U == ctrl_status_wait_flag) {
  323. ctrl_status_wait_flag = 1U;
  324. usbh_xfer (pudev, 0U, puhost->control.hc_in_num, 0U);
  325. } else {
  326. urb_status = hcd_urb_state_get(pudev, puhost->control.hc_in_num);
  327. switch (urb_status) {
  328. case URB_DONE:
  329. ctrl_status_wait_flag = 0U;
  330. /* handle URB_DONE status */
  331. scd_event_handle(pudev,
  332. puhost,
  333. pustate,
  334. CTRL_EVENT_COMPLETE,
  335. pustate->usbh_current_state);
  336. break;
  337. case URB_ERROR:
  338. ctrl_status_wait_flag = 0U;
  339. /* handle URB_STALL status*/
  340. scd_event_handle(pudev,
  341. puhost,
  342. pustate,
  343. CTRL_EVENT_ERROR,
  344. pustate->usbh_current_state);
  345. break;
  346. case URB_STALL:
  347. ctrl_status_wait_flag = 0U;
  348. /* handle URB_STALL status */
  349. scd_event_handle(pudev,
  350. puhost,
  351. pustate,
  352. CTRL_EVENT_STALLED,
  353. pustate->usbh_current_state);
  354. break;
  355. default:
  356. if (((uint16_t)USB_CURRENT_FRAME_GET() - puhost->control.timer) > timeout) {
  357. ctrl_status_wait_flag = 0U;
  358. /* handle timeout */
  359. scd_event_handle(pudev,
  360. puhost,
  361. pustate,
  362. CTRL_EVENT_ERROR,
  363. pustate->usbh_current_state);
  364. }
  365. break;
  366. }
  367. }
  368. } else {
  369. /* handle status out */
  370. if (0U == ctrl_status_wait_flag) {
  371. ctrl_status_wait_flag = 1U;
  372. pudev->host.host_channel[puhost->control.hc_out_num].data_tg_out ^= 1U;
  373. usbh_xfer (pudev, 0U, puhost->control.hc_out_num, 0U);
  374. } else {
  375. urb_status = hcd_urb_state_get(pudev, puhost->control.hc_out_num);
  376. switch (urb_status) {
  377. case URB_DONE:
  378. ctrl_status_wait_flag = 0U;
  379. /* handle URB_DONE status */
  380. scd_event_handle(pudev,
  381. puhost,
  382. pustate,
  383. CTRL_EVENT_COMPLETE,
  384. pustate->usbh_current_state);
  385. break;
  386. case URB_NOTREADY:
  387. /* handle URB_NOTREADY status */
  388. ctrl_status_wait_flag = 0U;
  389. break;
  390. case URB_ERROR:
  391. ctrl_status_wait_flag = 0U;
  392. /* handle URB_ERROR status */
  393. scd_event_handle(pudev,
  394. puhost,
  395. pustate,
  396. CTRL_EVENT_ERROR,
  397. pustate->usbh_current_state);
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. }
  404. }
  405. /*!
  406. \brief the handle function of CTRL_ERROR state
  407. \param[in] pudev: pointer to usb device
  408. \param[in] puhost: pointer to usb host
  409. \param[in] pustate: pointer to usb state driver
  410. \param[out] none
  411. \retval none
  412. */
  413. static void ctrl_error_handle (usb_core_handle_struct *pudev,
  414. usbh_host_struct *puhost,
  415. usbh_state_handle_struct *pustate)
  416. {
  417. puhost->usbh_backup_state.ctrl_backup_state = CTRL_ERROR;
  418. if (++puhost->control.error_count <= USBH_MAX_ERROR_COUNT) {
  419. /* do the transmission again, starting from setup packet */
  420. scd_event_handle(pudev, puhost, pustate, CTRL_EVENT_SETUP, pustate->usbh_current_state);
  421. } else {
  422. scd_event_handle(pudev, puhost, pustate, GO_TO_UP_STATE_EVENT, pustate->usbh_current_state);
  423. }
  424. }
  425. /*!
  426. \brief the handle function of CTRL_STALLED state
  427. \param[in] pudev: pointer to usb device
  428. \param[in] puhost: pointer to usb host
  429. \param[in] pustate: pointer to usb state driver
  430. \param[out] none
  431. \retval none
  432. */
  433. static void ctrl_stalled_handle (usb_core_handle_struct *pudev,
  434. usbh_host_struct *puhost,
  435. usbh_state_handle_struct *pustate)
  436. {
  437. puhost->usbh_backup_state.ctrl_backup_state = CTRL_STALLED;
  438. scd_event_handle(pudev, puhost, pustate, GO_TO_UP_STATE_EVENT, pustate->usbh_current_state);
  439. }
  440. /*!
  441. \brief the handle function of CTRL_COMPLETE state
  442. \param[in] pudev: pointer to usb device
  443. \param[in] puhost: pointer to usb host
  444. \param[in] pustate: pointer to usb state driver
  445. \param[out] none
  446. \retval none
  447. */
  448. static void ctrl_complete_handle (usb_core_handle_struct *pudev,
  449. usbh_host_struct *puhost,
  450. usbh_state_handle_struct *pustate)
  451. {
  452. puhost->usbh_backup_state.ctrl_backup_state = CTRL_COMPLETE;
  453. scd_event_handle(pudev, puhost, pustate, GO_TO_UP_STATE_EVENT, pustate->usbh_current_state);
  454. }
  455. /*!
  456. \brief send datas from the host channel
  457. \param[in] pudev: pointer to usb device
  458. \param[in] buf: data buffer address to send datas
  459. \param[in] hc_num: the number of the host channel
  460. \param[in] len: length of the send data
  461. \param[out] none
  462. \retval host operation status
  463. */
  464. usbh_status_enum usbh_xfer (usb_core_handle_struct *pudev,
  465. uint8_t *buf,
  466. uint8_t hc_num,
  467. uint16_t len)
  468. {
  469. usb_hostchannel_struct *puhc = &pudev->host.host_channel[hc_num];
  470. puhc->xfer_buff = buf;
  471. puhc->xfer_len = len;
  472. switch (puhc->endp_type) {
  473. case USB_EPTYPE_CTRL:
  474. if (0U == puhc->endp_in) {
  475. if (0U == len) {
  476. /* for status out stage, length = 0, status out pid = 1 */
  477. puhc->data_tg_out = 1U;
  478. }
  479. /* set the data toggle bit as per the flag */
  480. if (0U == puhc->data_tg_out) {
  481. /* put the pid 0 */
  482. puhc->DPID = HC_PID_DATA0;
  483. } else {
  484. /* put the pid 1 */
  485. puhc->DPID = HC_PID_DATA1;
  486. }
  487. } else {
  488. puhc->DPID = HC_PID_DATA1;
  489. }
  490. break;
  491. case USB_EPTYPE_ISOC:
  492. puhc->DPID = HC_PID_DATA0;
  493. break;
  494. case USB_EPTYPE_BULK:
  495. if (0U == puhc->endp_in) {
  496. /* set the data toggle bit as per the flag */
  497. if (0U == puhc->data_tg_out) {
  498. /* put the pid 0 */
  499. puhc->DPID = HC_PID_DATA0;
  500. } else {
  501. /* put the pid 1 */
  502. puhc->DPID = HC_PID_DATA1;
  503. }
  504. } else {
  505. if (0U == puhc->data_tg_in) {
  506. puhc->DPID = HC_PID_DATA0;
  507. } else {
  508. puhc->DPID = HC_PID_DATA1;
  509. }
  510. }
  511. break;
  512. case USB_EPTYPE_INTR:
  513. if (0U == puhc->endp_in) {
  514. if (0U == puhc->data_tg_out) {
  515. puhc->DPID = HC_PID_DATA0;
  516. } else {
  517. puhc->DPID = HC_PID_DATA1;
  518. }
  519. /* toggle data pid */
  520. puhc->data_tg_out ^= 1U;
  521. } else {
  522. if (0U == puhc->data_tg_in) {
  523. puhc->DPID = HC_PID_DATA0;
  524. } else {
  525. puhc->DPID = HC_PID_DATA1;
  526. }
  527. /* toggle data pid */
  528. puhc->data_tg_in ^= 1U;
  529. }
  530. break;
  531. default:
  532. break;
  533. }
  534. hcd_submit_request (pudev, hc_num);
  535. return USBH_OK;
  536. }
  537. /*!
  538. \brief send the setup packet to the device
  539. \param[in] pudev: pointer to usb device
  540. \param[in] buf: buffer pointer from which the data will be send to device
  541. \param[in] hc_num: host channel number
  542. \param[out] none
  543. \retval host operation status
  544. */
  545. usbh_status_enum usbh_ctltx_setup (usb_core_handle_struct *pudev, uint8_t *buf, uint8_t hc_num)
  546. {
  547. usb_hostchannel_struct *puhc = &pudev->host.host_channel[hc_num];
  548. puhc->DPID = HC_PID_SETUP;
  549. puhc->xfer_buff = buf;
  550. puhc->xfer_len = USBH_SETUP_PACKET_SIZE;
  551. return (usbh_status_enum)hcd_submit_request (pudev, hc_num);
  552. }
  553. /*!
  554. \brief this function prepare a hc and start a transfer
  555. \param[in] pudev: pointer to usb device
  556. \param[in] channel_num: host channel number which is in (0..7)
  557. \param[out] none
  558. \retval host operation status
  559. */
  560. uint32_t hcd_submit_request (usb_core_handle_struct *pudev, uint8_t channel_num)
  561. {
  562. usb_hostchannel_struct *puhc = &pudev->host.host_channel[channel_num];
  563. puhc->urb_state = URB_IDLE;
  564. puhc->xfer_count = 0U;
  565. return (uint32_t)usb_hostchannel_startxfer(pudev, channel_num);
  566. }