summaryrefslogtreecommitdiff
path: root/tinyusb
diff options
context:
space:
mode:
authorhathach <[email protected]>2018-04-18 18:05:08 +0700
committerhathach <[email protected]>2018-04-18 18:05:08 +0700
commit8c0a5f7d3b093102662798fa89799d0af76d0608 (patch)
treea4709b3a121de41d3ee2a5678baefcbd7ea24cdc /tinyusb
parenta9ada8ea5ee3cc7d130c489da1a30c9e68c586b9 (diff)
update nrf52840 examples
Diffstat (limited to 'tinyusb')
-rw-r--r--tinyusb/portable/nordic/nrf5x/hal_nrf5x.c265
1 files changed, 133 insertions, 132 deletions
diff --git a/tinyusb/portable/nordic/nrf5x/hal_nrf5x.c b/tinyusb/portable/nordic/nrf5x/hal_nrf5x.c
index f77bdaa12..16ceb0bc0 100644
--- a/tinyusb/portable/nordic/nrf5x/hal_nrf5x.c
+++ b/tinyusb/portable/nordic/nrf5x/hal_nrf5x.c
@@ -60,17 +60,6 @@
#define USB_NVIC_PRIO 7
-// TODO must cover SD present but not enabled
-#ifdef SOFTDEVICE_PRESENT
-#define POWER_DETECT NRF_EVT_POWER_USB_DETECTED
-#define POWER_READY NRF_EVT_POWER_USB_POWER_READY
-#define POWER_REMOVE NRF_EVT_POWER_USB_REMOVED
-#else
-#define POWER_DETECT NRF_DRV_POWER_USB_EVT_DETECTED
-#define POWER_READY NRF_DRV_POWER_USB_EVT_READY
-#define POWER_REMOVE NRF_DRV_POWER_USB_EVT_REMOVED
-#endif
-
/*------------------------------------------------------------------*/
/* FUNCTION DECLARATION
*------------------------------------------------------------------*/
@@ -115,19 +104,16 @@ bool tusb_hal_init(void)
// power_usb_event_handler must be called by SOC event hanlder
return true;
- }
+ }else
#endif
-
-#if 0
- // USB Power detection
- const nrf_drv_power_usbevt_config_t config =
{
- .handler = power_usb_event_handler
- };
- return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) );
-#else
- return true; // TODO remove
-#endif
+ // USB Power detection
+ const nrf_drv_power_usbevt_config_t config =
+ {
+ .handler = (nrf_drv_power_usb_event_handler_t) power_usb_event_handler
+ };
+ return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) );
+ }
}
void tusb_hal_int_enable(uint8_t rhport)
@@ -195,146 +181,161 @@ static void hfclk_disable(void)
*------------------------------------------------------------------*/
void power_usb_event_handler(uint32_t event)
{
- switch ( event )
+ uint32_t POWER_DETECT, POWER_READY, POWER_REMOVE;
+
+#ifdef SOFTDEVICE_PRESENT
+ if ( is_sd_enabled() )
{
- case POWER_DETECT:
- if ( !NRF_USBD->ENABLE )
- {
- /* Prepare for READY event receiving */
- nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
+ POWER_DETECT = NRF_EVT_POWER_USB_DETECTED;
+ POWER_READY = NRF_EVT_POWER_USB_POWER_READY;
+ POWER_REMOVE = NRF_EVT_POWER_USB_REMOVED;
+ }else
+#endif
+ {
+ POWER_DETECT = NRF_DRV_POWER_USB_EVT_DETECTED;
+ POWER_READY = NRF_DRV_POWER_USB_EVT_READY;
+ POWER_REMOVE = NRF_DRV_POWER_USB_EVT_REMOVED;
+ }
- /* Enable the peripheral */
- // ERRATA 171, 187
+ if ( POWER_DETECT == event )
+ {
+ if ( !NRF_USBD->ENABLE )
+ {
+ /* Prepare for READY event receiving */
+ nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
- if (nrf_drv_usbd_errata_187())
- {
+ /* Enable the peripheral */
+ // ERRATA 171, 187
+
+ if (nrf_drv_usbd_errata_187())
+ {
// CRITICAL_REGION_ENTER();
- 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;
- }
-// CRITICAL_REGION_EXIT();
+ if (*((volatile uint32_t *)(0x4006EC00)) == 0x00000000)
+ {
+ *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
+ *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
+ *((volatile uint32_t *)(0x4006EC00)) = 0x00009375;
}
-
- if (nrf_drv_usbd_errata_171())
+ else
{
-// CRITICAL_REGION_ENTER();
- 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;
- }
-// CRITICAL_REGION_EXIT();
+ *((volatile uint32_t *)(0x4006ED14)) = 0x00000003;
}
-
- nrf_usbd_enable();
-
- // Enable HFCLK
- hfclk_enable();
+// CRITICAL_REGION_EXIT();
}
- break;
-
- case POWER_READY:
- /* Waiting for USBD peripheral enabled */
- while ( !(NRF_USBD_EVENTCAUSE_READY_MASK & NRF_USBD->EVENTCAUSE) ) { }
- nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
- nrf_usbd_event_clear(NRF_USBD_EVENT_USBEVENT);
if (nrf_drv_usbd_errata_171())
{
// CRITICAL_REGION_ENTER();
- 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 (*((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;
+ }
// CRITICAL_REGION_EXIT();
}
- if (nrf_drv_usbd_errata_187())
- {
+ nrf_usbd_enable();
+
+ // Enable HFCLK
+ hfclk_enable();
+ }
+ }
+ else if ( POWER_READY == event )
+{
+ /* Waiting for USBD peripheral enabled */
+ while ( !(NRF_USBD_EVENTCAUSE_READY_MASK & NRF_USBD->EVENTCAUSE) ) { }
+ nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
+ nrf_usbd_event_clear(NRF_USBD_EVENT_USBEVENT);
+
+ if (nrf_drv_usbd_errata_171())
+ {
// CRITICAL_REGION_ENTER();
- 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;
- }
-// CRITICAL_REGION_EXIT();
- }
+ 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 ( nrf_drv_usbd_errata_166() )
- {
- *((volatile uint32_t *) (NRF_USBD_BASE + 0x800)) = 0x7E3;
- *((volatile uint32_t *) (NRF_USBD_BASE + 0x804)) = 0x40;
+// CRITICAL_REGION_EXIT();
+ }
- __ISB(); __DSB();
- }
+ if (nrf_drv_usbd_errata_187())
+ {
+// CRITICAL_REGION_ENTER();
+ 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;
+ }
+// CRITICAL_REGION_EXIT();
+ }
- nrf_usbd_isosplit_set(NRF_USBD_ISOSPLIT_Half);
+ if ( nrf_drv_usbd_errata_166() )
+ {
+ *((volatile uint32_t *) (NRF_USBD_BASE + 0x800)) = 0x7E3;
+ *((volatile uint32_t *) (NRF_USBD_BASE + 0x804)) = 0x40;
- // Enable interrupt. SOF is used as CDC auto flush
- NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_ACCESSFAULT_Msk |
- USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk |
- USBD_INTEN_EPDATA_Msk | USBD_INTEN_SOF_Msk;
+ __ISB(); __DSB();
+ }
- // FIXME Errata 104: USB complete event is not generated (happedn randomly).
- // Requires to enable SOF to perform clean up task.
- // nrf_drv_usbd_errata_104()
+ nrf_usbd_isosplit_set(NRF_USBD_ISOSPLIT_Half);
- // Enable interrupt, Priorities 0,1,4,5 (nRF52) are reserved for SoftDevice
- NVIC_SetPriority(USBD_IRQn, USB_NVIC_PRIO);
- NVIC_ClearPendingIRQ(USBD_IRQn);
- NVIC_EnableIRQ(USBD_IRQn);
+ // Enable interrupt. SOF is used as CDC auto flush
+ NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_ACCESSFAULT_Msk |
+ USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk |
+ USBD_INTEN_EPDATA_Msk | USBD_INTEN_SOF_Msk;
- // Wait for HFCLK
- while ( !hfclk_running() ) {}
+ // FIXME Errata 104: USB complete event is not generated (happedn randomly).
+ // Requires to enable SOF to perform clean up task.
+ // nrf_drv_usbd_errata_104()
- // Enable pull up
- nrf_usbd_pullup_enable();
- break;
+ // Enable interrupt, Priorities 0,1,4,5 (nRF52) are reserved for SoftDevice
+ NVIC_SetPriority(USBD_IRQn, USB_NVIC_PRIO);
+ NVIC_ClearPendingIRQ(USBD_IRQn);
+ NVIC_EnableIRQ(USBD_IRQn);
- case POWER_REMOVE:
- if ( NRF_USBD->ENABLE )
- {
- // Abort all transfers
+ // Wait for HFCLK
+ while ( !hfclk_running() ) {}
- // Disable pull up
- nrf_usbd_pullup_disable();
+ // Enable pull up
+ nrf_usbd_pullup_enable();
+ }
+ else if (POWER_REMOVE == event )
+ {
+ if ( NRF_USBD->ENABLE )
+ {
+ // Abort all transfers
- // Disable Interrupt
- NVIC_DisableIRQ(USBD_IRQn);
+ // Disable pull up
+ nrf_usbd_pullup_disable();
- // disable all interrupt
- NRF_USBD->INTENCLR = NRF_USBD->INTEN;
+ // Disable Interrupt
+ NVIC_DisableIRQ(USBD_IRQn);
- nrf_usbd_disable();
- hfclk_disable();
- }
- break;
+ // disable all interrupt
+ NRF_USBD->INTENCLR = NRF_USBD->INTEN;
- default: break;
+ nrf_usbd_disable();
+ hfclk_disable();
+ }
+ }
+ else
+ {
}
}