usbd_stm32f429_otghs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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_STM32F429HS)
  20. #define MAX_EP 6
  21. #define MAX_RX_PACKET 128
  22. #define MAX_CONTROL_EP 1
  23. #define MAX_FIFO_SZ 1024 /*in 32-bit chunks */
  24. #define RX_FIFO_SZ (10 + (2 * (MAX_RX_PACKET / 4) + 1))
  25. #define STATUS_VAL(x) (USBD_HW_ADDRFST | (x))
  26. static USB_OTG_GlobalTypeDef * const OTG = (void*)(USB_OTG_HS_PERIPH_BASE + USB_OTG_GLOBAL_BASE);
  27. static USB_OTG_DeviceTypeDef * const OTGD = (void*)(USB_OTG_HS_PERIPH_BASE + USB_OTG_DEVICE_BASE);
  28. static volatile uint32_t * const OTGPCTL = (void*)(USB_OTG_HS_PERIPH_BASE + USB_OTG_PCGCCTL_BASE);
  29. inline static volatile uint32_t* EPFIFO(uint8_t ep) {
  30. return (uint32_t*)(USB_OTG_HS_PERIPH_BASE + USB_OTG_FIFO_BASE + (ep << 12));
  31. }
  32. inline static USB_OTG_INEndpointTypeDef* EPIN(uint8_t ep) {
  33. return (void*)(USB_OTG_HS_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_HS_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->AHB1ENR & RCC_AHB1ENR_OTGHSEN)) return STATUS_VAL(0);
  49. if (!(OTGD->DCTL & USB_OTG_DCTL_SDIS)) {
  50. if (_FLD2VAL(USB_OTG_DSTS_ENUMSPD, OTGD->DSTS) == 0x00) return STATUS_VAL(USBD_HW_ENABLED | USBD_HW_SPEED_HS);
  51. if (_FLD2VAL(USB_OTG_DSTS_ENUMSPD, OTGD->DSTS) == 0x03) return STATUS_VAL(USBD_HW_ENABLED | USBD_HW_SPEED_FS);
  52. }
  53. return STATUS_VAL(USBD_HW_ENABLED | USBD_HW_SPEED_NC);
  54. }
  55. static void ep_setstall(uint8_t ep, bool stall) {
  56. if (ep & 0x80) {
  57. ep &= 0x7F;
  58. uint32_t _t = EPIN(ep)->DIEPCTL;
  59. if (_t & USB_OTG_DIEPCTL_USBAEP) {
  60. if (stall) {
  61. _BST(_t, USB_OTG_DIEPCTL_STALL);
  62. } else {
  63. _BMD(_t, USB_OTG_DIEPCTL_STALL,
  64. USB_OTG_DIEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_SNAK);
  65. }
  66. EPIN(ep)->DIEPCTL = _t;
  67. }
  68. } else {
  69. uint32_t _t = EPOUT(ep)->DOEPCTL;
  70. if (_t & USB_OTG_DOEPCTL_USBAEP) {
  71. if (stall) {
  72. _BST(_t, USB_OTG_DOEPCTL_STALL);
  73. } else {
  74. _BMD(_t, USB_OTG_DOEPCTL_STALL,
  75. USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK);
  76. }
  77. EPOUT(ep)->DOEPCTL = _t;
  78. }
  79. }
  80. }
  81. static bool ep_isstalled(uint8_t ep) {
  82. if (ep & 0x80) {
  83. ep &= 0x7F;
  84. return (EPIN(ep)->DIEPCTL & USB_OTG_DIEPCTL_STALL) ? true : false;
  85. } else {
  86. return (EPOUT(ep)->DOEPCTL & USB_OTG_DOEPCTL_STALL) ? true : false;
  87. }
  88. }
  89. static void enable(bool enable) {
  90. if (enable) {
  91. /* enabling USB_OTG in RCC */
  92. _BST(RCC->AHB1ENR, RCC_AHB1ENR_OTGHSEN);
  93. _WBS(OTG->GRSTCTL, USB_OTG_GRSTCTL_AHBIDL);
  94. /* configure OTG as device */
  95. OTG->GUSBCFG = USB_OTG_GUSBCFG_FDMOD | USB_OTG_GUSBCFG_PHYSEL |
  96. _VAL2FLD(USB_OTG_GUSBCFG_TRDT, 0x09) |
  97. _VAL2FLD(USB_OTG_GUSBCFG_TOCAL, 0x01);
  98. /* VBUS detect alternafe function AF12 for PB13 is missed in tech documentation */
  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_NOVBUSSENS | USB_OTG_GCCFG_SOFOUTEN;
  105. #else
  106. OTG->GCCFG = USB_OTG_GCCFG_NOVBUSSENS;
  107. #endif
  108. /* do core soft reset */
  109. _BST(OTG->GRSTCTL, USB_OTG_GRSTCTL_CSRST);
  110. _WBC(OTG->GRSTCTL, USB_OTG_GRSTCTL_CSRST);
  111. /* Setup USB FS speed */
  112. _BMD(OTGD->DCFG, USB_OTG_DCFG_DSPD, _VAL2FLD(USB_OTG_DCFG_DSPD, 0x03));
  113. /* start PHY clock */
  114. *OTGPCTL = 0;
  115. /* soft disconnect device */
  116. _BST(OTGD->DCTL, USB_OTG_DCTL_SDIS);
  117. /* setting max RX FIFO size */
  118. OTG->GRXFSIZ = RX_FIFO_SZ;
  119. /* setting up EP0 TX FIFO SZ as 64 byte */
  120. OTG->DIEPTXF0_HNPTXFSIZ = RX_FIFO_SZ | (0x10 << 16);
  121. /* unmask EP interrupts */
  122. OTGD->DIEPMSK = USB_OTG_DIEPMSK_XFRCM;
  123. /* unmask core interrupts */
  124. OTG->GINTMSK = USB_OTG_GINTMSK_USBRST | USB_OTG_GINTMSK_ENUMDNEM |
  125. #if !defined(USBD_SOF_DISABLED)
  126. USB_OTG_GINTMSK_SOFM |
  127. #endif
  128. USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_WUIM |
  129. USB_OTG_GINTMSK_IEPINT | USB_OTG_GINTMSK_RXFLVLM;
  130. /* clear pending interrupts */
  131. OTG->GINTSTS = 0xFFFFFFFF;
  132. /* unmask global interrupt */
  133. _BST(OTG->GAHBCFG, USB_OTG_GAHBCFG_GINT);
  134. } else {
  135. if (RCC->AHB1ENR & RCC_AHB1ENR_OTGHSEN) {
  136. _BST(RCC->AHB1RSTR, RCC_AHB1RSTR_OTGHRST);
  137. _BCL(RCC->AHB1RSTR, RCC_AHB1RSTR_OTGHRST);
  138. _BCL(RCC->AHB1ENR, RCC_AHB1ENR_OTGHSEN);
  139. }
  140. }
  141. }
  142. static uint8_t connect(bool connect) {
  143. if (connect) {
  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. static 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. static bool ep_config(uint8_t ep, uint8_t eptype, uint16_t epsize) {
  187. if (ep == 0) {
  188. /* configureing 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 << 29) | (1 << 19);
  211. EPOUT(ep)->DOEPCTL = mpsize | USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_CNAK;
  212. return true;
  213. }
  214. if (ep & 0x80) {
  215. ep &= 0x7F;
  216. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  217. /* configuring TX endpoint */
  218. /* setting up TX fifo and size register */
  219. if ((eptype == USB_EPTYPE_ISOCHRONUS) ||
  220. (eptype == (USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF))) {
  221. if (!set_tx_fifo(ep, epsize << 1)) return false;
  222. } else {
  223. if (!set_tx_fifo(ep, epsize)) return false;
  224. }
  225. /* enabling EP TX interrupt */
  226. OTGD->DAINTMSK |= (0x0001UL << ep);
  227. /* setting up TX control register*/
  228. switch (eptype) {
  229. case USB_EPTYPE_ISOCHRONUS:
  230. epi->DIEPCTL = USB_OTG_DIEPCTL_EPENA | USB_OTG_DIEPCTL_CNAK |
  231. (0x01 << 18) | USB_OTG_DIEPCTL_USBAEP |
  232. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  233. (ep << 22) | epsize;
  234. break;
  235. case USB_EPTYPE_BULK:
  236. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  237. epi->DIEPCTL = USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_USBAEP |
  238. (0x02 << 18) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  239. (ep << 22) | epsize;
  240. break;
  241. default:
  242. epi->DIEPCTL = USB_OTG_DIEPCTL_SNAK | USB_OTG_DIEPCTL_USBAEP |
  243. (0x03 << 18) | USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  244. (ep << 22) | epsize;
  245. break;
  246. }
  247. } else {
  248. /* configuring RX endpoint */
  249. USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep);
  250. /* setting up RX control register */
  251. switch (eptype) {
  252. case USB_EPTYPE_ISOCHRONUS:
  253. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  254. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  255. (0x01 << 18) | epsize;
  256. break;
  257. case USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF:
  258. case USB_EPTYPE_BULK:
  259. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  260. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  261. (0x02 << 18) | epsize;
  262. break;
  263. default:
  264. epo->DOEPCTL = USB_OTG_DOEPCTL_SD0PID_SEVNFRM | USB_OTG_DOEPCTL_CNAK |
  265. USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP |
  266. (0x03 << 18) | epsize;
  267. break;
  268. }
  269. }
  270. return true;
  271. }
  272. static void ep_deconfig(uint8_t ep) {
  273. ep &= 0x7F;
  274. volatile USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  275. volatile USB_OTG_OUTEndpointTypeDef* epo = EPOUT(ep);
  276. /* deconfiguring TX part */
  277. /* disable interrupt */
  278. OTGD->DAINTMSK &= ~(0x10001 << ep);
  279. /* decativating endpoint */
  280. _BCL(epi->DIEPCTL, USB_OTG_DIEPCTL_USBAEP);
  281. /* flushing FIFO */
  282. Flush_TX(ep);
  283. /* disabling endpoint */
  284. if ((epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) && (ep != 0)) {
  285. epi->DIEPCTL = USB_OTG_DIEPCTL_EPDIS;
  286. }
  287. /* clean EP interrupts */
  288. epi->DIEPINT = 0xFF;
  289. /* deconfiguring TX FIFO */
  290. if (ep > 0) {
  291. OTG->DIEPTXF[ep-1] = 0x02000200 + 0x200 * ep;
  292. }
  293. /* deconfigureing RX part */
  294. _BCL(epo->DOEPCTL, USB_OTG_DOEPCTL_USBAEP);
  295. if ((epo->DOEPCTL & USB_OTG_DOEPCTL_EPENA) && (ep != 0)) {
  296. epo->DOEPCTL = USB_OTG_DOEPCTL_EPDIS;
  297. }
  298. epo->DOEPINT = 0xFF;
  299. }
  300. static int32_t ep_read(uint8_t ep, void* buf, uint16_t blen) {
  301. int32_t len;
  302. volatile uint32_t *fifo = EPFIFO(0);
  303. /* no data in RX FIFO */
  304. if (!(OTG->GINTSTS & USB_OTG_GINTSTS_RXFLVL)) return -1;
  305. ep &= 0x7F;
  306. if ((OTG->GRXSTSR & USB_OTG_GRXSTSP_EPNUM) != ep) return -1;
  307. /* pop data from fifo */
  308. len = _FLD2VAL(USB_OTG_GRXSTSP_BCNT, OTG->GRXSTSP);
  309. for (unsigned i = 0; i < len; i +=4) {
  310. uint32_t _t = *fifo;
  311. if (blen >= 4) {
  312. /* Cortex M3, M4 supports unaligned access */
  313. *(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. static 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 > _FLD2VAL(USB_OTG_DTXFSTS_INEPTFSAV, epi->DTXFSTS)) return -1;
  334. if (ep != 0 && epi->DIEPCTL & USB_OTG_DIEPCTL_EPENA) {
  335. return -1;
  336. }
  337. _BMD(epi->DIEPTSIZ,
  338. USB_OTG_DIEPTSIZ_PKTCNT | USB_OTG_DIEPTSIZ_MULCNT | USB_OTG_DIEPTSIZ_XFRSIZ,
  339. _VAL2FLD(USB_OTG_DIEPTSIZ_PKTCNT, 1) | _VAL2FLD(USB_OTG_DIEPTSIZ_MULCNT, 1 ) | _VAL2FLD(USB_OTG_DIEPTSIZ_XFRSIZ, blen));
  340. _BMD(epi->DIEPCTL, USB_OTG_DIEPCTL_STALL, USB_OTG_DOEPCTL_CNAK);
  341. _BST(epi->DIEPCTL, USB_OTG_DOEPCTL_EPENA);
  342. while (_len--) {
  343. /* Cortex M3, M4 supports unaligned access */
  344. uint32_t _t = *(uint32_t*)buf;
  345. *_fifo = _t;
  346. buf += 4;
  347. }
  348. return blen;
  349. }
  350. static uint16_t get_frame (void) {
  351. return _FLD2VAL(USB_OTG_DSTS_FNSOF, OTGD->DSTS);
  352. }
  353. static void evt_poll(usbd_device *dev, usbd_evt_callback callback) {
  354. uint32_t evt;
  355. uint32_t ep = 0;
  356. while (1) {
  357. uint32_t _t = OTG->GINTSTS;
  358. /* bus RESET event */
  359. if (_t & USB_OTG_GINTSTS_USBRST) {
  360. OTG->GINTSTS = USB_OTG_GINTSTS_USBRST;
  361. for (uint8_t i = 0; i < MAX_EP; i++ ) {
  362. ep_deconfig(i);
  363. }
  364. Flush_RX();
  365. continue;
  366. } else if (_t & USB_OTG_GINTSTS_ENUMDNE) {
  367. OTG->GINTSTS = USB_OTG_GINTSTS_ENUMDNE;
  368. evt = usbd_evt_reset;
  369. } else if (_t & USB_OTG_GINTSTS_IEPINT) {
  370. for (;; ep++) {
  371. USB_OTG_INEndpointTypeDef* epi = EPIN(ep);
  372. if (ep >= MAX_EP) return;
  373. if (epi->DIEPINT & USB_OTG_DIEPINT_XFRC) {
  374. _BST(epi->DIEPINT, USB_OTG_DIEPINT_XFRC);
  375. evt = usbd_evt_eptx;
  376. ep |= 0x80;
  377. break;
  378. }
  379. }
  380. } else if (_t & USB_OTG_GINTSTS_RXFLVL) {
  381. _t = OTG->GRXSTSR;
  382. ep = _t & USB_OTG_GRXSTSP_EPNUM;
  383. switch (_FLD2VAL(USB_OTG_GRXSTSP_PKTSTS, _t)) {
  384. case 0x02: /* OUT recieved */
  385. evt = usbd_evt_eprx;
  386. break;
  387. case 0x06: /* SETUP recieved */
  388. /* flushing TX if something stuck in control endpoint */
  389. if (EPIN(ep)->DIEPTSIZ & USB_OTG_DIEPTSIZ_PKTCNT) {
  390. Flush_TX(ep);
  391. }
  392. evt = usbd_evt_epsetup;
  393. break;
  394. case 0x03: /* OUT completed */
  395. case 0x04: /* SETUP completed */
  396. _BST(EPOUT(ep)->DOEPCTL, USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  397. default:
  398. /* pop GRXSTSP */
  399. OTG->GRXSTSP;
  400. continue;
  401. }
  402. #if !defined(USBD_SOF_DISABLED)
  403. } else if (_t & USB_OTG_GINTSTS_SOF) {
  404. OTG->GINTSTS = USB_OTG_GINTSTS_SOF;
  405. evt = usbd_evt_sof;
  406. #endif
  407. } else if (_t & USB_OTG_GINTSTS_USBSUSP) {
  408. evt = usbd_evt_susp;
  409. OTG->GINTSTS = USB_OTG_GINTSTS_USBSUSP;
  410. } else if (_t & USB_OTG_GINTSTS_WKUINT) {
  411. OTG->GINTSTS = USB_OTG_GINTSTS_WKUINT;
  412. evt = usbd_evt_wkup;
  413. } else {
  414. /* no more supported events */
  415. return;
  416. }
  417. return callback(dev, evt, ep);
  418. }
  419. }
  420. static uint32_t fnv1a32_turn (uint32_t fnv, uint32_t data ) {
  421. for (int i = 0; i < 4 ; i++) {
  422. fnv ^= (data & 0xFF);
  423. fnv *= 16777619;
  424. data >>= 8;
  425. }
  426. return fnv;
  427. }
  428. static uint16_t get_serialno_desc(void *buffer) {
  429. struct usb_string_descriptor *dsc = buffer;
  430. uint16_t *str = dsc->wString;
  431. uint32_t fnv = 2166136261;
  432. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x00));
  433. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x04));
  434. fnv = fnv1a32_turn(fnv, *(uint32_t*)(UID_BASE + 0x08));
  435. for (int i = 28; i >= 0; i -= 4 ) {
  436. uint16_t c = (fnv >> i) & 0x0F;
  437. c += (c < 10) ? '0' : ('A' - 10);
  438. *str++ = c;
  439. }
  440. dsc->bDescriptorType = USB_DTYPE_STRING;
  441. dsc->bLength = 18;
  442. return 18;
  443. }
  444. __attribute__((externally_visible)) const struct usbd_driver usbd_otghs = {
  445. getinfo,
  446. enable,
  447. connect,
  448. setaddr,
  449. ep_config,
  450. ep_deconfig,
  451. ep_read,
  452. ep_write,
  453. ep_setstall,
  454. ep_isstalled,
  455. evt_poll,
  456. get_frame,
  457. get_serialno_desc,
  458. };
  459. #endif //USBD_STM32L476