summaryrefslogtreecommitdiff
path: root/components/drivers/usb/cherryusb/port/nrf5x/usb_dc_nrf5x.c
blob: 5f955ff3c975cecd8ccb07fa35aa603dbcb5627e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
#include <stddef.h>
#include <mdk/nrf52840_bitfields.h>
#include "nrf52840.h"
#include "usb_def.h"
#include "usb_dc.h"
#include "usbd_core.h"

#ifndef USBD_CONFIG_ISO_IN_ZLP
#define USBD_CONFIG_ISO_IN_ZLP 0
#endif

/*!< The default platform is NRF52840 */
#define NRF52_SERIES
/*!< Ep nums */
#define EP_NUMS 9
/*!< Ep mps */
#define EP_MPS 64
/*!< Nrf5x special */
#define EP_ISO_NUM 8

/*!< USBD peripheral address base */
#define NRF_USBD_BASE 0x40027000UL
/*!< Clock peripheral address base */
#define NRF_CLOCK_BASE 0x40000000UL

#define USBD_IRQn 39

#ifndef EP_ISO_MPS
#define EP_ISO_MPS 64
#endif

#define CHECK_ADD_IS_RAM(address) ((((uint32_t)address) & 0xE0000000u) == 0x20000000u) ? 1 : 0

/**
 * @brief   Endpoint information structure
 */
typedef struct _usbd_ep_info
{
  uint16_t mps;       /*!< Maximum packet length of endpoint */
  uint8_t eptype;     /*!< Endpoint Type */
  uint8_t ep_stalled; /* Endpoint stall flag */
  uint8_t ep_enable;  /* Endpoint enable */
  uint8_t *xfer_buf;
  uint32_t xfer_len;
  uint32_t actual_xfer_len;
  uint8_t ep_buffer[EP_MPS];
  /*!< Other endpoint parameters that may be used */
  volatile uint8_t is_using_dma;
  volatile uint8_t add_flag;
} usbd_ep_info;

/*!< nrf52840 usb */
struct _nrf52840_core_prvi
{
  uint8_t address;               /*!< address */
  usbd_ep_info ep_in[EP_NUMS];   /*!< ep in */
  usbd_ep_info ep_out[EP_NUMS];  /*!< ep out */
  struct usb_setup_packet setup; /*!< Setup package that may be used in interrupt processing (outside the protocol stack) */
  volatile uint8_t dma_running;
  int8_t in_count;
  volatile uint8_t iso_turn;
  volatile uint8_t iso_tx_is_ready;
  /**
   * For nrf5x, easydma will not move the setup packet into RAM.
   * We use a flag bit to judge whether the host sends setup,
   * and then notify usbd_ep_read to and from the register to read the setup package
   */
  volatile uint8_t is_setup_packet;
} usb_dc_cfg;

__WEAK void usb_dc_low_level_pre_init(void)
{
}

__WEAK void usb_dc_low_level_post_init(void)
{
}

__WEAK void usb_dc_low_level_deinit(void)
{
}


/**@brief Usbds the set remote wakeup.
 *
 * @param[in] busid do not used
 *
 * @retval
 */
int usbd_set_remote_wakeup (uint8_t busid)
{
    //TOGGLE_GPIOA9();
    //#TASK 1.唤醒使能+挂起标志+休眠标志
    // USB->CNTR |= (uint16_t)USB_CNTR_RESUME;
    return -1;
}

/**
 * @brief            Get setup package
 * @pre              None
 * @param[in]        setup Pointer to the address where the setup package is stored
 * @retval           None
 */
static inline void get_setup_packet(struct usb_setup_packet *setup)
{
  setup->bmRequestType = (uint8_t)(NRF_USBD->BMREQUESTTYPE);
  setup->bRequest = (uint8_t)(NRF_USBD->BREQUEST);
  setup->wIndex = (uint16_t)(NRF_USBD->WINDEXL | ((NRF_USBD->WINDEXH) << 8));
  setup->wLength = (uint16_t)(NRF_USBD->WLENGTHL | ((NRF_USBD->WLENGTHH) << 8));
  setup->wValue = (uint16_t)(NRF_USBD->WVALUEL | ((NRF_USBD->WVALUEH) << 8));
}

/**
 * @brief            Set tx easydma
 * @pre              None
 * @param[in]        ep      End point address
 * @param[in]        ptr     Data ram ptr
 * @param[in]        maxcnt  Max length
 * @retval           None
 */
static void nrf_usbd_ep_easydma_set_tx(uint8_t ep, uint32_t ptr, uint32_t maxcnt)
{
  uint8_t epid = USB_EP_GET_IDX(ep);
  if (epid == EP_ISO_NUM)
  {
    NRF_USBD->ISOIN.PTR = ptr;
    NRF_USBD->ISOIN.MAXCNT = maxcnt;
    return;
  }
  NRF_USBD->EPIN[epid].PTR = ptr;
  NRF_USBD->EPIN[epid].MAXCNT = maxcnt;
}

/**
 * @brief            Set rx easydma
 * @pre              None
 * @param[in]        ep      End point address
 * @param[in]        ptr     Data ram ptr
 * @param[in]        maxcnt  Max length
 * @retval           None
 */
static void nrf_usbd_ep_easydma_set_rx(uint8_t ep, uint32_t ptr, uint32_t maxcnt)
{
  uint8_t epid = USB_EP_GET_IDX(ep);
  if (epid == EP_ISO_NUM)
  {
    NRF_USBD->ISOOUT.PTR = ptr;
    NRF_USBD->ISOOUT.MAXCNT = maxcnt;
    return;
  }
  NRF_USBD->EPOUT[epid].PTR = ptr;
  NRF_USBD->EPOUT[epid].MAXCNT = maxcnt;
}

/**@brief Usbds the set address.
 *
 * @param[in] usbid   do not used
 * @param[in] address 8-bit valid address
 *
 * @retval >=0 success otherwise failureSet addressNone
 */
int usbd_set_address(uint8_t usbid, const uint8_t address)
{
  if (address == 0)
  {
    /*!< init 0 address */
  }
  else
  {
    /*!< For non-0 addresses, write the address to the register in the state phase of setting the address */
  }

  NRF_USBD->EVENTCAUSE |= NRF_USBD->EVENTCAUSE;
  NRF_USBD->EVENTS_USBEVENT = 0;

  NRF_USBD->INTENSET = USBD_INTEN_USBEVENT_Msk;
  /*!< nothing to do, handled by hardware; but don't STALL */
  usb_dc_cfg.address = address;
  return 0;
}

uint8_t usbd_get_port_speed(const uint8_t port)
{
    return USB_SPEED_FULL;
}

/**@brief Usbds the ep open.
 *
 * @param[in] busid  do not used
 * @param[in] ep_cfg Endpoint configuration structure pointer
 *
 * @retval >=0 success otherwise failureOpen endpointNone
 */
int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep_cfg)
{
  /*!< ep id */
  uint8_t epid = USB_EP_GET_IDX(ep_cfg->bEndpointAddress);
  /*!< ep max packet length */
  uint16_t mps = ep_cfg->wMaxPacketSize;
  if (USB_EP_DIR_IS_IN(ep_cfg->bEndpointAddress))
  {
    /*!< In */
    usb_dc_cfg.ep_in[epid].mps = mps;
    usb_dc_cfg.ep_in[epid].eptype = ep_cfg->bmAttributes;
    usb_dc_cfg.ep_in[epid].ep_enable = true;
    /*!< Open ep */
    if (ep_cfg->bmAttributes != USB_ENDPOINT_TYPE_ISOCHRONOUS)
    {
      /*!< Enable endpoint interrupt */
      NRF_USBD->INTENSET = (1 << (USBD_INTEN_ENDEPIN0_Pos + epid));
      /*!< Enable the in endpoint host to respond when sending in token */
      NRF_USBD->EPINEN |= (1 << (epid));
      __ISB();
      __DSB();
    }
    else
    {
      NRF_USBD->EVENTS_ENDISOIN = 0;
      /*!< SPLIT ISO buffer when ISO OUT endpoint is already opened. */
      if (usb_dc_cfg.ep_out[EP_ISO_NUM].mps)
        NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;

      /*!< Clear SOF event in case interrupt was not enabled yet. */
      if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0)
        NRF_USBD->EVENTS_SOF = 0;

      /*!< Enable SOF and ISOIN interrupts, and ISOIN endpoint. */
      NRF_USBD->INTENSET = USBD_INTENSET_ENDISOIN_Msk | USBD_INTENSET_SOF_Msk;
      NRF_USBD->EPINEN |= USBD_EPINEN_ISOIN_Msk;
    }
  }
  else if (USB_EP_DIR_IS_OUT(ep_cfg->bEndpointAddress))
  {
    /*!< Out */
    usb_dc_cfg.ep_out[epid].mps = mps;
    usb_dc_cfg.ep_out[epid].eptype = ep_cfg->bmAttributes;
    usb_dc_cfg.ep_out[epid].ep_enable = true;
    /*!< Open ep */
    if (ep_cfg->bmAttributes != USB_ENDPOINT_TYPE_ISOCHRONOUS)
    {
      NRF_USBD->INTENSET = (1 << (USBD_INTEN_ENDEPOUT0_Pos + epid));
      NRF_USBD->EPOUTEN |= (1 << (epid));
      __ISB();
      __DSB();
      /*!< Write any value to SIZE register will allow nRF to ACK/accept data */
      NRF_USBD->SIZE.EPOUT[epid] = 0;
    }
    else
    {
      /*!< SPLIT ISO buffer when ISO IN endpoint is already opened. */
      if (usb_dc_cfg.ep_in[EP_ISO_NUM].mps)
        NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;

      /*!< Clear old events */
      NRF_USBD->EVENTS_ENDISOOUT = 0;

      /*!< Clear SOF event in case interrupt was not enabled yet. */
      if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0)
        NRF_USBD->EVENTS_SOF = 0;

      /*!< Enable SOF and ISOOUT interrupts, and ISOOUT endpoint. */
      NRF_USBD->INTENSET = USBD_INTENSET_ENDISOOUT_Msk | USBD_INTENSET_SOF_Msk;
      NRF_USBD->EPOUTEN |= USBD_EPOUTEN_ISOOUT_Msk;
    }
  }

  /*!< Clear stall and reset DataToggle */
  NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | (ep_cfg->bEndpointAddress);
  NRF_USBD->DTOGGLE = (USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos) | (ep_cfg->bEndpointAddress);

  __ISB();
  __DSB();

  return 0;
}

/**@brief Usbds the ep close.
 *
 * @param[in] busid do not used
 * @param[in] ep    ep : Endpoint address
 *
 * @retval >=0 success otherwise failureClose endpointNone
 */
int usbd_ep_close(uint8_t busid, const uint8_t ep)
{
  /*!< ep id */
  uint8_t epid = USB_EP_GET_IDX(ep);
  if (epid != EP_ISO_NUM)
  {

    if (USB_EP_DIR_IS_OUT(ep))
    {
      usb_dc_cfg.ep_out[epid].ep_enable = false;
      NRF_USBD->INTENCLR = (1 << (USBD_INTEN_ENDEPOUT0_Pos + epid));
      NRF_USBD->EPOUTEN &= ~(1 << (epid));
    }
    else
    {
      usb_dc_cfg.ep_in[epid].ep_enable = false;
      NRF_USBD->INTENCLR = (1 << (USBD_INTEN_ENDEPIN0_Pos + epid));
      NRF_USBD->EPINEN &= ~(1 << (epid));
    }
  }
  else
  {
    /*!< ISO EP */
    if (USB_EP_DIR_IS_OUT(ep))
    {
      usb_dc_cfg.ep_out[epid].ep_enable = false;
      usb_dc_cfg.ep_out[EP_ISO_NUM].mps = 0;
      NRF_USBD->INTENCLR = USBD_INTENCLR_ENDISOOUT_Msk;
      NRF_USBD->EPOUTEN &= ~USBD_EPOUTEN_ISOOUT_Msk;
      NRF_USBD->EVENTS_ENDISOOUT = 0;
    }
    else
    {
      usb_dc_cfg.ep_in[epid].ep_enable = false;
      usb_dc_cfg.ep_in[EP_ISO_NUM].mps = 0;
      NRF_USBD->INTENCLR = USBD_INTENCLR_ENDISOIN_Msk;
      NRF_USBD->EPINEN &= ~USBD_EPINEN_ISOIN_Msk;
    }
    /*!< One of the ISO endpoints closed, no need to split buffers any more. */
    NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_OneDir;
    /*!< When both ISO endpoint are close there is no need for SOF any more. */
    if (usb_dc_cfg.ep_in[EP_ISO_NUM].mps + usb_dc_cfg.ep_out[EP_ISO_NUM].mps == 0)
    {
      NRF_USBD->INTENCLR = USBD_INTENCLR_SOF_Msk;
    }
  }
  __ISB();
  __DSB();

  return 0;
}

/**
 * @brief Setup in ep transfer setting and start transfer.
 *
 * This function is asynchronous.
 * This function is similar to uart with tx dma.
 *
 * This function is called to write data to the specified endpoint. The
 * supplied usbd_endpoint_callback function will be called when data is transmitted
 * out.
 *
 * @param[in]  busid     do not used
 * @param[in]  ep        Endpoint address corresponding to the one
 *                       listed in the device configuration table
 * @param[in]  data      Pointer to data to write
 * @param[in]  data_len  Length of the data requested to write. This may
 *                       be zero for a zero length status packet.
 * @return 0 on success, negative errno code on fail.
 */
int usbd_ep_start_write(uint8_t busid, const uint8_t ep, const uint8_t *data, uint32_t data_len)
{
  uint8_t ep_idx = USB_EP_GET_IDX(ep);

  if (!data && data_len)
  {
    return -1;
  }
  if (!usb_dc_cfg.ep_in[ep_idx].ep_enable)
  {
    return -2;
  }
  if ((uint32_t)data & 0x03)
  {
    return -3;
  }

  usb_dc_cfg.ep_in[ep_idx].xfer_buf = (uint8_t *)data;
  usb_dc_cfg.ep_in[ep_idx].xfer_len = data_len;
  usb_dc_cfg.ep_in[ep_idx].actual_xfer_len = 0;

  if (data_len == 0)
  {
    /*!< write 0 len data */
    nrf_usbd_ep_easydma_set_tx(ep_idx, (uint32_t)NULL, 0);
    NRF_USBD->TASKS_STARTEPIN[ep_idx] = 1;
  }
  else
  {
    /*!< Not zlp */
    data_len = MIN(data_len, usb_dc_cfg.ep_in[ep_idx].mps);
    if (!CHECK_ADD_IS_RAM(data))
    {
      /*!< Data is not in ram */
      /*!< Memcpy data to ram */
      memcpy(usb_dc_cfg.ep_in[ep_idx].ep_buffer, data, data_len);
      nrf_usbd_ep_easydma_set_tx(ep_idx, (uint32_t)usb_dc_cfg.ep_in[ep_idx].ep_buffer, data_len);
    }
    else
    {
      nrf_usbd_ep_easydma_set_tx(ep_idx, (uint32_t)data, data_len);
    }
    /**
     * Note that starting DMA transmission is to transmit data to USB peripherals,
     * and then wait for the host to get it
     */
    /*!< Start dma transfer */
    if (ep_idx != EP_ISO_NUM)
    {
      NRF_USBD->TASKS_STARTEPIN[ep_idx] = 1;
    }
    else
    {
      // NRF_USBD->TASKS_STARTISOIN = 1;
      usb_dc_cfg.iso_tx_is_ready = 1;
    }
  }
  return 0;
}

/**
 * @brief Setup out ep transfer setting and start transfer.
 *
 * This function is asynchronous.
 * This function is similar to uart with rx dma.
 *
 * This function is called to read data to the specified endpoint. The
 * supplied usbd_endpoint_callback function will be called when data is received
 * in.
 *
 * @param[in]  busid     do not used
 * @param[in]  ep        Endpoint address corresponding to the one
 *                       listed in the device configuration table
 * @param[in]  data      Pointer to data to read
 * @param[in]  data_len  Max length of the data requested to read.
 *
 * @return 0 on success, negative errno code on fail.
 */
int     usbd_ep_start_read(uint8_t busid, const uint8_t ep, uint8_t *data, uint32_t data_len)
{
  uint8_t ep_idx = USB_EP_GET_IDX(ep);

  if (!data && data_len)
  {
    return -1;
  }
  if (!usb_dc_cfg.ep_out[ep_idx].ep_enable)
  {
    return -2;
  }
  if ((uint32_t)data & 0x03)
  {
    return -3;
  }

  usb_dc_cfg.ep_out[ep_idx].xfer_buf = (uint8_t *)data;
  usb_dc_cfg.ep_out[ep_idx].xfer_len = data_len;
  usb_dc_cfg.ep_out[ep_idx].actual_xfer_len = 0;

  if (data_len == 0)
  {
    return 0;
  }
  else
  {
    data_len = MIN(data_len, usb_dc_cfg.ep_out[ep_idx].mps);
    if (!CHECK_ADD_IS_RAM(data))
    {
      /*!< Data address is not in ram */
      // TODO:
    }
    else
    {
      if (ep_idx == 0)
      {
        NRF_USBD->TASKS_EP0RCVOUT = 1;
      }
      nrf_usbd_ep_easydma_set_rx(ep_idx, (uint32_t)data, data_len);
    }
  }
  return 0;
}

/**
 * @brief            Endpoint setting pause
 * @pre              None
 * @param[in]        busid     do not used
 * @param[in]        ep : Endpoint address
 * @retval           >=0 success otherwise failure
 */
int usbd_ep_set_stall(uint8_t busid, const uint8_t ep)
{
  /*!< ep id */
  uint8_t epid = USB_EP_GET_IDX(ep);
  if (epid == 0)
  {
    NRF_USBD->TASKS_EP0STALL = 1;
  }
  else if (epid != EP_ISO_NUM)
  {
    NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_Stall << USBD_EPSTALL_STALL_Pos) | (ep);
  }
  __ISB();
  __DSB();
  return 0;
}

/**
 * @brief            Endpoint clear pause
 * @pre              None
 * @param[in]        busid     do not used
 * @param[in]        ep : Endpoint address
 * @retval           >=0 success otherwise failure
 */
int usbd_ep_clear_stall(uint8_t usbid, const uint8_t ep)
{
  /*!< ep id */
  uint8_t epid = USB_EP_GET_IDX(ep);

  if (epid != 0 && epid != EP_ISO_NUM)
  {
    /**
     * reset data toggle to DATA0
     * First write this register with VALUE=Nop to select the endpoint, then either read it to get the status from
     * VALUE, or write it again with VALUE=Data0 or Data1
     */
    NRF_USBD->DTOGGLE = ep;
    NRF_USBD->DTOGGLE = (USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos) | ep;

    /*!< Clear stall */
    NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep;

    /*!< Write any value to SIZE register will allow nRF to ACK/accept data */
    if (USB_EP_DIR_IS_OUT(ep))
      NRF_USBD->SIZE.EPOUT[epid] = 0;

    __ISB();
    __DSB();
  }

  return 0;
}

/**
 * @brief            Check endpoint status
 * @pre              None
 * @param[in]        busid     do not used
 * @param[in]        ep : Endpoint address
 * @param[out]       stalled : Outgoing endpoint status
 * @retval           >=0 success otherwise failure
 */
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{
  return 0;
}

/**
 * @brief            USB initialization
 * @pre              None
 * @param[in]        busid     do not used
 * @retval           >=0 success otherwise failure
 */
int usb_dc_init(uint8_t busid)
{
  /*!< dc init */
  usb_dc_low_level_pre_init();

  memset(&usb_dc_cfg, 0, sizeof(usb_dc_cfg));
  /*!< Clear USB Event Interrupt */
  NRF_USBD->EVENTS_USBEVENT = 0;
  NRF_USBD->EVENTCAUSE |= NRF_USBD->EVENTCAUSE;

  /*!< Reset interrupt */
  NRF_USBD->INTENCLR = NRF_USBD->INTEN;
  NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_EPDATA_Msk |
                       USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk | USBD_INTEN_STARTED_Msk;

  usb_dc_low_level_post_init();
  return 0;
}

/**
 * @brief            USB deinit
 * @pre              None
 * @param[in]        busid     do not used
 * @retval           >=0 success otherwise failure
 */
int usb_dc_deinit(uint8_t busid)
{
  /*!< dc deinit */
  return 0;
}

/**
 * @brief            USB interrupt processing function
 * @pre              None
 * @param[in]        None
 * @retval           None
 */
void USBD_IRQHandler(uint8_t busid)
{
  uint32_t const inten = NRF_USBD->INTEN;
  uint32_t int_status = 0;
  volatile uint32_t usb_event = 0;
  volatile uint32_t *regevt = &NRF_USBD->EVENTS_USBRESET;

  /*!< Traverse USB events */
  for (uint8_t i = 0; i < USBD_INTEN_EPDATA_Pos + 1; i++)
  {
    if ((inten & (1 << i)) && regevt[i])
    {
      int_status |= (1 << (i));
      /*!< event clear */
      regevt[i] = 0;
      __ISB();
      __DSB();
    }
  }

  /*!< bit 24 */
  if (int_status & USBD_INTEN_EPDATA_Msk)
  {
    /*!< out ep */
    for (uint8_t ep_out_ct = 1; ep_out_ct <= 7; ep_out_ct++)
    {
      if ((NRF_USBD->EPDATASTATUS) & (1 << (16 + ep_out_ct)))
      {
        NRF_USBD->EPDATASTATUS |= (1 << (16 + ep_out_ct));
        /*!< The data arrives at the usb fifo, starts the dma transmission, and transfers it to the user ram  */
        NRF_USBD->TASKS_STARTEPOUT[ep_out_ct] = 1;
      }
    }
    /*!< in ep */
    for (uint8_t ep_in_ct = 1; ep_in_ct <= 7; ep_in_ct++)
    {
      if ((NRF_USBD->EPDATASTATUS) & (1 << (0 + ep_in_ct)))
      {
        /*!< in ep tranfer to host successfully */
        NRF_USBD->EPDATASTATUS |= (1 << (0 + ep_in_ct));
        if (usb_dc_cfg.ep_in[ep_in_ct].xfer_len > usb_dc_cfg.ep_in[ep_in_ct].mps)
        {
          /*!< Need start in again */
          usb_dc_cfg.ep_in[ep_in_ct].xfer_buf += usb_dc_cfg.ep_in[ep_in_ct].mps;
          usb_dc_cfg.ep_in[ep_in_ct].xfer_len -= usb_dc_cfg.ep_in[ep_in_ct].mps;
          usb_dc_cfg.ep_in[ep_in_ct].actual_xfer_len += usb_dc_cfg.ep_in[ep_in_ct].mps;
          if (usb_dc_cfg.ep_in[ep_in_ct].xfer_len > usb_dc_cfg.ep_in[ep_in_ct].mps)
          {
            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);
          }
          else
          {
            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);
          }
          NRF_USBD->TASKS_STARTEPIN[ep_in_ct] = 1;
        }
        else
        {
          usb_dc_cfg.ep_in[ep_in_ct].actual_xfer_len += usb_dc_cfg.ep_in[ep_in_ct].xfer_len;
          usb_dc_cfg.ep_in[ep_in_ct].xfer_len = 0;
          usbd_event_ep_in_complete_handler(0, ep_in_ct | 0x80, usb_dc_cfg.ep_in[ep_in_ct].actual_xfer_len);
        }
      }
    }
  }

  /*!< bit 23 */
  if (int_status & USBD_INTEN_EP0SETUP_Msk)
  {
    /* Setup */
    /*!< Storing this setup package will help the following procedures */
    get_setup_packet(&(usb_dc_cfg.setup));
    /*!< Nrf52840 will set the address automatically by hardware,
         so the protocol stack of the address setting command sent by the host does not need to be processed */

    if (usb_dc_cfg.setup.wLength == 0)
    {
      NRF_USBD->TASKS_EP0STATUS = 1;
    }

    if (usb_dc_cfg.setup.bRequest != USB_REQUEST_SET_ADDRESS)
    {
      usbd_event_ep0_setup_complete_handler(0, (uint8_t *)&(usb_dc_cfg.setup));
    }
  }

  /*!< bit 22 */
  if (int_status & USBD_INTEN_USBEVENT_Msk)
  {
    usb_event = NRF_USBD->EVENTCAUSE;
    NRF_USBD->EVENTCAUSE = usb_event;
    if (usb_event & USBD_EVENTCAUSE_SUSPEND_Msk)
    {
      NRF_USBD->LOWPOWER = 1;
      /*!<  */
    }
    if (usb_event & USBD_EVENTCAUSE_RESUME_Msk)
    {
      /*!<  */
    }
    if (usb_event & USBD_EVENTCAUSE_USBWUALLOWED_Msk)
    {
      NRF_USBD->DPDMVALUE = USBD_DPDMVALUE_STATE_Resume;
      NRF_USBD->TASKS_DPDMDRIVE = 1;
      /**
       * There is no Resume interrupt for remote wakeup, enable SOF for to report bus ready state
       * Clear SOF event in case interrupt was not enabled yet.
       */
      if ((NRF_USBD->INTEN & USBD_INTEN_SOF_Msk) == 0)
        NRF_USBD->EVENTS_SOF = 0;
      NRF_USBD->INTENSET = USBD_INTENSET_SOF_Msk;
    }
  }

  /*!< bit 21 */
  if (int_status & USBD_INTEN_SOF_Msk)
  {
    bool iso_enabled = false;
    /*!< ISOOUT: Transfer data gathered in previous frame from buffer to RAM */
    if (NRF_USBD->EPOUTEN & USBD_EPOUTEN_ISOOUT_Msk)
    {
      iso_enabled = true;
      /*!< If ZERO bit is set, ignore ISOOUT length */
      if ((NRF_USBD->SIZE.ISOOUT) & USBD_SIZE_ISOOUT_ZERO_Msk)
      {
      }
      else
      {
        /*!< Trigger DMA move data from Endpoint -> SRAM */
        NRF_USBD->TASKS_STARTISOOUT = 1;
        /*!< EP_ISO_NUM out using dma */
        usb_dc_cfg.ep_out[EP_ISO_NUM].is_using_dma = 1;
      }
    }

    /*!< ISOIN: Notify client that data was transferred */
    if (NRF_USBD->EPINEN & USBD_EPINEN_ISOIN_Msk)
    {
      iso_enabled = true;
      if (usb_dc_cfg.iso_tx_is_ready == 1)
      {
        usb_dc_cfg.iso_tx_is_ready = 0;
        NRF_USBD->TASKS_STARTISOIN = 1;
      }
    }

    if (!iso_enabled)
    {
      /**
       * ISO endpoint is not used, SOF is only enabled one-time for remote wakeup
       * so we disable it now
       */
      NRF_USBD->INTENCLR = USBD_INTENSET_SOF_Msk;
    }
  }

  /*!< bit 20 */
  if (int_status & USBD_INTEN_ENDISOOUT_Msk)
  {
    if (usb_dc_cfg.ep_out[EP_ISO_NUM].is_using_dma == 1)
    {
      usb_dc_cfg.ep_out[EP_ISO_NUM].is_using_dma = 0;
      uint32_t read_count = NRF_USBD->SIZE.ISOOUT;
      usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_buf += read_count;
      usb_dc_cfg.ep_out[EP_ISO_NUM].actual_xfer_len += read_count;
      usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_len -= read_count;

      if ((read_count < usb_dc_cfg.ep_out[EP_ISO_NUM].mps) || (usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_len == 0))
      {
        usbd_event_ep_out_complete_handler(0, ((EP_ISO_NUM)&0x7f), usb_dc_cfg.ep_out[EP_ISO_NUM].actual_xfer_len);
      }
      else
      {
        if (usb_dc_cfg.ep_out[EP_ISO_NUM].xfer_len > usb_dc_cfg.ep_out[EP_ISO_NUM].mps)
        {
          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);
        }
        else
        {
          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);
        }
      }
    }
  }

  /**
   * Traverse ordinary out endpoint events, starting from endpoint 1 to endpoint 7,
   * and end 0 for special processing
   */
  for (uint8_t offset = 0; offset < 7; offset++)
  {
    if (int_status & (USBD_INTEN_ENDEPOUT1_Msk << offset))
    {
      /*!< Out 'offset' transfer complete */
      uint32_t read_count = NRF_USBD->SIZE.EPOUT[offset + 1];
      usb_dc_cfg.ep_out[offset + 1].xfer_buf += read_count;
      usb_dc_cfg.ep_out[offset + 1].actual_xfer_len += read_count;
      usb_dc_cfg.ep_out[offset + 1].xfer_len -= read_count;

      if ((read_count < usb_dc_cfg.ep_out[offset + 1].mps) || (usb_dc_cfg.ep_out[offset + 1].xfer_len == 0))
      {
        usbd_event_ep_out_complete_handler(0, ((offset + 1) & 0x7f), usb_dc_cfg.ep_out[offset + 1].actual_xfer_len);
      }
      else
      {
        if (usb_dc_cfg.ep_out[offset + 1].xfer_len > usb_dc_cfg.ep_out[offset + 1].mps)
        {
          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);
        }
        else
        {
          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);
        }
      }
    }
  }

  /*!< bit 12 */
  if (int_status & USBD_INTEN_ENDEPOUT0_Msk)
  {
    uint32_t read_count = NRF_USBD->SIZE.EPOUT[0];
    usb_dc_cfg.ep_out[0].actual_xfer_len += read_count;
    usb_dc_cfg.ep_out[0].xfer_len -= read_count;

    if (usb_dc_cfg.ep_out[0].xfer_len == 0)
    {
      /*!< Enable the state phase of endpoint 0 */
      NRF_USBD->TASKS_EP0STATUS = 1;
    }

    usbd_event_ep_out_complete_handler(0, 0x00, usb_dc_cfg.ep_out[0].actual_xfer_len);
  }

  /*!< bit 11 */
  if (int_status & USBD_INTEN_ENDISOIN_Msk)
  {
  }

  /*!< bit 10 */
  if (int_status & USBD_INTEN_EP0DATADONE_Msk)
  {
    switch (usb_dc_cfg.setup.bmRequestType >> USB_REQUEST_DIR_SHIFT)
    {
    case 1:
      /*!< IN */
      if (usb_dc_cfg.ep_in[0].xfer_len > usb_dc_cfg.ep_in[0].mps)
      {
        usb_dc_cfg.ep_in[0].xfer_len -= usb_dc_cfg.ep_in[0].mps;
        usb_dc_cfg.ep_in[0].actual_xfer_len += usb_dc_cfg.ep_in[0].mps;
        usbd_event_ep_in_complete_handler(0, 0 | 0x80, usb_dc_cfg.ep_in[0].actual_xfer_len);
      }
      else
      {
        usb_dc_cfg.ep_in[0].actual_xfer_len += usb_dc_cfg.ep_in[0].xfer_len;
        usb_dc_cfg.ep_in[0].xfer_len = 0;
        /*!< Enable the state phase of endpoint 0 */
        usbd_event_ep_in_complete_handler(0, 0 | 0x80, usb_dc_cfg.ep_in[0].actual_xfer_len);
        NRF_USBD->TASKS_EP0STATUS = 1;
      }
      break;
    case 0:
      if (usb_dc_cfg.setup.bRequest != USB_REQUEST_SET_ADDRESS)
      {
        /*!< The data arrives at the usb fifo, starts the dma transmission, and transfers it to the user ram  */
        NRF_USBD->TASKS_STARTEPOUT[0] = 1;
      }
      break;
    }
  }

  /**
   * Traversing ordinary in endpoint events, starting from endpoint 1 to endpoint 7,
   * endpoint 0 special processing
   */
  for (uint8_t offset = 0; offset < 7; offset++)
  {
    if (int_status & (USBD_INTEN_ENDEPIN1_Msk << offset))
    {
      /*!< DMA move data completed */
    }
  }

  /*!< bit 1 */
  if (int_status & USBD_INTEN_STARTED_Msk)
  {
    /*!< Easy dma start transfer data */
  }

  /*!< bit 2 */
  if (int_status & USBD_INTEN_ENDEPIN0_Msk)
  {
    /*!< EP0 IN DMA move data completed */
  }

  /*!< bit 0 */
  if (int_status & USBD_INTEN_USBRESET_Msk)
  {
    NRF_USBD->EPOUTEN = 1UL;
    NRF_USBD->EPINEN = 1UL;

    for (int i = 0; i < 8; i++)
    {
      NRF_USBD->TASKS_STARTEPIN[i] = 0;
      NRF_USBD->TASKS_STARTEPOUT[i] = 0;
    }

    NRF_USBD->TASKS_STARTISOIN = 0;
    NRF_USBD->TASKS_STARTISOOUT = 0;

    /*!< Clear USB Event Interrupt */
    NRF_USBD->EVENTS_USBEVENT = 0;
    NRF_USBD->EVENTCAUSE |= NRF_USBD->EVENTCAUSE;

    /*!< Reset interrupt */
    NRF_USBD->INTENCLR = NRF_USBD->INTEN;
    NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_EPDATA_Msk |
                         USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk | USBD_INTEN_STARTED_Msk;

    usbd_event_reset_handler(0);
  }
}

/**
 * Errata: USB cannot be enabled.
 */
static bool chyu_nrf52_errata_187(void)
{
#ifndef NRF52_SERIES
  return false;
#else
#if defined(NRF52820_XXAA) || defined(DEVELOP_IN_NRF52820) || defined(NRF52833_XXAA) || defined(DEVELOP_IN_NRF52833) || defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  uint32_t var1 = *(uint32_t *)0x10000130ul;
  uint32_t var2 = *(uint32_t *)0x10000134ul;
#endif
#if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  if (var1 == 0x08)
  {
    switch (var2)
    {
    case 0x00ul:
      return false;
    case 0x01ul:
      return true;
    case 0x02ul:
      return true;
    case 0x03ul:
      return true;
    default:
      return true;
    }
  }
#endif
#if defined(NRF52833_XXAA) || defined(DEVELOP_IN_NRF52833)
  if (var1 == 0x0D)
  {
    switch (var2)
    {
    case 0x00ul:
      return true;
    case 0x01ul:
      return true;
    default:
      return true;
    }
  }
#endif
#if defined(NRF52820_XXAA) || defined(DEVELOP_IN_NRF52820)
  if (var1 == 0x10)
  {
    switch (var2)
    {
    case 0x00ul:
      return true;
    case 0x01ul:
      return true;
    case 0x02ul:
      return true;
    default:
      return true;
    }
  }
#endif
  return false;
#endif
}

/**
 * Errata: USBD might not reach its active state.
 */
static bool chyu_nrf52_errata_171(void)
{
#ifndef NRF52_SERIES
  return false;
#else
#if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  uint32_t var1 = *(uint32_t *)0x10000130ul;
  uint32_t var2 = *(uint32_t *)0x10000134ul;
#endif
#if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  if (var1 == 0x08)
  {
    switch (var2)
    {
    case 0x00ul:
      return true;
    case 0x01ul:
      return true;
    case 0x02ul:
      return true;
    case 0x03ul:
      return true;
    default:
      return true;
    }
  }
#endif
  return false;
#endif
}

/**
 * Errata: ISO double buffering not functional.
 */
static bool chyu_nrf52_errata_166(void)
{
#ifndef NRF52_SERIES
  return false;
#else
#if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  uint32_t var1 = *(uint32_t *)0x10000130ul;
  uint32_t var2 = *(uint32_t *)0x10000134ul;
#endif
#if defined(NRF52840_XXAA) || defined(DEVELOP_IN_NRF52840)
  if (var1 == 0x08)
  {
    switch (var2)
    {
    case 0x00ul:
      return true;
    case 0x01ul:
      return true;
    case 0x02ul:
      return true;
    case 0x03ul:
      return true;
    default:
      return true;
    }
  }
#endif
  return false;
#endif
}

#ifdef SOFTDEVICE_PRESENT

#include "nrf_mbr.h"
#include "nrf_sdm.h"
#include "nrf_soc.h"

#ifndef SD_MAGIC_NUMBER
#define SD_MAGIC_NUMBER 0x51B1E5DB
#endif

static inline bool is_sd_existed(void)
{
  return *((uint32_t *)(SOFTDEVICE_INFO_STRUCT_ADDRESS + 4)) == SD_MAGIC_NUMBER;
}

/**
 * check if SD is existed and enabled
 */
static inline bool is_sd_enabled(void)
{
  if (!is_sd_existed())
    return false;

  uint8_t sd_en = false;
  (void)sd_softdevice_is_enabled(&sd_en);
  return sd_en;
}
#endif

static bool hfclk_running(void)
{
#ifdef SOFTDEVICE_PRESENT
  if (is_sd_enabled())
  {
    uint32_t is_running = 0;
    (void)sd_clock_hfclk_is_running(&is_running);
    return (is_running ? true : false);
  }
#endif

#if defined(CLOCK_HFCLKSTAT_SRC_Xtal) || defined(__NRFX_DOXYGEN__)
  return (NRF_CLOCK->HFCLKSTAT & (CLOCK_HFCLKSTAT_STATE_Msk | CLOCK_HFCLKSTAT_SRC_Msk)) ==
         (CLOCK_HFCLKSTAT_STATE_Msk | (CLOCK_HFCLKSTAT_SRC_Xtal << CLOCK_HFCLKSTAT_SRC_Pos));
#else
  return (NRF_CLOCK->HFCLKSTAT & (CLOCK_HFCLKSTAT_STATE_Msk | CLOCK_HFCLKSTAT_SRC_Msk)) ==
         (CLOCK_HFCLKSTAT_STATE_Msk | (CLOCK_HFCLKSTAT_SRC_HFXO << CLOCK_HFCLKSTAT_SRC_Pos));
#endif
}

enum
{
  NRF_CLOCK_EVENT_HFCLKSTARTED = offsetof(NRF_CLOCK_Type, EVENTS_HFCLKSTARTED), /*!< HFCLK oscillator started. */
};

enum
{
  NRF_CLOCK_TASK_HFCLKSTART = offsetof(NRF_CLOCK_Type, TASKS_HFCLKSTART), /*!< Start HFCLK clock source. */
  NRF_CLOCK_TASK_HFCLKSTOP = offsetof(NRF_CLOCK_Type, TASKS_HFCLKSTOP),   /*!< Stop HFCLK clock source. */
};

static void hfclk_enable(void)
{
  /*!< already running, nothing to do */
  if (hfclk_running())
    return;

#ifdef SOFTDEVICE_PRESENT
  if (is_sd_enabled())
  {
    (void)sd_clock_hfclk_request();
    return;
  }
#endif

  *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + NRF_CLOCK_EVENT_HFCLKSTARTED)) = 0x0UL;
  volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + (uint32_t)NRF_CLOCK_EVENT_HFCLKSTARTED));
  (void)dummy;
  *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + NRF_CLOCK_TASK_HFCLKSTART)) = 0x1UL;
}

static void hfclk_disable(void)
{
#ifdef SOFTDEVICE_PRESENT
  if (is_sd_enabled())
  {
    (void)sd_clock_hfclk_release();
    return;
  }
#endif

  *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + NRF_CLOCK_TASK_HFCLKSTOP)) = 0x1UL;
}

/**
 * Power & Clock Peripheral on nRF5x to manage USB
 * USB Bus power is managed by Power module, there are 3 VBUS power events:
 * Detected, Ready, Removed. Upon these power events, This function will
 * enable ( or disable ) usb & hfclk peripheral, set the usb pin pull up
 * accordingly to the controller Startup/Standby Sequence in USBD 51.4 specs.
 * Therefore this function must be called to handle USB power event by
 * - nrfx_power_usbevt_init() : if Softdevice is not used or enabled
 * - SoftDevice SOC event : if SD is used and enabled
 */
void cherry_usb_hal_nrf_power_event(uint32_t event)
{
  enum
  {
    USB_EVT_DETECTED = 0,
    USB_EVT_REMOVED = 1,
    USB_EVT_READY = 2
  };

  switch (event)
  {
  case USB_EVT_DETECTED:
    if (!NRF_USBD->ENABLE)
    {
      /*!< Prepare for receiving READY event: disable interrupt since we will blocking wait */
      NRF_USBD->INTENCLR = USBD_INTEN_USBEVENT_Msk;
      NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
      __ISB();
      __DSB();

#ifdef NRF52_SERIES /*!< NRF53 does not need this errata */
      /*!< ERRATA 171, 187, 166 */
      if (chyu_nrf52_errata_187())
      {
        if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
        {
          *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
          *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
          *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
        }
        else
        {
          *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
        }
      }

      if (chyu_nrf52_errata_171())
      {
        if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
        {
          *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
          *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
          *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
        }
        else
        {
          *((volatile uint32_t *)(0x4006EC14)) = 0x000000C0;
        }
      }
#endif

      /*!< Enable the peripheral (will cause Ready event) */
      NRF_USBD->ENABLE = 1;
      __ISB();
      __DSB();

      /*!< Enable HFCLK */
      hfclk_enable();
    }
    break;

  case USB_EVT_READY:
    /**
     * Skip if pull-up is enabled and HCLK is already running.
     * Application probably call this more than necessary.
     */
    if (NRF_USBD->USBPULLUP && hfclk_running())
      break;

    /*!< Waiting for USBD peripheral enabled */
    while (!(USBD_EVENTCAUSE_READY_Msk & NRF_USBD->EVENTCAUSE))
    {
    }

    NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
    __ISB();
    __DSB();

#ifdef NRF52_SERIES
    if (chyu_nrf52_errata_171())
    {
      if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
      {
        *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
        *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
        *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
      }
      else
      {
        *((volatile uint32_t *)(0x4006EC14)) = 0x00000000;
      }
    }

    if (chyu_nrf52_errata_187())
    {
      if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
      {
        *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
        *((volatile uint32_t *)(0x4006ED14)) = 0x00000000;
        *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
      }
      else
      {
        *((volatile uint32_t *)(0x4006ED14)) = 0x00000000;
      }
    }

    if (chyu_nrf52_errata_166())
    {
      *((volatile uint32_t *)(NRF_USBD_BASE + 0x800)) = 0x7E3;
      *((volatile uint32_t *)(NRF_USBD_BASE + 0x804)) = 0x40;

      __ISB();
      __DSB();
    }
#endif

    /*!< ISO buffer Lower half for IN, upper half for OUT */
    NRF_USBD->ISOSPLIT = USBD_ISOSPLIT_SPLIT_HalfIN;

    /*!< Enable bus-reset interrupt */
    NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk;

    /*!< Enable interrupt, priorities should be set by application */
    /*!< clear pending irq */
    NVIC->ICPR[(((uint32_t)USBD_IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)USBD_IRQn) & 0x1FUL));
    /**
     * Don't enable USBD interrupt yet, if dcd_init() did not finish yet
     * Interrupt will be enabled by tud_init(), when USB stack is ready
     * to handle interrupts.
     */
    /*!< Wait for HFCLK */
    while (!hfclk_running())
    {
    }

    /*!< Enable pull up */
    NRF_USBD->USBPULLUP = 1;
    __ISB();
    __DSB();
    break;

  case USB_EVT_REMOVED:
    if (NRF_USBD->ENABLE)
    {
      /*!< Disable pull up */
      NRF_USBD->USBPULLUP = 0;
      __ISB();
      __DSB();

      /*!< Disable Interrupt */
      NVIC->ICER[(((uint32_t)USBD_IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)USBD_IRQn) & 0x1FUL));
      __DSB();
      __ISB();
      /*!< disable all interrupt */
      NRF_USBD->INTENCLR = NRF_USBD->INTEN;

      NRF_USBD->ENABLE = 0;
      __ISB();
      __DSB();
      hfclk_disable();
    }
    break;

  default:
    break;
  }
}