usb_dc_nrf5x.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. #include <stddef.h>
  2. #include <mdk/nrf52840_bitfields.h>
  3. #include "nrf52840.h"
  4. #include "usb_def.h"
  5. #include "usb_dc.h"
  6. #include "usbd_core.h"
  7. #ifndef USBD_IRQHandler
  8. #define USBD_IRQHandler USBD_IRQHandler /*!< Use actual usb irq name instead */
  9. #endif
  10. #ifndef USBD_CONFIG_ISO_IN_ZLP
  11. #define USBD_CONFIG_ISO_IN_ZLP 0
  12. #endif
  13. /*!< The default platform is NRF52840 */
  14. #define NRF52_SERIES
  15. /*!< Ep nums */
  16. #define EP_NUMS 9
  17. /*!< Ep mps */
  18. #define EP_MPS 64
  19. /*!< Nrf5x special */
  20. #define EP_ISO_NUM 8
  21. /*!< USBD peripheral address base */
  22. #define NRF_USBD_BASE 0x40027000UL
  23. /*!< Clock peripheral address base */
  24. #define NRF_CLOCK_BASE 0x40000000UL
  25. #define USBD_IRQn 39
  26. #ifndef EP_ISO_MPS
  27. #define EP_ISO_MPS 64
  28. #endif
  29. #define CHECK_ADD_IS_RAM(address) ((((uint32_t)address) & 0xE0000000u) == 0x20000000u) ? 1 : 0
  30. /**
  31. * @brief Endpoint information structure
  32. */
  33. typedef struct _usbd_ep_info
  34. {
  35. uint16_t mps; /*!< Maximum packet length of endpoint */
  36. uint8_t eptype; /*!< Endpoint Type */
  37. uint8_t ep_stalled; /* Endpoint stall flag */
  38. uint8_t ep_enable; /* Endpoint enable */
  39. uint8_t *xfer_buf;
  40. uint32_t xfer_len;
  41. uint32_t actual_xfer_len;
  42. uint8_t ep_buffer[EP_MPS];
  43. /*!< Other endpoint parameters that may be used */
  44. volatile uint8_t is_using_dma;
  45. volatile uint8_t add_flag;
  46. } usbd_ep_info;
  47. /*!< nrf52840 usb */
  48. struct _nrf52840_core_prvi
  49. {
  50. uint8_t address; /*!< address */
  51. usbd_ep_info ep_in[EP_NUMS]; /*!< ep in */
  52. usbd_ep_info ep_out[EP_NUMS]; /*!< ep out */
  53. struct usb_setup_packet setup; /*!< Setup package that may be used in interrupt processing (outside the protocol stack) */
  54. volatile uint8_t dma_running;
  55. int8_t in_count;
  56. volatile uint8_t iso_turn;
  57. volatile uint8_t iso_tx_is_ready;
  58. /**
  59. * For nrf5x, easydma will not move the setup packet into RAM.
  60. * We use a flag bit to judge whether the host sends setup,
  61. * and then notify usbd_ep_read to and from the register to read the setup package
  62. */
  63. volatile uint8_t is_setup_packet;
  64. } usb_dc_cfg;
  65. __WEAK void usb_dc_low_level_pre_init(void)
  66. {
  67. }
  68. __WEAK void usb_dc_low_level_post_init(void)
  69. {
  70. }
  71. __WEAK void usb_dc_low_level_deinit(void)
  72. {
  73. }
  74. /**@brief Usbds the set remote wakeup.
  75. *
  76. * @param[in] busid do not used
  77. *
  78. * @retval
  79. */
  80. int usbd_set_remote_wakeup (uint8_t busid)
  81. {
  82. //TOGGLE_GPIOA9();
  83. //#TASK 1.唤醒使能+挂起标志+休眠标志
  84. // USB->CNTR |= (uint16_t)USB_CNTR_RESUME;
  85. return -1;
  86. }
  87. /**
  88. * @brief Get setup package
  89. * @pre None
  90. * @param[in] setup Pointer to the address where the setup package is stored
  91. * @retval None
  92. */
  93. static inline void get_setup_packet(struct usb_setup_packet *setup)
  94. {
  95. setup->bmRequestType = (uint8_t)(NRF_USBD->BMREQUESTTYPE);
  96. setup->bRequest = (uint8_t)(NRF_USBD->BREQUEST);
  97. setup->wIndex = (uint16_t)(NRF_USBD->WINDEXL | ((NRF_USBD->WINDEXH) << 8));
  98. setup->wLength = (uint16_t)(NRF_USBD->WLENGTHL | ((NRF_USBD->WLENGTHH) << 8));
  99. setup->wValue = (uint16_t)(NRF_USBD->WVALUEL | ((NRF_USBD->WVALUEH) << 8));
  100. }
  101. /**
  102. * @brief Set tx easydma
  103. * @pre None
  104. * @param[in] ep End point address
  105. * @param[in] ptr Data ram ptr
  106. * @param[in] maxcnt Max length
  107. * @retval None
  108. */
  109. static void nrf_usbd_ep_easydma_set_tx(uint8_t ep, uint32_t ptr, uint32_t maxcnt)
  110. {
  111. uint8_t epid = USB_EP_GET_IDX(ep);
  112. if (epid == EP_ISO_NUM)
  113. {
  114. NRF_USBD->ISOIN.PTR = ptr;
  115. NRF_USBD->ISOIN.MAXCNT = maxcnt;
  116. return;
  117. }
  118. NRF_USBD->EPIN[epid].PTR = ptr;
  119. NRF_USBD->EPIN[epid].MAXCNT = maxcnt;
  120. }
  121. /**
  122. * @brief Set rx easydma
  123. * @pre None
  124. * @param[in] ep End point address
  125. * @param[in] ptr Data ram ptr
  126. * @param[in] maxcnt Max length
  127. * @retval None
  128. */
  129. static void nrf_usbd_ep_easydma_set_rx(uint8_t ep, uint32_t ptr, uint32_t maxcnt)
  130. {
  131. uint8_t epid = USB_EP_GET_IDX(ep);
  132. if (epid == EP_ISO_NUM)
  133. {
  134. NRF_USBD->ISOOUT.PTR = ptr;
  135. NRF_USBD->ISOOUT.MAXCNT = maxcnt;
  136. return;
  137. }
  138. NRF_USBD->EPOUT[epid].PTR = ptr;
  139. NRF_USBD->EPOUT[epid].MAXCNT = maxcnt;
  140. }
  141. /**@brief Usbds the set address.
  142. *
  143. * @param[in] usbid do not used
  144. * @param[in] address 8-bit valid address
  145. *
  146. * @retval >=0 success otherwise failureSet addressNone
  147. */
  148. int usbd_set_address(uint8_t usbid, const uint8_t address)
  149. {
  150. if (address == 0)
  151. {
  152. /*!< init 0 address */
  153. }
  154. else
  155. {
  156. /*!< For non-0 addresses, write the address to the register in the state phase of setting the address */
  157. }
  158. NRF_USBD->EVENTCAUSE |= NRF_USBD->EVENTCAUSE;
  159. NRF_USBD->EVENTS_USBEVENT = 0;
  160. NRF_USBD->INTENSET = USBD_INTEN_USBEVENT_Msk;
  161. /*!< nothing to do, handled by hardware; but don't STALL */
  162. usb_dc_cfg.address = address;
  163. return 0;
  164. }
  165. uint8_t usbd_get_port_speed(const uint8_t port)
  166. {
  167. return USB_SPEED_FULL;
  168. }
  169. /**@brief Usbds the ep open.
  170. *
  171. * @param[in] busid do not used
  172. * @param[in] ep_cfg Endpoint configuration structure pointer
  173. *
  174. * @retval >=0 success otherwise failureOpen endpointNone
  175. */
  176. int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep_cfg)
  177. {
  178. /*!< ep id */
  179. uint8_t epid = USB_EP_GET_IDX(ep_cfg->bEndpointAddress);
  180. /*!< ep max packet length */
  181. uint16_t mps = ep_cfg->wMaxPacketSize;
  182. if (USB_EP_DIR_IS_IN(ep_cfg->bEndpointAddress))
  183. {
  184. /*!< In */
  185. usb_dc_cfg.ep_in[epid].mps = mps;
  186. usb_dc_cfg.ep_in[epid].eptype = ep_cfg->bmAttributes;
  187. usb_dc_cfg.ep_in[epid].ep_enable = true;
  188. /*!< Open ep */
  189. if (ep_cfg->bmAttributes != USB_ENDPOINT_TYPE_ISOCHRONOUS)
  190. {
  191. /*!< Enable endpoint interrupt */
  192. NRF_USBD->INTENSET = (1 << (USBD_INTEN_ENDEPIN0_Pos + epid));
  193. /*!< Enable the in endpoint host to respond when sending in token */
  194. NRF_USBD->EPINEN |= (1 << (epid));
  195. __ISB();
  196. __DSB();
  197. }
  198. else
  199. {
  200. NRF_USBD->EVENTS_ENDISOIN = 0;
  201. /*!< SPLIT ISO buffer when ISO OUT endpoint is already opened. */
  202. if (usb_dc_cfg.ep_out[EP_ISO_NUM].mps)
  203. NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;
  204. /*!< Clear SOF event in case interrupt was not enabled yet. */
  205. if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0)
  206. NRF_USBD->EVENTS_SOF = 0;
  207. /*!< Enable SOF and ISOIN interrupts, and ISOIN endpoint. */
  208. NRF_USBD->INTENSET = USBD_INTENSET_ENDISOIN_Msk | USBD_INTENSET_SOF_Msk;
  209. NRF_USBD->EPINEN |= USBD_EPINEN_ISOIN_Msk;
  210. }
  211. }
  212. else if (USB_EP_DIR_IS_OUT(ep_cfg->bEndpointAddress))
  213. {
  214. /*!< Out */
  215. usb_dc_cfg.ep_out[epid].mps = mps;
  216. usb_dc_cfg.ep_out[epid].eptype = ep_cfg->bmAttributes;
  217. usb_dc_cfg.ep_out[epid].ep_enable = true;
  218. /*!< Open ep */
  219. if (ep_cfg->bmAttributes != USB_ENDPOINT_TYPE_ISOCHRONOUS)
  220. {
  221. NRF_USBD->INTENSET = (1 << (USBD_INTEN_ENDEPOUT0_Pos + epid));
  222. NRF_USBD->EPOUTEN |= (1 << (epid));
  223. __ISB();
  224. __DSB();
  225. /*!< Write any value to SIZE register will allow nRF to ACK/accept data */
  226. NRF_USBD->SIZE.EPOUT[epid] = 0;
  227. }
  228. else
  229. {
  230. /*!< SPLIT ISO buffer when ISO IN endpoint is already opened. */
  231. if (usb_dc_cfg.ep_in[EP_ISO_NUM].mps)
  232. NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;
  233. /*!< Clear old events */
  234. NRF_USBD->EVENTS_ENDISOOUT = 0;
  235. /*!< Clear SOF event in case interrupt was not enabled yet. */
  236. if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0)
  237. NRF_USBD->EVENTS_SOF = 0;
  238. /*!< Enable SOF and ISOOUT interrupts, and ISOOUT endpoint. */
  239. NRF_USBD->INTENSET = USBD_INTENSET_ENDISOOUT_Msk | USBD_INTENSET_SOF_Msk;
  240. NRF_USBD->EPOUTEN |= USBD_EPOUTEN_ISOOUT_Msk;
  241. }
  242. }
  243. /*!< Clear stall and reset DataToggle */
  244. NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | (ep_cfg->bEndpointAddress);
  245. NRF_USBD->DTOGGLE = (USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos) | (ep_cfg->bEndpointAddress);
  246. __ISB();
  247. __DSB();
  248. return 0;
  249. }
  250. /**@brief Usbds the ep close.
  251. *
  252. * @param[in] busid do not used
  253. * @param[in] ep ep : Endpoint address
  254. *
  255. * @retval >=0 success otherwise failureClose endpointNone
  256. */
  257. int usbd_ep_close(uint8_t busid, const uint8_t ep)
  258. {
  259. /*!< ep id */
  260. uint8_t epid = USB_EP_GET_IDX(ep);
  261. if (epid != EP_ISO_NUM)
  262. {
  263. if (USB_EP_DIR_IS_OUT(ep))
  264. {
  265. usb_dc_cfg.ep_out[epid].ep_enable = false;
  266. NRF_USBD->INTENCLR = (1 << (USBD_INTEN_ENDEPOUT0_Pos + epid));
  267. NRF_USBD->EPOUTEN &= ~(1 << (epid));
  268. }
  269. else
  270. {
  271. usb_dc_cfg.ep_in[epid].ep_enable = false;
  272. NRF_USBD->INTENCLR = (1 << (USBD_INTEN_ENDEPIN0_Pos + epid));
  273. NRF_USBD->EPINEN &= ~(1 << (epid));
  274. }
  275. }
  276. else
  277. {
  278. /*!< ISO EP */
  279. if (USB_EP_DIR_IS_OUT(ep))
  280. {
  281. usb_dc_cfg.ep_out[epid].ep_enable = false;
  282. usb_dc_cfg.ep_out[EP_ISO_NUM].mps = 0;
  283. NRF_USBD->INTENCLR = USBD_INTENCLR_ENDISOOUT_Msk;
  284. NRF_USBD->EPOUTEN &= ~USBD_EPOUTEN_ISOOUT_Msk;
  285. NRF_USBD->EVENTS_ENDISOOUT = 0;
  286. }
  287. else
  288. {
  289. usb_dc_cfg.ep_in[epid].ep_enable = false;
  290. usb_dc_cfg.ep_in[EP_ISO_NUM].mps = 0;
  291. NRF_USBD->INTENCLR = USBD_INTENCLR_ENDISOIN_Msk;
  292. NRF_USBD->EPINEN &= ~USBD_EPINEN_ISOIN_Msk;
  293. }
  294. /*!< One of the ISO endpoints closed, no need to split buffers any more. */
  295. NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_OneDir;
  296. /*!< When both ISO endpoint are close there is no need for SOF any more. */
  297. if (usb_dc_cfg.ep_in[EP_ISO_NUM].mps + usb_dc_cfg.ep_out[EP_ISO_NUM].mps == 0)
  298. {
  299. NRF_USBD->INTENCLR = USBD_INTENCLR_SOF_Msk;
  300. }
  301. }
  302. __ISB();
  303. __DSB();
  304. return 0;
  305. }
  306. /**
  307. * @brief Setup in ep transfer setting and start transfer.
  308. *
  309. * This function is asynchronous.
  310. * This function is similar to uart with tx dma.
  311. *
  312. * This function is called to write data to the specified endpoint. The
  313. * supplied usbd_endpoint_callback function will be called when data is transmitted
  314. * out.
  315. *
  316. * @param[in] busid do not used
  317. * @param[in] ep Endpoint address corresponding to the one
  318. * listed in the device configuration table
  319. * @param[in] data Pointer to data to write
  320. * @param[in] data_len Length of the data requested to write. This may
  321. * be zero for a zero length status packet.
  322. * @return 0 on success, negative errno code on fail.
  323. */
  324. int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data, uint32_t data_len)
  325. {
  326. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  327. if (!data && data_len)
  328. {
  329. return -1;
  330. }
  331. if (!usb_dc_cfg.ep_in[ep_idx].ep_enable)
  332. {
  333. return -2;
  334. }
  335. if ((uint32_t)data & 0x03)
  336. {
  337. return -3;
  338. }
  339. usb_dc_cfg.ep_in[ep_idx].xfer_buf = (uint8_t *)data;
  340. usb_dc_cfg.ep_in[ep_idx].xfer_len = data_len;
  341. usb_dc_cfg.ep_in[ep_idx].actual_xfer_len = 0;
  342. if (data_len == 0)
  343. {
  344. /*!< write 0 len data */
  345. nrf_usbd_ep_easydma_set_tx(ep_idx, (uint32_t)NULL, 0);
  346. NRF_USBD->TASKS_STARTEPIN[ep_idx] = 1;
  347. }
  348. else
  349. {
  350. /*!< Not zlp */
  351. data_len = MIN(data_len, usb_dc_cfg.ep_in[ep_idx].mps);
  352. if (!CHECK_ADD_IS_RAM(data))
  353. {
  354. /*!< Data is not in ram */
  355. /*!< Memcpy data to ram */
  356. memcpy(usb_dc_cfg.ep_in[ep_idx].ep_buffer, data, data_len);
  357. nrf_usbd_ep_easydma_set_tx(ep_idx, (uint32_t)usb_dc_cfg.ep_in[ep_idx].ep_buffer, data_len);
  358. }
  359. else
  360. {
  361. nrf_usbd_ep_easydma_set_tx(ep_idx, (uint32_t)data, data_len);
  362. }
  363. /**
  364. * Note that starting DMA transmission is to transmit data to USB peripherals,
  365. * and then wait for the host to get it
  366. */
  367. /*!< Start dma transfer */
  368. if (ep_idx != EP_ISO_NUM)
  369. {
  370. NRF_USBD->TASKS_STARTEPIN[ep_idx] = 1;
  371. }
  372. else
  373. {
  374. // NRF_USBD->TASKS_STARTISOIN = 1;
  375. usb_dc_cfg.iso_tx_is_ready = 1;
  376. }
  377. }
  378. return 0;
  379. }
  380. /**
  381. * @brief Setup out ep transfer setting and start transfer.
  382. *
  383. * This function is asynchronous.
  384. * This function is similar to uart with rx dma.
  385. *
  386. * This function is called to read data to the specified endpoint. The
  387. * supplied usbd_endpoint_callback function will be called when data is received
  388. * in.
  389. *
  390. * @param[in] busid do not used
  391. * @param[in] ep Endpoint address corresponding to the one
  392. * listed in the device configuration table
  393. * @param[in] data Pointer to data to read
  394. * @param[in] data_len Max length of the data requested to read.
  395. *
  396. * @return 0 on success, negative errno code on fail.
  397. */
  398. int usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t data_len)
  399. {
  400. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  401. if (!data && data_len)
  402. {
  403. return -1;
  404. }
  405. if (!usb_dc_cfg.ep_out[ep_idx].ep_enable)
  406. {
  407. return -2;
  408. }
  409. if ((uint32_t)data & 0x03)
  410. {
  411. return -3;
  412. }
  413. usb_dc_cfg.ep_out[ep_idx].xfer_buf = (uint8_t *)data;
  414. usb_dc_cfg.ep_out[ep_idx].xfer_len = data_len;
  415. usb_dc_cfg.ep_out[ep_idx].actual_xfer_len = 0;
  416. if (data_len == 0)
  417. {
  418. return 0;
  419. }
  420. else
  421. {
  422. data_len = MIN(data_len, usb_dc_cfg.ep_out[ep_idx].mps);
  423. if (!CHECK_ADD_IS_RAM(data))
  424. {
  425. /*!< Data address is not in ram */
  426. // TODO:
  427. }
  428. else
  429. {
  430. if (ep_idx == 0)
  431. {
  432. NRF_USBD->TASKS_EP0RCVOUT = 1;
  433. }
  434. nrf_usbd_ep_easydma_set_rx(ep_idx, (uint32_t)data, data_len);
  435. }
  436. }
  437. return 0;
  438. }
  439. /**
  440. * @brief Endpoint setting pause
  441. * @pre None
  442. * @param[in] busid do not used
  443. * @param[in] ep : Endpoint address
  444. * @retval >=0 success otherwise failure
  445. */
  446. int usbd_ep_set_stall(uint8_t busid, const uint8_t ep)
  447. {
  448. /*!< ep id */
  449. uint8_t epid = USB_EP_GET_IDX(ep);
  450. if (epid == 0)
  451. {
  452. NRF_USBD->TASKS_EP0STALL = 1;
  453. }
  454. else if (epid != EP_ISO_NUM)
  455. {
  456. NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_Stall << USBD_EPSTALL_STALL_Pos) | (ep);
  457. }
  458. __ISB();
  459. __DSB();
  460. return 0;
  461. }
  462. /**
  463. * @brief Endpoint clear pause
  464. * @pre None
  465. * @param[in] busid do not used
  466. * @param[in] ep : Endpoint address
  467. * @retval >=0 success otherwise failure
  468. */
  469. int usbd_ep_clear_stall(uint8_t usbid, const uint8_t ep)
  470. {
  471. /*!< ep id */
  472. uint8_t epid = USB_EP_GET_IDX(ep);
  473. if (epid != 0 && epid != EP_ISO_NUM)
  474. {
  475. /**
  476. * reset data toggle to DATA0
  477. * First write this register with VALUE=Nop to select the endpoint, then either read it to get the status from
  478. * VALUE, or write it again with VALUE=Data0 or Data1
  479. */
  480. NRF_USBD->DTOGGLE = ep;
  481. NRF_USBD->DTOGGLE = (USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos) | ep;
  482. /*!< Clear stall */
  483. NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep;
  484. /*!< Write any value to SIZE register will allow nRF to ACK/accept data */
  485. if (USB_EP_DIR_IS_OUT(ep))
  486. NRF_USBD->SIZE.EPOUT[epid] = 0;
  487. __ISB();
  488. __DSB();
  489. }
  490. return 0;
  491. }
  492. /**
  493. * @brief Check endpoint status
  494. * @pre None
  495. * @param[in] busid do not used
  496. * @param[in] ep : Endpoint address
  497. * @param[out] stalled : Outgoing endpoint status
  498. * @retval >=0 success otherwise failure
  499. */
  500. int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
  501. {
  502. return 0;
  503. }
  504. /**
  505. * @brief USB initialization
  506. * @pre None
  507. * @param[in] busid do not used
  508. * @retval >=0 success otherwise failure
  509. */
  510. int usb_dc_init(uint8_t busid)
  511. {
  512. /*!< dc init */
  513. usb_dc_low_level_pre_init();
  514. memset(&usb_dc_cfg, 0, sizeof(usb_dc_cfg));
  515. /*!< Clear USB Event Interrupt */
  516. NRF_USBD->EVENTS_USBEVENT = 0;
  517. NRF_USBD->EVENTCAUSE |= NRF_USBD->EVENTCAUSE;
  518. /*!< Reset interrupt */
  519. NRF_USBD->INTENCLR = NRF_USBD->INTEN;
  520. NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_EPDATA_Msk |
  521. USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk | USBD_INTEN_STARTED_Msk;
  522. usb_dc_low_level_post_init();
  523. return 0;
  524. }
  525. /**
  526. * @brief USB deinit
  527. * @pre None
  528. * @param[in] busid do not used
  529. * @retval >=0 success otherwise failure
  530. */
  531. int usb_dc_deinit(uint8_t busid)
  532. {
  533. /*!< dc deinit */
  534. return 0;
  535. }
  536. /**
  537. * @brief USB interrupt processing function
  538. * @pre None
  539. * @param[in] None
  540. * @retval None
  541. */
  542. void USBD_IRQHandler(void)
  543. {
  544. uint32_t const inten = NRF_USBD->INTEN;
  545. uint32_t int_status = 0;
  546. volatile uint32_t usb_event = 0;
  547. volatile uint32_t *regevt = &NRF_USBD->EVENTS_USBRESET;
  548. /*!< Traverse USB events */
  549. for (uint8_t i = 0; i < USBD_INTEN_EPDATA_Pos + 1; i++)
  550. {
  551. if ((inten & (1 << i)) && regevt[i])
  552. {
  553. int_status |= (1 << (i));
  554. /*!< event clear */
  555. regevt[i] = 0;
  556. __ISB();
  557. __DSB();
  558. }
  559. }
  560. /*!< bit 24 */
  561. if (int_status & USBD_INTEN_EPDATA_Msk)
  562. {
  563. /*!< out ep */
  564. for (uint8_t ep_out_ct = 1; ep_out_ct <= 7; ep_out_ct++)
  565. {
  566. if ((NRF_USBD->EPDATASTATUS) & (1 << (16 + ep_out_ct)))
  567. {
  568. NRF_USBD->EPDATASTATUS |= (1 << (16 + ep_out_ct));
  569. /*!< The data arrives at the usb fifo, starts the dma transmission, and transfers it to the user ram */
  570. NRF_USBD->TASKS_STARTEPOUT[ep_out_ct] = 1;
  571. }
  572. }
  573. /*!< in ep */
  574. for (uint8_t ep_in_ct = 1; ep_in_ct <= 7; ep_in_ct++)
  575. {
  576. if ((NRF_USBD->EPDATASTATUS) & (1 << (0 + ep_in_ct)))
  577. {
  578. /*!< in ep tranfer to host successfully */
  579. NRF_USBD->EPDATASTATUS |= (1 << (0 + ep_in_ct));
  580. if (usb_dc_cfg.ep_in[ep_in_ct].xfer_len > usb_dc_cfg.ep_in[ep_in_ct].mps)
  581. {
  582. /*!< Need start in again */
  583. usb_dc_cfg.ep_in[ep_in_ct].xfer_buf += usb_dc_cfg.ep_in[ep_in_ct].mps;
  584. usb_dc_cfg.ep_in[ep_in_ct].xfer_len -= usb_dc_cfg.ep_in[ep_in_ct].mps;
  585. usb_dc_cfg.ep_in[ep_in_ct].actual_xfer_len += usb_dc_cfg.ep_in[ep_in_ct].mps;
  586. if (usb_dc_cfg.ep_in[ep_in_ct].xfer_len > usb_dc_cfg.ep_in[ep_in_ct].mps)
  587. {
  588. nrf_usbd_ep_easydma_set_tx(ep_in_ct, (uint32_t)usb_dc_cfg.ep_in[ep_in_ct].xfer_buf, usb_dc_cfg.ep_in[ep_in_ct].mps);
  589. }
  590. else
  591. {
  592. nrf_usbd_ep_easydma_set_tx(ep_in_ct, (uint32_t)usb_dc_cfg.ep_in[ep_in_ct].xfer_buf, usb_dc_cfg.ep_in[ep_in_ct].xfer_len);
  593. }
  594. NRF_USBD->TASKS_STARTEPIN[ep_in_ct] = 1;
  595. }
  596. else
  597. {
  598. usb_dc_cfg.ep_in[ep_in_ct].actual_xfer_len += usb_dc_cfg.ep_in[ep_in_ct].xfer_len;
  599. usb_dc_cfg.ep_in[ep_in_ct].xfer_len = 0;
  600. usbd_event_ep_in_complete_handler(0, ep_in_ct | 0x80, usb_dc_cfg.ep_in[ep_in_ct].actual_xfer_len);
  601. }
  602. }
  603. }
  604. }
  605. /*!< bit 23 */
  606. if (int_status & USBD_INTEN_EP0SETUP_Msk)
  607. {
  608. /* Setup */
  609. /*!< Storing this setup package will help the following procedures */
  610. get_setup_packet(&(usb_dc_cfg.setup));
  611. /*!< Nrf52840 will set the address automatically by hardware,
  612. so the protocol stack of the address setting command sent by the host does not need to be processed */
  613. if (usb_dc_cfg.setup.wLength == 0)
  614. {
  615. NRF_USBD->TASKS_EP0STATUS = 1;
  616. }
  617. if (usb_dc_cfg.setup.bRequest != USB_REQUEST_SET_ADDRESS)
  618. {
  619. usbd_event_ep0_setup_complete_handler(0, (uint8_t *)&(usb_dc_cfg.setup));
  620. }
  621. }
  622. /*!< bit 22 */
  623. if (int_status & USBD_INTEN_USBEVENT_Msk)
  624. {
  625. usb_event = NRF_USBD->EVENTCAUSE;
  626. NRF_USBD->EVENTCAUSE = usb_event;
  627. if (usb_event & USBD_EVENTCAUSE_SUSPEND_Msk)
  628. {
  629. NRF_USBD->LOWPOWER = 1;
  630. /*!< */
  631. }
  632. if (usb_event & USBD_EVENTCAUSE_RESUME_Msk)
  633. {
  634. /*!< */
  635. }
  636. if (usb_event & USBD_EVENTCAUSE_USBWUALLOWED_Msk)
  637. {
  638. NRF_USBD->DPDMVALUE = USBD_DPDMVALUE_STATE_Resume;
  639. NRF_USBD->TASKS_DPDMDRIVE = 1;
  640. /**
  641. * There is no Resume interrupt for remote wakeup, enable SOF for to report bus ready state
  642. * Clear SOF event in case interrupt was not enabled yet.
  643. */
  644. if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0)
  645. NRF_USBD->EVENTS_SOF = 0;
  646. NRF_USBD->INTENSET = USBD_INTENSET_SOF_Msk;
  647. }
  648. }
  649. /*!< bit 21 */
  650. if (int_status & USBD_INTEN_SOF_Msk)
  651. {
  652. bool iso_enabled = false;
  653. /*!< ISOOUT: Transfer data gathered in previous frame from buffer to RAM */
  654. if (NRF_USBD->EPOUTEN & USBD_EPOUTEN_ISOOUT_Msk)
  655. {
  656. iso_enabled = true;
  657. /*!< If ZERO bit is set, ignore ISOOUT length */
  658. if ((NRF_USBD->SIZE.ISOOUT) & USBD_SIZE_ISOOUT_ZERO_Msk)
  659. {
  660. }
  661. else
  662. {
  663. /*!< Trigger DMA move data from Endpoint -> SRAM */
  664. NRF_USBD->TASKS_STARTISOOUT = 1;
  665. /*!< EP_ISO_NUM out using dma */
  666. usb_dc_cfg.ep_out[EP_ISO_NUM].is_using_dma = 1;
  667. }
  668. }
  669. /*!< ISOIN: Notify client that data was transferred */
  670. if (NRF_USBD->EPINEN & USBD_EPINEN_ISOIN_Msk)
  671. {
  672. iso_enabled = true;
  673. if (usb_dc_cfg.iso_tx_is_ready == 1)
  674. {
  675. usb_dc_cfg.iso_tx_is_ready = 0;
  676. NRF_USBD->TASKS_STARTISOIN = 1;
  677. }
  678. }
  679. if (!iso_enabled)
  680. {
  681. /**
  682. * ISO endpoint is not used, SOF is only enabled one-time for remote wakeup
  683. * so we disable it now
  684. */
  685. NRF_USBD->INTENCLR = USBD_INTENSET_SOF_Msk;
  686. }
  687. }
  688. /*!< bit 20 */
  689. if (int_status & USBD_INTEN_ENDISOOUT_Msk)
  690. {
  691. if (usb_dc_cfg.ep_out[EP_ISO_NUM].is_using_dma == 1)
  692. {
  693. usb_dc_cfg.ep_out[EP_ISO_NUM].is_using_dma = 0;
  694. uint32_t read_count = NRF_USBD->SIZE.ISOOUT;
  695. usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_buf += read_count;
  696. usb_dc_cfg.ep_out[EP_ISO_NUM].actual_xfer_len += read_count;
  697. usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_len -= read_count;
  698. if ((read_count < usb_dc_cfg.ep_out[EP_ISO_NUM].mps) || (usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_len == 0))
  699. {
  700. usbd_event_ep_out_complete_handler(0, ((EP_ISO_NUM)&0x7f), usb_dc_cfg.ep_out[EP_ISO_NUM].actual_xfer_len);
  701. }
  702. else
  703. {
  704. if (usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_len > usb_dc_cfg.ep_out[EP_ISO_NUM].mps)
  705. {
  706. nrf_usbd_ep_easydma_set_rx(((EP_ISO_NUM)&0x7f), (uint32_t)usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_buf, usb_dc_cfg.ep_out[EP_ISO_NUM].mps);
  707. }
  708. else
  709. {
  710. nrf_usbd_ep_easydma_set_rx(((EP_ISO_NUM)&0x7f), (uint32_t)usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_buf, usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_len);
  711. }
  712. }
  713. }
  714. }
  715. /**
  716. * Traverse ordinary out endpoint events, starting from endpoint 1 to endpoint 7,
  717. * and end 0 for special processing
  718. */
  719. for (uint8_t offset = 0; offset < 7; offset++)
  720. {
  721. if (int_status & (USBD_INTEN_ENDEPOUT1_Msk << offset))
  722. {
  723. /*!< Out 'offset' transfer complete */
  724. uint32_t read_count = NRF_USBD->SIZE.EPOUT[offset + 1];
  725. usb_dc_cfg.ep_out[offset + 1].xfer_buf += read_count;
  726. usb_dc_cfg.ep_out[offset + 1].actual_xfer_len += read_count;
  727. usb_dc_cfg.ep_out[offset + 1].xfer_len -= read_count;
  728. if ((read_count < usb_dc_cfg.ep_out[offset + 1].mps) || (usb_dc_cfg.ep_out[offset + 1].xfer_len == 0))
  729. {
  730. usbd_event_ep_out_complete_handler(0, ((offset + 1) & 0x7f), usb_dc_cfg.ep_out[offset + 1].actual_xfer_len);
  731. }
  732. else
  733. {
  734. if (usb_dc_cfg.ep_out[offset + 1].xfer_len > usb_dc_cfg.ep_out[offset + 1].mps)
  735. {
  736. nrf_usbd_ep_easydma_set_rx(((offset + 1) & 0x7f), (uint32_t)usb_dc_cfg.ep_out[offset + 1].xfer_buf, usb_dc_cfg.ep_out[offset + 1].mps);
  737. }
  738. else
  739. {
  740. nrf_usbd_ep_easydma_set_rx(((offset + 1) & 0x7f), (uint32_t)usb_dc_cfg.ep_out[offset + 1].xfer_buf, usb_dc_cfg.ep_out[offset + 1].xfer_len);
  741. }
  742. }
  743. }
  744. }
  745. /*!< bit 12 */
  746. if (int_status & USBD_INTEN_ENDEPOUT0_Msk)
  747. {
  748. uint32_t read_count = NRF_USBD->SIZE.EPOUT[0];
  749. usb_dc_cfg.ep_out[0].actual_xfer_len += read_count;
  750. usb_dc_cfg.ep_out[0].xfer_len -= read_count;
  751. if (usb_dc_cfg.ep_out[0].xfer_len == 0)
  752. {
  753. /*!< Enable the state phase of endpoint 0 */
  754. NRF_USBD->TASKS_EP0STATUS = 1;
  755. }
  756. usbd_event_ep_out_complete_handler(0, 0x00, usb_dc_cfg.ep_out[0].actual_xfer_len);
  757. }
  758. /*!< bit 11 */
  759. if (int_status & USBD_INTEN_ENDISOIN_Msk)
  760. {
  761. }
  762. /*!< bit 10 */
  763. if (int_status & USBD_INTEN_EP0DATADONE_Msk)
  764. {
  765. switch (usb_dc_cfg.setup.bmRequestType >> USB_REQUEST_DIR_SHIFT)
  766. {
  767. case 1:
  768. /*!< IN */
  769. if (usb_dc_cfg.ep_in[0].xfer_len > usb_dc_cfg.ep_in[0].mps)
  770. {
  771. usb_dc_cfg.ep_in[0].xfer_len -= usb_dc_cfg.ep_in[0].mps;
  772. usb_dc_cfg.ep_in[0].actual_xfer_len += usb_dc_cfg.ep_in[0].mps;
  773. usbd_event_ep_in_complete_handler(0, 0 | 0x80, usb_dc_cfg.ep_in[0].actual_xfer_len);
  774. }
  775. else
  776. {
  777. usb_dc_cfg.ep_in[0].actual_xfer_len += usb_dc_cfg.ep_in[0].xfer_len;
  778. usb_dc_cfg.ep_in[0].xfer_len = 0;
  779. /*!< Enable the state phase of endpoint 0 */
  780. usbd_event_ep_in_complete_handler(0, 0 | 0x80, usb_dc_cfg.ep_in[0].actual_xfer_len);
  781. NRF_USBD->TASKS_EP0STATUS = 1;
  782. }
  783. break;
  784. case 0:
  785. if (usb_dc_cfg.setup.bRequest != USB_REQUEST_SET_ADDRESS)
  786. {
  787. /*!< The data arrives at the usb fifo, starts the dma transmission, and transfers it to the user ram */
  788. NRF_USBD->TASKS_STARTEPOUT[0] = 1;
  789. }
  790. break;
  791. }
  792. }
  793. /**
  794. * Traversing ordinary in endpoint events, starting from endpoint 1 to endpoint 7,
  795. * endpoint 0 special processing
  796. */
  797. for (uint8_t offset = 0; offset < 7; offset++)
  798. {
  799. if (int_status & (USBD_INTEN_ENDEPIN1_Msk << offset))
  800. {
  801. /*!< DMA move data completed */
  802. }
  803. }
  804. /*!< bit 1 */
  805. if (int_status & USBD_INTEN_STARTED_Msk)
  806. {
  807. /*!< Easy dma start transfer data */
  808. }
  809. /*!< bit 2 */
  810. if (int_status & USBD_INTEN_ENDEPIN0_Msk)
  811. {
  812. /*!< EP0 IN DMA move data completed */
  813. }
  814. /*!< bit 0 */
  815. if (int_status & USBD_INTEN_USBRESET_Msk)
  816. {
  817. NRF_USBD->EPOUTEN = 1UL;
  818. NRF_USBD->EPINEN = 1UL;
  819. for (int i = 0; i < 8; i++)
  820. {
  821. NRF_USBD->TASKS_STARTEPIN[i] = 0;
  822. NRF_USBD->TASKS_STARTEPOUT[i] = 0;
  823. }
  824. NRF_USBD->TASKS_STARTISOIN = 0;
  825. NRF_USBD->TASKS_STARTISOOUT = 0;
  826. /*!< Clear USB Event Interrupt */
  827. NRF_USBD->EVENTS_USBEVENT = 0;
  828. NRF_USBD->EVENTCAUSE |= NRF_USBD->EVENTCAUSE;
  829. /*!< Reset interrupt */
  830. NRF_USBD->INTENCLR = NRF_USBD->INTEN;
  831. NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_EPDATA_Msk |
  832. USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk | USBD_INTEN_STARTED_Msk;
  833. usbd_event_reset_handler(0);
  834. }
  835. }
  836. /**
  837. * Errata: USB cannot be enabled.
  838. */
  839. static bool chyu_nrf52_errata_187(void)
  840. {
  841. #ifndef NRF52_SERIES
  842. return false;
  843. #else
  844. #if defined(NRF52820_XXAA) || defined(DEVELOP_IN_NRF52820) || defined(NRF52833_XXAA) || defined(DEVELOP_IN_NRF52833) || defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  845. uint32_t var1 = *(uint32_t *)0x10000130ul;
  846. uint32_t var2 = *(uint32_t *)0x10000134ul;
  847. #endif
  848. #if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  849. if (var1 == 0x08)
  850. {
  851. switch (var2)
  852. {
  853. case 0x00ul:
  854. return false;
  855. case 0x01ul:
  856. return true;
  857. case 0x02ul:
  858. return true;
  859. case 0x03ul:
  860. return true;
  861. default:
  862. return true;
  863. }
  864. }
  865. #endif
  866. #if defined(NRF52833_XXAA) || defined(DEVELOP_IN_NRF52833)
  867. if (var1 == 0x0D)
  868. {
  869. switch (var2)
  870. {
  871. case 0x00ul:
  872. return true;
  873. case 0x01ul:
  874. return true;
  875. default:
  876. return true;
  877. }
  878. }
  879. #endif
  880. #if defined(NRF52820_XXAA) || defined(DEVELOP_IN_NRF52820)
  881. if (var1 == 0x10)
  882. {
  883. switch (var2)
  884. {
  885. case 0x00ul:
  886. return true;
  887. case 0x01ul:
  888. return true;
  889. case 0x02ul:
  890. return true;
  891. default:
  892. return true;
  893. }
  894. }
  895. #endif
  896. return false;
  897. #endif
  898. }
  899. /**
  900. * Errata: USBD might not reach its active state.
  901. */
  902. static bool chyu_nrf52_errata_171(void)
  903. {
  904. #ifndef NRF52_SERIES
  905. return false;
  906. #else
  907. #if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  908. uint32_t var1 = *(uint32_t *)0x10000130ul;
  909. uint32_t var2 = *(uint32_t *)0x10000134ul;
  910. #endif
  911. #if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  912. if (var1 == 0x08)
  913. {
  914. switch (var2)
  915. {
  916. case 0x00ul:
  917. return true;
  918. case 0x01ul:
  919. return true;
  920. case 0x02ul:
  921. return true;
  922. case 0x03ul:
  923. return true;
  924. default:
  925. return true;
  926. }
  927. }
  928. #endif
  929. return false;
  930. #endif
  931. }
  932. /**
  933. * Errata: ISO double buffering not functional.
  934. */
  935. static bool chyu_nrf52_errata_166(void)
  936. {
  937. #ifndef NRF52_SERIES
  938. return false;
  939. #else
  940. #if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  941. uint32_t var1 = *(uint32_t *)0x10000130ul;
  942. uint32_t var2 = *(uint32_t *)0x10000134ul;
  943. #endif
  944. #if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  945. if (var1 == 0x08)
  946. {
  947. switch (var2)
  948. {
  949. case 0x00ul:
  950. return true;
  951. case 0x01ul:
  952. return true;
  953. case 0x02ul:
  954. return true;
  955. case 0x03ul:
  956. return true;
  957. default:
  958. return true;
  959. }
  960. }
  961. #endif
  962. return false;
  963. #endif
  964. }
  965. #ifdef SOFTDEVICE_PRESENT
  966. #include "nrf_mbr.h"
  967. #include "nrf_sdm.h"
  968. #include "nrf_soc.h"
  969. #ifndef SD_MAGIC_NUMBER
  970. #define SD_MAGIC_NUMBER 0x51B1E5DB
  971. #endif
  972. static inline bool is_sd_existed(void)
  973. {
  974. return *((uint32_t *)(SOFTDEVICE_INFO_STRUCT_ADDRESS + 4)) == SD_MAGIC_NUMBER;
  975. }
  976. /**
  977. * check if SD is existed and enabled
  978. */
  979. static inline bool is_sd_enabled(void)
  980. {
  981. if (!is_sd_existed())
  982. return false;
  983. uint8_t sd_en = false;
  984. (void)sd_softdevice_is_enabled(&sd_en);
  985. return sd_en;
  986. }
  987. #endif
  988. static bool hfclk_running(void)
  989. {
  990. #ifdef SOFTDEVICE_PRESENT
  991. if (is_sd_enabled())
  992. {
  993. uint32_t is_running = 0;
  994. (void)sd_clock_hfclk_is_running(&is_running);
  995. return (is_running ? true : false);
  996. }
  997. #endif
  998. #if defined(CLOCK_HFCLKSTAT_SRC_Xtal) || defined(__NRFX_DOXYGEN__)
  999. return (NRF_CLOCK->HFCLKSTAT & (CLOCK_HFCLKSTAT_STATE_Msk | CLOCK_HFCLKSTAT_SRC_Msk)) ==
  1000. (CLOCK_HFCLKSTAT_STATE_Msk | (CLOCK_HFCLKSTAT_SRC_Xtal << CLOCK_HFCLKSTAT_SRC_Pos));
  1001. #else
  1002. return (NRF_CLOCK->HFCLKSTAT & (CLOCK_HFCLKSTAT_STATE_Msk | CLOCK_HFCLKSTAT_SRC_Msk)) ==
  1003. (CLOCK_HFCLKSTAT_STATE_Msk | (CLOCK_HFCLKSTAT_SRC_HFXO << CLOCK_HFCLKSTAT_SRC_Pos));
  1004. #endif
  1005. }
  1006. enum
  1007. {
  1008. NRF_CLOCK_EVENT_HFCLKSTARTED = offsetof(NRF_CLOCK_Type, EVENTS_HFCLKSTARTED), /*!< HFCLK oscillator started. */
  1009. };
  1010. enum
  1011. {
  1012. NRF_CLOCK_TASK_HFCLKSTART = offsetof(NRF_CLOCK_Type, TASKS_HFCLKSTART), /*!< Start HFCLK clock source. */
  1013. NRF_CLOCK_TASK_HFCLKSTOP = offsetof(NRF_CLOCK_Type, TASKS_HFCLKSTOP), /*!< Stop HFCLK clock source. */
  1014. };
  1015. static void hfclk_enable(void)
  1016. {
  1017. /*!< already running, nothing to do */
  1018. if (hfclk_running())
  1019. return;
  1020. #ifdef SOFTDEVICE_PRESENT
  1021. if (is_sd_enabled())
  1022. {
  1023. (void)sd_clock_hfclk_request();
  1024. return;
  1025. }
  1026. #endif
  1027. *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + NRF_CLOCK_EVENT_HFCLKSTARTED)) = 0x0UL;
  1028. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + (uint32_t)NRF_CLOCK_EVENT_HFCLKSTARTED));
  1029. (void)dummy;
  1030. *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + NRF_CLOCK_TASK_HFCLKSTART)) = 0x1UL;
  1031. }
  1032. static void hfclk_disable(void)
  1033. {
  1034. #ifdef SOFTDEVICE_PRESENT
  1035. if (is_sd_enabled())
  1036. {
  1037. (void)sd_clock_hfclk_release();
  1038. return;
  1039. }
  1040. #endif
  1041. *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + NRF_CLOCK_TASK_HFCLKSTOP)) = 0x1UL;
  1042. }
  1043. /**
  1044. * Power & Clock Peripheral on nRF5x to manage USB
  1045. * USB Bus power is managed by Power module, there are 3 VBUS power events:
  1046. * Detected, Ready, Removed. Upon these power events, This function will
  1047. * enable ( or disable ) usb & hfclk peripheral, set the usb pin pull up
  1048. * accordingly to the controller Startup/Standby Sequence in USBD 51.4 specs.
  1049. * Therefore this function must be called to handle USB power event by
  1050. * - nrfx_power_usbevt_init() : if Softdevice is not used or enabled
  1051. * - SoftDevice SOC event : if SD is used and enabled
  1052. */
  1053. void cherry_usb_hal_nrf_power_event(uint32_t event)
  1054. {
  1055. enum
  1056. {
  1057. USB_EVT_DETECTED = 0,
  1058. USB_EVT_REMOVED = 1,
  1059. USB_EVT_READY = 2
  1060. };
  1061. switch (event)
  1062. {
  1063. case USB_EVT_DETECTED:
  1064. if (!NRF_USBD->ENABLE)
  1065. {
  1066. /*!< Prepare for receiving READY event: disable interrupt since we will blocking wait */
  1067. NRF_USBD->INTENCLR = USBD_INTEN_USBEVENT_Msk;
  1068. NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
  1069. __ISB();
  1070. __DSB();
  1071. #ifdef NRF52_SERIES /*!< NRF53 does not need this errata */
  1072. /*!< ERRATA 171, 187, 166 */
  1073. if (chyu_nrf52_errata_187())
  1074. {
  1075. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1076. {
  1077. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1078. *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
  1079. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1080. }
  1081. else
  1082. {
  1083. *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
  1084. }
  1085. }
  1086. if (chyu_nrf52_errata_171())
  1087. {
  1088. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1089. {
  1090. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1091. *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
  1092. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1093. }
  1094. else
  1095. {
  1096. *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
  1097. }
  1098. }
  1099. #endif
  1100. /*!< Enable the peripheral (will cause Ready event) */
  1101. NRF_USBD->ENABLE = 1;
  1102. __ISB();
  1103. __DSB();
  1104. /*!< Enable HFCLK */
  1105. hfclk_enable();
  1106. }
  1107. break;
  1108. case USB_EVT_READY:
  1109. /**
  1110. * Skip if pull-up is enabled and HCLK is already running.
  1111. * Application probably call this more than necessary.
  1112. */
  1113. if (NRF_USBD->USBPULLUP && hfclk_running())
  1114. break;
  1115. /*!< Waiting for USBD peripheral enabled */
  1116. while (!(USBD_EVENTCAUSE_READY_Msk & NRF_USBD->EVENTCAUSE))
  1117. {
  1118. }
  1119. NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
  1120. __ISB();
  1121. __DSB();
  1122. #ifdef NRF52_SERIES
  1123. if (chyu_nrf52_errata_171())
  1124. {
  1125. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1126. {
  1127. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1128. *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
  1129. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1130. }
  1131. else
  1132. {
  1133. *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
  1134. }
  1135. }
  1136. if (chyu_nrf52_errata_187())
  1137. {
  1138. if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
  1139. {
  1140. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1141. *((volatile uint32_t *)(0x4006ED14)) = 0x00000000;
  1142. *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
  1143. }
  1144. else
  1145. {
  1146. *((volatile uint32_t *)(0x4006ED14)) = 0x00000000;
  1147. }
  1148. }
  1149. if (chyu_nrf52_errata_166())
  1150. {
  1151. *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7E3;
  1152. *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) = 0x40;
  1153. __ISB();
  1154. __DSB();
  1155. }
  1156. #endif
  1157. /*!< ISO buffer Lower half for IN, upper half for OUT */
  1158. NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;
  1159. /*!< Enable bus-reset interrupt */
  1160. NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk;
  1161. /*!< Enable interrupt, priorities should be set by application */
  1162. /*!< clear pending irq */
  1163. NVIC->ICPR[(((uint32_t)USBD_IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)USBD_IRQn) & 0x1FUL));
  1164. /**
  1165. * Don't enable USBD interrupt yet, if dcd_init() did not finish yet
  1166. * Interrupt will be enabled by tud_init(), when USB stack is ready
  1167. * to handle interrupts.
  1168. */
  1169. /*!< Wait for HFCLK */
  1170. while (!hfclk_running())
  1171. {
  1172. }
  1173. /*!< Enable pull up */
  1174. NRF_USBD->USBPULLUP = 1;
  1175. __ISB();
  1176. __DSB();
  1177. break;
  1178. case USB_EVT_REMOVED:
  1179. if (NRF_USBD->ENABLE)
  1180. {
  1181. /*!< Disable pull up */
  1182. NRF_USBD->USBPULLUP = 0;
  1183. __ISB();
  1184. __DSB();
  1185. /*!< Disable Interrupt */
  1186. NVIC->ICER[(((uint32_t)USBD_IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)USBD_IRQn) & 0x1FUL));
  1187. __DSB();
  1188. __ISB();
  1189. /*!< disable all interrupt */
  1190. NRF_USBD->INTENCLR = NRF_USBD->INTEN;
  1191. NRF_USBD->ENABLE = 0;
  1192. __ISB();
  1193. __DSB();
  1194. hfclk_disable();
  1195. }
  1196. break;
  1197. default:
  1198. break;
  1199. }
  1200. }