usbd_stm32f429_otgfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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_compat.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. static USB_OTG_GlobalTypeDef * const OTG = (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_GLOBAL_BASE);
  27. static USB_OTG_DeviceTypeDef * const OTGD = (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_DEVICE_BASE);
  28. static volatile uint32_t * const OTGPCTL = (void*)(USB_OTG_FS_PERIPH_BASE + USB_OTG_PCGCCTL_BASE);
  29. inline static uint32_t* EPFIFO(uint32_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(uint32_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(uint32_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. static 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. static 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. static 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. static void enable(bool enable) {
  87. if (enable) {
  88. /* enabling USB_OTG in RCC */
  89. _BST(RCC->AHB2ENR, RCC_AHB2ENR_OTGFSEN);
  90. /* waiting AHB ready */
  91. _WBS(OTG->GRSTCTL, USB_OTG_GRSTCTL_AHBIDL);
  92. /* configure OTG as device */
  93. _BMD(OTG->GUSBCFG,
  94. USB_OTG_GUSBCFG_SRPCAP | _VAL2FLD(USB_OTG_GUSBCFG_TRDT, 0x0F),
  95. USB_OTG_GUSBCFG_FDMOD | _VAL2FLD(USB_OTG_GUSBCFG_TRDT, 0x06));
  96. /* configuring Vbus sense and SOF output */
  97. #if defined (USBD_VBUS_DETECT) && defined(USBD_SOF_OUT)
  98. OTG->GCCFG = USB_OTG_GCCFG_VBUSBSEN | USB_OTG_GCCFG_SOFOUTEN;
  99. #elif defined(USBD_VBUS_DETECT)
  100. OTG->GCCFG = USB_OTG_GCCFG_VBUSBSEN;
  101. #elif defined(USBD_SOF_OUT)
  102. OTG->GCCFG = USB_OTG_GCCFG_NOVBUSSENS | USB_OTG_GCCFG_SOFOUTEN;
  103. #else
  104. OTG->GCCFG = USB_OTG_GCCFG_NOVBUSSENS;
  105. #endif
  106. /* enable PHY clock */
  107. *OTGPCTL = 0;
  108. /* soft disconnect device */
  109. _BST(OTGD->DCTL, USB_OTG_DCTL_SDIS);
  110. /* Setup USB FS speed and frame interval */
  111. _BMD(OTGD->DCFG, USB_OTG_DCFG_PERSCHIVL | USB_OTG_DCFG_DSPD,
  112. _VAL2FLD(USB_OTG_DCFG_PERSCHIVL, 0) | _VAL2FLD(USB_OTG_DCFG_DSPD, 0x03));
  113. /* setting max RX FIFO size */
  114. OTG->GRXFSIZ = RX_FIFO_SZ;
  115. /* setting up EP0 TX FIFO SZ as 64 byte */
  116. OTG->DIEPTXF0_HNPTXFSIZ = RX_FIFO_SZ | (0x10 << 16);
  117. /* unmask EP interrupts */
  118. OTGD->DIEPMSK = USB_OTG_DIEPMSK_XFRCM;
  119. /* unmask core interrupts */
  120. OTG->GINTMSK = USB_OTG_GINTMSK_USBRST | USB_OTG_GINTMSK_ENUMDNEM |
  121. #if !defined(USBD_SOF_DISABLED)
  122. USB_OTG_GINTMSK_SOFM |
  123. #endif
  124. USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_WUIM |
  125. USB_OTG_GINTMSK_IEPINT | USB_OTG_GINTMSK_RXFLVLM;
  126. /* clear pending interrupts */
  127. OTG->GINTSTS = 0xFFFFFFFF;
  128. /* unmask global interrupt */
  129. _BST(OTG->GAHBCFG, USB_OTG_GAHBCFG_GINT);
  130. } else {
  131. if (RCC->AHB2ENR & RCC_AHB2ENR_OTGFSEN) {
  132. _BST(RCC->AHB2RSTR, RCC_AHB2RSTR_OTGFSRST);
  133. _BCL(RCC->AHB2RSTR, RCC_AHB2RSTR_OTGFSRST);
  134. _BCL(RCC->AHB2ENR, RCC_AHB2ENR_OTGFSEN);
  135. }
  136. }
  137. }
  138. static uint8_t connect(bool connect) {
  139. if (connect) {
  140. /* The ST made a strange thing again. Really i dont'understand what is the reason to name
  141. signal as PWRDWN (Power down PHY) when it works as "Power up" */
  142. _BST(OTG->GCCFG, USB_OTG_GCCFG_PWRDWN);
  143. _BCL(OTGD->DCTL, USB_OTG_DCTL_SDIS);
  144. } else {
  145. _BST(OTGD->DCTL, USB_OTG_DCTL_SDIS);
  146. _BCL(OTG->GCCFG, USB_OTG_GCCFG_PWRDWN);
  147. }
  148. return usbd_lane_unk;
  149. }
  150. static void setaddr (uint8_t addr) {
  151. _BMD(OTGD->DCFG, USB_OTG_DCFG_DAD, addr << 4);
  152. }
  153. /**\brief Helper. Set up TX fifo
  154. * \param ep endpoint index
  155. * \param epsize required max packet size in bytes
  156. * \return true if TX fifo is successfully set
  157. */
  158. static bool set_tx_fifo(uint8_t ep, uint16_t epsize) {
  159. uint32_t _fsa = OTG->DIEPTXF0_HNPTXFSIZ;
  160. /* calculating initial TX FIFO address. next from EP0 TX fifo */
  161. _fsa = 0xFFFF & (_fsa + (_fsa >> 16));
  162. /* looking for next free TX fifo address */
  163. for (int i = 0; i < (MAX_EP - 1); i++) {
  164. uint32_t _t = OTG->DIEPTXF[i];
  165. if ((_t & 0xFFFF) < 0x200) {
  166. _t = 0xFFFF & (_t + (_t >> 16));
  167. if (_t > _fsa) {
  168. _fsa = _t;
  169. }
  170. }
  171. }
  172. /* calculating requited TX fifo size */
  173. /* getting in 32 bit terms */
  174. epsize = (epsize + 0x03) >> 2;
  175. /* it must be 16 32-bit words minimum */
  176. if (epsize < 0x10) epsize = 0x10;
  177. /* checking for the available fifo */
  178. if ((_fsa + epsize) > MAX_FIFO_SZ) return false;
  179. /* programming fifo register */
  180. _fsa |= (epsize << 16);
  181. OTG->DIEPTXF[ep - 1] = _fsa;
  182. return true;
  183. }
  184. static bool ep_config(uint8_t ep, uint8_t eptype, uint16_t epsize) {
  185. if (ep == 0) {
  186. /* configureing control endpoint EP0 */
  187. uint32_t mpsize;
  188. if (epsize <= 0x08) {
  189. epsize = 0x08;
  190. mpsize = 0x03;
  191. } else if (epsize <= 0x10) {
  192. epsize = 0x10;
  193. mpsize = 0x02;
  194. } else if (epsize <= 0x20) {
  195. epsize = 0x20;
  196. mpsize = 0x01;
  197. } else {
  198. epsize = 0x40;
  199. mpsize = 0x00;
  200. }
  201. /* EP0 TX FIFO size is setted on init level */
  202. /* enabling RX and TX interrupts from EP0 */
  203. OTGD->DAINTMSK |= 0x00010001;
  204. /* setting up EP0 TX and RX registers */
  205. /*EPIN(ep)->DIEPTSIZ = epsize;*/
  206. EPIN(ep)->DIEPCTL = mpsize | USB_OTG_DIEPCTL_SNAK;
  207. /* 1 setup packet, 1 packets total */
  208. EPOUT(ep)->DOEPTSIZ = epsize | (1 << 29) | (1 << 19);
  209. EPOUT(ep)->DOEPCTL = mpsize | USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK;
  210. return true;
  211. }
  212. if (ep & 0x80) {
  213. ep &= 0x7F;
  214. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  215. /* configuring TX endpoint */
  216. /* setting up TX fifo and size register */
  217. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  218. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  219. if (!set_tx_fifo(ep, epsize << 1)) return false;
  220. } else {
  221. if (!set_tx_fifo(ep, epsize)) return false;
  222. }
  223. /* enabling EP TX interrupt */
  224. OTGD->DAINTMSK |= (0x0001UL << ep);
  225. /* setting up TX control register*/
  226. switch (eptype) {
  227. case USB_EPTYPE_ISOCHRONUS:
  228. epi->DIEPCTL = USB_OTG_DIEPCTL_EPENA | USB_OTG_DIEPCTL_CNAK |
  229. (0x01 << 18) | USB_OTG_DIEPCTL_USBAEP |
  230. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  231. (ep << 22) | epsize;
  232. break;
  233. case USB_EPTYPE_BULK:
  234. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  235. epi->DIEPCTL = USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_USBAEP |
  236. (0x02 << 18) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  237. (ep << 22) | epsize;
  238. break;
  239. default:
  240. epi->DIEPCTL = USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_USBAEP |
  241. (0x03 << 18) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  242. (ep << 22) | epsize;
  243. break;
  244. }
  245. } else {
  246. /* configuring RX endpoint */
  247. USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep);
  248. /* setting up RX control register */
  249. switch (eptype) {
  250. case USB_EPTYPE_ISOCHRONUS:
  251. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  252. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  253. (0x01 << 18) | epsize;
  254. break;
  255. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  256. case USB_EPTYPE_BULK:
  257. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  258. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  259. (0x02 << 18) | epsize;
  260. break;
  261. default:
  262. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  263. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  264. (0x03 << 18) | epsize;
  265. break;
  266. }
  267. }
  268. return true;
  269. }
  270. static void ep_deconfig(uint8_t ep) {
  271. ep &= 0x7F;
  272. volatile USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  273. volatile USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep);
  274. /* deconfiguring TX part */
  275. /* disable interrupt */
  276. OTGD->DAINTMSK &= ~(0x10001 << ep);
  277. /* decativating endpoint */
  278. _BCL(epi->DIEPCTL, USB_OTG_DIEPCTL_USBAEP);
  279. /* flushing FIFO */
  280. Flush_TX(ep);
  281. /* disabling endpoint */
  282. if ((epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) && (ep != 0)) {
  283. epi->DIEPCTL = USB_OTG_DIEPCTL_EPDIS;
  284. }
  285. /* clean EP interrupts */
  286. epi->DIEPINT = 0xFF;
  287. /* deconfiguring TX FIFO */
  288. if (ep > 0) {
  289. OTG->DIEPTXF[ep-1] = 0x02000200 + 0x200 * ep;
  290. }
  291. /* deconfigureing RX part */
  292. _BCL(epo->DOEPCTL, USB_OTG_DOEPCTL_USBAEP);
  293. if ((epo->DOEPCTL & USB_OTG_DOEPCTL_EPENA) && (ep != 0)) {
  294. epo->DOEPCTL = USB_OTG_DOEPCTL_EPDIS;
  295. }
  296. epo->DOEPINT = 0xFF;
  297. }
  298. static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) {
  299. uint32_t len, tmp = 0;
  300. volatile uint32_t *fifo = EPFIFO(0);
  301. /* no data in RX FIFO */
  302. if (!(OTG->GINTSTS & USB_OTG_GINTSTS_RXFLVL)) return -1;
  303. ep &= 0x7F;
  304. if ((OTG->GRXSTSR & USB_OTG_GRXSTSP_EPNUM) != ep) return -1;
  305. /* pop data from fifo */
  306. len = _FLD2VAL(USB_OTG_GRXSTSP_BCNT, OTG->GRXSTSP);
  307. for (int idx = 0; idx < len; idx++) {
  308. if ((idx & 0x03) == 0x00) {
  309. tmp = *fifo;
  310. }
  311. if (idx < blen) {
  312. ((uint8_t*)buf)[idx] = tmp & 0xFF;
  313. tmp >>= 8;
  314. }
  315. }
  316. _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  317. return (len < blen) ? len : blen;
  318. }
  319. static int32_t ep_write(uint8_t ep, const void *buf, uint16_t blen) {
  320. uint32_t len, tmp = 0;
  321. ep &= 0x7F;
  322. volatile uint32_t* fifo = EPFIFO(ep);
  323. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  324. /* check if EP enabled*/
  325. if (ep != 0 && epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) return -1;
  326. /* transfer data size in 32-bit words */
  327. len = (blen + 3) >> 2;
  328. /* no enough space in TX fifo */
  329. if (len > 0 && len > _FLD2VAL(USB_OTG_DTXFSTS_INEPTFSAV, epi->DTXFSTS)) return -1;
  330. //epi->DIEPTSIZ = 0;
  331. epi->DIEPTSIZ = (1 << 19) + blen;
  332. _BST(epi->DIEPCTL, USB_OTG_DIEPCTL_EPENA | USB_OTG_DIEPCTL_CNAK);
  333. /* push data to FIFO */
  334. for (int idx = 0; idx < blen; idx++) {
  335. tmp |= (uint32_t)((const uint8_t*)buf)[idx] << ((idx & 0x03) << 3);
  336. if ((idx & 0x03) == 0x03 || (idx + 1) == blen) {
  337. *fifo = tmp;
  338. tmp = 0;
  339. }
  340. }
  341. return blen;
  342. }
  343. static uint16_t get_frame (void) {
  344. return _FLD2VAL(USB_OTG_DSTS_FNSOF, OTGD->DSTS);
  345. }
  346. static void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
  347. uint32_t evt;
  348. uint32_t ep = 0;
  349. while (1) {
  350. uint32_t _t = OTG->GINTSTS;
  351. /* bus RESET event */
  352. if (_t & USB_OTG_GINTSTS_USBRST) {
  353. OTG->GINTSTS = USB_OTG_GINTSTS_USBRST;
  354. for (uint8_t i = 0; i < MAX_EP; i++ ) {
  355. ep_deconfig(i);
  356. }
  357. Flush_RX();
  358. continue;
  359. } else if (_t & USB_OTG_GINTSTS_ENUMDNE) {
  360. OTG->GINTSTS = USB_OTG_GINTSTS_ENUMDNE;
  361. evt = usbd_evt_reset;
  362. } else if (_t & USB_OTG_GINTSTS_IEPINT) {
  363. for (;; ep++) {
  364. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  365. if (ep >= MAX_EP) return;
  366. if (epi->DIEPINT & USB_OTG_DIEPINT_XFRC) {
  367. epi->DIEPINT = USB_OTG_DIEPINT_XFRC;
  368. evt = usbd_evt_eptx;
  369. ep |= 0x80;
  370. break;
  371. }
  372. }
  373. } else if (_t & USB_OTG_GINTSTS_RXFLVL) {
  374. _t = OTG->GRXSTSR;
  375. ep = _t & USB_OTG_GRXSTSP_EPNUM;
  376. switch (_FLD2VAL(USB_OTG_GRXSTSP_PKTSTS, _t)) {
  377. case 0x02: /* OUT recieved */
  378. evt = usbd_evt_eprx;
  379. break;
  380. case 0x06: /* SETUP recieved */
  381. /* flushing TX if sonething stuck in control endpoint */
  382. if (EPIN(ep)->DIEPTSIZ & USB_OTG_DIEPTSIZ_PKTCNT) {
  383. Flush_TX(ep);
  384. }
  385. evt = usbd_evt_epsetup;
  386. break;
  387. default:
  388. /* pop GRXSTSP */
  389. OTG->GRXSTSP;
  390. continue;
  391. }
  392. #if !defined(USBD_SOF_DISABLED)
  393. } else if (_t & USB_OTG_GINTSTS_SOF) {
  394. OTG->GINTSTS = USB_OTG_GINTSTS_SOF;
  395. evt = usbd_evt_sof;
  396. #endif
  397. } else if (_t & USB_OTG_GINTSTS_USBSUSP) {
  398. evt = usbd_evt_susp;
  399. OTG->GINTSTS = USB_OTG_GINTSTS_USBSUSP;
  400. } else if (_t & USB_OTG_GINTSTS_WKUINT) {
  401. OTG->GINTSTS = USB_OTG_GINTSTS_WKUINT;
  402. evt = usbd_evt_wkup;
  403. } else {
  404. /* no more supported events */
  405. return;
  406. }
  407. callback(dev, evt, ep);
  408. }
  409. }
  410. static uint32_t fnv1a32_turn (uint32_t fnv, uint32_t data ) {
  411. for (int i = 0; i < 4 ; i++) {
  412. fnv ^= (data & 0xFF);
  413. fnv *= 16777619;
  414. data >>= 8;
  415. }
  416. return fnv;
  417. }
  418. static uint16_t get_serialno_desc(void *buffer) {
  419. struct usb_string_descriptor *dsc = buffer;
  420. uint16_t *str = dsc->wString;
  421. uint32_t fnv = 2166136261;
  422. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x00));
  423. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x04));
  424. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x08));
  425. for (int i = 28; i >= 0; i -= 4 ) {
  426. uint16_t c = (fnv >> i) & 0x0F;
  427. c += (c < 10) ? '0' : ('A' - 10);
  428. *str++ = c;
  429. }
  430. dsc->bDescriptorType = USB_DTYPE_STRING;
  431. dsc->bLength = 18;
  432. return 18;
  433. }
  434. __attribute__((externally_visible)) const struct usbd_driver usbd_otgfs = {
  435. getinfo,
  436. enable,
  437. connect,
  438. setaddr,
  439. ep_config,
  440. ep_deconfig,
  441. ep_read,
  442. ep_write,
  443. ep_setstall,
  444. ep_isstalled,
  445. evt_poll,
  446. get_frame,
  447. get_serialno_desc,
  448. };
  449. #endif //USBD_STM32F429FS