SAMA5D36 xplained板USB无法连接

时间:2018-06-02 09:49:05

标签: embedded atmel microchip bare-metal

我为usb cdc serial SAMA5D36 xplained board闪存代码。所有配置都在进行,但USB没有连接。我认为这是因为Vbus引脚没有变高,设备断开连接。 你能帮我解决一下这个问题的原因吗?解决这个问题。

日志:

--Auto-Config the TWI Bus by the board
--PHY: ETH0 PHY: Enter power down mode
--PHY: ETH1 PHY: Enter power down mode
--NAND: ONFI flash detected
--NAND: Manufacturer ID: 0x2c Chip ID: 0x32
--NAND: Disable On-Die ECC
--NAND: Initialize PMECC params, cap: 0x4, sector: 0x200
--NAND: Image: Copy 0x80000 bytes from 0x40000 to 0x20000000
--NAND: Done to load image
-- USB Device CDC Serial Project 1.4 --                                         
-- SAMA5D3X-EK                                                                  
-- Compiled: Feb 28 2014 15:02:24 --                                            
-- ESC to Enable/Disable ECHO on cdc serial --                                  
-- Press t to test transfer speed   --                                          
-I- VBus configuration                                                          
-I- conn                                                                        
-I- VBUS discon                                      




  /* ----------------------------------------------------------------------------
 *         SAM Software Package License 
 * ----------------------------------------------------------------------------

 */
/** \cond usb_cdc_serial
 * \page usb_cdc_serial USB CDC Serial Converter Example
 *
 * \section Purpose
 *
 * The USB CDC Serial Project will help you to get familiar with the
 * USB Device Port(UDP) and USART interface on SAMA5D3 microcontrollers. Also
 * it can help you to be familiar with the USB Framework that is used for
 * rapid development of USB-compliant class drivers such as USB Communication
 * Device class (CDC).
 *
 * \section Requirements
 *
 * This package can be used with SAMA5D3x evaluation kits.
 *
 * \section DescriptionDMAD_PrepareMultiTransfer
 *
 * This demo simulates a USB to RS-232 Serial Port Converter.
 *
 * When an EK running this program connected to a host (PC for example), with
 * USB cable, the EK appears as a serial COM port for the host, after driver
 * installation with the offered 6119.inf. Then the host can send or receive
 * data through the port with host software. The data stream from the host is
 * then sent to the EK, and forward to USART port of AT91SAM chips. The USART
 * port of the EK is monitored by the timer and the incoming data will be sent
 * to the host.
 *
 * \section Usage
 *
 * -# Build the program and download it inside the evaluation board. Please
 *    refer to the
 *    <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6421.pdf">
 *    SAM-BA User Guide</a>, the
 *    <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6310.pdf">
 *    GNU-Based Software Development</a> application note or to the
 *    <a href="ftp://ftp.iar.se/WWWfiles/arm/Guides/EWARM_UserGuide.ENU.pdf">
 *    IAR EWARM User Guide</a>, depending on your chosen solution.
 * -# On the computer, open and configure a terminal application
 *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
 *   - 115200 bauds
 *   - 8 bits of data
 *   - No parity
 *   - 1 stop bit
 *   - No flow control
 * -# Start the application.
 * -# In the terminal window, the following text should appear:
 *     \code
 *     -- USB Device CDC Serial Project xxx --
 *     -- SAMxxxxx-xx
 *     -- Compiled: xxx xx xxxx xx:xx:xx --
 *     \endcode
 * -# When connecting USB cable to windows, the host
 *    reports a new USB device attachment (if it's the first time you connect
 *    an %audio speaker demo board to your host). You can use the inf file
 *    libraries\\usb\\device\\cdc-serial\\drv\\6119.inf to install the serial
 *    port. Then new "AT91 USB to Serial Converter (COMx)" appears in the
 *    hardware device list.
 * -# You can run hyperterminal to send data to the port. And it can be seen
 *    at the other hyperterminal connected to the USART port of the EK.
 *
 * \section References
 * - usb_cdc_serial/main.c
 * - usart: USART interface driver
 * - tc: TIMER/COUNTER interface driver
 * - usb: USB Framework, USB CDC driver and UDP interface driver
 *    - \ref usbd_framework
 *       - \ref usbd_api
 *    - \ref usbd_cdc
 *       - \ref usbd_cdc_serial_drv
 *       - \ref usbd_cdc_host_drv
 */

/**
 * \file
 *
 * This file contains all the specific code for the
 * usb_cdc_serial example.
 *
 */

/*----------------------------------------------------------------------------
 *         Headers
 *----------------------------------------------------------------------------*/

#include "board.h"

#include "USBD.h"
#include "CDCDSerialDriver.h"

#include <stdbool.h>
#include <stdint.h>

/*----------------------------------------------------------------------------
 *      Definitions
 *----------------------------------------------------------------------------*/

/** Size in bytes of the packet used for reading data from the USB & USART */
#define DATAPACKETSIZE (64)

/** Size in bytes of the buffer used for reading data from the USB & USART */
#define DATABUFFERSIZE (DATAPACKETSIZE+2)

/** Pins used for USART transfer */
#define PINS_USART      PIN_USART1_TXD, PIN_USART1_RXD
/** Register base for USART operatoin */
#define BASE_USART      USART1
/** USART ID */
#define ID_USART        ID_USART1

/** Speed test buffer size */
#define TEST_BUFFER_SIZE    (2*1024)
/** Speed test loop count */
#define TEST_COUNT          (30)

/*----------------------------------------------------------------------------
 *      External variables
 *----------------------------------------------------------------------------*/

extern const USBDDriverDescriptors cdcdSerialDriverDescriptors;

/*----------------------------------------------------------------------------
 *      Internal variables
 *----------------------------------------------------------------------------*/

/** Global DMA driver for all transfer */
static sDmad dmad;

/** DMA channel for RX */
static uint32_t usartDmaRxChannel;

/** DMA channel for TX */
static uint32_t usartDmaTxChannel;

/** USART link list for data RX */
static sDmaTransferDescriptor dmaTdUsRx[2];

/** List of pins that must be configured for use by the application. */
static const Pin pins[] = {PINS_USART};

/** Double-buffer for storing incoming USART data. */
static uint8_t usartBuffers[2][DATABUFFERSIZE];

/** Current USART buffer index. */
static uint8_t usartCurrentBuffer = 0;

/** Buffer for storing incoming USB data. */
static uint8_t usbBuffer[DATABUFFERSIZE];

/** Serial Port ON/OFF */
static uint8_t isCdcSerialON = 0;

/** CDC Echo back ON/OFF */
static uint8_t isCdcEchoON = 0;

/** TC tick: 1/250 s */
static volatile uint32_t tcTick = 0;

/** USB Tx flag */
static volatile uint8_t txDoneFlag = 0;
/** Test buffer */
static uint8_t testBuffer[TEST_BUFFER_SIZE];

/*----------------------------------------------------------------------------
 *         Internal Prototypes
 *----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
 *         VBus monitoring
 *----------------------------------------------------------------------------*/

/** VBus pin instance. */
static const Pin pinVbus = PIN_USB_VBUS;

/**
 * Handles interrupts coming from PIO controllers.
 */
static void ISR_Vbus(const Pin *pPin)
{
    /* Check current level on VBus */
    if (PIO_Get(pPin))
    {
        TRACE_INFO("VBUS conn\n\r");
        USBD_Connect();
    }
    else
    {
        TRACE_INFO("VBUS discon\n\r");
        USBD_Disconnect();
    }
}

/**
 * Configures the VBus pin to trigger an interrupt when the level on that pin
 * changes.
 */
static void VBus_Configure( void )
{
    printf("usb 124\n\r");
    TRACE_INFO("VBus configuration\n\r");

    /* Configure PIO */
    PIO_Configure(&pinVbus, 1);
    PIO_ConfigureIt(&pinVbus, ISR_Vbus);
    PIO_EnableIt(&pinVbus);

    /* Check current level on VBus */
    if (PIO_Get(&pinVbus))
    {
        /* if VBUS present, force the connect */
        TRACE_INFO("conn\n\r");
        USBD_Connect();
    }
    else
    {
        printf("usb discon\n\r");
        USBD_Disconnect();
    }
}

/*----------------------------------------------------------------------------
 *         USB Power Control
 *----------------------------------------------------------------------------*/

#ifdef PIN_USB_POWER_ENA
/** Power Enable A (MicroAB Socket) pin instance. */
static const Pin pinPOnA = PIN_USB_POWER_ENA;
#endif
#ifdef PIN_USB_POWER_ENB
/** Power Enable B (A Socket) pin instance. */
static const Pin pinPOnB = PIN_USB_POWER_ENB;
#endif
#ifdef PIN_USB_POWER_ENC
/** Power Enable C (A Socket) pin instance. */
static const Pin pinPOnC = PIN_USB_POWER_ENC;
#endif
/**
 * Configures the Power Enable pin to disable self power.
 */
static void USBPower_Configure( void )
{
  #ifdef PIN_USB_POWER_ENA
    PIO_Configure(&pinPOnA, 1);
  #endif
  #ifdef PIN_USB_POWER_ENB
    PIO_Configure(&pinPOnB, 1);
  #endif
  #ifdef PIN_USB_POWER_ENC
    PIO_Configure(&pinPOnC, 1);
  #endif
}

/*----------------------------------------------------------------------------
 *  Interrupt handlers
 *----------------------------------------------------------------------------*/

/**
 * ISR for DMA interrupt
 */
static void ISR_DMA(void)
{
    DMAD_Handler(&dmad);
}

/**
 * USART interrupt handler
 */
static void ISR_USART(void)
{
    Usart *pUs = BASE_USART;
    uint32_t status;
    uint16_t serialState;

    status  = USART_GetStatus(pUs);
    status &= USART_GetItMask(pUs);

    /* If USB device is not configured, do nothing */
    if (!isCdcSerialON)
    {
        USART_DisableIt(pUs, 0xFFFFFFFF);
        return;
    }

    /* Errors */
    serialState = CDCDSerialDriver_GetSerialState();
    /* Overrun */
    if ((status & US_CSR_OVRE) != 0)
    {
        TRACE_WARNING( "USART_IrqHandler: Overrun\n\r");
        serialState |= CDCSerialState_OVERRUN;
    }
    /* Framing error */
    if ((status & US_CSR_FRAME) != 0)
    {
        TRACE_WARNING( "USART_IrqHandler: Framing error\n\r");
        serialState |= CDCSerialState_FRAMING;
    }
    CDCDSerialDriver_SetSerialState(serialState);
}

static void _UsartDmaRx(uint32_t );

/**
 * Handles interrupts coming from Timer #0.
 */
static void ISR_TC0(void)
{
    Tc *pTc0 = TC0;
    sDmad *pDmad = &dmad;
    uint8_t size;
    uint32_t dmaChannel = usartDmaRxChannel;
    uint8_t  iController = (dmaChannel >> 8);
    uint8_t  iChannel    = (dmaChannel) & 0xFF;
    uint8_t  nextBuffer = (1 - usartCurrentBuffer);
    volatile uint32_t status = pTc0->TC_CHANNEL[0].TC_SR;

    if ((status & TC_SR_CPCS) != 0)
    {
        tcTick ++;

        if (isCdcSerialON)
        {
            /* Get Received size */
            size = DMAC_GetDestinationAddr(pDmad->pDmacs[iController], iChannel)
                    - (uint32_t)(usartBuffers[usartCurrentBuffer]);
            if (size == 0)
            {
                pTc0->TC_CHANNEL[0].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
                return;
            }

            /* Stop DMA */
            DMAD_StopTransfer(pDmad, dmaChannel);
            /* Restart DMA in next buffer */
            _UsartDmaRx( nextBuffer );

            /* Send current buffer through the USB */
            while (CDCDSerialDriver_Write(usartBuffers[usartCurrentBuffer],
                                          size, 0, 0) != USBD_STATUS_SUCCESS);

            usartCurrentBuffer = nextBuffer;
        }
        pTc0->TC_CHANNEL[0].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
    }
}

/*-----------------------------------------------------------------------------
 *         Callback re-implementation
 *-----------------------------------------------------------------------------*/

extern void USBD_IrqHandler(void);
/**
 * Invoked after the USB driver has been initialized. By default, configures
 * the UDP/UDPHS interrupt.
 */
void USBDCallbacks_Initialized(void)
{
    IRQ_ConfigureIT(ID_UDPHS, 0, USBD_IrqHandler);
    IRQ_EnableIT(ID_UDPHS);
}

/**
 * Invoked when the configuration of the device changes. Parse used endpoints.
 * \param cfgnum New configuration number.
 */
void USBDDriverCallbacks_ConfigurationChanged(unsigned char cfgnum)
{
    CDCDSerialDriver_ConfigurationChangedHandler(cfgnum);
}

/**
 * Invoked when a new SETUP request is received from the host. Forwards the
 * request to the Mass Storage device driver handler function.
 * \param request  Pointer to a USBGenericRequest instance.
 */
void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
{
    CDCDSerialDriver_RequestHandler(request);
}

/*----------------------------------------------------------------------------
 *         Internal functions
 *----------------------------------------------------------------------------*/

/**
 *  \brief Send single buffer data through DMA
 */
static void _UsartDmaTx( uint32_t dwDestAddr,
                         void* pBuffer, uint16_t wSize )
{
    sDmad *pDmad = &dmad;
    sDmaTransferDescriptor td;
    uint32_t dmaChannel = usartDmaTxChannel;
    CP15_coherent_dcache_for_dma ((uint32_t)&pBuffer, ((uint32_t)(&pBuffer) + wSize));
    /* Setup transfer */
    td.dwSrcAddr = (uint32_t) pBuffer;
    td.dwDstAddr = (uint32_t) dwDestAddr;
    td.dwCtrlA   = DMAC_CTRLA_BTSIZE(wSize)
                    | DMAC_CTRLA_SRC_WIDTH_BYTE | DMAC_CTRLA_DST_WIDTH_BYTE;
    td.dwCtrlB   = DMAC_CTRLB_SRC_DSCR | DMAC_CTRLB_DST_DSCR
                    | DMAC_CTRLB_SIF_AHB_IF0
                    | DMAC_CTRLB_DIF_AHB_IF2
                    | DMAC_CTRLB_FC_MEM2PER_DMA_FC
                    | DMAC_CTRLB_SRC_INCR_INCREMENTING
                    | DMAC_CTRLB_DST_INCR_FIXED;
    td.dwDscAddr = 0;
    DMAD_PrepareSingleTransfer(pDmad, dmaChannel, &td);
    DMAD_StartTransfer(pDmad, dmaChannel);
}

/**
 *  \brief Prepare link list for USART RX
 *  Ringed link list initialized for 2 USART buffer.
 */
static void _UsartDmaRxSetup( void )
{
    Usart *pUs = BASE_USART;
    sDmaTransferDescriptor *pTds = dmaTdUsRx;
    /* Setup TD list for RX */
    pTds[0].dwSrcAddr = (uint32_t) &pUs->US_RHR;
    pTds[0].dwDstAddr = (uint32_t)  usartBuffers[0];
    pTds[0].dwCtrlA   = DMAC_CTRLA_BTSIZE(DATAPACKETSIZE)
                      | DMAC_CTRLA_SRC_WIDTH_BYTE | DMAC_CTRLA_DST_WIDTH_BYTE;
    pTds[0].dwCtrlB   = 0
                      | DMAC_CTRLB_SIF_AHB_IF2
                      | DMAC_CTRLB_DIF_AHB_IF0
                      | DMAC_CTRLB_FC_PER2MEM_DMA_FC
                      | DMAC_CTRLB_SRC_INCR_FIXED
                      | DMAC_CTRLB_DST_INCR_INCREMENTING;
    pTds[0].dwDscAddr = (uint32_t) &pTds[1];

    pTds[1].dwSrcAddr = (uint32_t) &pUs->US_RHR;
    pTds[1].dwDstAddr = (uint32_t)  usartBuffers[1];
    pTds[1].dwCtrlA   = DMAC_CTRLA_BTSIZE(DATAPACKETSIZE)
                      | DMAC_CTRLA_SRC_WIDTH_BYTE | DMAC_CTRLA_DST_WIDTH_BYTE;
    pTds[1].dwCtrlB   = 0
                      | DMAC_CTRLB_SIF_AHB_IF2
                      | DMAC_CTRLB_DIF_AHB_IF0
                      | DMAC_CTRLB_FC_PER2MEM_DMA_FC
                      | DMAC_CTRLB_SRC_INCR_FIXED
                      | DMAC_CTRLB_DST_INCR_INCREMENTING;
    pTds[1].dwDscAddr = (uint32_t) &pTds[0];
    CP15_coherent_dcache_for_dma ((uint32_t)pTds, ((uint32_t)pTds) + sizeof(pTds));
    CP15_coherent_dcache_for_dma ((uint32_t)&usartBuffers[0], ((uint32_t)(&usartBuffers[0]) + DATAPACKETSIZE));
    CP15_coherent_dcache_for_dma ((uint32_t)&usartBuffers[1], ((uint32_t)(&usartBuffers[1]) + DATAPACKETSIZE));
}

/**
 *  \brief Start waiting USART data
 *  Start DMA, the 1st DMA buffer is free USART buffer assigned.
 */
static void _UsartDmaRx( uint32_t startBuffer )
{
    sDmad *pDmad = &dmad;
    sDmaTransferDescriptor *pTds = dmaTdUsRx;
    uint32_t dmaChannel = usartDmaRxChannel;
    DMAD_PrepareMultiTransfer(pDmad, dmaChannel, &pTds[startBuffer]);
    DMAD_StartTransfer(pDmad, dmaChannel);
}

/**
 * DBGU help dump
 */
static void _DebugHelp(void)
{
    printf("-- ESC to Enable/Disable ECHO on cdc serial --\n\r");
    printf("-- Press t to test transfer speed   --\n\r");
}
                       uint32_t remaining)
{
    unused = unused;
    Usart *pUs = BASE_USART;

    /* Check that data has been received successfully */
    if (status == USBD_STATUS_SUCCESS)
    {

        /* Send back CDC data */
        if (isCdcEchoON)
        {
            CDCDSerialDriver_Write(usbBuffer, received, 0, 0);
        }

        /* Send data through USART */
        if (isCdcSerialON)
        {
            _UsartDmaTx( (uint32_t)&pUs->US_THR, usbBuffer, received );
        }

        /* Check if bytes have been discarded */
        if ((received == DATAPACKETSIZE) && (remaining > 0)) {

            TRACE_WARNING(
                      "_UsbDataReceived: %u bytes discarded\n\r",
                      (unsigned int)remaining);
        }
    }
    else {

        TRACE_WARNING( "_UsbDataReceived: Transfer error\n\r");
    }
}

/**
 * Callback invoked when data has been sent.
 */
static void _UsbDataSent(void)
{
    txDoneFlag = 1;
}

/**
 * \brief DMA RX callback function
 */
static void _UsDmaRxCallback( uint8_t status, void *pArg)
{
    pArg = pArg;
    if (status != DMAD_OK) return;

    /* Disable timer */
    TC_Stop(TC0, 0);

    /* Send buffer through the USB */
    while (CDCDSerialDriver_Write(usartBuffers[usartCurrentBuffer],
                            DATAPACKETSIZE, 0, 0) != USBD_STATUS_SUCCESS);

    /* Restart read on buffer */
    usartCurrentBuffer = 1 - usartCurrentBuffer;
    _UsartDmaRx(usartCurrentBuffer);

    /* Restart timer */
    TC_Start(TC0, 0);

}

/**
 * \brief DMA TX callback function
 */
static void _UsDmaTxCallback( uint8_t status, void *pArg)
{
    pArg = pArg;
    if (status != DMAD_OK) return;

    /* Restart USB read */
    CDCDSerialDriver_Read(usbBuffer,
                          DATAPACKETSIZE,
                          (TransferCallback) _UsbDataReceived,
                          0);
}

/**
 * \brief DMA driver configuration
 */
static void _ConfigureDma( void )
{
    sDmad *pDmad = &dmad;
    uint32_t dwCfg;
    uint8_t iController;
    /* Driver initialize */
    DMAD_Initialize( &dmad, 0 );
    /* IRQ configure */
    IRQ_ConfigureIT(ID_DMAC0, 0, ISR_DMA);
    IRQ_ConfigureIT(ID_DMAC1, 0, ISR_DMA);
    IRQ_EnableIT(ID_DMAC0);
    IRQ_EnableIT(ID_DMAC1);
    /* Allocate DMA channels for USART */
    usartDmaTxChannel = DMAD_AllocateChannel( pDmad,
                                              DMAD_TRANSFER_MEMORY, ID_USART);
    usartDmaRxChannel = DMAD_AllocateChannel( pDmad,
                                              ID_USART, DMAD_TRANSFER_MEMORY);
    if (   usartDmaTxChannel == DMAD_ALLOC_FAILED 
        || usartDmaRxChannel == DMAD_ALLOC_FAILED )
    {
        printf("DMA channel allocat error\n\r");
        while(1);
    }

    /* Set RX callback */
    DMAD_SetCallback(pDmad, usartDmaRxChannel,
                    (DmadTransferCallback)_UsDmaRxCallback, 0);
    /* Set TX callback */
    DMAD_SetCallback(pDmad, usartDmaTxChannel,
                    (DmadTransferCallback)_UsDmaTxCallback, 0);

    /* Configure DMA RX channel */
    iController = (usartDmaRxChannel >> 8);
    dwCfg = 0
           | DMAC_CFG_SRC_PER(
              DMAIF_Get_ChannelNumber( iController, ID_USART, DMAD_TRANSFER_RX ))
           | DMAC_CFG_SRC_H2SEL
           | DMAC_CFG_SOD
           | DMAC_CFG_FIFOCFG_ASAP_CFG;
    DMAD_PrepareChannel( pDmad, usartDmaRxChannel, dwCfg );
    /* Configure DMA TX channel */
    iController = (usartDmaTxChannel >> 8);
    dwCfg = 0
           | DMAC_CFG_DST_PER(
              DMAIF_Get_ChannelNumber( iController, ID_USART, DMAD_TRANSFER_TX ))
           | DMAC_CFG_DST_H2SEL
           | DMAC_CFG_SOD
           | DMAC_CFG_FIFOCFG_ALAP_CFG;
    DMAD_PrepareChannel( pDmad, usartDmaTxChannel, dwCfg );
}

/**
 * Configure USART to work @ 115200
 */
static void _ConfigureUsart(void)
{
    PIO_Configure(pins, PIO_LISTSIZE(pins));
    PMC_EnablePeripheral(ID_USART);
    USART_DisableIt(BASE_USART, 0xFFFFFFFF);
    USART_Configure(BASE_USART,
                    USART_MODE_ASYNCHRONOUS,
                    115200,
                    BOARD_MCK);

    USART_SetTransmitterEnabled(BASE_USART, 1);
    USART_SetReceiverEnabled(BASE_USART, 1);

    IRQ_ConfigureIT(ID_USART, 0, ISR_USART);
    IRQ_EnableIT(ID_USART);
}

/**
 * Configure TC0 to generate an interrupt every 4ms
 */
static void _ConfigureTc0(void)
{
    uint32_t div, tcclks;

    /* Enable TC0 peripheral */
    PMC_EnablePeripheral(ID_TC0);
    /* Configure TC0 for 250Hz frequency and trigger on RC compare */
    TC_FindMckDivisor(250, BOARD_MCK, &div, &tcclks, BOARD_MCK);
    TC_Configure(TC0, 0, tcclks | TC_CMR_CPCTRG);
    TC0->TC_CHANNEL[0].TC_RC = (BOARD_MCK / div) / 250;
    /* Configure and enable interrupt on RC compare */
    IRQ_ConfigureIT(ID_TC0, 0, ISR_TC0);
    IRQ_EnableIT(ID_TC0);
    TC0->TC_CHANNEL[0].TC_IER = TC_IER_CPCS;
    /* Start TC when USB connected & RX enabled */
}

/**
 * Test USB CDC Serial Speed
 */
static void _TestSpeed(void)
{
    uint32_t startT, endT;
    uint32_t i, testCnt;

    if (!isCdcSerialON)
    {
        printf("\n\r!! Host serial program not ready!\n\r");
        return;
    }
    printf("\n\r- USB CDC Serial Speed test:\n\r");

    /* Test data initialize */
    for (i = 0; i < TEST_BUFFER_SIZE; i ++) testBuffer[i] = (i % 10) + '0';

    printf("- Send 0,1,2 ... to host:\n\r");
    startT = tcTick;
    for (testCnt = 0; testCnt < TEST_COUNT; testCnt ++)
    {
        txDoneFlag = 0;
        CDCDSerialDriver_Write(testBuffer,
                               TEST_BUFFER_SIZE,
                               (TransferCallback) _UsbDataSent, 0);
        while(!txDoneFlag);
    }
    /* Finish sending */
    CDCDSerialDriver_Write(testBuffer, 0, 0, 0);
    endT = tcTick;
    printf("- Done: Size %d, Count %u, Time %u ~ %u\n\r",
        TEST_BUFFER_SIZE, (unsigned int)testCnt, (unsigned int)startT, (unsigned int)endT);
    printf("- Speed %uKB/s\n\r",
        (unsigned int)((TEST_BUFFER_SIZE*testCnt)/4/(endT-startT)));
}

/*----------------------------------------------------------------------------
 *          Main
 *----------------------------------------------------------------------------*/

/**
 * \brief usb_cdc_serial Application entry point.
 *
 * Initializes drivers and start the USB <-> Serial bridge.
 */
int main(void)
{
    uint8_t isUsbConnected = 0;

    /* Disable watchdog */
    WDT_Disable( WDT );

    /* Enable MMU, Icache and Dcache */
    MMU_Initialize((uint32_t *)0x30C000);
    CP15_EnableMMU();
    CP15_EnableDcache();
    CP15_EnableIcache();

    /* Output example information */
    printf("-- USB Device CDC Serial Project %s --\n\r", SOFTPACK_VERSION);
    printf("-- %s\n\r", BOARD_NAME);
    printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

    /* If they are present, configure Vbus & Wake-up pins */
    PIO_InitializeInterrupts(0);

    /* Initialize all USB power (off) */
    USBPower_Configure();

    /* Configure DMA driver */
    _ConfigureDma();

    /* Configure USART */
    _ConfigureUsart();
    _UsartDmaRxSetup();

    /* Configure timer 0 */
    _ConfigureTc0();

    /* CDC serial driver initialization */
    CDCDSerialDriver_Initialize(&cdcdSerialDriverDescriptors);

    /* Help informaiton */
    _DebugHelp();

    /* connect if needed */
    VBus_Configure();

    /* Driver loop */
    while (1)
    {
        /* Device is not configured */
        if (USBD_GetState() < USBD_STATE_CONFIGURED)
        {
            if (isUsbConnected)
            {
                isUsbConnected = 0;
                isCdcSerialON  = 0;
                TC_Stop(TC0, 0);
            }
        }
        else if (isUsbConnected == 0)
        {
            isUsbConnected = 1;
            TC_Start(TC0, 0);
        }

        /* Serial port ON/OFF */
        if (CDCDSerialDriver_GetControlLineState() & CDCControlLineState_DTR)
        {
            if (!isCdcSerialON)
            {
                isCdcSerialON = 1;

                /* Start receiving data on the USART */
                usartCurrentBuffer = 0;
                _UsartDmaRx( usartCurrentBuffer );
                USART_EnableIt( BASE_USART, US_CSR_FRAME | US_CSR_OVRE );
                /* Start receiving data on the USB */
                CDCDSerialDriver_Read(usbBuffer,
                                      DATAPACKETSIZE,
                                      (TransferCallback) _UsbDataReceived,
                                      0);
            }
        }
        else if (isCdcSerialON)
        {
            isCdcSerialON = 0;
        }

        if (DBGU_IsRxReady())
        {
            uint8_t key = DBGU_GetChar();
            /* ESC: CDC Echo ON/OFF */
            if (key == 27) {
                printf("** CDC Echo %s\n\r",
                       isCdcEchoON ? "OFF" : "ON");
                isCdcEchoON = !isCdcEchoON;
            }
            /* 't': Test CDC writing speed */
            else if (key == 't')
            {
                _TestSpeed();
            }
            else
            {
                printf("Alive\n\r");
                CDCDSerialDriver_Write((char*)"Alive\n\r", 8, 0, 0);
                _DebugHelp();
            }
        }
    }
}
/** \endcond */

0 个答案:

没有答案