usbd_enum.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*!
  2. \file usbd_enum.c
  3. \brief USB enumeration function
  4. \version 2023-06-30, V2.1.6, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "usbd_enum.h"
  30. #include "usbd_transc.h"
  31. /* USB enumeration handle functions */
  32. static usb_reqsta _usb_std_getstatus (usb_dev *udev, usb_req *req);
  33. static usb_reqsta _usb_std_clearfeature (usb_dev *udev, usb_req *req);
  34. static usb_reqsta _usb_std_setfeature (usb_dev *udev, usb_req *req);
  35. static usb_reqsta _usb_std_setaddress (usb_dev *udev, usb_req *req);
  36. static usb_reqsta _usb_std_getdescriptor (usb_dev *udev, usb_req *req);
  37. static usb_reqsta _usb_std_setdescriptor (usb_dev *udev, usb_req *req);
  38. static usb_reqsta _usb_std_getconfiguration (usb_dev *udev, usb_req *req);
  39. static usb_reqsta _usb_std_setconfiguration (usb_dev *udev, usb_req *req);
  40. static usb_reqsta _usb_std_reserved (usb_dev *udev, usb_req *req);
  41. static usb_reqsta _usb_std_getinterface (usb_dev *udev, usb_req *req);
  42. static usb_reqsta _usb_std_setinterface (usb_dev *udev, usb_req *req);
  43. static usb_reqsta _usb_std_synchframe (usb_dev *udev, usb_req *req);
  44. static uint8_t* _usb_dev_desc_get (usb_dev *udev, uint8_t index, uint16_t *len);
  45. static uint8_t* _usb_config_desc_get (usb_dev *udev, uint8_t index, uint16_t *len);
  46. static uint8_t* _usb_str_desc_get (usb_dev *udev, uint8_t index, uint16_t *len);
  47. static uint8_t* _usb_bos_desc_get (usb_dev *udev, uint8_t index, uint16_t *len);
  48. static void int_to_unicode (uint32_t value, uint8_t *pbuf, uint8_t len);
  49. /* standard device request handler */
  50. static usb_reqsta (*_std_dev_req[]) (usb_dev *udev, usb_req *req) = {
  51. [USB_GET_STATUS] = _usb_std_getstatus,
  52. [USB_CLEAR_FEATURE] = _usb_std_clearfeature,
  53. [USB_RESERVED2] = _usb_std_reserved,
  54. [USB_SET_FEATURE] = _usb_std_setfeature,
  55. [USB_RESERVED4] = _usb_std_reserved,
  56. [USB_SET_ADDRESS] = _usb_std_setaddress,
  57. [USB_GET_DESCRIPTOR] = _usb_std_getdescriptor,
  58. [USB_SET_DESCRIPTOR] = _usb_std_setdescriptor,
  59. [USB_GET_CONFIGURATION] = _usb_std_getconfiguration,
  60. [USB_SET_CONFIGURATION] = _usb_std_setconfiguration,
  61. [USB_GET_INTERFACE] = _usb_std_getinterface,
  62. [USB_SET_INTERFACE] = _usb_std_setinterface,
  63. [USB_SYNCH_FRAME] = _usb_std_synchframe,
  64. };
  65. /* get standard descriptor handler */
  66. static uint8_t* (*std_desc_get[])(usb_dev *udev, uint8_t index, uint16_t *len) = {
  67. [USB_DESCTYPE_DEV - 1U] = _usb_dev_desc_get,
  68. [USB_DESCTYPE_CONFIG - 1U] = _usb_config_desc_get,
  69. [USB_DESCTYPE_STR - 1U] = _usb_str_desc_get
  70. };
  71. /*!
  72. \brief handle USB standard device request
  73. \param[in] udev: pointer to USB device instance
  74. \param[in] req: pointer to USB device request
  75. \param[out] none
  76. \retval USB device operation cur_status
  77. */
  78. usb_reqsta usbd_standard_request (usb_dev *udev, usb_req *req)
  79. {
  80. /* call device request handle function */
  81. return (*_std_dev_req[req->bRequest]) (udev, req);
  82. }
  83. /*!
  84. \brief handle USB device class request
  85. \param[in] udev: pointer to USB device instance
  86. \param[in] req: pointer to USB device class request
  87. \param[out] none
  88. \retval USB device request status
  89. */
  90. usb_reqsta usbd_class_request (usb_dev *udev, usb_req *req)
  91. {
  92. if ((uint8_t)USBD_CONFIGURED == udev->cur_status) {
  93. if (BYTE_LOW(req->wIndex) < USBD_ITF_MAX_NUM) {
  94. /* call device class handle function */
  95. return (usb_reqsta)udev->class_core->req_process(udev, req);
  96. }
  97. }
  98. return REQ_NOTSUPP;
  99. }
  100. /*!
  101. \brief handle USB vendor request
  102. \param[in] udev: pointer to USB device instance
  103. \param[in] req: pointer to USB vendor request
  104. \param[out] none
  105. \retval USB device request status
  106. */
  107. usb_reqsta usbd_vendor_request (usb_dev *udev, usb_req *req)
  108. {
  109. (void)udev;
  110. (void)req;
  111. /* added by user */
  112. return REQ_NOTSUPP;
  113. }
  114. /*!
  115. \brief no operation, just for reserved
  116. \param[in] udev: pointer to USB device instance
  117. \param[in] req: pointer to USB device request
  118. \param[out] none
  119. \retval USB device request status
  120. */
  121. static usb_reqsta _usb_std_reserved (usb_dev *udev, usb_req *req)
  122. {
  123. (void)udev;
  124. (void)req;
  125. /* no operation */
  126. return REQ_NOTSUPP;
  127. }
  128. /*!
  129. \brief get the device descriptor
  130. \param[in] udev: pointer to USB device instance
  131. \param[in] index: no use
  132. \param[out] len: data length pointer
  133. \retval descriptor buffer pointer
  134. */
  135. static uint8_t* _usb_dev_desc_get (usb_dev *udev, uint8_t index, uint16_t *len)
  136. {
  137. (void)index;
  138. *len = udev->desc->dev_desc[0];
  139. return udev->desc->dev_desc;
  140. }
  141. /*!
  142. \brief get the configuration descriptor
  143. \brief[in] udev: pointer to USB device instance
  144. \brief[in] index: no use
  145. \param[out] len: data length pointer
  146. \retval descriptor buffer pointer
  147. */
  148. static uint8_t* _usb_config_desc_get (usb_dev *udev, uint8_t index, uint16_t *len)
  149. {
  150. (void)index;
  151. *len = udev->desc->config_desc[2] | (udev->desc->config_desc[3]<< 8);
  152. return udev->desc->config_desc;
  153. }
  154. /*!
  155. \brief get the BOS descriptor
  156. \brief[in] udev: pointer to USB device instance
  157. \brief[in] index: no use
  158. \param[out] len: data length pointer
  159. \retval descriptor buffer pointer
  160. */
  161. static uint8_t* _usb_bos_desc_get (usb_dev *udev, uint8_t index, uint16_t *len)
  162. {
  163. if (NULL != udev->desc->bos_desc) {
  164. (void)index;
  165. *len = (uint16_t)udev->desc->bos_desc[2] | (uint16_t)((uint16_t)udev->desc->bos_desc[3] << 8);
  166. return udev->desc->bos_desc;
  167. } else {
  168. *len = 0U;
  169. return NULL;
  170. }
  171. }
  172. /*!
  173. \brief get string descriptor
  174. \param[in] udev: pointer to USB device instance
  175. \param[in] index: string descriptor index
  176. \param[out] len: pointer to string length
  177. \retval string descriptor
  178. */
  179. static uint8_t* _usb_str_desc_get (usb_dev *udev, uint8_t index, uint16_t *len)
  180. {
  181. uint8_t* desc = udev->desc->strings[index];
  182. *len = desc[0];
  183. return desc;
  184. }
  185. /*!
  186. \brief handle Get_Status request
  187. \param[in] udev: pointer to USB device instance
  188. \param[in] req: pointer to USB device request
  189. \param[out] none
  190. \retval USB device request status
  191. */
  192. static usb_reqsta _usb_std_getstatus (usb_dev *udev, usb_req *req)
  193. {
  194. uint8_t recp = BYTE_LOW(req->wIndex);
  195. usb_reqsta req_status = REQ_NOTSUPP;
  196. static uint8_t status[2] = {0U};
  197. switch(req->bmRequestType & USB_RECPTYPE_MASK) {
  198. /* handle device get status request */
  199. case USB_RECPTYPE_DEV:
  200. switch (udev->cur_status) {
  201. case USBD_ADDRESSED:
  202. case USBD_CONFIGURED:
  203. if (udev->pm.power_mode) {
  204. status[0] = USB_STATUS_SELF_POWERED;
  205. } else {
  206. status[0] = 0U;
  207. }
  208. if (udev->pm.remote_wakeup) {
  209. status[0] |= USB_STATUS_REMOTE_WAKEUP;
  210. } else {
  211. status[0] = 0U;
  212. }
  213. req_status = REQ_SUPP;
  214. break;
  215. default:
  216. break;
  217. }
  218. break;
  219. /* handle interface get status request */
  220. case USB_RECPTYPE_ITF:
  221. if (((uint8_t)USBD_CONFIGURED == udev->cur_status) && (recp < USBD_ITF_MAX_NUM)) {
  222. req_status = REQ_SUPP;
  223. }
  224. break;
  225. /* handle endpoint get status request */
  226. case USB_RECPTYPE_EP:
  227. if ((uint8_t)USBD_CONFIGURED == udev->cur_status) {
  228. if (0x80U == (recp & 0x80U)) {
  229. status[0] = udev->transc_in[EP_ID(recp)].ep_stall;
  230. } else {
  231. status[0] = udev->transc_out[recp].ep_stall;
  232. }
  233. req_status = REQ_SUPP;
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. if (REQ_SUPP == req_status) {
  240. usb_transc_config(&udev->transc_in[0], status, 2U, 2U);
  241. }
  242. return req_status;
  243. }
  244. /*!
  245. \brief handle USB Clear_Feature request
  246. \param[in] udev: pointer to USB device instance
  247. \param[in] req: USB device request
  248. \param[out] none
  249. \retval USB device request status
  250. */
  251. static usb_reqsta _usb_std_clearfeature (usb_dev *udev, usb_req *req)
  252. {
  253. uint8_t ep = 0U;
  254. switch (req->bmRequestType & (uint8_t)USB_RECPTYPE_MASK) {
  255. case USB_RECPTYPE_DEV:
  256. switch (udev->cur_status) {
  257. case USBD_ADDRESSED:
  258. case USBD_CONFIGURED:
  259. /* clear device remote wakeup feature */
  260. if ((uint16_t)USB_FEATURE_REMOTE_WAKEUP == req->wValue) {
  261. udev->pm.remote_wakeup = 0U;
  262. return REQ_SUPP;
  263. }
  264. break;
  265. default:
  266. break;
  267. }
  268. break;
  269. case USB_RECPTYPE_EP:
  270. /* get endpoint address */
  271. ep = BYTE_LOW(req->wIndex);
  272. if (((uint8_t)USBD_CONFIGURED == udev->cur_status) && (EP_ID(ep) < EP_COUNT)) {
  273. /* clear endpoint halt feature */
  274. if (((uint16_t)USB_FEATURE_EP_HALT == req->wValue) && (!CTL_EP(ep))) {
  275. /* check whether the endpoint status is disabled */
  276. if(0U != usbd_ep_status_get(udev, ep)){
  277. usbd_ep_clear_stall(udev, ep);
  278. udev->class_core->req_process(udev, req);
  279. return REQ_SUPP;
  280. }
  281. }
  282. }
  283. break;
  284. case USB_RECPTYPE_ITF:
  285. break;
  286. default:
  287. break;
  288. }
  289. return REQ_NOTSUPP;
  290. }
  291. /*!
  292. \brief handle USB Set_Feature request
  293. \param[in] udev: pointer to USB device instance
  294. \param[in] req: pointer to USB device request
  295. \param[out] none
  296. \retval USB device request status
  297. */
  298. static usb_reqsta _usb_std_setfeature (usb_dev *udev, usb_req *req)
  299. {
  300. uint8_t ep = 0U;
  301. switch (req->bmRequestType & (uint8_t)USB_RECPTYPE_MASK) {
  302. case USB_RECPTYPE_DEV:
  303. switch (udev->cur_status) {
  304. case USBD_ADDRESSED:
  305. case USBD_CONFIGURED:
  306. /* set device remote wakeup feature */
  307. if ((uint16_t)USB_FEATURE_REMOTE_WAKEUP == req->wValue) {
  308. udev->pm.remote_wakeup = 1U;
  309. return REQ_SUPP;
  310. }
  311. break;
  312. default:
  313. break;
  314. }
  315. break;
  316. case USB_RECPTYPE_EP:
  317. /* get endpoint address */
  318. ep = BYTE_LOW(req->wIndex);
  319. if (((uint8_t)USBD_CONFIGURED == udev->cur_status) && (EP_ID(ep) < EP_COUNT)) {
  320. /* set endpoint halt feature */
  321. if (((uint16_t)USB_FEATURE_EP_HALT == req->wValue) && (!CTL_EP(ep))) {
  322. /* check whether the endpoint status is disabled */
  323. if(0U != usbd_ep_status_get(udev, ep)){
  324. usbd_ep_stall(udev, ep);
  325. udev->class_core->req_process(udev, req);
  326. return REQ_SUPP;
  327. }
  328. }
  329. }
  330. break;
  331. case USB_RECPTYPE_ITF:
  332. break;
  333. default:
  334. break;
  335. }
  336. return REQ_NOTSUPP;
  337. }
  338. /*!
  339. \brief handle USB Set_Address request
  340. \param[in] udev: pointer to USB device instance
  341. \param[in] req: pointer to USB device request
  342. \param[out] none
  343. \retval USB device request status
  344. */
  345. static usb_reqsta _usb_std_setaddress (usb_dev *udev, usb_req *req)
  346. {
  347. if ((0U == req->wIndex) && (0U == req->wLength)) {
  348. udev->dev_addr = (uint8_t)(req->wValue) & 0x7FU;
  349. if (udev->cur_status != (uint8_t)USBD_CONFIGURED) {
  350. if (udev->dev_addr) {
  351. udev->cur_status = (uint8_t)USBD_ADDRESSED;
  352. } else {
  353. udev->cur_status = (uint8_t)USBD_DEFAULT;
  354. }
  355. return REQ_SUPP;
  356. }
  357. }
  358. return REQ_NOTSUPP;
  359. }
  360. /*!
  361. \brief handle USB Get_Descriptor request
  362. \param[in] udev: pointer to USB device instance
  363. \param[in] req: pointer to USB device request
  364. \param[out] none
  365. \retval USB device request status
  366. */
  367. static usb_reqsta _usb_std_getdescriptor (usb_dev *udev, usb_req *req)
  368. {
  369. uint8_t desc_type = 0U;
  370. uint8_t desc_index = 0U;
  371. usb_reqsta status = REQ_NOTSUPP;
  372. usb_transc *transc = &udev->transc_in[0];
  373. switch (req->bmRequestType & USB_RECPTYPE_MASK) {
  374. case USB_RECPTYPE_DEV:
  375. desc_type = BYTE_HIGH(req->wValue);
  376. desc_index = BYTE_LOW(req->wValue);
  377. switch (desc_type) {
  378. case USB_DESCTYPE_DEV:
  379. transc->xfer_buf = std_desc_get[desc_type - 1U](udev, desc_index, &transc->xfer_len);
  380. if (64U == req->wLength) {
  381. transc->xfer_len = 8U;
  382. }
  383. break;
  384. case USB_DESCTYPE_CONFIG:
  385. transc->xfer_buf = std_desc_get[desc_type - 1U](udev, desc_index, &transc->xfer_len);
  386. break;
  387. case USB_DESCTYPE_STR:
  388. if (desc_index < STR_IDX_MAX) {
  389. transc->xfer_buf = std_desc_get[desc_type - 1U](udev, desc_index, &transc->xfer_len);
  390. }
  391. break;
  392. case USB_DESCTYPE_ITF:
  393. case USB_DESCTYPE_EP:
  394. case USB_DESCTYPE_DEV_QUALIFIER:
  395. case USB_DESCTYPE_OTHER_SPD_CONFIG:
  396. case USB_DESCTYPE_ITF_POWER:
  397. break;
  398. case USB_DESCTYPE_BOS:
  399. transc->xfer_buf = _usb_bos_desc_get(udev, desc_index, &transc->xfer_len);
  400. break;
  401. default:
  402. break;
  403. }
  404. break;
  405. case USB_RECPTYPE_ITF:
  406. /* get device class special descriptor */
  407. status = (usb_reqsta)(udev->class_core->req_process(udev, req));
  408. break;
  409. case USB_RECPTYPE_EP:
  410. break;
  411. default:
  412. break;
  413. }
  414. if ((transc->xfer_len) && (req->wLength)) {
  415. transc->xfer_len = USB_MIN(transc->xfer_len, req->wLength);
  416. if ((transc->xfer_len < udev->control.req.wLength) &&
  417. (0U == transc->xfer_len % transc->max_len)) {
  418. udev->control.ctl_zlp = 1U;
  419. }
  420. status = REQ_SUPP;
  421. }
  422. return status;
  423. }
  424. /*!
  425. \brief handle USB Set_Descriptor request
  426. \param[in] udev: pointer to USB device instance
  427. \param[in] req: pointer to USB device request
  428. \param[out] none
  429. \retval USB device request status
  430. */
  431. static usb_reqsta _usb_std_setdescriptor (usb_dev *udev, usb_req *req)
  432. {
  433. (void)udev;
  434. (void)req;
  435. /* no handle */
  436. return REQ_SUPP;
  437. }
  438. /*!
  439. \brief handle USB Get_Configuration request
  440. \param[in] udev: pointer to USB device instance
  441. \param[in] req: pointer to USB device request
  442. \param[out] none
  443. \retval USB device request status
  444. */
  445. static usb_reqsta _usb_std_getconfiguration (usb_dev *udev, usb_req *req)
  446. {
  447. (void)req;
  448. usb_reqsta req_status = REQ_NOTSUPP;
  449. switch (udev->cur_status) {
  450. case USBD_ADDRESSED:
  451. if (0U == udev->config) {
  452. req_status = REQ_SUPP;
  453. }
  454. break;
  455. case USBD_CONFIGURED:
  456. if (udev->config) {
  457. req_status = REQ_SUPP;
  458. }
  459. break;
  460. default:
  461. break;
  462. }
  463. if (REQ_SUPP == req_status) {
  464. usb_transc_config(&udev->transc_in[0], &(udev->config), 1U, 1U);
  465. }
  466. return req_status;
  467. }
  468. /*!
  469. \brief handle USB Set_Configuration request
  470. \param[in] udev: pointer to USB device instance
  471. \param[in] req: pointer to USB device request
  472. \param[out] none
  473. \retval USB device operation cur_status
  474. */
  475. static usb_reqsta _usb_std_setconfiguration (usb_dev *udev, usb_req *req)
  476. {
  477. static uint8_t config;
  478. usb_reqsta status = REQ_NOTSUPP;
  479. config = (uint8_t)(req->wValue);
  480. if (config <= USBD_CFG_MAX_NUM) {
  481. switch (udev->cur_status) {
  482. case USBD_ADDRESSED:
  483. if (config){
  484. (void)udev->class_core->init(udev, config);
  485. udev->config = config;
  486. udev->cur_status = (uint8_t)USBD_CONFIGURED;
  487. }
  488. status = REQ_SUPP;
  489. break;
  490. case USBD_CONFIGURED:
  491. if (0U == config) {
  492. (void)udev->class_core->deinit(udev, config);
  493. udev->config = config;
  494. udev->cur_status = (uint8_t)USBD_ADDRESSED;
  495. } else if (config != udev->config) {
  496. /* clear old configuration */
  497. (void)udev->class_core->deinit(udev, udev->config);
  498. /* set new configuration */
  499. udev->config = config;
  500. (void)udev->class_core->init(udev, config);
  501. } else {
  502. /* no operation */
  503. }
  504. status = REQ_SUPP;
  505. break;
  506. case USBD_DEFAULT:
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. return status;
  513. }
  514. /*!
  515. \brief handle USB Get_Interface request
  516. \param[in] udev: pointer to USB device instance
  517. \param[in] req: pointer to USB device request
  518. \param[out] none
  519. \retval USB device request status
  520. */
  521. static usb_reqsta _usb_std_getinterface (usb_dev *udev, usb_req *req)
  522. {
  523. switch (udev->cur_status) {
  524. case USBD_DEFAULT:
  525. break;
  526. case USBD_ADDRESSED:
  527. break;
  528. case USBD_CONFIGURED:
  529. if (BYTE_LOW(req->wIndex) < USBD_ITF_MAX_NUM) {
  530. usb_transc_config(&udev->transc_in[0], &(udev->class_core->req_altset), 1U, 1U);
  531. return REQ_SUPP;
  532. }
  533. break;
  534. default:
  535. break;
  536. }
  537. return REQ_NOTSUPP;
  538. }
  539. /*!
  540. \brief handle USB Set_Interface request
  541. \param[in] udev: pointer to USB device instance
  542. \param[in] req: pointer to USB device request
  543. \param[out] none
  544. \retval USB device request status
  545. */
  546. static usb_reqsta _usb_std_setinterface (usb_dev *udev, usb_req *req)
  547. {
  548. switch (udev->cur_status) {
  549. case USBD_DEFAULT:
  550. break;
  551. case USBD_ADDRESSED:
  552. break;
  553. case USBD_CONFIGURED:
  554. if (BYTE_LOW(req->wIndex) < USBD_ITF_MAX_NUM) {
  555. udev->class_core->req_altset = (uint8_t)req->wValue;
  556. udev->class_core->req_process(udev, req);
  557. return REQ_SUPP;
  558. }
  559. break;
  560. default:
  561. break;
  562. }
  563. return REQ_NOTSUPP;
  564. }
  565. /*!
  566. \brief handle USB SynchFrame request
  567. \param[in] udev: pointer to USB device instance
  568. \param[in] req: pointer to USB device request
  569. \param[out] none
  570. \retval USB device request status
  571. */
  572. static usb_reqsta _usb_std_synchframe (usb_dev *udev, usb_req *req)
  573. {
  574. (void)udev;
  575. (void)req;
  576. /* no handle */
  577. return REQ_SUPP;
  578. }
  579. /*!
  580. \brief convert hex 32bits value into unicode char
  581. \param[in] value: Hex 32bits value
  582. \param[in] pbuf: buffer pointer to store unicode char
  583. \param[in] len: value length
  584. \param[out] none
  585. \retval none
  586. */
  587. static void int_to_unicode (uint32_t value, uint8_t *pbuf, uint8_t len)
  588. {
  589. uint8_t index = 0U;
  590. for (index = 0U; index < len; index++) {
  591. if ((value >> 28U) < 0x0AU) {
  592. pbuf[2U * index] = (uint8_t)((value >> 28) + '0');
  593. } else {
  594. pbuf[2U * index] = (uint8_t)((value >> 28) + 'A' - 10U);
  595. }
  596. value = value << 4U;
  597. pbuf[2U * index + 1U] = 0U;
  598. }
  599. }
  600. /*!
  601. \brief convert hex 32bits value into unicode char
  602. \param[in] unicode_str: pointer to unicode string
  603. \param[out] none
  604. \retval none
  605. */
  606. void serial_string_get (uint16_t *unicode_str)
  607. {
  608. if (6U != (unicode_str[0] & 0x00FFU)) {
  609. uint32_t DeviceSerial0, DeviceSerial1, DeviceSerial2;
  610. DeviceSerial0 = *(uint32_t*)DEVICE_ID1;
  611. DeviceSerial1 = *(uint32_t*)DEVICE_ID2;
  612. DeviceSerial2 = *(uint32_t*)DEVICE_ID3;
  613. DeviceSerial0 += DeviceSerial2;
  614. if (0U != DeviceSerial0) {
  615. int_to_unicode(DeviceSerial0, (uint8_t*)&(unicode_str[1]), 8U);
  616. int_to_unicode(DeviceSerial1, (uint8_t*)&(unicode_str[9]), 4U);
  617. }
  618. } else {
  619. uint32_t device_serial = *(uint32_t*)DEVICE_ID;
  620. if (0U != device_serial) {
  621. unicode_str[1] = (uint16_t)(device_serial & 0x0000FFFFU);
  622. unicode_str[2] = (uint16_t)((device_serial & 0xFFFF0000U) >> 16U);
  623. }
  624. }
  625. }