usbd_stm32f429_otgfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "stm32.h"
  18. #include "usb.h"
  19. #if defined(USBD_STM32F429FS)
  20. #define MAX_EP 4
  21. #define MAX_RX_PACKET 128
  22. #define MAX_CONTROL_EP 1
  23. #define MAX_FIFO_SZ 320 /*in 32-bit chunks */
  24. #define RX_FIFO_SZ ((4 * MAX_CONTROL_EP + 6) + ((MAX_RX_PACKET / 4) + 1) + (MAX_EP * 2) + 1)
  25. #define STATUS_VAL(x) (USBD_HW_ADDRFST | (x))
  26. USB_OTG_GlobalTypeDef * const OTG = (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_GLOBAL_BASE);
  27. USB_OTG_DeviceTypeDef * const OTGD = (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_DEVICE_BASE);
  28. volatile uint32_t * const OTGPCTL = (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_PCGCCTL_BASE);
  29. inline static volatile uint32_t* EPFIFO(uint8_t ep) {
  30. return (uint32_t*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_FIFO_BASE + (ep << 12));
  31. }
  32. inline static USB_OTG_INEndpointTypeDef* EPIN(uint8_t ep) {
  33. return (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_IN_ENDPOINT_BASE + (ep << 5));
  34. }
  35. inline static USB_OTG_OUTEndpointTypeDef* EPOUT(uint8_t ep) {
  36. return (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_OUT_ENDPOINT_BASE + (ep << 5));
  37. }
  38. inline static void Flush_RX(void) {
  39. _BST(OTG->GRSTCTL, USB_OTG_GRSTCTL_RXFFLSH);
  40. _WBC(OTG->GRSTCTL, USB_OTG_GRSTCTL_RXFFLSH);
  41. }
  42. inline static void Flush_TX(uint8_t ep) {
  43. _BMD(OTG->GRSTCTL, USB_OTG_GRSTCTL_TXFNUM,
  44. _VAL2FLD(USB_OTG_GRSTCTL_TXFNUM, ep) | USB_OTG_GRSTCTL_TXFFLSH);
  45. _WBC(OTG->GRSTCTL, USB_OTG_GRSTCTL_TXFFLSH);
  46. }
  47. uint32_t getinfo(void) {
  48. if (!(RCC->AHB2ENR & RCC_AHB2ENR_OTGFSEN)) return STATUS_VAL(0);
  49. if (!(OTGD->DCTL & USB_OTG_DCTL_SDIS)) return STATUS_VAL(USBD_HW_ENABLED | USBD_HW_SPEED_FS);
  50. return STATUS_VAL(USBD_HW_ENABLED);
  51. }
  52. void ep_setstall(uint8_t ep, bool stall) {
  53. if (ep & 0x80) {
  54. ep &= 0x7F;
  55. uint32_t _t = EPIN(ep)->DIEPCTL;
  56. if (_t & USB_OTG_DIEPCTL_USBAEP) {
  57. if (stall) {
  58. _BST(_t, USB_OTG_DIEPCTL_STALL);
  59. } else {
  60. _BMD(_t, USB_OTG_DIEPCTL_STALL,
  61. USB_OTG_DIEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_SNAK);
  62. }
  63. EPIN(ep)->DIEPCTL = _t;
  64. }
  65. } else {
  66. uint32_t _t = EPOUT(ep)->DOEPCTL;
  67. if (_t & USB_OTG_DOEPCTL_USBAEP) {
  68. if (stall) {
  69. _BST(_t, USB_OTG_DOEPCTL_STALL);
  70. } else {
  71. _BMD(_t, USB_OTG_DOEPCTL_STALL,
  72. USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK);
  73. }
  74. EPOUT(ep)->DOEPCTL = _t;
  75. }
  76. }
  77. }
  78. bool ep_isstalled(uint8_t ep) {
  79. if (ep & 0x80) {
  80. ep &= 0x7F;
  81. return (EPIN(ep)->DIEPCTL & USB_OTG_DIEPCTL_STALL) ? true : false;
  82. } else {
  83. return (EPOUT(ep)->DOEPCTL & USB_OTG_DOEPCTL_STALL) ? true : false;
  84. }
  85. }
  86. void enable(bool enable) {
  87. if (enable) {
  88. /* enabling USB_OTG in RCC */
  89. _BST(RCC->AHB2ENR, RCC_AHB2ENR_OTGFSEN);
  90. /* do core soft reset */
  91. _WBS(OTG->GRSTCTL, USB_OTG_GRSTCTL_AHBIDL);
  92. _BST(OTG->GRSTCTL, USB_OTG_GRSTCTL_CSRST);
  93. _WBC(OTG->GRSTCTL, USB_OTG_GRSTCTL_CSRST);
  94. /* configure OTG as device */
  95. OTG->GUSBCFG = USB_OTG_GUSBCFG_FDMOD | USB_OTG_GUSBCFG_PHYSEL |
  96. _VAL2FLD(USB_OTG_GUSBCFG_TRDT, 0x06);
  97. /* configuring Vbus sense and SOF output */
  98. #if defined (USBD_VBUS_DETECT) && defined(USBD_SOF_OUT)
  99. OTG->GCCFG = USB_OTG_GCCFG_VBUSBSEN | USB_OTG_GCCFG_SOFOUTEN;
  100. #elif defined(USBD_VBUS_DETECT)
  101. OTG->GCCFG = USB_OTG_GCCFG_VBUSBSEN;
  102. #elif defined(USBD_SOF_OUT)
  103. OTG->GCCFG = USB_OTG_GCCFG_NOVBUSSENS | USB_OTG_GCCFG_SOFOUTEN;
  104. #else
  105. OTG->GCCFG = USB_OTG_GCCFG_NOVBUSSENS;
  106. #endif
  107. /* enable PHY clock */
  108. *OTGPCTL = 0;
  109. /* soft disconnect device */
  110. _BST(OTGD->DCTL, USB_OTG_DCTL_SDIS);
  111. /* Setup USB FS speed and frame interval */
  112. _BMD(OTGD->DCFG, USB_OTG_DCFG_PERSCHIVL | USB_OTG_DCFG_DSPD,
  113. _VAL2FLD(USB_OTG_DCFG_PERSCHIVL, 0) | _VAL2FLD(USB_OTG_DCFG_DSPD, 0x03));
  114. /* setting max RX FIFO size */
  115. OTG->GRXFSIZ = RX_FIFO_SZ;
  116. /* setting up EP0 TX FIFO SZ as 64 byte */
  117. OTG->DIEPTXF0_HNPTXFSIZ = RX_FIFO_SZ | (0x10 << 16);
  118. /* unmask EP interrupts */
  119. OTGD->DIEPMSK = USB_OTG_DIEPMSK_XFRCM;
  120. /* unmask core interrupts */
  121. OTG->GINTMSK = USB_OTG_GINTMSK_USBRST | USB_OTG_GINTMSK_ENUMDNEM |
  122. #if !defined(USBD_SOF_DISABLED)
  123. USB_OTG_GINTMSK_SOFM |
  124. #endif
  125. USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_WUIM |
  126. USB_OTG_GINTMSK_IEPINT | USB_OTG_GINTMSK_RXFLVLM;
  127. /* clear pending interrupts */
  128. OTG->GINTSTS = 0xFFFFFFFF;
  129. /* unmask global interrupt */
  130. _BST(OTG->GAHBCFG, USB_OTG_GAHBCFG_GINT);
  131. } else {
  132. if (RCC->AHB2ENR & RCC_AHB2ENR_OTGFSEN) {
  133. _BST(RCC->AHB2RSTR, RCC_AHB2RSTR_OTGFSRST);
  134. _BCL(RCC->AHB2RSTR, RCC_AHB2RSTR_OTGFSRST);
  135. _BCL(RCC->AHB2ENR, RCC_AHB2ENR_OTGFSEN);
  136. }
  137. }
  138. }
  139. uint8_t connect(bool connect) {
  140. if (connect) {
  141. /* The ST made a strange thing again. Really i dont'understand what is the reason to name
  142. signal as PWRDWN (Power down PHY) when it works as "Power up" */
  143. _BST(OTG->GCCFG, USB_OTG_GCCFG_PWRDWN);
  144. _BCL(OTGD->DCTL, USB_OTG_DCTL_SDIS);
  145. } else {
  146. _BST(OTGD->DCTL, USB_OTG_DCTL_SDIS);
  147. _BCL(OTG->GCCFG, USB_OTG_GCCFG_PWRDWN);
  148. }
  149. return usbd_lane_unk;
  150. }
  151. void setaddr (uint8_t addr) {
  152. _BMD(OTGD->DCFG, USB_OTG_DCFG_DAD, addr << 4);
  153. }
  154. /**\brief Helper. Set up TX fifo
  155. * \param ep endpoint index
  156. * \param epsize required max packet size in bytes
  157. * \return true if TX fifo is successfully set
  158. */
  159. static bool set_tx_fifo(uint8_t ep, uint16_t epsize) {
  160. uint32_t _fsa = OTG->DIEPTXF0_HNPTXFSIZ;
  161. /* calculating initial TX FIFO address. next from EP0 TX fifo */
  162. _fsa = 0xFFFF & (_fsa + (_fsa >> 16));
  163. /* looking for next free TX fifo address */
  164. for (int i = 0; i < (MAX_EP - 1); i++) {
  165. uint32_t _t = OTG->DIEPTXF[i];
  166. if ((_t & 0xFFFF) < 0x200) {
  167. _t = 0xFFFF & (_t + (_t >> 16));
  168. if (_t > _fsa) {
  169. _fsa = _t;
  170. }
  171. }
  172. }
  173. /* calculating requited TX fifo size */
  174. /* getting in 32 bit terms */
  175. epsize = (epsize + 0x03) >> 2;
  176. /* it must be 16 32-bit words minimum */
  177. if (epsize < 0x10) epsize = 0x10;
  178. /* checking for the available fifo */
  179. if ((_fsa + epsize) > MAX_FIFO_SZ) return false;
  180. /* programming fifo register */
  181. _fsa |= (epsize << 16);
  182. OTG->DIEPTXF[ep - 1] = _fsa;
  183. return true;
  184. }
  185. bool ep_config(uint8_t ep, uint8_t eptype, uint16_t epsize) {
  186. if (ep == 0) {
  187. /* configureing control endpoint EP0 */
  188. uint32_t mpsize;
  189. if (epsize <= 0x08) {
  190. epsize = 0x08;
  191. mpsize = 0x03;
  192. } else if (epsize <= 0x10) {
  193. epsize = 0x10;
  194. mpsize = 0x02;
  195. } else if (epsize <= 0x20) {
  196. epsize = 0x20;
  197. mpsize = 0x01;
  198. } else {
  199. epsize = 0x40;
  200. mpsize = 0x00;
  201. }
  202. /* EP0 TX FIFO size is setted on init level */
  203. /* enabling RX and TX interrupts from EP0 */
  204. OTGD->DAINTMSK |= 0x00010001;
  205. /* setting up EP0 TX and RX registers */
  206. /*EPIN(ep)->DIEPTSIZ = epsize;*/
  207. EPIN(ep)->DIEPCTL = mpsize | USB_OTG_DIEPCTL_SNAK;
  208. /* 1 setup packet, 1 packets total */
  209. EPOUT(ep)->DOEPTSIZ = epsize | (1 << 29) | (1 << 19);
  210. EPOUT(ep)->DOEPCTL = mpsize | USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK;
  211. return true;
  212. }
  213. if (ep & 0x80) {
  214. ep &= 0x7F;
  215. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  216. /* configuring TX endpoint */
  217. /* setting up TX fifo and size register */
  218. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  219. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  220. if (!set_tx_fifo(ep, epsize << 1)) return false;
  221. } else {
  222. if (!set_tx_fifo(ep, epsize)) return false;
  223. }
  224. /* enabling EP TX interrupt */
  225. OTGD->DAINTMSK |= (0x0001UL << ep);
  226. /* setting up TX control register*/
  227. switch (eptype) {
  228. case USB_EPTYPE_ISOCHRONUS:
  229. epi->DIEPCTL = USB_OTG_DIEPCTL_EPENA | USB_OTG_DIEPCTL_CNAK |
  230. (0x01 << 18) | USB_OTG_DIEPCTL_USBAEP |
  231. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  232. (ep << 22) | epsize;
  233. break;
  234. case USB_EPTYPE_BULK:
  235. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  236. epi->DIEPCTL = USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_USBAEP |
  237. (0x02 << 18) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  238. (ep << 22) | epsize;
  239. break;
  240. default:
  241. epi->DIEPCTL = USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_USBAEP |
  242. (0x03 << 18) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  243. (ep << 22) | epsize;
  244. break;
  245. }
  246. } else {
  247. /* configuring RX endpoint */
  248. USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep);
  249. /* setting up RX control register */
  250. switch (eptype) {
  251. case USB_EPTYPE_ISOCHRONUS:
  252. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  253. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  254. (0x01 << 18) | epsize;
  255. break;
  256. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  257. case USB_EPTYPE_BULK:
  258. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  259. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  260. (0x02 << 18) | epsize;
  261. break;
  262. default:
  263. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  264. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  265. (0x03 << 18) | epsize;
  266. break;
  267. }
  268. }
  269. return true;
  270. }
  271. void ep_deconfig(uint8_t ep) {
  272. ep &= 0x7F;
  273. volatile USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  274. volatile USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep);
  275. /* deconfiguring TX part */
  276. /* disable interrupt */
  277. OTGD->DAINTMSK &= ~(0x10001 << ep);
  278. /* decativating endpoint */
  279. _BCL(epi->DIEPCTL, USB_OTG_DIEPCTL_USBAEP);
  280. /* flushing FIFO */
  281. Flush_TX(ep);
  282. /* disabling endpoint */
  283. if ((epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) && (ep != 0)) {
  284. epi->DIEPCTL = USB_OTG_DIEPCTL_EPDIS;
  285. }
  286. /* clean EP interrupts */
  287. epi->DIEPINT = 0xFF;
  288. /* deconfiguring TX FIFO */
  289. if (ep > 0) {
  290. OTG->DIEPTXF[ep-1] = 0x02000200 + 0x200 * ep;
  291. }
  292. /* deconfigureing RX part */
  293. _BCL(epo->DOEPCTL, USB_OTG_DOEPCTL_USBAEP);
  294. if ((epo->DOEPCTL & USB_OTG_DOEPCTL_EPENA) && (ep != 0)) {
  295. epo->DOEPCTL = USB_OTG_DOEPCTL_EPDIS;
  296. }
  297. epo->DOEPINT = 0xFF;
  298. }
  299. int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) {
  300. int32_t len;
  301. volatile uint32_t *fifo = EPFIFO(0);
  302. /* no data in RX FIFO */
  303. if (!(OTG->GINTSTS & USB_OTG_GINTSTS_RXFLVL)) return -1;
  304. ep &= 0x7F;
  305. if ((OTG->GRXSTSR & USB_OTG_GRXSTSP_EPNUM) != ep) return -1;
  306. /* pop data from fifo */
  307. len = _FLD2VAL(USB_OTG_GRXSTSP_BCNT, OTG->GRXSTSP);
  308. for (unsigned i = 0; i < len; i +=4) {
  309. uint32_t _t = *fifo;
  310. if (blen >= 4) {
  311. *(__attribute__((packed))uint32_t*)buf = _t;
  312. blen -= 4;
  313. buf += 4;
  314. } else {
  315. while (blen){
  316. *(uint8_t*)buf++ = 0xFF & _t;
  317. _t >>= 8;
  318. blen --;
  319. }
  320. }
  321. }
  322. return len;
  323. }
  324. int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {
  325. ep &= 0x7F;
  326. volatile uint32_t* _fifo = EPFIFO(ep);
  327. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  328. /* transfer data size in 32-bit words */
  329. uint32_t _len = (blen + 3) >> 2;
  330. /* no enough space in TX fifo */
  331. if (_len > epi->DTXFSTS) return -1;
  332. if (ep != 0 && epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) {
  333. return -1;
  334. }
  335. epi->DIEPTSIZ = 0;
  336. epi->DIEPTSIZ = (1 << 19) + blen;
  337. _BMD(epi->DIEPCTL, USB_OTG_DIEPCTL_STALL, USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK);
  338. while (_len--) {
  339. *_fifo = *(__attribute__((packed)) uint32_t*)buf;
  340. buf += 4;
  341. }
  342. return blen;
  343. }
  344. uint16_t get_frame (void) {
  345. return _FLD2VAL(USB_OTG_DSTS_FNSOF, OTGD->DSTS);
  346. }
  347. void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
  348. uint32_t evt;
  349. uint32_t ep = 0;
  350. while (1) {
  351. uint32_t _t = OTG->GINTSTS;
  352. /* bus RESET event */
  353. if (_t & USB_OTG_GINTSTS_USBRST) {
  354. OTG->GINTSTS = USB_OTG_GINTSTS_USBRST;
  355. for (uint8_t i = 0; i < MAX_EP; i++ ) {
  356. ep_deconfig(i);
  357. }
  358. Flush_RX();
  359. continue;
  360. } else if (_t & USB_OTG_GINTSTS_ENUMDNE) {
  361. OTG->GINTSTS = USB_OTG_GINTSTS_ENUMDNE;
  362. evt = usbd_evt_reset;
  363. } else if (_t & USB_OTG_GINTSTS_IEPINT) {
  364. for (;; ep++) {
  365. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  366. if (ep >= MAX_EP) return;
  367. if (epi->DIEPINT & USB_OTG_DIEPINT_XFRC) {
  368. epi->DIEPINT = USB_OTG_DIEPINT_XFRC;
  369. evt = usbd_evt_eptx;
  370. ep |= 0x80;
  371. break;
  372. }
  373. }
  374. } else if (_t & USB_OTG_GINTSTS_RXFLVL) {
  375. _t = OTG->GRXSTSR;
  376. ep = _t & USB_OTG_GRXSTSP_EPNUM;
  377. switch (_FLD2VAL(USB_OTG_GRXSTSP_PKTSTS, _t)) {
  378. case 0x02: /* OUT recieved */
  379. evt = usbd_evt_eprx;
  380. break;
  381. case 0x06: /* SETUP recieved */
  382. /* flushing TX if sonething stuck in control endpoint */
  383. if (EPIN(ep)->DIEPTSIZ & USB_OTG_DIEPTSIZ_PKTCNT) {
  384. Flush_TX(ep);
  385. }
  386. evt = usbd_evt_epsetup;
  387. break;
  388. case 0x03: /* OUT completed */
  389. case 0x04: /* SETUP completed */
  390. _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  391. default:
  392. /* pop GRXSTSP */
  393. OTG->GRXSTSP;
  394. continue;
  395. }
  396. #if !defined(USBD_SOF_DISABLED)
  397. } else if (_t & USB_OTG_GINTSTS_SOF) {
  398. OTG->GINTSTS = USB_OTG_GINTSTS_SOF;
  399. evt = usbd_evt_sof;
  400. #endif
  401. } else if (_t & USB_OTG_GINTSTS_USBSUSP) {
  402. evt = usbd_evt_susp;
  403. OTG->GINTSTS = USB_OTG_GINTSTS_USBSUSP;
  404. } else if (_t & USB_OTG_GINTSTS_WKUINT) {
  405. OTG->GINTSTS = USB_OTG_GINTSTS_WKUINT;
  406. evt = usbd_evt_wkup;
  407. } else {
  408. /* no more supported events */
  409. return;
  410. }
  411. return callback(dev, evt, ep);
  412. }
  413. }
  414. static uint32_t fnv1a32_turn (uint32_t fnv, uint32_t data ) {
  415. for (int i = 0; i < 4 ; i++) {
  416. fnv ^= (data & 0xFF);
  417. fnv *= 16777619;
  418. data >>= 8;
  419. }
  420. return fnv;
  421. }
  422. uint16_t get_serialno_desc(void *buffer) {
  423. struct usb_string_descriptor *dsc = buffer;
  424. uint16_t *str = dsc->wString;
  425. uint32_t fnv = 2166136261;
  426. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x00));
  427. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x04));
  428. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x08));
  429. for (int i = 28; i >= 0; i -= 4 ) {
  430. uint16_t c = (fnv >> i) & 0x0F;
  431. c += (c < 10) ? '0' : ('A' - 10);
  432. *str++ = c;
  433. }
  434. dsc->bDescriptorType = USB_DTYPE_STRING;
  435. dsc->bLength = 18;
  436. return 18;
  437. }
  438. __attribute__((externally_visible)) const struct usbd_driver usbd_otgfs = {
  439. getinfo,
  440. enable,
  441. connect,
  442. setaddr,
  443. ep_config,
  444. ep_deconfig,
  445. ep_read,
  446. ep_write,
  447. ep_setstall,
  448. ep_isstalled,
  449. evt_poll,
  450. get_frame,
  451. get_serialno_desc,
  452. };
  453. #endif //USBD_STM32L476