usbd_stm32f105_otgfs.c 17 KB

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