clean up
This commit is contained in:
		@@ -314,11 +314,11 @@ tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
 | 
			
		||||
  return TUSB_ERROR_NONE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if CFG_TUD_CDC_FLUSH_ON_SOF
 | 
			
		||||
void cdcd_sof(uint8_t rhport)
 | 
			
		||||
{
 | 
			
		||||
#if CFG_TUD_CDC_FLUSH_ON_SOF
 | 
			
		||||
  tud_n_cdc_flush(rhport);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -102,7 +102,11 @@ tusb_error_t cdcd_control_request_st(uint8_t rhport, tusb_control_request_t cons
 | 
			
		||||
tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t edpt_addr, tusb_event_t event, uint32_t xferred_bytes);
 | 
			
		||||
void cdcd_close(uint8_t rhport);
 | 
			
		||||
 | 
			
		||||
#if CFG_TUD_CDC_FLUSH_ON_SOF
 | 
			
		||||
void cdcd_sof(uint8_t rhport);
 | 
			
		||||
#else
 | 
			
		||||
#define cdcd_sof    NULL
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -48,8 +48,6 @@
 | 
			
		||||
 | 
			
		||||
#include "device/dcd.h"
 | 
			
		||||
 | 
			
		||||
#define ERRATA_104_FIX    0
 | 
			
		||||
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/* MACRO TYPEDEF CONSTANT ENUM
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
@@ -68,17 +66,10 @@ enum
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  uint8_t* buffer;
 | 
			
		||||
 | 
			
		||||
  uint16_t total_len;
 | 
			
		||||
  uint16_t actual_len;
 | 
			
		||||
 | 
			
		||||
  uint8_t  mps; // max packet size
 | 
			
		||||
 | 
			
		||||
#if ERRATA_104_FIX
 | 
			
		||||
  // FIXME Errata 104 walkaround
 | 
			
		||||
  uint16_t frame_num;
 | 
			
		||||
  uint8_t  prev_size;
 | 
			
		||||
#endif
 | 
			
		||||
} nom_xfer_t;
 | 
			
		||||
 | 
			
		||||
/*static*/ struct
 | 
			
		||||
@@ -299,15 +290,6 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
 | 
			
		||||
  xfer->total_len  = total_bytes;
 | 
			
		||||
  xfer->actual_len = 0;
 | 
			
		||||
 | 
			
		||||
#if ERRATA_104_FIX
 | 
			
		||||
  // FIXME Errata 104 walkaround
 | 
			
		||||
  if ( nrf_drv_usbd_errata_104() )
 | 
			
		||||
  {
 | 
			
		||||
    xfer->frame_num = (uint16_t) NRF_USBD->FRAMECNTR;
 | 
			
		||||
    xfer->prev_size = NRF_USBD->SIZE.EPOUT[epnum];
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  normal_xact_start(epnum, dir);
 | 
			
		||||
 | 
			
		||||
  return true;
 | 
			
		||||
@@ -355,24 +337,6 @@ bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr)
 | 
			
		||||
/*------------------------------------------------------------------*/
 | 
			
		||||
/*
 | 
			
		||||
 *------------------------------------------------------------------*/
 | 
			
		||||
 | 
			
		||||
#if ERRATA_104_FIX
 | 
			
		||||
static uint16_t frame_sofar(uint16_t fr)
 | 
			
		||||
{
 | 
			
		||||
  uint16_t diff = (uint16_t) NRF_USBD->FRAMECNTR;
 | 
			
		||||
 | 
			
		||||
  if ( diff > fr )
 | 
			
		||||
  {
 | 
			
		||||
    diff -= fr;
 | 
			
		||||
  }else
 | 
			
		||||
  {
 | 
			
		||||
    diff = (diff + 1024) - fr; // Frame counter cap at 1024
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return diff;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void USBD_IRQHandler(void)
 | 
			
		||||
{
 | 
			
		||||
  uint32_t const inten  = NRF_USBD->INTEN;
 | 
			
		||||
@@ -491,15 +455,6 @@ void USBD_IRQHandler(void)
 | 
			
		||||
        // FIXME nrf52840 rev A does not NAK OUT packet properly
 | 
			
		||||
        TU_ASSERT(xfer->actual_len < xfer->total_len, );
 | 
			
		||||
 | 
			
		||||
#if ERRATA_104_FIX
 | 
			
		||||
        // FIXME Errata 104 walkaround
 | 
			
		||||
        if ( nrf_drv_usbd_errata_104() )
 | 
			
		||||
        {
 | 
			
		||||
          xfer->prev_size = xact_len;
 | 
			
		||||
          xfer->frame_num = (uint16_t) NRF_USBD->FRAMECNTR;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
        // Trigger DMA move data from Endpoint -> SRAM
 | 
			
		||||
        NRF_USBD->EPOUT[epnum].PTR    = (uint32_t) xfer->buffer;
 | 
			
		||||
        NRF_USBD->EPOUT[epnum].MAXCNT = xact_len;
 | 
			
		||||
@@ -541,57 +496,6 @@ void USBD_IRQHandler(void)
 | 
			
		||||
  if ( int_status & USBD_INTEN_SOF_Msk )
 | 
			
		||||
  {
 | 
			
		||||
    dcd_bus_event(0, USBD_BUS_EVENT_SOF);
 | 
			
		||||
 | 
			
		||||
#if ERRATA_104_FIX
 | 
			
		||||
    // FIXME Errata 104 The EPDATA event might not be generated, and the related update of EPDATASTATUS does not occur.
 | 
			
		||||
    // There is no way for software to tell if a xfer is complete or not.
 | 
			
		||||
    // Walkaround: we will asssume an non-control IN transfer is always complete after 10 frames
 | 
			
		||||
    if ( nrf_drv_usbd_errata_104() )
 | 
			
		||||
    {
 | 
			
		||||
      // Check all OUT transfer, if the SIZE changes --> consider it is complete
 | 
			
		||||
      for (int ep=1; ep<8; ep++)
 | 
			
		||||
      {
 | 
			
		||||
        nom_xfer_t* xfer = get_td(ep, TUSB_DIR_OUT);
 | 
			
		||||
        uint8_t const xact_len = NRF_USBD->SIZE.EPOUT[ep];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        if ( frame_sofar(xfer->frame_num) > 20)
 | 
			
		||||
        {
 | 
			
		||||
          if ( (xact_len != 0) && (xfer->actual_len < xfer->total_len) && (xfer->prev_size != xact_len) )
 | 
			
		||||
          {
 | 
			
		||||
            xfer->prev_size = xact_len;
 | 
			
		||||
            xfer->frame_num = (uint16_t) NRF_USBD->FRAMECNTR;
 | 
			
		||||
 | 
			
		||||
            // Trigger DMA move data from Endpoint -> SRAM
 | 
			
		||||
            NRF_USBD->EPOUT[ep].PTR    = (uint32_t) xfer->buffer;
 | 
			
		||||
            NRF_USBD->EPOUT[ep].MAXCNT = xact_len;
 | 
			
		||||
 | 
			
		||||
            edpt_dma_start(ep, TUSB_DIR_OUT);
 | 
			
		||||
 | 
			
		||||
            xfer->buffer     += xact_len;
 | 
			
		||||
            xfer->actual_len += xact_len;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Check all the queued IN transfer, retire all transfer if 10 frames has passed
 | 
			
		||||
      for (int ep=1; ep< 8; ep++)
 | 
			
		||||
      {
 | 
			
		||||
        nom_xfer_t* xfer = get_td(ep, TUSB_DIR_IN);
 | 
			
		||||
 | 
			
		||||
        if (xfer->actual_len < xfer->total_len)
 | 
			
		||||
        {
 | 
			
		||||
          // Walkaround, mark this transfer as complete
 | 
			
		||||
          if (frame_sofar(xfer->frame_num) > 10)
 | 
			
		||||
          {
 | 
			
		||||
            xfer->actual_len = xfer->total_len;
 | 
			
		||||
            dcd_xfer_complete(0, ep | TUSB_DIR_IN_MASK, xfer->actual_len, true);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -45,6 +45,8 @@
 | 
			
		||||
#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"
 | 
			
		||||
@@ -193,11 +195,11 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
        nrf_usbd_eventcause_clear(NRF_USBD_EVENTCAUSE_READY_MASK);
 | 
			
		||||
 | 
			
		||||
        /* Enable the peripheral */
 | 
			
		||||
        // ERRATA 171, 187
 | 
			
		||||
 | 
			
		||||
        // ERRATA 171, 187, 166
 | 
			
		||||
        if ( nrf_drv_usbd_errata_187() )
 | 
			
		||||
        {
 | 
			
		||||
  //          CRITICAL_REGION_ENTER();
 | 
			
		||||
          CRITICAL_REGION_ENTER()
 | 
			
		||||
          ;
 | 
			
		||||
          if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
 | 
			
		||||
          {
 | 
			
		||||
            *((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
 | 
			
		||||
@@ -208,12 +210,14 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
          {
 | 
			
		||||
            *((volatile uint32_t *) (0x4006ED14)) = 0x00000003;
 | 
			
		||||
          }
 | 
			
		||||
  //          CRITICAL_REGION_EXIT();
 | 
			
		||||
          CRITICAL_REGION_EXIT()
 | 
			
		||||
          ;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ( nrf_drv_usbd_errata_171() )
 | 
			
		||||
        {
 | 
			
		||||
  //          CRITICAL_REGION_ENTER();
 | 
			
		||||
          CRITICAL_REGION_ENTER()
 | 
			
		||||
          ;
 | 
			
		||||
          if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
 | 
			
		||||
          {
 | 
			
		||||
            *((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
 | 
			
		||||
@@ -224,7 +228,8 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
          {
 | 
			
		||||
            *((volatile uint32_t *) (0x4006EC14)) = 0x000000C0;
 | 
			
		||||
          }
 | 
			
		||||
  //          CRITICAL_REGION_EXIT();
 | 
			
		||||
          CRITICAL_REGION_EXIT()
 | 
			
		||||
          ;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        nrf_usbd_enable();
 | 
			
		||||
@@ -236,13 +241,16 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
 | 
			
		||||
    case NRFX_POWER_USB_EVT_READY:
 | 
			
		||||
      /* Waiting for USBD peripheral enabled */
 | 
			
		||||
      while ( !(NRF_USBD_EVENTCAUSE_READY_MASK & NRF_USBD->EVENTCAUSE) ) { }
 | 
			
		||||
      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_171() )
 | 
			
		||||
      {
 | 
			
		||||
  //          CRITICAL_REGION_ENTER();
 | 
			
		||||
        CRITICAL_REGION_ENTER()
 | 
			
		||||
        ;
 | 
			
		||||
        if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
 | 
			
		||||
        {
 | 
			
		||||
          *((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
 | 
			
		||||
@@ -254,12 +262,14 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
          *((volatile uint32_t *) (0x4006EC14)) = 0x00000000;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
  //          CRITICAL_REGION_EXIT();
 | 
			
		||||
        CRITICAL_REGION_EXIT()
 | 
			
		||||
        ;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if ( nrf_drv_usbd_errata_187() )
 | 
			
		||||
      {
 | 
			
		||||
  //          CRITICAL_REGION_ENTER();
 | 
			
		||||
        CRITICAL_REGION_ENTER()
 | 
			
		||||
        ;
 | 
			
		||||
        if ( *((volatile uint32_t *) (0x4006EC00)) == 0x00000000 )
 | 
			
		||||
        {
 | 
			
		||||
          *((volatile uint32_t *) (0x4006EC00)) = 0x00009375;
 | 
			
		||||
@@ -270,7 +280,8 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
        {
 | 
			
		||||
          *((volatile uint32_t *) (0x4006ED14)) = 0x00000000;
 | 
			
		||||
        }
 | 
			
		||||
  //          CRITICAL_REGION_EXIT();
 | 
			
		||||
        CRITICAL_REGION_EXIT()
 | 
			
		||||
        ;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if ( nrf_drv_usbd_errata_166() )
 | 
			
		||||
@@ -278,7 +289,8 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
        *((volatile uint32_t *) (NRF_USBD_BASE + 0x800)) = 0x7E3;
 | 
			
		||||
        *((volatile uint32_t *) (NRF_USBD_BASE + 0x804)) = 0x40;
 | 
			
		||||
 | 
			
		||||
        __ISB(); __DSB();
 | 
			
		||||
        __ISB();
 | 
			
		||||
        __DSB();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      nrf_usbd_isosplit_set(NRF_USBD_ISOSPLIT_Half);
 | 
			
		||||
@@ -286,11 +298,7 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
      // 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   | USBD_INTEN_SOF_Msk;
 | 
			
		||||
 | 
			
		||||
      // FIXME Errata 104: USB complete event is not generated (happedn randomly).
 | 
			
		||||
      // Requires to enable SOF to perform clean up task.
 | 
			
		||||
      // nrf_drv_usbd_errata_104()
 | 
			
		||||
      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);
 | 
			
		||||
@@ -298,7 +306,9 @@ void tusb_hal_nrf_power_event(uint32_t event)
 | 
			
		||||
      NVIC_EnableIRQ(USBD_IRQn);
 | 
			
		||||
 | 
			
		||||
      // Wait for HFCLK
 | 
			
		||||
      while ( !hfclk_running() ) {}
 | 
			
		||||
      while ( !hfclk_running() )
 | 
			
		||||
      {
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Enable pull up
 | 
			
		||||
      nrf_usbd_pullup_enable();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user