Skip to content

Commit 42d31ab

Browse files
AGlass0fMilkArto Kinnunen
authored andcommitted
Added in flag for preventing double IRQ triggering and added asserts for Nordic driver calls
1 parent a33ecbf commit 42d31ab

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhy_Nordic.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "USBPhyHw.h"
1818

1919
#include "platform/mbed_critical.h"
20+
#include "platform/mbed_assert.h"
2021

2122
#include "nrf_clock.h"
2223

@@ -66,6 +67,7 @@ void USBD_HAL_IRQHandler(void);
6667
static USBPhyHw *instance = 0;
6768

6869
static volatile bool virtual_status_xfer_event;
70+
static volatile bool irq_already_pending;
6971

7072
static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event);
7173
static void power_usb_event_handler(nrf_drv_power_usb_evt_t event);
@@ -104,17 +106,18 @@ void USBPhyHw::init(USBPhyEvents *events) {
104106

105107
// Initialize power module to track USB Power events
106108
ret = nrf_drv_power_init(NULL);
107-
APP_ERROR_CHECK(ret);
109+
MBED_ASSERT(ret == NRF_SUCCESS);
110+
108111

109112
// Register callback for USB Power events
110113
static const nrf_drv_power_usbevt_config_t config = { .handler =
111114
power_usb_event_handler };
112115
ret = nrf_drv_power_usbevt_init(&config);
113-
APP_ERROR_CHECK(ret);
116+
MBED_ASSERT(ret == NRF_SUCCESS);
114117

115118
// Initialize USB Device driver
116119
ret = nrf_drv_usbd_init(usbd_event_handler);
117-
APP_ERROR_CHECK(ret);
120+
MBED_ASSERT(ret == NRF_SUCCESS);
118121

119122
/* Configure selected size of the packed on EP0 */
120123
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) {
124127
instance = this;
125128

126129
virtual_status_xfer_event = false;
130+
irq_already_pending = false;
127131

128132
/*
129133
* TODO - Configure ISOIN endpoint to respond with ZLP when
@@ -146,7 +150,7 @@ void USBPhyHw::deinit() {
146150
disconnect();
147151
// Disable the USB Device driver
148152
ret_code_t ret = nrf_drv_usbd_uninit();
149-
APP_ERROR_CHECK(ret);
153+
MBED_ASSERT(ret == NRF_SUCCESS);
150154
//NVIC_DisableIRQ(USBD_IRQn); // This is handled by the Nordic driver
151155

152156
// Disable the power peripheral driver
@@ -300,6 +304,8 @@ void USBPhyHw::ep0_read(uint8_t *data, uint32_t size) {
300304

301305
virtual_status_xfer_event = true;
302306

307+
irq_already_pending = NVIC_GetPendingIRQ(USBD_IRQn);
308+
303309
// Trigger an interrupt to process the virtual status event
304310
NVIC_SetPendingIRQ(USBD_IRQn);
305311

@@ -314,7 +320,8 @@ void USBPhyHw::ep0_read(uint8_t *data, uint32_t size) {
314320

315321
nrf_drv_usbd_setup_data_clear(); // tell the hardware to receive another OUT packet
316322

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);
318325
}
319326

320327
uint32_t USBPhyHw::ep0_read_result() {
@@ -342,6 +349,8 @@ void USBPhyHw::ep0_write(uint8_t *buffer, uint32_t size) {
342349

343350
virtual_status_xfer_event = true;
344351

352+
irq_already_pending = NVIC_GetPendingIRQ(USBD_IRQn);
353+
345354
// Trigger an interrupt to process the virtual status event
346355
NVIC_SetPendingIRQ(USBD_IRQn);
347356

@@ -357,7 +366,8 @@ void USBPhyHw::ep0_write(uint8_t *buffer, uint32_t size) {
357366
if(size == 0)
358367
transfer->flags |= NRF_DRV_USBD_TRANSFER_ZLP_FLAG;
359368

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);
361371
}
362372

363373
void USBPhyHw::ep0_stall() {
@@ -637,7 +647,11 @@ void USBD_HAL_IRQHandler(void)
637647
}
638648

639649
virtual_status_xfer_event = false;
640-
return;
650+
651+
if(!irq_already_pending)
652+
return;
653+
654+
irq_already_pending = false;
641655
}
642656
// Call Nordic driver IRQ handler
643657
USBD_IRQHandler();

0 commit comments

Comments
 (0)