Autoformatted with astyle

pull/10952/head
George Beckstein 2019-03-14 22:23:19 -04:00 committed by Arto Kinnunen
parent b2ae05f991
commit 974e8899e0
2 changed files with 443 additions and 401 deletions

View File

@ -48,7 +48,8 @@ static volatile bool virtual_status_xfer_event;
static void usbd_event_handler(nrf_drv_usbd_evt_t const *const p_event);
static void power_usb_event_handler(nrfx_power_usb_evt_t event);
USBPhy *get_usb_phy() {
USBPhy *get_usb_phy()
{
static USBPhyHw usbphy;
return &usbphy;
}
@ -56,15 +57,18 @@ USBPhy *get_usb_phy() {
USBPhyHw::USBPhyHw() :
events(NULL), sof_enabled(false), connect_enabled(false),
usb_event_type(USB_HW_EVENT_NONE),
usb_power_event(NRFX_POWER_USB_EVT_REMOVED) {
usb_power_event(NRFX_POWER_USB_EVT_REMOVED)
{
}
USBPhyHw::~USBPhyHw() {
USBPhyHw::~USBPhyHw()
{
}
void USBPhyHw::init(USBPhyEvents *events) {
void USBPhyHw::init(USBPhyEvents *events)
{
this->events = events;
@ -76,8 +80,10 @@ void USBPhyHw::init(USBPhyEvents *events) {
// Register callback for USB Power events
static const nrfx_power_usbevt_config_t config = { .handler =
power_usb_event_handler };
static const nrfx_power_usbevt_config_t config = {
.handler = power_usb_event_handler
};
nrfx_power_usbevt_init(&config);
// Initialize USB Device driver
@ -107,13 +113,14 @@ void USBPhyHw::init(USBPhyEvents *events) {
}
void USBPhyHw::deinit() {
void USBPhyHw::deinit()
{
// Disconnect and disable interrupt
disconnect();
// Disable the USB Device driver
ret_code_t ret = nrf_drv_usbd_uninit();
MBED_ASSERT(ret == NRF_SUCCESS);
//NVIC_DisableIRQ(USBD_IRQn); // This is handled by the Nordic driver
// Disable the power peripheral driver
nrfx_power_uninit();
@ -122,15 +129,18 @@ void USBPhyHw::deinit() {
instance = 0;
}
bool USBPhyHw::powered() {
bool USBPhyHw::powered()
{
if (nrfx_power_usbstatus_get() == NRFX_POWER_USB_STATE_CONNECTED
|| nrfx_power_usbstatus_get() == NRFX_POWER_USB_STATE_READY)
|| nrfx_power_usbstatus_get() == NRFX_POWER_USB_STATE_READY) {
return true;
else
} else {
return false;
}
}
void USBPhyHw::connect() {
void USBPhyHw::connect()
{
// To save power, we only enable the USBD peripheral
// when there's actually VBUS detected
@ -139,40 +149,47 @@ void USBPhyHw::connect() {
this->connect_enabled = true;
// If VBUS is already available, enable immediately
if(nrfx_power_usbstatus_get() == NRFX_POWER_USB_STATE_CONNECTED)
{
if (nrfx_power_usbstatus_get() == NRFX_POWER_USB_STATE_CONNECTED) {
// Enabling USB will cause NRF_DRV_POWER_USB_EVT_READY
// to occur, which will start the USBD peripheral
// when the internal regulator has settled
if(!nrf_drv_usbd_is_enabled())
if (!nrf_drv_usbd_is_enabled()) {
nrf_drv_usbd_enable();
}
if (nrfx_power_usbstatus_get() == NRFX_POWER_USB_STATE_READY
&& !nrf_drv_usbd_is_started())
&& !nrf_drv_usbd_is_started()) {
nrf_drv_usbd_start(true);
}
}
}
void USBPhyHw::disconnect() {
void USBPhyHw::disconnect()
{
this->connect_enabled = false;
if(nrf_drv_usbd_is_started())
if (nrf_drv_usbd_is_started()) {
nrf_drv_usbd_stop();
if(nrf_drv_usbd_is_enabled())
}
if (nrf_drv_usbd_is_enabled()) {
nrf_drv_usbd_disable();
}
}
void USBPhyHw::configure() {
void USBPhyHw::configure()
{
// Not needed
}
void USBPhyHw::unconfigure() {
void USBPhyHw::unconfigure()
{
// Remove all endpoints (except control, obviously)
nrf_drv_usbd_ep_default_config();
}
void USBPhyHw::sof_enable() {
void USBPhyHw::sof_enable()
{
// TODO - Enable SOF interrupt
// Can this safely be done if
// nrf_drv_usbd_start is called with SoF enabled?
@ -180,25 +197,28 @@ void USBPhyHw::sof_enable() {
sof_enabled = true;
}
void USBPhyHw::sof_disable() {
void USBPhyHw::sof_disable()
{
// TODO - Disable SOF interrupt
// Can this safely be done if
// nrf_drv_usbd_start is called with SoF enabled?
sof_enabled = false;
}
void USBPhyHw::set_address(uint8_t address) {
void USBPhyHw::set_address(uint8_t address)
{
// nothing to do, handled by hardware; but don't STALL
}
void USBPhyHw::remote_wakeup() {
void USBPhyHw::remote_wakeup()
{
// Not supported(?)
}
const usb_ep_table_t *USBPhyHw::endpoint_table() {
static const usb_ep_table_t template_table =
const usb_ep_table_t *USBPhyHw::endpoint_table()
{
static const usb_ep_table_t template_table = {
1536, // 64 bytes per bulk/int endpoint pair (8), 1023 bytes for iso endpoint pair (1)
{
{ USB_EP_ATTR_ALLOW_CTRL | USB_EP_ATTR_DIR_IN_AND_OUT, 0, 0 },
@ -222,11 +242,13 @@ const usb_ep_table_t *USBPhyHw::endpoint_table() {
return &template_table;
}
uint32_t USBPhyHw::ep0_set_max_packet(uint32_t max_packet) {
uint32_t USBPhyHw::ep0_set_max_packet(uint32_t max_packet)
{
disable_usb_interrupts();
if (max_packet > MAX_PACKET_SIZE_SETUP)
if (max_packet > MAX_PACKET_SIZE_SETUP) {
max_packet = MAX_PACKET_SIZE_SETUP;
}
nrf_drv_usbd_ep_max_packet_size_set(NRF_DRV_USBD_EPOUT0, max_packet);
nrf_drv_usbd_ep_max_packet_size_set(NRF_DRV_USBD_EPIN0, max_packet);
@ -237,7 +259,8 @@ uint32_t USBPhyHw::ep0_set_max_packet(uint32_t max_packet) {
}
// read setup packet
void USBPhyHw::ep0_setup_read_result(uint8_t *buffer, uint32_t size) {
void USBPhyHw::ep0_setup_read_result(uint8_t *buffer, uint32_t size)
{
disable_usb_interrupts();
@ -249,21 +272,21 @@ void USBPhyHw::ep0_setup_read_result(uint8_t *buffer, uint32_t size) {
enable_usb_interrupts();
}
void USBPhyHw::ep0_read(uint8_t *data, uint32_t size) {
void USBPhyHw::ep0_read(uint8_t *data, uint32_t size)
{
// Check for status stage
if(data == NULL && size == 0)
{
if (data == NULL && size == 0) {
// If the data stage transfer direction was OUT
if(setup_buf.bmRequestType & SETUP_TRANSFER_DIR_MASK)
{
if (setup_buf.bmRequestType & SETUP_TRANSFER_DIR_MASK) {
// This is the status stage -- trigger the status task and notify the Mbed stack
// Don't trigger status stage unless endpoint is not busy!
// (Causes an undocumented hardware-initiated stall on the control endpoint)
if(nrf_drv_usbd_ep_is_busy(NRF_DRV_USBD_EPIN0))
if (nrf_drv_usbd_ep_is_busy(NRF_DRV_USBD_EPIN0)) {
nrf_usbd_shorts_enable(NRF_USBD_SHORT_EP0DATADONE_EP0STATUS_MASK);
else
} else {
nrf_usbd_task_trigger(NRF_USBD_TASK_EP0STATUS);
}
virtual_status_xfer_event = true;
@ -285,28 +308,29 @@ void USBPhyHw::ep0_read(uint8_t *data, uint32_t size) {
MBED_ASSERT(ret == NRF_SUCCESS);
}
uint32_t USBPhyHw::ep0_read_result() {
uint32_t USBPhyHw::ep0_read_result()
{
return nrf_drv_usbd_epout_size_get(NRF_DRV_USBD_EPOUT0);
}
void USBPhyHw::ep0_write(uint8_t *buffer, uint32_t size) {
void USBPhyHw::ep0_write(uint8_t *buffer, uint32_t size)
{
// Check for status stage
if(buffer == NULL && size == 0)
{
if (buffer == NULL && size == 0) {
// If the requested size was 0 OR the data stage transfer direction was OUT
if (setup_buf.wLength == 0
|| ((setup_buf.bmRequestType & SETUP_TRANSFER_DIR_MASK) == 0))
{
|| ((setup_buf.bmRequestType & SETUP_TRANSFER_DIR_MASK) == 0)) {
// This is the status stage -- trigger the status task and notify the Mbed stack
// Don't trigger status stage unless endpoint is not busy!
// (Causes an undocumented hardware-initiated stall on the control endpoint)
if(nrf_drv_usbd_ep_is_busy(NRF_DRV_USBD_EPOUT0))
if (nrf_drv_usbd_ep_is_busy(NRF_DRV_USBD_EPOUT0)) {
nrf_usbd_shorts_enable(NRF_USBD_SHORT_EP0DATADONE_EP0STATUS_MASK);
else
} else {
nrf_usbd_task_trigger(NRF_USBD_TASK_EP0STATUS);
}
virtual_status_xfer_event = true;
@ -322,39 +346,46 @@ void USBPhyHw::ep0_write(uint8_t *buffer, uint32_t size) {
transfer->p_data.tx = buffer;
transfer->size = size;
if(size == 0)
if (size == 0) {
transfer->flags |= NRF_DRV_USBD_TRANSFER_ZLP_FLAG;
}
ret_code_t ret = nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, transfer);
MBED_ASSERT(ret == NRF_SUCCESS);
}
void USBPhyHw::ep0_stall() {
void USBPhyHw::ep0_stall()
{
// Note: This stall must be automatically cleared by the next setup packet
nrf_drv_usbd_setup_stall();
}
bool USBPhyHw::endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type) {
bool USBPhyHw::endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type)
{
nrf_drv_usbd_ep_t nrf_ep = get_nordic_endpoint(endpoint);
nrf_drv_usbd_ep_enable(nrf_ep);
nrf_drv_usbd_ep_max_packet_size_set(nrf_ep, max_packet);
return nrf_drv_usbd_ep_enable_check(nrf_ep);
}
void USBPhyHw::endpoint_remove(usb_ep_t endpoint) {
void USBPhyHw::endpoint_remove(usb_ep_t endpoint)
{
nrf_drv_usbd_ep_t nrf_ep = get_nordic_endpoint(endpoint);
// Reset data toggle for bulk/interrupt endpoints
if(nrf_ep != NRF_DRV_USBD_EPOUT8 && nrf_ep != NRF_DRV_USBD_EPIN8)
if (nrf_ep != NRF_DRV_USBD_EPOUT8 && nrf_ep != NRF_DRV_USBD_EPIN8) {
nrf_drv_usbd_ep_dtoggle_clear(nrf_ep);
}
nrf_drv_usbd_ep_disable(nrf_ep);
}
void USBPhyHw::endpoint_stall(usb_ep_t endpoint) {
void USBPhyHw::endpoint_stall(usb_ep_t endpoint)
{
nrf_drv_usbd_ep_stall(get_nordic_endpoint(endpoint));
}
void USBPhyHw::endpoint_unstall(usb_ep_t endpoint) {
void USBPhyHw::endpoint_unstall(usb_ep_t endpoint)
{
nrf_drv_usbd_ep_t ep = get_nordic_endpoint(endpoint);
nrf_drv_usbd_ep_stall_clear(ep);
@ -368,7 +399,8 @@ void USBPhyHw::endpoint_unstall(usb_ep_t endpoint) {
nrf_drv_usbd_ep_enable(ep);
}
bool USBPhyHw::endpoint_read(usb_ep_t endpoint, uint8_t *data, uint32_t size) {
bool USBPhyHw::endpoint_read(usb_ep_t endpoint, uint8_t *data, uint32_t size)
{
nrf_drv_usbd_transfer_t *transfer = get_transfer_buffer(endpoint);
memset(transfer, 0, sizeof(nrf_drv_usbd_transfer_t));
transfer->p_data.rx = data;
@ -378,11 +410,13 @@ bool USBPhyHw::endpoint_read(usb_ep_t endpoint, uint8_t *data, uint32_t size) {
return (ret == NRF_SUCCESS);
}
uint32_t USBPhyHw::endpoint_read_result(usb_ep_t endpoint) {
uint32_t USBPhyHw::endpoint_read_result(usb_ep_t endpoint)
{
return nrf_drv_usbd_epout_size_get(get_nordic_endpoint(endpoint));
}
bool USBPhyHw::endpoint_write(usb_ep_t endpoint, uint8_t *data, uint32_t size) {
bool USBPhyHw::endpoint_write(usb_ep_t endpoint, uint8_t *data, uint32_t size)
{
nrf_drv_usbd_transfer_t *transfer = get_transfer_buffer(endpoint);
memset(transfer, 0, sizeof(nrf_drv_usbd_transfer_t));
transfer->p_data.tx = data;
@ -390,18 +424,21 @@ bool USBPhyHw::endpoint_write(usb_ep_t endpoint, uint8_t *data, uint32_t size) {
// If this is a zero-length-packet (ZLP)
// Set the ZLP flag
if(size == 0)
if (size == 0) {
transfer->flags |= NRF_DRV_USBD_TRANSFER_ZLP_FLAG;
}
ret_code_t ret = nrf_drv_usbd_ep_transfer(get_nordic_endpoint(endpoint), transfer);
return (ret == NRF_SUCCESS);
}
void USBPhyHw::endpoint_abort(usb_ep_t endpoint) {
void USBPhyHw::endpoint_abort(usb_ep_t endpoint)
{
nrf_drv_usbd_ep_abort(get_nordic_endpoint(endpoint));
}
void USBPhyHw::process() {
void USBPhyHw::process()
{
if (usb_event_type == USB_HW_EVENT_USBD) {
@ -420,30 +457,28 @@ void USBPhyHw::process() {
events->reset();
break;
case NRF_DRV_USBD_EVT_SOF:
if(sof_enabled)
if (sof_enabled) {
events->sof(usb_event.data.sof.framecnt);
}
break;
case NRF_DRV_USBD_EVT_EPTRANSFER:
if(usb_event.data.eptransfer.status == NRF_USBD_EP_OK)
{
if(!nrf_drv_usbd_ep_stall_check(usb_event.data.eptransfer.ep))
{
if(IS_IN_EP(usb_event.data.eptransfer.ep))
{
if((usb_event.data.eptransfer.ep & 0x7F) == 0)
if (usb_event.data.eptransfer.status == NRF_USBD_EP_OK) {
if (!nrf_drv_usbd_ep_stall_check(usb_event.data.eptransfer.ep)) {
if (IS_IN_EP(usb_event.data.eptransfer.ep)) {
if ((usb_event.data.eptransfer.ep & 0x7F) == 0) {
events->ep0_in();
else
} else {
events->in((usb_ep_t) usb_event.data.eptransfer.ep);
}
else
{
if((usb_event.data.eptransfer.ep & 0x7F) == 0)
} else {
if ((usb_event.data.eptransfer.ep & 0x7F) == 0) {
events->ep0_out();
else
} else {
events->out((usb_ep_t) usb_event.data.eptransfer.ep);
}
}
}
}
break;
case NRF_DRV_USBD_EVT_SETUP: {
nrf_drv_usbd_ep_stall_clear(NRF_DRV_USBD_EPIN0);
@ -460,15 +495,14 @@ void USBPhyHw::process() {
break;
}
}
else if (usb_event_type == USB_HW_EVENT_POWER)
{
} else if (usb_event_type == USB_HW_EVENT_POWER) {
// Process USB power-related events
switch (usb_power_event) {
case NRFX_POWER_USB_EVT_DETECTED:
if (this->connect_enabled) {
if(!nrf_drv_usbd_is_enabled())
if (!nrf_drv_usbd_is_enabled()) {
nrf_drv_usbd_enable();
}
events->power(true);
}
break;
@ -476,21 +510,21 @@ void USBPhyHw::process() {
events->power(false);
break;
case NRFX_POWER_USB_EVT_READY:
if(!nrf_drv_usbd_is_started())
if (!nrf_drv_usbd_is_started()) {
nrf_drv_usbd_start(true);
}
break;
default:
ASSERT(false);
}
}
else if (usb_event_type == USB_HW_EVENT_VIRTUAL_STATUS)
{
} else if (usb_event_type == USB_HW_EVENT_VIRTUAL_STATUS) {
// Notify Mbed stack of status stage transfer completion
if(setup_buf.bmRequestType & SETUP_TRANSFER_DIR_MASK) // DATA IN transfer, Status OUT transfer
if (setup_buf.bmRequestType & SETUP_TRANSFER_DIR_MASK) { // DATA IN transfer, Status OUT transfer
events->ep0_out();
else // DATA OUT transfer, Status IN transfer
} else { // DATA OUT transfer, Status IN transfer
events->ep0_in();
}
}
// Unflag the event type
usb_event_type = USB_HW_EVENT_NONE;
@ -500,7 +534,8 @@ void USBPhyHw::process() {
}
void USBPhyHw::_usb_event_handler(
nrf_drv_usbd_evt_t const * const p_event) {
nrf_drv_usbd_evt_t const *const p_event)
{
disable_usb_interrupts();
// Copy the event data into internal memory
memcpy(&instance->usb_event, p_event, sizeof(instance->usb_event));
@ -509,7 +544,8 @@ void USBPhyHw::_usb_event_handler(
instance->events->start_process();
}
void USBPhyHw::_usb_power_event_handler(nrfx_power_usb_evt_t event) {
void USBPhyHw::_usb_power_event_handler(nrfx_power_usb_evt_t event)
{
disable_usb_interrupts();
// Copy the event data into internal memory
instance->usb_power_event = event;
@ -518,7 +554,8 @@ void USBPhyHw::_usb_power_event_handler(nrfx_power_usb_evt_t event) {
instance->events->start_process();
}
void USBPhyHw::_usb_virtual_status_event_handler(void) {
void USBPhyHw::_usb_virtual_status_event_handler(void)
{
disable_usb_interrupts();
// Tell the upper layers of the stack to process the event
@ -526,12 +563,14 @@ void USBPhyHw::_usb_virtual_status_event_handler(void) {
instance->events->start_process();
}
nrf_drv_usbd_transfer_t* USBPhyHw::get_transfer_buffer(usb_ep_t endpoint) {
nrf_drv_usbd_transfer_t *USBPhyHw::get_transfer_buffer(usb_ep_t endpoint)
{
// Index is base endpoint number * 2 (output), add 1 for input endpoints
return &transfer_buf[(((endpoint & 0x7F) << 1) + ((endpoint & 0x80) >> 7))];
}
nrf_drv_usbd_ep_t USBPhyHw::get_nordic_endpoint(usb_ep_t endpoint) {
nrf_drv_usbd_ep_t USBPhyHw::get_nordic_endpoint(usb_ep_t endpoint)
{
// Clear the most-significant-bit (input endpoint flag)
return (nrf_drv_usbd_ep_t) endpoint;
}
@ -550,26 +589,30 @@ void USBPhyHw::_reset(void)
nrf_usbd_event_clear((nrf_usbd_event_t)0x01FFFFFF);
}
void USBPhyHw::enable_usb_interrupts(void) {
void USBPhyHw::enable_usb_interrupts(void)
{
// Enable USB and USB-related power interrupts
NRFX_IRQ_ENABLE(USBD_IRQn);
nrfx_power_usbevt_enable();
}
void USBPhyHw::disable_usb_interrupts(void) {
void USBPhyHw::disable_usb_interrupts(void)
{
// Disable USB and USB-related power interrupts
NRFX_IRQ_DISABLE(USBD_IRQn);
nrfx_power_usbevt_disable();
}
static void power_usb_event_handler(nrfx_power_usb_evt_t event) {
static void power_usb_event_handler(nrfx_power_usb_evt_t event)
{
if (instance) {
// Pass the event on to the USBPhyHW instance
instance->_usb_power_event_handler(event);
}
}
static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) {
static void usbd_event_handler(nrf_drv_usbd_evt_t const *const p_event)
{
if (instance) {
// Pass the event on to the USBPhyHW instance
instance->_usb_event_handler(p_event);
@ -579,8 +622,7 @@ static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event) {
void USBD_HAL_IRQHandler(void)
{
// Process the virtual status stage transfer event
if(virtual_status_xfer_event)
{
if (virtual_status_xfer_event) {
if (instance) {
instance->_usb_virtual_status_event_handler();
}