27
27
#if defined( USB_DEVICE )
28
28
29
29
#include "em_cmu.h"
30
+ #include "em_core.h"
30
31
#include "em_usbtypes.h"
31
32
#include "em_usbhal.h"
32
33
#include "em_usbd.h"
@@ -69,9 +70,10 @@ static const char *stateNames[] =
69
70
******************************************************************************/
70
71
void USBD_AbortAllTransfers ( void )
71
72
{
72
- INT_Disable ();
73
+ CORE_DECLARE_IRQ_STATE ;
74
+ CORE_ENTER_CRITICAL ();
73
75
USBDHAL_AbortAllTransfers ( USB_STATUS_EP_ABORTED );
74
- INT_Enable ();
76
+ CORE_EXIT_CRITICAL ();
75
77
}
76
78
77
79
/***************************************************************************/ /**
@@ -85,6 +87,7 @@ int USBD_AbortTransfer( int epAddr )
85
87
{
86
88
USB_XferCompleteCb_TypeDef callback ;
87
89
USBD_Ep_TypeDef * ep = USBD_GetEpFromAddr ( epAddr );
90
+ CORE_DECLARE_IRQ_STATE ;
88
91
89
92
if ( ep == NULL )
90
93
{
@@ -100,10 +103,10 @@ int USBD_AbortTransfer( int epAddr )
100
103
return USB_STATUS_ILLEGAL ;
101
104
}
102
105
103
- INT_Disable ();
106
+ CORE_ENTER_CRITICAL ();
104
107
if ( ep -> state == D_EP_IDLE )
105
108
{
106
- INT_Enable ();
109
+ CORE_EXIT_CRITICAL ();
107
110
return USB_STATUS_OK ;
108
111
}
109
112
@@ -125,7 +128,7 @@ int USBD_AbortTransfer( int epAddr )
125
128
callback ( USB_STATUS_EP_ABORTED , ep -> xferred , ep -> remaining );
126
129
}
127
130
128
- INT_Enable ();
131
+ CORE_EXIT_CRITICAL ();
129
132
return USB_STATUS_OK ;
130
133
}
131
134
@@ -139,9 +142,10 @@ int USBD_AbortTransfer( int epAddr )
139
142
******************************************************************************/
140
143
void USBD_Connect ( void )
141
144
{
142
- INT_Disable ();
145
+ CORE_DECLARE_IRQ_STATE ;
146
+ CORE_ENTER_CRITICAL ();
143
147
USBDHAL_Connect ();
144
- INT_Enable ();
148
+ CORE_EXIT_CRITICAL ();
145
149
}
146
150
147
151
/***************************************************************************/ /**
@@ -154,9 +158,10 @@ void USBD_Connect( void )
154
158
******************************************************************************/
155
159
void USBD_Disconnect ( void )
156
160
{
157
- INT_Disable ();
161
+ CORE_DECLARE_IRQ_STATE ;
162
+ CORE_ENTER_CRITICAL ();
158
163
USBDHAL_Disconnect ();
159
- INT_Enable ();
164
+ CORE_EXIT_CRITICAL ();
160
165
}
161
166
162
167
/***************************************************************************/ /**
@@ -239,6 +244,7 @@ const char *USBD_GetUsbStateName( USBD_State_TypeDef state )
239
244
int USBD_Init ( const USBD_Init_TypeDef * p )
240
245
{
241
246
USBD_Ep_TypeDef * ep ;
247
+ CORE_DECLARE_IRQ_STATE ;
242
248
243
249
#if !defined( USB_CORECLK_HFRCO ) || !defined( CMU_OSCENCMD_USHFRCOEN )
244
250
/* Devices supporting crystal-less USB can use HFRCO or HFXO as core clock. */
@@ -308,7 +314,7 @@ int USBD_Init( const USBD_Init_TypeDef *p )
308
314
*/
309
315
totalRxFifoSize += 10 + 1 + ( 2 * (MAX_NUM_OUT_EPS + 1 ) );
310
316
311
- INT_Disable ();
317
+ CORE_ENTER_CRITICAL ();
312
318
313
319
/* Enable USB clock */
314
320
CMU -> HFCORECLKEN0 |= CMU_HFCORECLKEN0_USB | CMU_HFCORECLKEN0_USBC ;
@@ -339,7 +345,7 @@ int USBD_Init( const USBD_Init_TypeDef *p )
339
345
}
340
346
else
341
347
{
342
- INT_Enable ();
348
+ CORE_EXIT_CRITICAL ();
343
349
DEBUG_USB_API_PUTS ( "\nUSBD_Init(), FIFO setup error" );
344
350
EFM_ASSERT ( false );
345
351
return USB_STATUS_ILLEGAL ;
@@ -356,7 +362,7 @@ int USBD_Init( const USBD_Init_TypeDef *p )
356
362
USBD_SetUsbState ( USBD_STATE_NONE );
357
363
}
358
364
359
- INT_Enable ();
365
+ CORE_EXIT_CRITICAL ();
360
366
return USB_STATUS_OK ;
361
367
}
362
368
@@ -389,6 +395,7 @@ int USBD_Init( const USBD_Init_TypeDef *p )
389
395
int USBD_Read ( int epAddr , void * data , int byteCount ,
390
396
USB_XferCompleteCb_TypeDef callback )
391
397
{
398
+ CORE_DECLARE_IRQ_STATE ;
392
399
USBD_Ep_TypeDef * ep = USBD_GetEpFromAddr ( epAddr );
393
400
394
401
USB_PRINTF ("USBD: Read addr %x, data %p, size %d, cb 0x%lx\n" ,
@@ -416,24 +423,25 @@ int USBD_Read( int epAddr, void *data, int byteCount,
416
423
return USB_STATUS_ILLEGAL ;
417
424
}
418
425
419
- INT_Disable ();
426
+ CORE_ENTER_CRITICAL ();
427
+
420
428
if ( USBDHAL_EpIsStalled ( ep ) )
421
429
{
422
- INT_Enable ();
430
+ CORE_EXIT_CRITICAL ();
423
431
DEBUG_USB_API_PUTS ( "\nUSBD_Read(), Endpoint is halted" );
424
432
return USB_STATUS_EP_STALLED ;
425
433
}
426
434
427
435
if ( ep -> state != D_EP_IDLE )
428
436
{
429
- INT_Enable ();
437
+ CORE_EXIT_CRITICAL ();
430
438
DEBUG_USB_API_PUTS ( "\nUSBD_Read(), Endpoint is busy" );
431
439
return USB_STATUS_EP_BUSY ;
432
440
}
433
441
434
442
if ( ( ep -> num > 0 ) && ( USBD_GetUsbState () != USBD_STATE_CONFIGURED ) )
435
443
{
436
- INT_Enable ();
444
+ CORE_EXIT_CRITICAL ();
437
445
DEBUG_USB_API_PUTS ( "\nUSBD_Read(), Device not configured" );
438
446
return USB_STATUS_DEVICE_UNCONFIGURED ;
439
447
}
@@ -448,7 +456,7 @@ int USBD_Read( int epAddr, void *data, int byteCount,
448
456
}
449
457
else if ( ep -> in != false )
450
458
{
451
- INT_Enable ();
459
+ CORE_EXIT_CRITICAL ();
452
460
DEBUG_USB_API_PUTS ( "\nUSBD_Read(), Illegal EP direction" );
453
461
EFM_ASSERT ( false );
454
462
return USB_STATUS_ILLEGAL ;
@@ -458,7 +466,7 @@ int USBD_Read( int epAddr, void *data, int byteCount,
458
466
ep -> xferCompleteCb = callback ;
459
467
460
468
USBD_ArmEp ( ep );
461
- INT_Enable ();
469
+ CORE_EXIT_CRITICAL ();
462
470
return USB_STATUS_OK ;
463
471
}
464
472
@@ -477,22 +485,26 @@ int USBD_Read( int epAddr, void *data, int byteCount,
477
485
******************************************************************************/
478
486
int USBD_RemoteWakeup ( void )
479
487
{
480
- INT_Disable ();
488
+ CORE_DECLARE_IRQ_STATE ;
489
+ CORE_ENTER_CRITICAL ();
481
490
482
491
if ( ( dev -> state != USBD_STATE_SUSPENDED ) ||
483
492
( dev -> remoteWakeupEnabled == false ) )
484
493
{
485
- INT_Enable ();
494
+ CORE_EXIT_CRITICAL ();
486
495
DEBUG_USB_API_PUTS ( "\nUSBD_RemoteWakeup(), Illegal remote wakeup" );
487
496
return USB_STATUS_ILLEGAL ;
488
497
}
489
498
490
499
USBDHAL_SetRemoteWakeup ();
491
- INT_Enable ();
500
+ CORE_EXIT_CRITICAL ();
501
+
492
502
USBTIMER_DelayMs ( 10 );
493
- INT_Disable ();
503
+
504
+ CORE_ENTER_CRITICAL ();
494
505
USBDHAL_ClearRemoteWakeup ();
495
- INT_Enable ();
506
+ CORE_EXIT_CRITICAL ();
507
+
496
508
return USB_STATUS_OK ;
497
509
}
498
510
@@ -565,6 +577,7 @@ void USBD_SetUsbState( USBD_State_TypeDef newState )
565
577
int USBD_StallEp ( int epAddr )
566
578
{
567
579
USB_Status_TypeDef retVal ;
580
+ CORE_DECLARE_IRQ_STATE ;
568
581
USBD_Ep_TypeDef * ep = USBD_GetEpFromAddr ( epAddr );
569
582
570
583
if ( ep == NULL )
@@ -581,9 +594,9 @@ int USBD_StallEp( int epAddr )
581
594
return USB_STATUS_ILLEGAL ;
582
595
}
583
596
584
- INT_Disable ();
597
+ CORE_ENTER_CRITICAL ();
585
598
retVal = USBDHAL_StallEp ( ep );
586
- INT_Enable ();
599
+ CORE_EXIT_CRITICAL ();
587
600
588
601
if ( retVal != USB_STATUS_OK )
589
602
{
@@ -626,6 +639,7 @@ void USBD_Stop( void )
626
639
int USBD_UnStallEp ( int epAddr )
627
640
{
628
641
USB_Status_TypeDef retVal ;
642
+ CORE_DECLARE_IRQ_STATE ;
629
643
USBD_Ep_TypeDef * ep = USBD_GetEpFromAddr ( epAddr );
630
644
631
645
if ( ep == NULL )
@@ -642,9 +656,9 @@ int USBD_UnStallEp( int epAddr )
642
656
return USB_STATUS_ILLEGAL ;
643
657
}
644
658
645
- INT_Disable ();
659
+ CORE_ENTER_CRITICAL ();
646
660
retVal = USBDHAL_UnStallEp ( ep );
647
- INT_Enable ();
661
+ CORE_EXIT_CRITICAL ();
648
662
649
663
if ( retVal != USB_STATUS_OK )
650
664
{
@@ -678,6 +692,7 @@ int USBD_Write( int epAddr, void *data, int byteCount,
678
692
USB_XferCompleteCb_TypeDef callback )
679
693
{
680
694
USBD_Ep_TypeDef * ep = USBD_GetEpFromAddr ( epAddr );
695
+ CORE_DECLARE_IRQ_STATE ;
681
696
682
697
USB_PRINTF ("USBD: Write addr %x, data %p, size %d, cb 0x%lx\n" ,
683
698
epAddr , data , byteCount , (uint32_t )callback );
@@ -704,24 +719,25 @@ int USBD_Write( int epAddr, void *data, int byteCount,
704
719
return USB_STATUS_ILLEGAL ;
705
720
}
706
721
707
- INT_Disable ();
722
+ CORE_ENTER_CRITICAL ();
723
+
708
724
if ( USBDHAL_EpIsStalled ( ep ) )
709
725
{
710
- INT_Enable ();
726
+ CORE_EXIT_CRITICAL ();
711
727
DEBUG_USB_API_PUTS ( "\nUSBD_Write(), Endpoint is halted" );
712
728
return USB_STATUS_EP_STALLED ;
713
729
}
714
730
715
731
if ( ep -> state != D_EP_IDLE )
716
732
{
717
- INT_Enable ();
733
+ CORE_EXIT_CRITICAL ();
718
734
DEBUG_USB_API_PUTS ( "\nUSBD_Write(), Endpoint is busy" );
719
735
return USB_STATUS_EP_BUSY ;
720
736
}
721
737
722
738
if ( ( ep -> num > 0 ) && ( USBD_GetUsbState () != USBD_STATE_CONFIGURED ) )
723
739
{
724
- INT_Enable ();
740
+ CORE_EXIT_CRITICAL ();
725
741
DEBUG_USB_API_PUTS ( "\nUSBD_Write(), Device not configured" );
726
742
return USB_STATUS_DEVICE_UNCONFIGURED ;
727
743
}
@@ -736,7 +752,7 @@ int USBD_Write( int epAddr, void *data, int byteCount,
736
752
}
737
753
else if ( ep -> in != true )
738
754
{
739
- INT_Enable ();
755
+ CORE_EXIT_CRITICAL ();
740
756
DEBUG_USB_API_PUTS ( "\nUSBD_Write(), Illegal EP direction" );
741
757
EFM_ASSERT ( false );
742
758
return USB_STATUS_ILLEGAL ;
@@ -746,7 +762,7 @@ int USBD_Write( int epAddr, void *data, int byteCount,
746
762
ep -> xferCompleteCb = callback ;
747
763
748
764
USBD_ArmEp ( ep );
749
- INT_Enable ();
765
+ CORE_EXIT_CRITICAL ();
750
766
return USB_STATUS_OK ;
751
767
}
752
768
@@ -841,6 +857,7 @@ static void USBD_ResetEndpoints(void)
841
857
int USBD_AddEndpoint (int epAddr , int transferType ,
842
858
int maxPacketSize , int bufferMult )
843
859
{
860
+ CORE_DECLARE_IRQ_STATE ;
844
861
USBD_Ep_TypeDef * ep ;
845
862
846
863
numEps ++ ;
@@ -890,7 +907,7 @@ int USBD_AddEndpoint(int epAddr, int transferType,
890
907
ep -> num , numEps , ep -> in , ep -> addr , ep -> type , ep -> packetSize , ep -> fifoSize ,
891
908
totalTxFifoSize , totalRxFifoSize );
892
909
893
- INT_Disable ();
910
+ CORE_ENTER_CRITICAL ();
894
911
#if defined( CMU_OSCENCMD_USHFRCOEN )
895
912
/* Happy Gecko workaround: disable LEM GATE mode if using ISOC endpoints. */
896
913
if ( transferType == USB_EPTYPE_ISOC )
@@ -900,7 +917,7 @@ int USBD_AddEndpoint(int epAddr, int transferType,
900
917
#endif
901
918
902
919
int ret = USBDHAL_ReconfigureFifos (totalRxFifoSize , totalTxFifoSize );
903
- INT_Enable ();
920
+ CORE_EXIT_CRITICAL ();
904
921
905
922
if ( ret != USB_STATUS_OK ) {
906
923
return ret ;
0 commit comments