17
17
#include " USBPhyHw.h"
18
18
19
19
#include " platform/mbed_critical.h"
20
+ #include " platform/mbed_assert.h"
20
21
21
22
#include " nrf_clock.h"
22
23
@@ -66,6 +67,7 @@ void USBD_HAL_IRQHandler(void);
66
67
static USBPhyHw *instance = 0 ;
67
68
68
69
static volatile bool virtual_status_xfer_event;
70
+ static volatile bool irq_already_pending;
69
71
70
72
static void usbd_event_handler (nrf_drv_usbd_evt_t const * const p_event);
71
73
static void power_usb_event_handler (nrf_drv_power_usb_evt_t event);
@@ -104,17 +106,18 @@ void USBPhyHw::init(USBPhyEvents *events) {
104
106
105
107
// Initialize power module to track USB Power events
106
108
ret = nrf_drv_power_init (NULL );
107
- APP_ERROR_CHECK (ret);
109
+ MBED_ASSERT (ret == NRF_SUCCESS);
110
+
108
111
109
112
// Register callback for USB Power events
110
113
static const nrf_drv_power_usbevt_config_t config = { .handler =
111
114
power_usb_event_handler };
112
115
ret = nrf_drv_power_usbevt_init (&config);
113
- APP_ERROR_CHECK (ret);
116
+ MBED_ASSERT (ret == NRF_SUCCESS );
114
117
115
118
// Initialize USB Device driver
116
119
ret = nrf_drv_usbd_init (usbd_event_handler);
117
- APP_ERROR_CHECK (ret);
120
+ MBED_ASSERT (ret == NRF_SUCCESS );
118
121
119
122
/* Configure selected size of the packed on EP0 */
120
123
nrf_drv_usbd_ep_max_packet_size_set (NRF_DRV_USBD_EPOUT0, MAX_PACKET_SIZE_SETUP);
@@ -124,6 +127,7 @@ void USBPhyHw::init(USBPhyEvents *events) {
124
127
instance = this ;
125
128
126
129
virtual_status_xfer_event = false ;
130
+ irq_already_pending = false ;
127
131
128
132
/*
129
133
* TODO - Configure ISOIN endpoint to respond with ZLP when
@@ -146,7 +150,7 @@ void USBPhyHw::deinit() {
146
150
disconnect ();
147
151
// Disable the USB Device driver
148
152
ret_code_t ret = nrf_drv_usbd_uninit ();
149
- APP_ERROR_CHECK (ret);
153
+ MBED_ASSERT (ret == NRF_SUCCESS );
150
154
// NVIC_DisableIRQ(USBD_IRQn); // This is handled by the Nordic driver
151
155
152
156
// Disable the power peripheral driver
@@ -300,6 +304,8 @@ void USBPhyHw::ep0_read(uint8_t *data, uint32_t size) {
300
304
301
305
virtual_status_xfer_event = true ;
302
306
307
+ irq_already_pending = NVIC_GetPendingIRQ (USBD_IRQn);
308
+
303
309
// Trigger an interrupt to process the virtual status event
304
310
NVIC_SetPendingIRQ (USBD_IRQn);
305
311
@@ -314,7 +320,8 @@ void USBPhyHw::ep0_read(uint8_t *data, uint32_t size) {
314
320
315
321
nrf_drv_usbd_setup_data_clear (); // tell the hardware to receive another OUT packet
316
322
317
- nrf_drv_usbd_ep_transfer (NRF_DRV_USBD_EPOUT0, transfer);
323
+ ret_code_t ret = nrf_drv_usbd_ep_transfer (NRF_DRV_USBD_EPOUT0, transfer);
324
+ MBED_ASSERT (ret == NRF_SUCCESS);
318
325
}
319
326
320
327
uint32_t USBPhyHw::ep0_read_result () {
@@ -342,6 +349,8 @@ void USBPhyHw::ep0_write(uint8_t *buffer, uint32_t size) {
342
349
343
350
virtual_status_xfer_event = true ;
344
351
352
+ irq_already_pending = NVIC_GetPendingIRQ (USBD_IRQn);
353
+
345
354
// Trigger an interrupt to process the virtual status event
346
355
NVIC_SetPendingIRQ (USBD_IRQn);
347
356
@@ -357,7 +366,8 @@ void USBPhyHw::ep0_write(uint8_t *buffer, uint32_t size) {
357
366
if (size == 0 )
358
367
transfer->flags |= NRF_DRV_USBD_TRANSFER_ZLP_FLAG;
359
368
360
- nrf_drv_usbd_ep_transfer (NRF_DRV_USBD_EPIN0, transfer);
369
+ ret_code_t ret = nrf_drv_usbd_ep_transfer (NRF_DRV_USBD_EPIN0, transfer);
370
+ MBED_ASSERT (ret == NRF_SUCCESS);
361
371
}
362
372
363
373
void USBPhyHw::ep0_stall () {
@@ -637,7 +647,11 @@ void USBD_HAL_IRQHandler(void)
637
647
}
638
648
639
649
virtual_status_xfer_event = false ;
640
- return ;
650
+
651
+ if (!irq_already_pending)
652
+ return ;
653
+
654
+ irq_already_pending = false ;
641
655
}
642
656
// Call Nordic driver IRQ handler
643
657
USBD_IRQHandler ();
0 commit comments