usbh_ctrl.c 24 KB

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