Skip to content

Commit

Permalink
improve nrf52 port
Browse files Browse the repository at this point in the history
  • Loading branch information
hathach committed Apr 5, 2018
1 parent b6accc6 commit b9fc708
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions tinyusb/portable/nordic/nrf5x/dcd_nrf5x.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void bus_reset(void)
bool dcd_init (uint8_t rhport)
{
(void) rhport;
return true;
}

void dcd_connect (uint8_t rhport)
Expand Down
36 changes: 26 additions & 10 deletions tinyusb/portable/nordic/nrf5x/hal_nrf5x.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,22 @@ bool tusb_hal_init(void)
sd_power_usbpwrrdy_enable(true);
sd_power_usbremoved_enable(true);

// power_usb_event_handler must be called by soc event

// 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(POWER_DETECT);
}

if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk )
{
power_usb_event_handler(POWER_READY);
}

// power_usb_event_handler must be called by SOC event hanlder
return true;
}
#endif
Expand All @@ -106,6 +120,8 @@ bool tusb_hal_init(void)
.handler = power_usb_event_handler
};
return ( NRF_SUCCESS == nrf_drv_power_usbevt_init(&config) );
#else
return true; // TODO remove
#endif
}

Expand Down Expand Up @@ -170,7 +186,7 @@ static void hfclk_disable(void)
}

/*------------------------------------------------------------------*/
/* Controller Start up Sequence (USBD 51.4 specs )
/* Controller Start up Sequence (USBD 51.4 specs)
*------------------------------------------------------------------*/
void power_usb_event_handler(uint32_t event)
{
Expand All @@ -187,17 +203,14 @@ void power_usb_event_handler(uint32_t event)

// Enable HFCLK
hfclk_enable();

/* Waiting for peripheral to enable, this should take a few us */
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);
}
break;

case POWER_READY:
// Wait for HFCLK TODO move before pull up
while ( !hfclk_running() ) {}
/* 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_166() )
{
Expand All @@ -224,6 +237,9 @@ void power_usb_event_handler(uint32_t event)
NVIC_ClearPendingIRQ(USBD_IRQn);
NVIC_EnableIRQ(USBD_IRQn);

// Wait for HFCLK
while ( !hfclk_running() ) {}

// Enable pull up
nrf_usbd_pullup_enable();
break;
Expand Down

0 comments on commit b9fc708

Please sign in to comment.