getting nrf5x to work with S140 6.0.0

This commit is contained in:
hathach 2018-04-04 20:02:54 +07:00
parent 4bb1553d5c
commit b6accc622b
2 changed files with 56 additions and 35 deletions

View File

@ -46,11 +46,6 @@
#include "device/dcd.h" #include "device/dcd.h"
#ifdef SOFTDEVICE_PRESENT
#include "nrf_sdm.h"
#include "nrf_soc.h"
#endif
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* MACRO TYPEDEF CONSTANT ENUM /* MACRO TYPEDEF CONSTANT ENUM
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/

View File

@ -40,35 +40,72 @@
#include "nrf.h" #include "nrf.h"
#include "nrf_gpio.h" #include "nrf_gpio.h"
#include "nrf_clock.h" #include "nrf_clock.h"
#include "nrf_drv_power.h"
#include "nrf_usbd.h" #include "nrf_usbd.h"
#include "nrf_drv_usbd_errata.h" #include "nrf_drv_usbd_errata.h"
#ifdef SOFTDEVICE_PRESENT
#include "nrf_sdm.h"
#include "nrf_soc.h"
#else
#include "nrf_drv_power.h"
#endif
#include "tusb_hal.h" #include "tusb_hal.h"
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* MACRO TYPEDEF CONSTANT ENUM /* MACRO TYPEDEF CONSTANT ENUM
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
// TODO must cover SD present but not enabled
/*------------------------------------------------------------------*/ #ifdef SOFTDEVICE_PRESENT
/* VARIABLE DECLARATION #define POWER_DETECT NRF_EVT_POWER_USB_DETECTED
*------------------------------------------------------------------*/ #define POWER_READY NRF_EVT_POWER_USB_POWER_READY
static void power_usb_event_handler(uint32_t event); #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 /* FUNCTION DECLARATION
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
void power_usb_event_handler(uint32_t event);
// check if SD is present and enabled
static bool is_sd_enabled(void)
{
uint8_t sd_en = false;
#ifdef SOFTDEVICE_PRESENT
(void) sd_softdevice_is_enabled(&sd_en);
#endif
return sd_en;
}
bool tusb_hal_init(void) bool tusb_hal_init(void)
{ {
#ifndef SOFTDEVICE_PRESENT #ifdef SOFTDEVICE_PRESENT
if ( is_sd_enabled() )
{
sd_power_usbdetected_enable(true);
sd_power_usbpwrrdy_enable(true);
sd_power_usbremoved_enable(true);
// power_usb_event_handler must be called by soc event
return true;
}
#endif
#if 0
// USB Power detection // USB Power detection
const nrf_drv_power_usbevt_config_t config = const nrf_drv_power_usbevt_config_t config =
{ {
.handler = power_usb_event_handler .handler = power_usb_event_handler
}; };
return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) ); return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) );
#else
#endif #endif
} }
@ -84,23 +121,10 @@ void tusb_hal_int_disable(uint8_t rhport)
NVIC_DisableIRQ(USBD_IRQn); NVIC_DisableIRQ(USBD_IRQn);
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
/* Controller Start up Sequence /* HFCLK helper
*------------------------------------------------------------------*/ *------------------------------------------------------------------*/
/*------------- HFCLK helper -------------*/
static bool is_sd_enabled(void)
{
#ifdef SOFTDEVICE_PRESENT
uint8_t sd_en = 0;
(void) sd_softdevice_is_enabled(&sd_en);
return sd_en;
#else
return false;
#endif
}
static bool hfclk_running(void) static bool hfclk_running(void)
{ {
#ifdef SOFTDEVICE_PRESENT #ifdef SOFTDEVICE_PRESENT
@ -145,12 +169,14 @@ static void hfclk_disable(void)
nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP); nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP);
} }
/*------------- 51.4 specs USBD start-up sequene -------------*/ /*------------------------------------------------------------------*/
static void power_usb_event_handler(uint32_t event) /* Controller Start up Sequence (USBD 51.4 specs )
*------------------------------------------------------------------*/
void power_usb_event_handler(uint32_t event)
{ {
switch ( event ) switch ( event )
{ {
case NRF_DRV_POWER_USB_EVT_DETECTED: case POWER_DETECT:
if ( !NRF_USBD->ENABLE ) if ( !NRF_USBD->ENABLE )
{ {
/* Prepare for READY event receiving */ /* Prepare for READY event receiving */
@ -169,8 +195,8 @@ static void power_usb_event_handler(uint32_t event)
} }
break; break;
case NRF_DRV_POWER_USB_EVT_READY: case POWER_READY:
// Wait for HFCLK // Wait for HFCLK TODO move before pull up
while ( !hfclk_running() ) {} while ( !hfclk_running() ) {}
if ( nrf_drv_usbd_errata_166() ) if ( nrf_drv_usbd_errata_166() )
@ -202,7 +228,7 @@ static void power_usb_event_handler(uint32_t event)
nrf_usbd_pullup_enable(); nrf_usbd_pullup_enable();
break; break;
case NRF_DRV_POWER_USB_EVT_REMOVED: case POWER_REMOVE:
if ( NRF_USBD->ENABLE ) if ( NRF_USBD->ENABLE )
{ {
// Abort all transfers // Abort all transfers