From f4b327e69e4d7b9e9ad652b61cda1b36f38cdfd5 Mon Sep 17 00:00:00 2001 From: George Beckstein Date: Sun, 11 Nov 2018 20:16:04 -0500 Subject: [PATCH] Changed driver to request hf and lf clock sources... not sure why USB Ready event isn't ever getting triggered --- .../TARGET_MCU_NRF52840/USBPhyHw.h | 1 - .../TARGET_MCU_NRF52840/USBPhy_Nordic.cpp | 41 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhyHw.h b/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhyHw.h index 712da93d7c..1e62ab146e 100644 --- a/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhyHw.h +++ b/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhyHw.h @@ -68,7 +68,6 @@ private: USBPhyEvents *events; bool sof_enabled; - bool connect_enabled; typedef enum usb_hw_event_type_t { USB_HW_EVENT_NONE = 0, diff --git a/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhy_Nordic.cpp b/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhy_Nordic.cpp index dcf1c0d6c4..f662d3771d 100644 --- a/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhy_Nordic.cpp +++ b/usb/device/targets/TARGET_NORDIC/TARGET_MCU_NRF52840/USBPhy_Nordic.cpp @@ -16,6 +16,8 @@ #include "USBPhyHw.h" +#include "nrf_drv_clock.h" + #define MAX_PACKET_SIZE_SETUP NRF_DRV_USBD_EPSIZE #define MAX_PACKET_NON_ISO NRF_DRV_USBD_EPSIZE #define MAX_PACKET_ISO NRF_DRV_USBD_ISOSIZE @@ -36,7 +38,6 @@ USBPhy *get_usb_phy() { USBPhyHw::USBPhyHw() : events(NULL), sof_enabled(false), - connect_enabled(false), usb_event_type(USB_HW_EVENT_NONE), usb_power_event(NRF_DRV_POWER_USB_EVT_REMOVED) { @@ -93,13 +94,32 @@ bool USBPhyHw::powered() { } void USBPhyHw::connect() { - connect_enabled = true; - //nrf_drv_usbd_start(true); + + nrf_drv_clock_init(); + nrf_drv_clock_hfclk_request(NULL); + nrf_drv_clock_lfclk_request(NULL); + + while (!(nrf_drv_clock_hfclk_is_running() && + nrf_drv_clock_lfclk_is_running())){ + /* Just waiting */ + } + + if(!nrf_drv_usbd_is_enabled()) + nrf_drv_usbd_enable(); + if(!nrf_drv_usbd_is_started()) + nrf_drv_usbd_start(true); } void USBPhyHw::disconnect() { - connect_enabled = false; - //nrf_drv_usbd_stop(); + + if(nrf_drv_usbd_is_started()) + nrf_drv_usbd_stop(); + if(nrf_drv_usbd_is_enabled()) + nrf_drv_usbd_disable(); + + nrf_drv_clock_hfclk_release(); + nrf_drv_clock_lfclk_release(); + //nrf_drv_clock_uninit(); // Should this be called? What if other peripherals are using it? } void USBPhyHw::configure() { @@ -308,24 +328,15 @@ void USBPhyHw::process() { // Process USB power-related events switch (usb_power_event) { case NRF_DRV_POWER_USB_EVT_DETECTED: - if(!nrf_drv_usbd_is_enabled()) - nrf_drv_usbd_enable(); events->power(true); break; case NRF_DRV_POWER_USB_EVT_REMOVED: events->power(false); - if(nrf_drv_usbd_is_started()) - nrf_drv_usbd_stop(); - if(nrf_drv_usbd_is_enabled()) - nrf_drv_usbd_disable(); break; case NRF_DRV_POWER_USB_EVT_READY: - if(!nrf_drv_usbd_is_started() && connect_enabled) - nrf_drv_usbd_start(true); break; default: - ASSERT(false) - ; + ASSERT(false); } }