refactor nrf5x hal
This commit is contained in:
		@@ -57,13 +57,24 @@
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* MACRO TYPEDEF CONSTANT ENUM
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#define USB_NVIC_PRIO   7
 | 
			
		||||
 | 
			
		||||
// TODO switch to use nrfx_power.h in sdk15
 | 
			
		||||
enum
 | 
			
		||||
{
 | 
			
		||||
  NRFX_POWER_USB_EVT_DETECTED,
 | 
			
		||||
  NRFX_POWER_USB_EVT_REMOVED,
 | 
			
		||||
  NRFX_POWER_USB_EVT_READY
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* FUNCTION DECLARATION
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
void power_usb_event_handler(uint32_t event);
 | 
			
		||||
void tusb_hal_nrf_power_event(uint32_t event);
 | 
			
		||||
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* HFCLK helper
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
// check if SD is present and enabled
 | 
			
		||||
static bool is_sd_enabled(void)
 | 
			
		||||
@@ -77,65 +88,6 @@ static bool is_sd_enabled(void)
 | 
			
		||||
  return sd_en;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bool tusb_hal_init(void)
 | 
			
		||||
{
 | 
			
		||||
#ifdef SOFTDEVICE_PRESENT
 | 
			
		||||
  if ( is_sd_enabled() )
 | 
			
		||||
  {
 | 
			
		||||
    sd_power_usbdetected_enable(true);
 | 
			
		||||
    sd_power_usbpwrrdy_enable(true);
 | 
			
		||||
    sd_power_usbremoved_enable(true);
 | 
			
		||||
 | 
			
		||||
    // USB power may already be ready at this time -> no event generated
 | 
			
		||||
    // We need to execute the hanlder based on the status
 | 
			
		||||
    uint32_t usb_reg;
 | 
			
		||||
    sd_power_usbregstatus_get(&usb_reg);
 | 
			
		||||
 | 
			
		||||
    if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk )
 | 
			
		||||
    {
 | 
			
		||||
      power_usb_event_handler(NRF_EVT_POWER_USB_DETECTED);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk )
 | 
			
		||||
    {
 | 
			
		||||
      power_usb_event_handler(NRF_EVT_POWER_USB_POWER_READY);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 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 = (nrf_drv_power_usb_event_handler_t) power_usb_event_handler
 | 
			
		||||
    };
 | 
			
		||||
    return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) );
 | 
			
		||||
#else
 | 
			
		||||
    return true;
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tusb_hal_int_enable(uint8_t rhport)
 | 
			
		||||
{
 | 
			
		||||
  (void) rhport;
 | 
			
		||||
  NVIC_EnableIRQ(USBD_IRQn);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tusb_hal_int_disable(uint8_t rhport)
 | 
			
		||||
{
 | 
			
		||||
  (void) rhport;
 | 
			
		||||
  NVIC_DisableIRQ(USBD_IRQn);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* HFCLK helper
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
static bool hfclk_running(void)
 | 
			
		||||
{
 | 
			
		||||
#ifdef SOFTDEVICE_PRESENT
 | 
			
		||||
@@ -180,31 +132,72 @@ static void hfclk_disable(void)
 | 
			
		||||
  nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* Controller Start up Sequence (USBD 51.4 specs)
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
void power_usb_event_handler(uint32_t event)
 | 
			
		||||
{
 | 
			
		||||
  uint32_t POWER_DETECT, POWER_READY, POWER_REMOVE;
 | 
			
		||||
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* TUSB HAL
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
bool tusb_hal_init(void)
 | 
			
		||||
{
 | 
			
		||||
#ifdef SOFTDEVICE_PRESENT
 | 
			
		||||
  if ( is_sd_enabled() )
 | 
			
		||||
  {
 | 
			
		||||
    POWER_DETECT = NRF_EVT_POWER_USB_DETECTED;
 | 
			
		||||
    POWER_READY  = NRF_EVT_POWER_USB_POWER_READY;
 | 
			
		||||
    POWER_REMOVE = NRF_EVT_POWER_USB_REMOVED;
 | 
			
		||||
    sd_power_usbdetected_enable(true);
 | 
			
		||||
    sd_power_usbpwrrdy_enable(true);
 | 
			
		||||
    sd_power_usbremoved_enable(true);
 | 
			
		||||
 | 
			
		||||
    // USB power may already be ready at this time -> no event generated
 | 
			
		||||
    // We need to execute the hanlder based on the status
 | 
			
		||||
    uint32_t usb_reg;
 | 
			
		||||
    sd_power_usbregstatus_get(&usb_reg);
 | 
			
		||||
 | 
			
		||||
    if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk )
 | 
			
		||||
    {
 | 
			
		||||
      tusb_hal_nrf_power_event(NRF_EVT_POWER_USB_DETECTED);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk )
 | 
			
		||||
    {
 | 
			
		||||
      tusb_hal_nrf_power_event(NRF_EVT_POWER_USB_POWER_READY);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // tusb_hal_nrf_power_event must be called by SOC event hanlder
 | 
			
		||||
    return true;
 | 
			
		||||
  }else
 | 
			
		||||
#endif
 | 
			
		||||
  {
 | 
			
		||||
#if 0
 | 
			
		||||
    POWER_DETECT = NRF_DRV_POWER_USB_EVT_DETECTED;
 | 
			
		||||
    POWER_READY  = NRF_DRV_POWER_USB_EVT_READY;
 | 
			
		||||
    POWER_REMOVE = NRF_DRV_POWER_USB_EVT_REMOVED;
 | 
			
		||||
    // USB Power detection
 | 
			
		||||
    const nrf_drv_power_usbevt_config_t config =
 | 
			
		||||
    {
 | 
			
		||||
        .handler = (nrf_drv_power_usb_event_handler_t) tusb_hal_nrf_power_event
 | 
			
		||||
    };
 | 
			
		||||
    return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) );
 | 
			
		||||
#else
 | 
			
		||||
    return true;
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
  if ( POWER_DETECT == event )
 | 
			
		||||
void tusb_hal_int_enable(uint8_t rhport)
 | 
			
		||||
{
 | 
			
		||||
  (void) rhport;
 | 
			
		||||
  NVIC_EnableIRQ(USBD_IRQn);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tusb_hal_int_disable(uint8_t rhport)
 | 
			
		||||
{
 | 
			
		||||
  (void) rhport;
 | 
			
		||||
  NVIC_DisableIRQ(USBD_IRQn);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* Controller Start up Sequence (USBD 51.4 specs)
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
{
 | 
			
		||||
  switch ( event )
 | 
			
		||||
  {
 | 
			
		||||
    case NRFX_POWER_USB_EVT_DETECTED:
 | 
			
		||||
      if ( !NRF_USBD->ENABLE )
 | 
			
		||||
      {
 | 
			
		||||
        /* Prepare for READY event receiving */
 | 
			
		||||
@@ -250,9 +243,9 @@ void power_usb_event_handler(uint32_t event)
 | 
			
		||||
        // Enable HFCLK
 | 
			
		||||
        hfclk_enable();
 | 
			
		||||
      }
 | 
			
		||||
  }
 | 
			
		||||
  else if ( POWER_READY == event )
 | 
			
		||||
{
 | 
			
		||||
    break;
 | 
			
		||||
 | 
			
		||||
    case NRFX_POWER_USB_EVT_READY:
 | 
			
		||||
      /* Waiting for USBD peripheral enabled */
 | 
			
		||||
      while ( !(NRF_USBD_EVENTCAUSE_READY_MASK & NRF_USBD->EVENTCAUSE) ) { }
 | 
			
		||||
      nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
 | 
			
		||||
@@ -320,9 +313,9 @@ void power_usb_event_handler(uint32_t event)
 | 
			
		||||
 | 
			
		||||
      // Enable pull up
 | 
			
		||||
      nrf_usbd_pullup_enable();
 | 
			
		||||
  }
 | 
			
		||||
  else if (POWER_REMOVE == event )
 | 
			
		||||
  {
 | 
			
		||||
    break;
 | 
			
		||||
 | 
			
		||||
    case NRFX_POWER_USB_EVT_REMOVED:
 | 
			
		||||
      if ( NRF_USBD->ENABLE )
 | 
			
		||||
      {
 | 
			
		||||
        // Abort all transfers
 | 
			
		||||
@@ -339,9 +332,9 @@ void power_usb_event_handler(uint32_t event)
 | 
			
		||||
        nrf_usbd_disable();
 | 
			
		||||
        hfclk_disable();
 | 
			
		||||
      }
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    break;
 | 
			
		||||
 | 
			
		||||
    default: break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user