rename subfolder source to src
This commit is contained in:
@@ -0,0 +1,538 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file dcd_nrf5x.c
|
||||
@author hathach
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, hathach (tinyusb.org)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if MODE_DEVICE_SUPPORTED && CFG_TUSB_MCU == OPT_MCU_NRF5X
|
||||
|
||||
// TODO remove
|
||||
#include "nrf.h"
|
||||
#include "nrf_power.h"
|
||||
#include "nrf_usbd.h"
|
||||
#include "nrf_clock.h"
|
||||
|
||||
#include "nrf_drv_usbd_errata.h"
|
||||
|
||||
#include "device/dcd.h"
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* MACRO TYPEDEF CONSTANT ENUM
|
||||
*------------------------------------------------------------------*/
|
||||
enum
|
||||
{
|
||||
// Max allowed by USB specs
|
||||
MAX_PACKET_SIZE = 64,
|
||||
|
||||
// Mask of all END event (IN & OUT) for all endpoints. ENDEPIN0-7, ENDEPOUT0-7, ENDISOIN, ENDISOOUT
|
||||
EDPT_END_ALL_MASK = 0x1FFBFCUL
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* VARIABLE DECLARATION
|
||||
*------------------------------------------------------------------*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t* buffer;
|
||||
uint16_t total_len;
|
||||
uint16_t actual_len;
|
||||
uint8_t mps; // max packet size
|
||||
|
||||
// FIXME nrf52840 does not NAK OUT packet properly
|
||||
bool data_received;
|
||||
|
||||
} nom_xfer_t;
|
||||
|
||||
/*static*/ struct
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint8_t* buffer;
|
||||
uint16_t total_len;
|
||||
uint16_t actual_len;
|
||||
|
||||
uint8_t dir;
|
||||
}control;
|
||||
|
||||
// Non control: 7 endpoints IN & OUT (offset 1)
|
||||
nom_xfer_t xfer[7][2];
|
||||
|
||||
volatile bool dma_running;
|
||||
}_dcd;
|
||||
|
||||
void bus_reset(void)
|
||||
{
|
||||
for(int i=0; i<8; i++)
|
||||
{
|
||||
NRF_USBD->TASKS_STARTEPIN[i] = 0;
|
||||
NRF_USBD->TASKS_STARTEPOUT[i] = 0;
|
||||
}
|
||||
|
||||
NRF_USBD->TASKS_STARTISOIN = 0;
|
||||
NRF_USBD->TASKS_STARTISOOUT = 0;
|
||||
|
||||
varclr_(&_dcd);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Controller API
|
||||
*------------------------------------------------------------------*/
|
||||
bool dcd_init (uint8_t rhport)
|
||||
{
|
||||
(void) rhport;
|
||||
return true;
|
||||
}
|
||||
|
||||
void dcd_connect (uint8_t rhport)
|
||||
{
|
||||
|
||||
}
|
||||
void dcd_disconnect (uint8_t rhport)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void dcd_set_address (uint8_t rhport, uint8_t dev_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
// Set Address is automatically update by hw controller
|
||||
}
|
||||
|
||||
void dcd_set_config (uint8_t rhport, uint8_t config_num)
|
||||
{
|
||||
(void) rhport;
|
||||
(void) config_num;
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Control
|
||||
*------------------------------------------------------------------*/
|
||||
static void edpt_dma_start(uint8_t epnum, uint8_t dir)
|
||||
{
|
||||
// Only one dma could be active, TODO resolve when this is called in ISR and dma is running
|
||||
while ( _dcd.dma_running )
|
||||
{
|
||||
TU_ASSERT ( 0 == (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk), );
|
||||
}
|
||||
|
||||
_dcd.dma_running = true;
|
||||
|
||||
if ( dir == TUSB_DIR_OUT )
|
||||
{
|
||||
NRF_USBD->TASKS_STARTEPOUT[epnum] = 1;
|
||||
} else
|
||||
{
|
||||
NRF_USBD->TASKS_STARTEPIN[epnum] = 1;
|
||||
}
|
||||
|
||||
__ISB(); __DSB();
|
||||
}
|
||||
|
||||
static void edpt_dma_end(void)
|
||||
{
|
||||
_dcd.dma_running = false;
|
||||
}
|
||||
|
||||
static void xact_control_start(void)
|
||||
{
|
||||
// Each transaction is up to 64 bytes
|
||||
uint8_t const xact_len = min16_of(_dcd.control.total_len-_dcd.control.actual_len, MAX_PACKET_SIZE);
|
||||
|
||||
if ( _dcd.control.dir == TUSB_DIR_OUT )
|
||||
{
|
||||
// TODO control out
|
||||
NRF_USBD->EPOUT[0].PTR = (uint32_t) _dcd.control.buffer;
|
||||
NRF_USBD->EPOUT[0].MAXCNT = xact_len;
|
||||
|
||||
NRF_USBD->TASKS_EP0RCVOUT = 1;
|
||||
__ISB(); __DSB();
|
||||
}else
|
||||
{
|
||||
NRF_USBD->EPIN[0].PTR = (uint32_t) _dcd.control.buffer;
|
||||
NRF_USBD->EPIN[0].MAXCNT = xact_len;
|
||||
|
||||
edpt_dma_start(0, TUSB_DIR_IN);
|
||||
}
|
||||
|
||||
_dcd.control.buffer += xact_len;
|
||||
_dcd.control.actual_len += xact_len;
|
||||
}
|
||||
|
||||
bool dcd_control_xfer (uint8_t rhport, tusb_dir_t dir, uint8_t * buffer, uint16_t length)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
if ( length )
|
||||
{
|
||||
// Data Phase
|
||||
_dcd.control.total_len = length;
|
||||
_dcd.control.actual_len = 0;
|
||||
_dcd.control.buffer = buffer;
|
||||
_dcd.control.dir = (uint8_t) dir;
|
||||
|
||||
xact_control_start();
|
||||
}else
|
||||
{
|
||||
// Status Phase
|
||||
NRF_USBD->TASKS_EP0STATUS = 1;
|
||||
__ISB(); __DSB();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
static inline nom_xfer_t* get_td(uint8_t epnum, uint8_t dir)
|
||||
{
|
||||
return &_dcd.xfer[epnum-1][dir];
|
||||
}
|
||||
|
||||
/*------------- Bulk/Int OUT transfer -------------*/
|
||||
|
||||
/**
|
||||
* Prepare Bulk/Int out transaction, Endpoint start to accept/ACK Data
|
||||
* @param epnum
|
||||
*/
|
||||
static void xact_out_prepare(uint8_t epnum)
|
||||
{
|
||||
// Write any value to size will allow hw to ACK (accept data)
|
||||
NRF_USBD->SIZE.EPOUT[epnum] = 0;
|
||||
__ISB(); __DSB();
|
||||
}
|
||||
|
||||
static void xact_out_dma(uint8_t epnum)
|
||||
{
|
||||
nom_xfer_t* xfer = get_td(epnum, TUSB_DIR_OUT);
|
||||
|
||||
uint8_t const xact_len = NRF_USBD->SIZE.EPOUT[epnum];
|
||||
|
||||
// Trigger DMA move data from Endpoint -> SRAM
|
||||
NRF_USBD->EPOUT[epnum].PTR = (uint32_t) xfer->buffer;
|
||||
NRF_USBD->EPOUT[epnum].MAXCNT = xact_len;
|
||||
|
||||
edpt_dma_start(epnum, TUSB_DIR_OUT);
|
||||
|
||||
xfer->buffer += xact_len;
|
||||
xfer->actual_len += xact_len;
|
||||
}
|
||||
|
||||
|
||||
/*------------- Bulk/Int IN transfer -------------*/
|
||||
|
||||
/**
|
||||
* Prepare Bulk/Int in transaction, transfer data from Memory -> Endpoint
|
||||
* @param epnum
|
||||
*/
|
||||
static void xact_in_prepare(uint8_t epnum)
|
||||
{
|
||||
nom_xfer_t* xfer = get_td(epnum, TUSB_DIR_IN);
|
||||
|
||||
// Each transaction is up to Max Packet Size
|
||||
uint8_t const xact_len = min16_of(xfer->total_len - xfer->actual_len, xfer->mps);
|
||||
|
||||
NRF_USBD->EPIN[epnum].PTR = (uint32_t) xfer->buffer;
|
||||
NRF_USBD->EPIN[epnum].MAXCNT = xact_len;
|
||||
|
||||
xfer->buffer += xact_len;
|
||||
|
||||
edpt_dma_start(epnum, TUSB_DIR_IN);
|
||||
}
|
||||
|
||||
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
uint8_t const epnum = edpt_number(desc_edpt->bEndpointAddress);
|
||||
uint8_t const dir = edpt_dir(desc_edpt->bEndpointAddress);
|
||||
|
||||
_dcd.xfer[epnum-1][dir].mps = desc_edpt->wMaxPacketSize.size;
|
||||
|
||||
if ( dir == TUSB_DIR_OUT )
|
||||
{
|
||||
NRF_USBD->INTENSET = BIT_(USBD_INTEN_ENDEPOUT0_Pos + epnum);
|
||||
NRF_USBD->EPOUTEN |= BIT_(epnum);
|
||||
}else
|
||||
{
|
||||
NRF_USBD->INTENSET = BIT_(USBD_INTEN_ENDEPIN0_Pos + epnum);
|
||||
NRF_USBD->EPINEN |= BIT_(epnum);
|
||||
}
|
||||
__ISB(); __DSB();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
uint8_t const epnum = edpt_number(ep_addr);
|
||||
uint8_t const dir = edpt_dir(ep_addr);
|
||||
|
||||
nom_xfer_t* xfer = get_td(epnum, dir);
|
||||
|
||||
xfer->buffer = buffer;
|
||||
xfer->total_len = total_bytes;
|
||||
xfer->actual_len = 0;
|
||||
|
||||
if ( dir == TUSB_DIR_OUT )
|
||||
{
|
||||
if ( xfer->data_received )
|
||||
{
|
||||
xfer->data_received = false;
|
||||
|
||||
// FIXME nrf52840 does not NAK OUT packet properly
|
||||
// Data already received preivously
|
||||
xact_out_dma(epnum);
|
||||
}else
|
||||
{
|
||||
xact_out_prepare(epnum);
|
||||
}
|
||||
}else
|
||||
{
|
||||
xact_in_prepare(epnum);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
if ( ep_addr == 0)
|
||||
{
|
||||
NRF_USBD->TASKS_EP0STALL = 1;
|
||||
}else
|
||||
{
|
||||
NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_Stall << USBD_EPSTALL_STALL_Pos) | ep_addr;
|
||||
}
|
||||
|
||||
__ISB(); __DSB();
|
||||
}
|
||||
|
||||
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
if ( ep_addr )
|
||||
{
|
||||
NRF_USBD->EPSTALL = (USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | ep_addr;
|
||||
}
|
||||
}
|
||||
|
||||
bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr)
|
||||
{
|
||||
(void) rhport;
|
||||
|
||||
// USBD shouldn't check control endpoint state
|
||||
if ( 0 == ep_addr ) return false;
|
||||
|
||||
uint8_t const epnum = edpt_number(ep_addr);
|
||||
uint8_t const dir = edpt_dir(ep_addr);
|
||||
|
||||
nom_xfer_t* xfer = get_td(epnum, dir);
|
||||
|
||||
return xfer->actual_len < xfer->total_len;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
void USBD_IRQHandler(void)
|
||||
{
|
||||
uint32_t const inten = NRF_USBD->INTEN;
|
||||
uint32_t int_status = 0;
|
||||
|
||||
volatile uint32_t* regevt = &NRF_USBD->EVENTS_USBRESET;
|
||||
|
||||
for(int i=0; i<32; i++)
|
||||
{
|
||||
if ( BIT_TEST_(inten, i) && regevt[i] )
|
||||
{
|
||||
int_status |= BIT_(i);
|
||||
|
||||
// event clear
|
||||
regevt[i] = 0;
|
||||
__ISB(); __DSB();
|
||||
}
|
||||
}
|
||||
|
||||
/*------------- Interrupt Processing -------------*/
|
||||
if ( int_status & USBD_INTEN_USBRESET_Msk )
|
||||
{
|
||||
bus_reset();
|
||||
|
||||
dcd_bus_event(0, USBD_BUS_EVENT_RESET);
|
||||
}
|
||||
|
||||
if ( int_status & EDPT_END_ALL_MASK )
|
||||
{
|
||||
// DMA complete move data from SRAM -> Endpoint
|
||||
edpt_dma_end();
|
||||
}
|
||||
|
||||
/*------------- Control Transfer -------------*/
|
||||
if ( int_status & USBD_INTEN_EP0SETUP_Msk )
|
||||
{
|
||||
uint8_t setup[8] = {
|
||||
NRF_USBD->BMREQUESTTYPE, NRF_USBD->BREQUEST, NRF_USBD->WVALUEL, NRF_USBD->WVALUEH,
|
||||
NRF_USBD->WINDEXL, NRF_USBD->WINDEXH, NRF_USBD->WLENGTHL, NRF_USBD->WLENGTHH
|
||||
};
|
||||
|
||||
dcd_setup_received(0, setup);
|
||||
}
|
||||
|
||||
if ( int_status & USBD_INTEN_EP0DATADONE_Msk )
|
||||
{
|
||||
if ( _dcd.control.dir == TUSB_DIR_OUT )
|
||||
{
|
||||
// OUT data from Host -> Endpoint
|
||||
// Trigger DMA to move Endpoint -> SRAM
|
||||
edpt_dma_start(0, TUSB_DIR_OUT);
|
||||
}else
|
||||
{
|
||||
// IN: data transferred from Endpoint -> Host
|
||||
if ( _dcd.control.actual_len < _dcd.control.total_len )
|
||||
{
|
||||
xact_control_start();
|
||||
}else
|
||||
{
|
||||
// Control IN complete
|
||||
dcd_control_complete(0, _dcd.control.actual_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OUT data moved from Endpoint -> SRAM
|
||||
if ( int_status & USBD_INTEN_ENDEPOUT0_Msk)
|
||||
{
|
||||
if ( _dcd.control.actual_len < _dcd.control.total_len )
|
||||
{
|
||||
xact_control_start();
|
||||
}else
|
||||
{
|
||||
// Control OUT complete
|
||||
dcd_control_complete(0, _dcd.control.actual_len);
|
||||
}
|
||||
}
|
||||
|
||||
/*------------- Bulk/Interrupt Transfer -------------*/
|
||||
if ( int_status & USBD_INTEN_EPDATA_Msk)
|
||||
{
|
||||
uint32_t data_status = NRF_USBD->EPDATASTATUS;
|
||||
|
||||
nrf_usbd_epdatastatus_clear(data_status);
|
||||
|
||||
// In: data from Endpoint -> Host
|
||||
for(uint8_t epnum=1; epnum<8; epnum++)
|
||||
{
|
||||
if ( BIT_TEST_(data_status, epnum ) )
|
||||
{
|
||||
nom_xfer_t* xfer = get_td(epnum, TUSB_DIR_IN);
|
||||
|
||||
xfer->actual_len += NRF_USBD->EPIN[epnum].MAXCNT;
|
||||
|
||||
if ( xfer->actual_len < xfer->total_len )
|
||||
{
|
||||
// more to xfer
|
||||
xact_in_prepare(epnum);
|
||||
} else
|
||||
{
|
||||
// BULK/INT IN complete
|
||||
dcd_xfer_complete(0, epnum | TUSB_DIR_IN_MASK, xfer->actual_len, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OUT: data from Host -> Endpoint
|
||||
for(uint8_t epnum=1; epnum<8; epnum++)
|
||||
{
|
||||
if ( BIT_TEST_(data_status, 16+epnum ) )
|
||||
{
|
||||
nom_xfer_t* xfer = get_td(epnum, TUSB_DIR_OUT);
|
||||
|
||||
if (xfer->actual_len < xfer->total_len)
|
||||
{
|
||||
xact_out_dma(epnum);
|
||||
}else
|
||||
{
|
||||
// FIXME nrf52840 does not NAK OUT packet properly
|
||||
// It will always ACK next package although we haven't write to SIZE yet
|
||||
|
||||
// Mark this endpoint with data received
|
||||
xfer->data_received = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OUT: data from DMA -> SRAM
|
||||
for(uint8_t epnum=1; epnum<8; epnum++)
|
||||
{
|
||||
if ( BIT_TEST_(int_status, USBD_INTEN_ENDEPOUT0_Pos+epnum) )
|
||||
{
|
||||
nom_xfer_t* xfer = get_td(epnum, TUSB_DIR_OUT);
|
||||
|
||||
uint8_t const xact_len = NRF_USBD->EPOUT[epnum].AMOUNT;
|
||||
|
||||
// Transfer complete if transaction len < Max Packet Size or total len is transferred
|
||||
if ( (xact_len == xfer->mps) && (xfer->actual_len < xfer->total_len) )
|
||||
{
|
||||
// Prepare for more data from Host -> Endpoint
|
||||
xact_out_prepare(epnum);
|
||||
}else
|
||||
{
|
||||
xfer->total_len = xfer->actual_len;
|
||||
|
||||
// BULK/INT OUT complete
|
||||
dcd_xfer_complete(0, epnum, xfer->actual_len, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// SOF interrupt
|
||||
if ( int_status & USBD_INTEN_SOF_Msk )
|
||||
{
|
||||
dcd_bus_event(0, USBD_BUS_EVENT_SOF);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,334 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file hal_nrf5x.c
|
||||
@author hathach
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, hathach (tinyusb.org)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#if MODE_DEVICE_SUPPORTED && CFG_TUSB_MCU == OPT_MCU_NRF5X
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "nrf.h"
|
||||
#include "nrf_gpio.h"
|
||||
#include "nrf_clock.h"
|
||||
#include "nrf_usbd.h"
|
||||
#include "nrf_drv_usbd_errata.h"
|
||||
|
||||
#include "app_util_platform.h"
|
||||
|
||||
#ifdef SOFTDEVICE_PRESENT
|
||||
#include "nrf_sdm.h"
|
||||
#include "nrf_soc.h"
|
||||
#else
|
||||
#include "nrf_drv_power.h"
|
||||
#endif
|
||||
|
||||
#include "tusb_hal.h"
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* MACRO TYPEDEF CONSTANT ENUM
|
||||
*------------------------------------------------------------------*/
|
||||
#define USB_NVIC_PRIO 7
|
||||
|
||||
// TODO switch to use nrfx_power.h in sdk15
|
||||
enum
|
||||
{
|
||||
NRFX_POWER_USB_EVT_DETECTED = 0,
|
||||
NRFX_POWER_USB_EVT_REMOVED,
|
||||
NRFX_POWER_USB_EVT_READY
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* FUNCTION DECLARATION
|
||||
*------------------------------------------------------------------*/
|
||||
void tusb_hal_nrf_power_event(uint32_t event);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* HFCLK helper
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
static bool hfclk_running(void)
|
||||
{
|
||||
#ifdef SOFTDEVICE_PRESENT
|
||||
if ( is_sd_enabled() )
|
||||
{
|
||||
uint32_t is_running;
|
||||
(void) sd_clock_hfclk_is_running(&is_running);
|
||||
return (is_running ? true : false);
|
||||
}
|
||||
#endif
|
||||
|
||||
return nrf_clock_hf_is_running(NRF_CLOCK_HFCLK_HIGH_ACCURACY);
|
||||
}
|
||||
|
||||
static void hfclk_enable(void)
|
||||
{
|
||||
// already running, nothing to do
|
||||
if ( hfclk_running() ) return;
|
||||
|
||||
#ifdef SOFTDEVICE_PRESENT
|
||||
if ( is_sd_enabled() )
|
||||
{
|
||||
(void)sd_clock_hfclk_request();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
|
||||
nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART);
|
||||
}
|
||||
|
||||
static void hfclk_disable(void)
|
||||
{
|
||||
#ifdef SOFTDEVICE_PRESENT
|
||||
if ( is_sd_enabled() )
|
||||
{
|
||||
(void)sd_clock_hfclk_release();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTOP);
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* TUSB HAL
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
// tusb_hal_nrf_power_event must be called by SOC event handler
|
||||
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 handler 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(NRFX_POWER_USB_EVT_DETECTED);
|
||||
}
|
||||
|
||||
if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk )
|
||||
{
|
||||
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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 */
|
||||
nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
|
||||
|
||||
/* Enable the peripheral */
|
||||
// ERRATA 171, 187, 166
|
||||
|
||||
// Somehow Errata 187 check failed for pca10056 1.0.0 (2018.19)
|
||||
if ( nrf_drv_usbd_errata_187() )
|
||||
{
|
||||
CRITICAL_REGION_ENTER();
|
||||
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
*((volatile uint32_t *) (0x4006ED14)) = 0x00000003;
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
}
|
||||
else
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006ED14)) = 0x00000003;
|
||||
}
|
||||
CRITICAL_REGION_EXIT();
|
||||
}
|
||||
|
||||
if ( nrf_drv_usbd_errata_171() )
|
||||
{
|
||||
CRITICAL_REGION_ENTER();
|
||||
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
*((volatile uint32_t *) (0x4006EC14)) = 0x000000C0;
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
}
|
||||
else
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006EC14)) = 0x000000C0;
|
||||
}
|
||||
CRITICAL_REGION_EXIT();
|
||||
}
|
||||
|
||||
nrf_usbd_enable();
|
||||
|
||||
// Enable HFCLK
|
||||
hfclk_enable();
|
||||
}
|
||||
break;
|
||||
|
||||
case NRFX_POWER_USB_EVT_READY:
|
||||
/* Waiting for USBD peripheral enabled */
|
||||
while ( !(USBD_EVENTCAUSE_READY_Msk & NRF_USBD->EVENTCAUSE) ) { }
|
||||
|
||||
nrf_usbd_eventcause_clear(USBD_EVENTCAUSE_READY_Msk);
|
||||
nrf_usbd_event_clear(USBD_EVENTCAUSE_READY_Msk);
|
||||
|
||||
if ( nrf_drv_usbd_errata_171() )
|
||||
{
|
||||
CRITICAL_REGION_ENTER();
|
||||
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
*((volatile uint32_t *) (0x4006EC14)) = 0x00000000;
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
}
|
||||
else
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006EC14)) = 0x00000000;
|
||||
}
|
||||
|
||||
CRITICAL_REGION_EXIT();
|
||||
}
|
||||
|
||||
// Somehow Errata 187 check failed for pca10056 1.0.0 (2018.19)
|
||||
if ( nrf_drv_usbd_errata_187() )
|
||||
{
|
||||
CRITICAL_REGION_ENTER();
|
||||
if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
*((volatile uint32_t *) (0x4006ED14)) = 0x00000000;
|
||||
*((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
|
||||
}
|
||||
else
|
||||
{
|
||||
*((volatile uint32_t *) (0x4006ED14)) = 0x00000000;
|
||||
}
|
||||
CRITICAL_REGION_EXIT();
|
||||
}
|
||||
|
||||
if ( nrf_drv_usbd_errata_166() )
|
||||
{
|
||||
*((volatile uint32_t *) (NRF_USBD_BASE + 0x800)) = 0x7E3;
|
||||
*((volatile uint32_t *) (NRF_USBD_BASE + 0x804)) = 0x40;
|
||||
|
||||
__ISB();
|
||||
__DSB();
|
||||
}
|
||||
|
||||
nrf_usbd_isosplit_set(NRF_USBD_ISOSPLIT_Half);
|
||||
|
||||
// Enable interrupt. SOF is used as CDC auto flush
|
||||
NRF_USBD->INTENSET = USBD_INTEN_USBRESET_Msk | USBD_INTEN_USBEVENT_Msk | USBD_INTEN_ACCESSFAULT_Msk |
|
||||
USBD_INTEN_EP0SETUP_Msk | USBD_INTEN_EP0DATADONE_Msk | USBD_INTEN_ENDEPIN0_Msk | USBD_INTEN_ENDEPOUT0_Msk |
|
||||
USBD_INTEN_EPDATA_Msk | ((CFG_TUD_CDC && CFG_TUD_CDC_FLUSH_ON_SOF) ? USBD_INTEN_SOF_Msk : 0);
|
||||
|
||||
// Enable interrupt, Priorities 0,1,4,5 (nRF52) are reserved for SoftDevice
|
||||
NVIC_SetPriority(USBD_IRQn, USB_NVIC_PRIO);
|
||||
NVIC_ClearPendingIRQ(USBD_IRQn);
|
||||
NVIC_EnableIRQ(USBD_IRQn);
|
||||
|
||||
// Wait for HFCLK
|
||||
while ( !hfclk_running() )
|
||||
{
|
||||
}
|
||||
|
||||
// Enable pull up
|
||||
nrf_usbd_pullup_enable();
|
||||
break;
|
||||
|
||||
case NRFX_POWER_USB_EVT_REMOVED:
|
||||
if ( NRF_USBD->ENABLE )
|
||||
{
|
||||
// Abort all transfers
|
||||
|
||||
// Disable pull up
|
||||
nrf_usbd_pullup_disable();
|
||||
|
||||
// Disable Interrupt
|
||||
NVIC_DisableIRQ(USBD_IRQn);
|
||||
|
||||
// disable all interrupt
|
||||
NRF_USBD->INTENCLR = NRF_USBD->INTEN;
|
||||
|
||||
nrf_usbd_disable();
|
||||
hfclk_disable();
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user