SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
00028 /* ---------------------------------------------------------------------------- */
00029 
00030 /** \cond usb_cdc
00031  *  \page usb_cdc USB communication device class Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  The USB communication device class Example will help you to get familiar
00036  *  with the USB communication device class on SAMV7/E7 Microcontrollers. Also
00037  *  it can help you to be familiar with the USB Framework that is used for
00038  *  rapid development of USB-compliant class drivers such as USB communication
00039  *  device class.
00040  *
00041  *  \section Requirements
00042  *
00043  *  This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained
00044  *  board.
00045  *  The device uses the USB communication device class (CDC) drivers to take
00046  *  advantage of the installed PC RS-232 software to talk over the USB.
00047  *  The example is a bridge between a USART (USART2) from the main microchip
00048  *  and the USB host CDC interface.
00049  *
00050  *  \section Usage
00051  *
00052  *  -# Build the program and download it inside the board.
00053  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00054  *  -# On the computer, open and configure a terminal application
00055  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00056  *    - 115200 baud rate
00057  *    - 8 bits of data
00058  *    - No parity
00059  *    - 1 stop bit
00060  *    - No flow control
00061  *  -# Start the application.
00062  *  -# In the terminal window, the following text should appear:
00063  *  \code
00064  *  -- USB Device CDC Serial Project xxx --
00065  *  -- SAMxxxxx-xx
00066  *  -- Compiled: xxx xx xxxx xx:xx:xx --
00067  *  \endcode
00068  *
00069  *  \section References
00070  *  - usb_cdc/main.c
00071  *  - usb: USB Framework, USB communication device class driver
00072  *      - \ref usbd_framework
00073  *      - \ref usbd_api
00074  *      - \ref usbd_cdc
00075  */
00076 
00077 /**
00078  *  \file
00079  *
00080  *  This file contains all the specific code for the
00081  *  usb_cdc example.
00082  */
00083 /*----------------------------------------------------------------------------
00084  *         Headers
00085  *----------------------------------------------------------------------------*/
00086 
00087 #include "board.h"
00088 
00089 #include "USBD.h"
00090 #include "CDCDSerialDriver.h"
00091 
00092 #include <stdbool.h>
00093 #include <stdint.h>
00094 
00095 /*----------------------------------------------------------------------------
00096  *      Definitions
00097  *----------------------------------------------------------------------------*/
00098 
00099 /** Size in bytes of the packet used for reading data from the USB & USART */
00100 #define DATAPACKETSIZE (128)
00101 
00102 /** Size in bytes of the buffer used for reading data from the USB & USART */
00103 #define DATABUFFERSIZE (DATAPACKETSIZE+2)
00104 
00105 /** Pins used for USART transfer */
00106 #define PINS_USART      PIN_USART2_TXD, PIN_USART2_RXD
00107 /** Register base for USART operation */
00108 #define BASE_USART      USART2
00109 /** USART ID */
00110 #define ID_USART        ID_USART2
00111 
00112 #define USART_TIMEOUT           115200
00113 /** test buffer size */
00114 #define TEST_BUFFER_SIZE    (2*1024)
00115 /** write loop count */
00116 #define TEST_COUNT          (30)
00117 
00118 /*----------------------------------------------------------------------------
00119  *      External variables
00120  *----------------------------------------------------------------------------*/
00121 
00122 extern const USBDDriverDescriptors cdcdSerialDriverDescriptors;
00123 
00124 /*----------------------------------------------------------------------------
00125  *      Internal variables
00126  *----------------------------------------------------------------------------*/
00127 
00128 /** Global DMA driver for all transfer */
00129 static sXdmad dmad;
00130 
00131 /** DMA channel for RX */
00132 static uint32_t usartDmaRxChannel;
00133 /** DMA channel for TX */
00134 static uint32_t usartDmaTxChannel;
00135 
00136 /** USART link list for data RX */
00137 static LinkedListDescriporView1 dmaRxLinkList;
00138 
00139 /** List of pins that must be configured for use by the application. */
00140 static const Pin pins[] = {PINS_USART};
00141 
00142 /** Double-buffer for storing incoming USART data. */
00143 COMPILER_ALIGNED(32) static uint8_t usartBuffers[2][DATABUFFERSIZE];
00144 
00145 /** Current USART buffer index. */
00146 static uint8_t usartCurrentBuffer = 0;
00147 
00148 /** Buffer for storing incoming USB data. */
00149 COMPILER_ALIGNED(32) static uint8_t usbBuffer[DATABUFFERSIZE];
00150 
00151 /** Serial Port ON/OFF */
00152 static uint8_t isCdcSerialON = 0;
00153 
00154 /** CDC Echo back ON/OFF */
00155 static uint8_t isCdcEchoON = 0;
00156 
00157 /** USB Tx flag */
00158 static volatile uint8_t txDoneFlag = 0;
00159 /** Test buffer */
00160 COMPILER_ALIGNED(32) static uint8_t testBuffer[TEST_BUFFER_SIZE];
00161 static void _UsartDmaRx(void);
00162 void *memset(void *pBuffer, int value, size_t num);
00163 
00164 /*----------------------------------------------------------------------------
00165  *  Interrupt handlers
00166  *----------------------------------------------------------------------------*/
00167 /**
00168  * ISR for xDMA interrupt
00169  */
00170 void XDMAC_Handler(void)
00171 {
00172     XDMAD_Handler(&dmad);
00173 }
00174 
00175 /**
00176  * USART interrupt handler
00177  */
00178 void USART2_Handler(void)
00179 {
00180     Usart *pUs = BASE_USART;
00181     uint32_t status;
00182     uint16_t serialState;
00183     uint32_t count;
00184 
00185     status  = USART_GetStatus(pUs);
00186     status &= USART_GetItMask(pUs);
00187 
00188     /* If USB device is not configured, do nothing */
00189     if (!isCdcSerialON) {
00190         USART_DisableIt(pUs, 0xFFFFFFFF);
00191         return;
00192     }
00193 
00194     if (status & US_CSR_TIMEOUT) {
00195         /*Clear TIMEOUT Flag and Start Time-out After Next Character Received*/
00196         USART_AcknowledgeRxTimeOut(BASE_USART, 0);
00197         /* Flush the DMA FIFO */
00198         XDMAC_SoftwareFlushReq(dmad.pXdmacs, usartDmaRxChannel);
00199         /* Transfer the last pack through USB */
00200         count = dmad.pXdmacs->XDMAC_CHID[usartDmaRxChannel].XDMAC_CUBC;
00201         SCB_InvalidateDCache_by_Addr((uint32_t *)usartBuffers, DATAPACKETSIZE - count);
00202 
00203         while (CDCDSerialDriver_Write(usartBuffers, DATAPACKETSIZE - count, 0, 0)
00204                != USBD_STATUS_SUCCESS);
00205 
00206         /*Reset DMA transfer*/
00207         XDMAD_StopTransfer(&dmad, usartDmaRxChannel);
00208         _UsartDmaRx();
00209     } else {
00210         /* Errors */
00211         serialState = CDCDSerialDriver_GetSerialState();
00212 
00213         /* Overrun */
00214         if ((status & US_CSR_OVRE) != 0) {
00215             TRACE_WARNING("USART_IrqHandler: Overrun\n\r");
00216             serialState |= CDCSerialState_OVERRUN;
00217         }
00218 
00219         /* Framing error */
00220         if ((status & US_CSR_FRAME) != 0) {
00221             TRACE_WARNING("USART_IrqHandler: Framing error\n\r");
00222             serialState |= CDCSerialState_FRAMING;
00223         }
00224 
00225         CDCDSerialDriver_SetSerialState(serialState);
00226     }
00227 }
00228 
00229 /*-----------------------------------------------------------------------------
00230  *         Callback re-implementation
00231  *-----------------------------------------------------------------------------*/
00232 /**
00233  * Invoked when the configuration of the device changes. Parse used endpoints.
00234  * \param cfgnum New configuration number.
00235  */
00236 void USBDDriverCallbacks_ConfigurationChanged(unsigned char cfgnum)
00237 {
00238     CDCDSerialDriver_ConfigurationChangedHandler(cfgnum);
00239 }
00240 
00241 /**
00242  * Invoked when a new SETUP request is received from the host. Forwards the
00243  * request to the Mass Storage device driver handler function.
00244  * \param request  Pointer to a USBGenericRequest instance.
00245  */
00246 void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
00247 {
00248     CDCDSerialDriver_RequestHandler(request);
00249 }
00250 
00251 /*----------------------------------------------------------------------------
00252  *         Internal functions
00253  *----------------------------------------------------------------------------*/
00254 
00255 
00256 /**
00257  *  \brief Send single buffer data through DMA
00258  */
00259 static void _UsartDmaTx(uint32_t dwDestAddr,
00260                          void *pBuffer, uint16_t wSize)
00261 {
00262     sXdmad *pDmad = &dmad;
00263     /* Setup transfer */
00264     sXdmadCfg xdmadCfg;
00265     xdmadCfg.mbr_ubc = wSize;
00266     xdmadCfg.mbr_sa = (uint32_t) pBuffer;
00267     xdmadCfg.mbr_da = (uint32_t) dwDestAddr;
00268     xdmadCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN
00269                        | XDMAC_CC_MEMSET_NORMAL_MODE
00270                        | XDMAC_CC_DSYNC_MEM2PER
00271                        | XDMAC_CC_CSIZE_CHK_1
00272                        | XDMAC_CC_DWIDTH_BYTE
00273                        | XDMAC_CC_SIF_AHB_IF1
00274                        | XDMAC_CC_DIF_AHB_IF1
00275                        | XDMAC_CC_SAM_INCREMENTED_AM
00276                        | XDMAC_CC_DAM_FIXED_AM
00277                        | XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber(ID_USART, XDMAD_TRANSFER_TX));
00278     xdmadCfg.mbr_bc = 0;
00279     XDMAD_ConfigureTransfer(pDmad, usartDmaTxChannel, &xdmadCfg, 0, 0, (
00280                                  XDMAC_CIE_BIE   |
00281                                  XDMAC_CIE_DIE   |
00282                                  XDMAC_CIE_FIE   |
00283                                  XDMAC_CIE_RBIE  |
00284                                  XDMAC_CIE_WBIE  |
00285                                  XDMAC_CIE_ROIE));
00286 
00287     SCB_CleanDCache_by_Addr((uint32_t *)pBuffer, wSize);
00288 
00289     XDMAD_StartTransfer(pDmad, usartDmaTxChannel);
00290 }
00291 
00292 /**
00293  *  \brief Prepare link list for USART RX
00294  *  Ringed link list initialized for 2 USART buffer.
00295  */
00296 static void _UsartDmaRxSetup(void)
00297 {
00298     Usart *pUs = BASE_USART;
00299     dmaRxLinkList.mbr_ubc = XDMA_UBC_NVIEW_NDV1
00300                             | XDMA_UBC_NDE_FETCH_EN
00301                             | XDMA_UBC_NSEN_UPDATED
00302                             | XDMAC_CUBC_UBLEN(DATAPACKETSIZE);
00303     dmaRxLinkList.mbr_sa  = (uint32_t)&pUs->US_RHR;
00304     dmaRxLinkList.mbr_da = (uint32_t)usartBuffers[0];
00305 
00306 }
00307 
00308 /**
00309  *  \brief Start waiting USART data
00310  *  Start DMA, the 1st DMA buffer is free USART buffer assigned.
00311  */
00312 static void _UsartDmaRx()
00313 {
00314     sXdmad *pDmad = &dmad;
00315     sXdmadCfg xdmadUsartRxCfg;
00316     uint32_t xdmaUsartRxCndc, xdmaInt;
00317     xdmadUsartRxCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN
00318                               | XDMAC_CC_MBSIZE_SINGLE
00319                               | XDMAC_CC_DSYNC_PER2MEM
00320                               | XDMAC_CC_CSIZE_CHK_1
00321                               | XDMAC_CC_DWIDTH_BYTE
00322                               | XDMAC_CC_SIF_AHB_IF1
00323                               | XDMAC_CC_DIF_AHB_IF1
00324                               | XDMAC_CC_SAM_FIXED_AM
00325                               | XDMAC_CC_DAM_INCREMENTED_AM
00326                               | XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber(ID_USART, XDMAD_TRANSFER_RX));
00327     xdmadUsartRxCfg.mbr_sa = dmaRxLinkList.mbr_sa;
00328     xdmadUsartRxCfg.mbr_da = dmaRxLinkList.mbr_da;
00329     xdmadUsartRxCfg.mbr_ubc = DATAPACKETSIZE;
00330     xdmadUsartRxCfg.mbr_bc = 0;
00331     xdmaUsartRxCndc = 0;
00332 
00333 
00334     xdmaInt = XDMAC_CIE_BIE;
00335 
00336     XDMAD_ConfigureTransfer(pDmad, usartDmaRxChannel, &xdmadUsartRxCfg,
00337                              xdmaUsartRxCndc, (uint32_t)&dmaRxLinkList, xdmaInt);
00338     XDMAD_StartTransfer(pDmad, usartDmaRxChannel);
00339 }
00340 
00341 /**
00342  * DBGU help dump
00343  */
00344 static void _DebugHelp(void)
00345 {
00346     printf("\n\r==========================================================\n\r");
00347     printf("-- ESC to Enable/Disable ECHO on cdc serial --\n\r");
00348     printf("-- Press 't' to test transfer --\n\r");
00349     printf("\n\r==========================================================\n\r");
00350 }
00351 
00352 /**
00353  * Callback invoked when data has been sent.
00354  */
00355 static void _UsbDataSent(void)
00356 {
00357     txDoneFlag = 1;
00358 }
00359 /*----------------------------------------------------------------------------
00360  * Callback invoked when data has been received on the USB.
00361  *----------------------------------------------------------------------------*/
00362 static void _UsbDataReceived(uint32_t unused,
00363                              uint8_t status,
00364                              uint32_t received,
00365                              uint32_t remaining)
00366 {
00367     unused = unused;
00368     Usart *pUs = BASE_USART;
00369 
00370     /* Check that data has been received successfully */
00371     if (status == USBD_STATUS_SUCCESS) {
00372 
00373         SCB_InvalidateDCache_by_Addr((uint32_t *)usbBuffer, received);
00374 
00375         /* Send back CDC data */
00376         if (isCdcEchoON) {
00377             while (CDCDSerialDriver_Write(usbBuffer, received, 0, 0)
00378                    != USBD_STATUS_SUCCESS);
00379         }
00380 
00381         /* Send data through USART */
00382         if (isCdcSerialON)
00383             _UsartDmaTx((uint32_t)&pUs->US_THR, usbBuffer, received);
00384 
00385         /* Check if bytes have been discarded */
00386         if ((received == DATAPACKETSIZE) && (remaining > 0)) {
00387             TRACE_WARNING(
00388                 "_UsbDataReceived: %u bytes discarded\n\r",
00389                 (unsigned int)remaining);
00390         }
00391     } else {
00392         TRACE_WARNING("_UsbDataReceived: Transfer error\n\r");
00393     }
00394 }
00395 
00396 
00397 /**
00398  * \brief DMA RX callback function
00399  */
00400 static void _UsDmaRxCallback(uint32_t channel, void *pArg)
00401 {
00402     pArg = pArg;
00403 
00404     if (channel != usartDmaRxChannel)
00405         return;
00406 
00407     /*Clear TIMEOUT Flag and Start Time-out After Next Character Received*/
00408     USART_AcknowledgeRxTimeOut(BASE_USART, 0);
00409 
00410     SCB_InvalidateDCache_by_Addr((uint32_t *)(usartBuffers[usartCurrentBuffer]),
00411                                  DATABUFFERSIZE);
00412 
00413     /* Send buffer through the USB */
00414     while (CDCDSerialDriver_Write(usartBuffers[usartCurrentBuffer],
00415                                   DATAPACKETSIZE, 0, 0) != USBD_STATUS_SUCCESS);
00416 
00417     /* Restart read on buffer */
00418     _UsartDmaRx();
00419 
00420 }
00421 
00422 /**
00423  * \brief DMA TX callback function
00424  */
00425 static void _UsDmaTxCallback(uint32_t channel, void *pArg)
00426 {
00427     pArg = pArg;
00428 
00429     if (channel != usartDmaTxChannel)
00430         return;
00431 
00432     /* Restart USB read */
00433     CDCDSerialDriver_Read(usbBuffer,
00434                           DATAPACKETSIZE,
00435                           (TransferCallback) _UsbDataReceived,
00436                           0);
00437 }
00438 
00439 /**
00440  * \brief DMA driver configuration
00441  */
00442 static void _ConfigureDma(void)
00443 {
00444     sXdmad *pDmad = &dmad;
00445     /* Driver initialize */
00446     XDMAD_Initialize(&dmad, 0);
00447     /* IRQ configure */
00448     NVIC_EnableIRQ(XDMAC_IRQn);
00449 
00450     /* Allocate DMA channels for USART */
00451     usartDmaTxChannel = XDMAD_AllocateChannel(pDmad, XDMAD_TRANSFER_MEMORY,
00452                         ID_USART);
00453     usartDmaRxChannel = XDMAD_AllocateChannel(pDmad, ID_USART,
00454                         XDMAD_TRANSFER_MEMORY);
00455 
00456     /* Set RX callback */
00457     XDMAD_SetCallback(pDmad, usartDmaRxChannel,
00458                       (XdmadTransferCallback)_UsDmaRxCallback,
00459                       0);
00460     /* Set TX callback */
00461     XDMAD_SetCallback(pDmad, usartDmaTxChannel,
00462                       (XdmadTransferCallback)_UsDmaTxCallback,
00463                       0);
00464     XDMAD_PrepareChannel(pDmad, usartDmaRxChannel);
00465     XDMAD_PrepareChannel(pDmad, usartDmaTxChannel);
00466 }
00467 
00468 /**
00469  * Configure USART to work @ 115200
00470  */
00471 static void _ConfigureUsart(void)
00472 {
00473     PIO_Configure(pins, PIO_LISTSIZE(pins));
00474     PMC_EnablePeripheral(ID_USART);
00475     USART_DisableIt(BASE_USART, 0xFFFFFFFF);
00476     USART_Configure(BASE_USART,
00477                     USART_MODE_ASYNCHRONOUS,
00478                     115200,
00479                     BOARD_MCK);
00480 
00481     USART_SetTransmitterEnabled(BASE_USART, 1);
00482     USART_SetReceiverEnabled(BASE_USART, 1);
00483     NVIC_EnableIRQ(USART2_IRQn);
00484 }
00485 
00486 /**
00487  * Test USB CDC Serial
00488  */
00489 static void _SendText(void)
00490 {
00491     uint32_t i, testCnt;
00492 
00493     if (!isCdcSerialON) {
00494         printf("\n\r!! Host serial program not ready!\n\r");
00495         return;
00496     }
00497 
00498     printf("\n\r- USB CDC Serial writing ...\n\r");
00499 
00500     /* Test data initialize */
00501     for (i = 0; i < TEST_BUFFER_SIZE; i ++) testBuffer[i] = (i % 10) + '0';
00502 
00503     printf("- Send 0,1,2 ... to host:\n\r");
00504 
00505     for (testCnt = 0; testCnt < TEST_COUNT; testCnt ++) {
00506         txDoneFlag = 0;
00507         CDCDSerialDriver_Write(testBuffer,
00508                                TEST_BUFFER_SIZE,
00509                                (TransferCallback) _UsbDataSent, 0);
00510 
00511         while (!txDoneFlag);
00512     }
00513 }
00514 
00515 /**
00516  * Configure USBHS settings for USB device
00517  */
00518 static void _ConfigureUotghs(void)
00519 {
00520     /* UTMI parallel mode, High/Full/Low Speed */
00521     /* UOTGCK not used in this configuration (High Speed) */
00522     PMC->PMC_SCDR = PMC_SCDR_USBCLK;
00523     /* USB clock register: USB Clock Input is UTMI PLL */
00524     PMC->PMC_USB = PMC_USB_USBS;
00525     /* Enable peripheral clock for USBHS */
00526     PMC_EnablePeripheral(ID_USBHS);
00527     USBHS->USBHS_CTRL = USBHS_CTRL_UIMOD_DEVICE;
00528     /* Enable PLL 480 MHz */
00529     PMC->CKGR_UCKR = CKGR_UCKR_UPLLEN | CKGR_UCKR_UPLLCOUNT(0xF);
00530 
00531     /* Wait that PLL is considered locked by the PMC */
00532     while (!(PMC->PMC_SR & PMC_SR_LOCKU));
00533 
00534     /* IRQ */
00535     NVIC_EnableIRQ(USBHS_IRQn);
00536 }
00537 
00538 
00539 /*! \brief Main function. Execution starts here.
00540  */
00541 int main(void)
00542 {
00543     uint8_t isUsbConnected = 0;
00544     /* Disable watchdog */
00545     WDT_Disable(WDT);
00546 
00547 
00548     SCB_EnableICache();
00549     SCB_EnableDCache();
00550 
00551     /* Output example information */
00552     printf("-- USB Device CDC Serial Project %s --\n\r", SOFTPACK_VERSION);
00553     printf("-- %s\n\r", BOARD_NAME);
00554     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
00555             COMPILER_NAME);
00556 
00557     /* Initialize PIO interrupts */
00558     PIO_InitializeInterrupts(0);
00559 
00560     /* Interrupt priority */
00561     NVIC_SetPriority(USBHS_IRQn, 2);
00562 
00563     /* Configure DMA driver */
00564     _ConfigureDma();
00565 
00566     /* Configure USART */
00567     _ConfigureUsart();
00568     _UsartDmaRxSetup();
00569 
00570     /* Initialize OTG clocks */
00571     _ConfigureUotghs();
00572 
00573     /* CDC serial driver initialization */
00574     CDCDSerialDriver_Initialize(&cdcdSerialDriverDescriptors);
00575 
00576     /* Help information */
00577     _DebugHelp();
00578 
00579     // Start USB stack to authorize VBus monitoring
00580     USBD_Connect();
00581 
00582     /* Driver loop */
00583     while (1) {
00584         /* Device is not configured */
00585         if (USBD_GetState() < USBD_STATE_CONFIGURED) {
00586             if (isUsbConnected) {
00587                 isUsbConnected = 0;
00588                 isCdcSerialON  = 0;
00589             }
00590         } else if (isUsbConnected == 0)
00591             isUsbConnected = 1;
00592 
00593         /* Serial port ON/OFF */
00594         if (CDCDSerialDriver_GetControlLineState() & CDCControlLineState_DTR) {
00595             if (!isCdcSerialON) {
00596                 isCdcSerialON = 1;
00597 
00598                 /* Start receiving data on the USART */
00599                 _UsartDmaRx();
00600                 USART_EnableIt(BASE_USART, US_CSR_FRAME | US_CSR_OVRE | US_IER_TIMEOUT);
00601                 USART_EnableRecvTimeOut(BASE_USART, USART_TIMEOUT);
00602                 /* Start receiving data on the USB */
00603                 CDCDSerialDriver_Read(usbBuffer,
00604                                       DATAPACKETSIZE,
00605                                       (TransferCallback) _UsbDataReceived,
00606                                       0);
00607             }
00608         } else if (isCdcSerialON)
00609             isCdcSerialON = 0;
00610 
00611         if (DBG_IsRxReady()) {
00612             uint8_t key = DBG_GetChar();
00613 
00614             /* ESC: CDC Echo ON/OFF */
00615             if (key == 27) {
00616                 printf("** CDC Echo %s\n\r",
00617                        isCdcEchoON ? "OFF" : "ON");
00618                 isCdcEchoON = !isCdcEchoON;
00619             }
00620             /* 't': Test CDC writing  */
00621             else if (key == 't')
00622                 _SendText();
00623             else {
00624                 printf("Alive\n\r");
00625 
00626                 while (CDCDSerialDriver_Write((char *)"Alive\n\r", 8, 0, 0)
00627                        != USBD_STATUS_SUCCESS);
00628 
00629                 _DebugHelp();
00630             }
00631         }
00632     }
00633 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines