usb_dc_nrf5x.c 35 KB

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