SAMV71 Xplained Ultra Software Package 1.3

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 /** \cond usb_audio_speaker
00030  *  \page usb_audio_speaker USB Audio Speaker Example
00031  *
00032  *  \section Purpose
00033  *
00034  *  The USB Audio Speaker Example will help you to get familiar with the
00035  *  USB Device Port(UDP) and SSC on SAMv7 Microcontrollers. Also
00036  *  it can help you to be familiar with the USB Framework that is used for
00037  *  rapid development of USB-compliant class drivers such as USB Audio Device
00038  *  class.
00039  *
00040  *  \section Requirements
00041  *
00042  *  This package can be used with SAMV71 Xplained board that have both
00043  *  UDP and SSC.
00044  *
00045  *  \section Description
00046  *
00047  *  The demo simulates an USB Desktop Speaker.
00048  *
00049  *  When an Xplained board running this program connected to a host (PC for 
00050  *  example), with USB cable, the Xplained board appears as a desktop speaker 
00051  *  for the host. Then the host can play sound through host software. The audio 
00052  *  stream from the host is then sent to the Xplained board, and eventually sent
00053  *  to audio DAC connected to the amplifier. 
00054  *
00055  *  \section Usage
00056  *
00057  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. 
00058  *     Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00059  *  -# On the computer, open and configure a terminal application
00060  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00061  *    - 115200 bauds
00062  *    - 8 bits of data
00063  *    - No parity
00064  *    - 1 stop bit
00065  *    - No flow control
00066  *  -# Start the application.
00067  *  -# In the terminal window, the following text should appear:
00068  *  \code
00069  *  -- USB Device Audio Speaker Example xxx --
00070  *  -- SAMxxxxx-xx
00071  *  -- Compiled: xxx xx xxxx xx:xx:xx --
00072  *  \endcode
00073  *  -# When connecting USB cable to windowss, the host
00074  *     reports a new USB device attachment (if it's the first time you connect
00075  *     an audio speaker demo board to your host). You can find new
00076  *     "USB Composite Device" and "USB Audio Device" appear in the hardware
00077  *     device list.
00078  *  -# You can play sound in host side through the USB Audio Device, and it
00079  *     can be heard from the earphone connected to the Xplained board.
00080  *
00081  *  \section References
00082  *  - usb_audio_speaker/main.c
00083  *  - ssc: SSC interface driver
00084  *  - usb: USB Framework, Audio Device Class driver and UDP interface driver
00085  *      - \ref usbd_framework
00086  *         - \ref usbd_api
00087  *      - \ref usbd_audio_speaker
00088  */
00089 
00090 /**
00091  *  \file
00092  *
00093  *  This file contains all the specific code for the
00094  *  usb_audio_speaker example.
00095  */
00096 
00097 /*----------------------------------------------------------------------------
00098  *         Headers
00099  *----------------------------------------------------------------------------*/
00100 
00101 #include "board.h"
00102 
00103 #include "USBD_LEDs.h"
00104 #include "USBD_Config.h"
00105 
00106 #include "AUDDSpeakerDriver.h"
00107 
00108 #include <stdio.h>
00109 #include <stdbool.h>
00110 #include <stdint.h>
00111 #include <string.h>
00112 
00113 /*----------------------------------------------------------------------------
00114  *         Definitions
00115  *----------------------------------------------------------------------------*/
00116 
00117 /**  Number of available audio buffers. */
00118 #define BUFFER_NUMBER       500
00119 /**  Size of one buffer in bytes. */
00120 #define BUFFER_SIZE     (AUDDevice_BYTESPERFRAME)
00121 
00122 /**  Delay in ms for starting the DAC transmission
00123      after a frame has been received. */
00124 #define DAC_DELAY           2
00125 
00126 /** SSC: Number of slots in a frame */
00127 #define SLOT_BY_FRAME           (2)
00128 /** SSC: Number of bits in a slot */
00129 #define BITS_BY_SLOT            (16)
00130 #define I2S_SLAVE_TX_SETTING       ((SSC_TCMR_CKS_TK) |             \
00131                                 (SSC_TCMR_CKO_NONE) |            \
00132                                 (SSC_TCMR_START_TF_EDGE) |       \
00133                                 (SSC_TCMR_STTDLY(1)) |           \
00134                                 (SSC_TCMR_PERIOD(0)))
00135 
00136 #define I2S_SLAVE_TX_FRM_SETTING  ((SSC_TFMR_DATLEN(BITS_BY_SLOT - 1)) |  \
00137                                 (SSC_TFMR_MSBF) |                      \
00138                                 (SSC_TFMR_DATNB(SLOT_BY_FRAME - 1)) |  \
00139                                 (SSC_TFMR_FSOS_NONE))
00140 
00141 
00142 #define I2S_SLAVE_RX_SETTING       ((SSC_RCMR_CKS_TK) |       \
00143                                 (SSC_RCMR_CKO_NONE) |      \
00144                                 (SSC_RCMR_CKI) |           \
00145                                 (SSC_RCMR_START_RF_EDGE) | \
00146                                 (SSC_RCMR_STTDLY(1)) |     \
00147                                 (SSC_RCMR_PERIOD(0)))
00148 
00149 #define I2S_SLAVE_RX_FRM_SETTING   ((SSC_RFMR_DATLEN(BITS_BY_SLOT - 1)) | \
00150                                 (SSC_RFMR_MSBF) |                      \
00151                                 (SSC_RFMR_DATNB(SLOT_BY_FRAME - 1)) |  \
00152                                 (SSC_RFMR_FSOS_NONE))
00153 
00154 /** TWI clock */
00155 #define TWI_CLOCK               400000
00156 /** Audio sample rate */
00157 #define SAMPLE_RATE             (48000)
00158 
00159 /*----------------------------------------------------------------------------
00160  *         External variables
00161  *----------------------------------------------------------------------------*/
00162 
00163 /** Descriptor list for USB Audio Speaker Device Driver */
00164 extern const USBDDriverDescriptors auddSpeakerDriverDescriptors;
00165 
00166 /*----------------------------------------------------------------------------
00167  *         Internal variables
00168  *----------------------------------------------------------------------------*/
00169 
00170 /**  Data buffers for receiving audio frames from the USB host. */
00171 
00172 static uint8_t buffers[BUFFER_NUMBER][BUFFER_SIZE];
00173 
00174 /**  Number of samples stored in each data buffer. */
00175 static volatile uint32_t bufferSizes[BUFFER_NUMBER];
00176 /**  Next buffer in which USB data can be stored. */
00177 static volatile uint32_t inBufferIndex = 0;
00178 /**  Next buffer which should be sent to the DAC. */
00179 static uint32_t outBufferIndex = 0;
00180 /**  Number of buffers that can be sent to the DAC. */
00181 static volatile uint32_t numBuffersToSend = 0;
00182 
00183 /**  Current state of the DAC transmission. */
00184 static volatile uint32_t isDacActive = 0;
00185 static volatile uint32_t isFirstFrame = 1;
00186 /**  Number of buffers to wait for before the DAC starts to transmit data. */
00187 static volatile uint32_t dacDelay;
00188 
00189 /** Twi instance*/
00190 static Twid twid;
00191 /** Global DMA driver for all transfer */
00192 static sXdmad dmad;
00193 /** DMA channel for TX */
00194 static uint32_t sscDmaTxChannel;
00195 
00196 /** List of pins to configure. */
00197 static const Pin pinsAudio[] = { PIN_TWI_TWD0, 
00198                                 PIN_TWI_TWCK0, 
00199                                 PIN_SSC_TD,  
00200                                 PIN_SSC_TK,
00201                                 PIN_SSC_TF, 
00202                                 PIN_SSC_RD, 
00203                                 PIN_SSC_RK, 
00204                                 PIN_SSC_RF, 
00205                                 PIN_PCK2};
00206 
00207 /*----------------------------------------------------------------------------
00208  *         Internal functions
00209  *----------------------------------------------------------------------------*/
00210 /**
00211  * Interrupt handler for the XDMAC.
00212  */
00213 void XDMAC_Handler( void )
00214 {
00215     XDMAD_Handler( &dmad );
00216 }
00217 
00218 /**
00219  *  \brief Start DMA sending/waiting data.
00220  */
00221 static void _SscDma(volatile uint32_t *pReg, uint32_t dmaChannel,  
00222                     void* pBuffer, uint16_t wSize)
00223 {
00224     sXdmad *pDmad = &dmad;
00225     sXdmadCfg xdmadCfg;
00226     
00227     xdmadCfg.mbr_ubc = wSize ;
00228     xdmadCfg.mbr_sa = (uint32_t) pBuffer;
00229     xdmadCfg.mbr_da = (uint32_t) pReg;
00230     xdmadCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN 
00231                     | XDMAC_CC_MBSIZE_SINGLE
00232                     | XDMAC_CC_DSYNC_MEM2PER
00233                     | XDMAC_CC_CSIZE_CHK_1 
00234                     | XDMAC_CC_DWIDTH_HALFWORD 
00235                     | XDMAC_CC_SIF_AHB_IF0 
00236                     | XDMAC_CC_DIF_AHB_IF1 
00237                     | XDMAC_CC_SAM_INCREMENTED_AM
00238                     | XDMAC_CC_DAM_FIXED_AM 
00239                     | XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber( 
00240                         ID_SSC, XDMAD_TRANSFER_TX));
00241     xdmadCfg.mbr_bc = 0;
00242     xdmadCfg.mbr_ds = 0;
00243     xdmadCfg.mbr_sus = 0;
00244     xdmadCfg.mbr_dus = 0;
00245    
00246     memory_sync();
00247     XDMAD_ConfigureTransfer( pDmad, dmaChannel, &xdmadCfg, 0, 0,(XDMAC_CIE_BIE   |
00248                                                              XDMAC_CIE_DIE   |
00249                                                              XDMAC_CIE_FIE   |
00250                                                              XDMAC_CIE_RBIE  |
00251                                                              XDMAC_CIE_WBIE  |
00252                                                              XDMAC_CIE_ROIE));
00253 
00254     SCB_CleanDCache();
00255     XDMAD_StartTransfer( pDmad, dmaChannel );
00256     
00257 }
00258 
00259 /**
00260  *  \brief DMA TX callback
00261  */
00262 static void _SscTxCallback(uint8_t status, void* pArg)
00263 {
00264     sXdmad *pDmad = &dmad;
00265     Xdmac *pXdmac = pDmad->pXdmacs;
00266     if (status != XDMAD_OK) return;
00267     pArg = pArg; /*dummy */
00268     if (numBuffersToSend == 0) {
00269         /* End of transmission */
00270         isDacActive = 0;
00271         return;
00272     }
00273     /* Load next buffer */
00274    
00275     XDMAC_SetSourceAddr(pXdmac, sscDmaTxChannel,  (uint32_t) buffers[outBufferIndex]);
00276     XDMAC_SetMicroblockControl(pXdmac, sscDmaTxChannel, bufferSizes[outBufferIndex]);
00277     outBufferIndex = (outBufferIndex + 1) % BUFFER_NUMBER;
00278     numBuffersToSend --;
00279     memory_sync();
00280     XDMAD_StartTransfer( pDmad, sscDmaTxChannel );
00281 }
00282 
00283 /**
00284  * \brief DMA driver configuration
00285  */
00286 static void _ConfigureDma( void )
00287 {
00288     sXdmad *pDmad = &dmad;
00289     /* Driver initialize */
00290     XDMAD_Initialize( pDmad, 0 );
00291     /* IRQ configure */
00292     NVIC_ClearPendingIRQ(XDMAC_IRQn);
00293     NVIC_SetPriority(XDMAC_IRQn, 1);
00294     NVIC_EnableIRQ(XDMAC_IRQn);
00295 
00296     /* Allocate DMA channels for SSC */
00297     sscDmaTxChannel = XDMAD_AllocateChannel( pDmad, XDMAD_TRANSFER_MEMORY, ID_SSC);
00298     if (sscDmaTxChannel == XDMAD_ALLOC_FAILED ) {
00299         printf("xDMA channel allocation error\n\r");
00300         while(1);
00301     }
00302    
00303     /* Set TX callback */
00304     XDMAD_SetCallback(pDmad, sscDmaTxChannel,(XdmadTransferCallback)_SscTxCallback, 0);
00305     XDMAD_PrepareChannel(pDmad, sscDmaTxChannel );
00306 }
00307 
00308 /**
00309  * Enable/Disable audio channels
00310  */
00311 static void AudioPlayEnable(uint8_t enable)
00312 {
00313     if (enable == 1) {
00314         SSC_EnableTransmitter(SSC);
00315     } else if (enable == 0) {
00316         SSC_DisableTransmitter(SSC);
00317     }
00318 }
00319 
00320 /**
00321  * Adjust codec for sync
00322  * \param adjust Sync case: default(0)/faster(>0)/slower(<0)
00323  */
00324 static void _SyncAdjust(int32_t adjust)
00325 {
00326     if (adjust > 0) {
00327         /* Fractional multiply for FLL_K, Fref = 0x8000 (1/2) */
00328         WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_FLL_CRTL3, 0xFF00);
00329         /* FLL_GAIN=0, FLL_N=187 */
00330         return;
00331     }
00332     if (adjust < 0) {
00333         /* Fractional multiply for FLL_K, Fref = 0x8000 (1/2) */
00334         WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_FLL_CRTL3, 0x5000);
00335         /* FLL_GAIN=0, FLL_N=187 */
00336         return;
00337     }
00338     /* Default: 32K -> 48K*256, FLL: 32768*187.5/16/8
00339      */
00340     /* FLL_FRATIO=4 (/16), FLL_OUTDIV= 7 (/8) */
00341     /* Fractional multiply for FLL_K, Fref = 0x8000 (1/2) */
00342     WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_FLL_CRTL3, 0x8000 + 0x3000);
00343     /* FLL_GAIN=0, FLL_N=187 */
00344     return; 
00345 }
00346 
00347 /**
00348  * Configure the TWI and DACC for audio output.
00349  * \param sampleRate Audio sample rate.
00350  * \param mck        MCK frequency.
00351  */
00352 static void _ConfigureAudioPlay(uint32_t sampleRate, uint32_t mck)
00353 {
00354     /* -- Pins Configuration -- */
00355     PIO_Configure(pinsAudio, PIO_LISTSIZE(pinsAudio));
00356 
00357     /* -- SSC Configuration -- */
00358     sampleRate = sampleRate; /*dummy */
00359     SSC_Configure(SSC,0, mck);
00360     SSC_DisableTransmitter(SSC);
00361     SSC_ConfigureTransmitter(SSC, I2S_SLAVE_TX_SETTING,I2S_SLAVE_TX_FRM_SETTING);
00362     SSC_ConfigureReceiver(SSC,I2S_SLAVE_RX_SETTING,I2S_SLAVE_RX_FRM_SETTING);
00363     
00364     /* Enable TWI peripheral clock */
00365     PMC_EnablePeripheral(ID_TWIHS0);
00366     /* Configure and enable the TWI (required for accessing the DAC) */
00367     TWI_ConfigureMaster(TWIHS0, TWI_CLOCK, mck);
00368     TWID_Initialize(&twid, TWIHS0);
00369     
00370     /* Initialize the audio DAC
00371      */
00372 
00373     WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_RESET, 0);
00374     Wait(100);
00375     /* WM8904 as master */
00376     if(WM8904_Read(&twid, WM8904_SLAVE_ADDRESS, 0)!=0x8904) {
00377           printf("WM8904 not found!\n\r");
00378           while(1);
00379     }
00380     WM8904_Init(&twid, WM8904_SLAVE_ADDRESS,PMC_MCKR_CSS_SLOW_CLK);
00381     _SyncAdjust(0);
00382     PMC_ConfigurePCK2(PMC_MCKR_CSS_SLOW_CLK, PMC_MCKR_PRES_CLK_1 );
00383     /* Mute */
00384     AudioPlayEnable(0);
00385 }
00386 
00387 /**
00388  *  Invoked when a frame has been received.
00389  */
00390 static void FrameReceived(uint32_t unused,
00391                         uint8_t status,
00392                         uint32_t transferred,
00393                         uint32_t remaining)
00394 {
00395     unused = unused; /*dummy */
00396     remaining = remaining; /*dummy */
00397     if (status == USBD_STATUS_SUCCESS) {
00398         bufferSizes[inBufferIndex] = transferred / AUDDevice_BYTESPERSAMPLE;
00399         inBufferIndex = (inBufferIndex + 1) % BUFFER_NUMBER;
00400         numBuffersToSend++;
00401         /* Start DAc transmission if necessary */
00402         if (!isDacActive) {
00403             dacDelay = DAC_DELAY;
00404             isDacActive = 1;
00405         } else if (dacDelay > 0) {
00406             /* Wait until a few buffers have been received */
00407             dacDelay--;
00408             } else if (isFirstFrame) {
00409                 /* Start sending buffers */
00410                 isFirstFrame = 0;
00411                 AudioPlayEnable(1);
00412                 _SscDma(&(SSC->SSC_THR), sscDmaTxChannel, 
00413                         buffers[outBufferIndex], bufferSizes[outBufferIndex]);
00414                 outBufferIndex = (outBufferIndex + 1) % BUFFER_NUMBER;
00415                 numBuffersToSend --;
00416             }
00417         } else if (status == USBD_STATUS_ABORTED) {
00418             /* Error , ABORT, add NULL buffer */
00419             bufferSizes[inBufferIndex] = 0;
00420         } else {
00421     /* Packet is discarded */
00422     }
00423     /* Receive next packet */
00424     AUDDSpeakerDriver_Read(buffers[inBufferIndex],
00425                            AUDDevice_BYTESPERFRAME,
00426                            (TransferCallback) FrameReceived,
00427                            0); // No optional argument
00428 }
00429 
00430 /*----------------------------------------------------------------------------
00431  *         Callbacks re-implementation
00432  *----------------------------------------------------------------------------*/
00433 /**
00434  * Invoked when the configuration of the device changes. Parse used endpoints.
00435  * \param cfgnum New configuration number.
00436  */
00437 void USBDDriverCallbacks_ConfigurationChanged(unsigned char cfgnum)
00438 {
00439     AUDDSpeakerDriver_ConfigurationChangeHandler(cfgnum);
00440 }
00441 
00442 /**
00443  * Invoked whenever the active setting of an interface is changed by the
00444  * host. Reset streaming interface.
00445  * \param interface Interface number.
00446  * \param setting Newly active setting.
00447  */
00448 void USBDDriverCallbacks_InterfaceSettingChanged(unsigned char interface,
00449                                              unsigned char setting)
00450 {
00451     AUDDSpeakerDriver_InterfaceSettingChangedHandler(interface, setting);
00452 }
00453 
00454 /**
00455  *  Invoked whenever a SETUP request is received from the host. Forwards the
00456  *  request to the standard handler.
00457  */
00458 void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
00459 {
00460     AUDDSpeakerDriver_RequestHandler(request);
00461 }
00462 
00463 /**
00464  *  Invoked when an audio channel get muted or unmuted. Mutes/unmutes the
00465  *  channel at the DAC level.
00466  *  \param channel  Channel number that changed.
00467  *  \param muted    Indicates the new mute status of the channel.
00468  */
00469 void AUDDSpeakerDriver_MuteChanged(uint8_t channel, uint8_t muted)
00470 {
00471     /* Speaker Master channel */
00472     if (channel == AUDDSpeakerDriver_MASTERCHANNEL) {
00473         if (muted) {
00474             AudioPlayEnable(0);
00475             TRACE_WARNING("MuteMaster ");
00476         } else {
00477             TRACE_INFO("UnmuteMaster ");
00478             AudioPlayEnable(1);
00479         }
00480     }
00481 }
00482 
00483 /**
00484  *  Invoked when an audio streaming interface setting changed. Actually control
00485  *  streaming rate.
00486  *  \param newSetting  New stream (interface) setting.
00487  */
00488 void AUDDSpeakerDriver_StreamSettingChanged(uint8_t newSetting)
00489 {
00490     if (newSetting) {
00491         LED_Set(USBD_LEDOTHER);
00492         numBuffersToSend = 0;
00493     } else {
00494         LED_Clear(USBD_LEDOTHER);
00495     }
00496 }
00497 
00498 /**
00499  * Configure clock settings for USB device
00500  */
00501 static void _ConfigureUsbhs(void)
00502 {
00503     /* UTMI parallel mode, High/Full/Low Speed */
00504     /* UUSBCK not used in this configuration (High Speed) */
00505     PMC->PMC_SCDR = PMC_SCDR_USBCLK;
00506     /* USB clock register: USB Clock Input is UTMI PLL */
00507     PMC->PMC_USB = PMC_USB_USBS;
00508     /* Enable peripheral clock for USBHS */
00509     PMC_EnablePeripheral(ID_USBHS);
00510     USBHS->USBHS_CTRL = USBHS_CTRL_UIMOD_DEVICE;
00511     /* Enable PLL 480 MHz */
00512     PMC->CKGR_UCKR = CKGR_UCKR_UPLLEN | CKGR_UCKR_UPLLCOUNT(0xF);
00513     /* Wait that PLL is considered locked by the PMC */
00514     while( !(PMC->PMC_SR & PMC_SR_LOCKU) );
00515     /* IRQ */
00516     NVIC_EnableIRQ(USBHS_IRQn) ;
00517 }
00518 
00519 /*----------------------------------------------------------------------------
00520  *         Exported functions
00521  *----------------------------------------------------------------------------*/
00522 /**
00523  *  \brief usb_audio_speaker Application entry point.
00524  *
00525  *  Starts the driver and waits for an audio input stream to forward to the DAC.
00526  */
00527 int main(void)
00528 {
00529     volatile uint8_t usbConn = 0;
00530     volatile uint8_t audioOn = 0;
00531     volatile uint32_t num = 0;
00532     int32_t  numDiff = 0, prevDiff = 0;
00533     int8_t   clockAdjust = 0;
00534 
00535     /* Disable watchdog */
00536     WDT_Disable( WDT );
00537 
00538     printf("-- USB Device Audio Speaker Example %s --\n\r", SOFTPACK_VERSION);
00539     printf("-- %s\n\r", BOARD_NAME);
00540     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00541     
00542     SCB_EnableICache();
00543     SCB_EnableDCache();
00544 
00545     /* LED */
00546     LED_Configure(LED_YELLOW0);
00547 
00548     TimeTick_Configure();
00549     /* Interrupt priority */
00550     NVIC_SetPriority( USBHS_IRQn, 2 );
00551 
00552     /* Initialize all USB power (off) */
00553     _ConfigureUsbhs();
00554     /* Configure Audio */
00555     _ConfigureAudioPlay(AUDDevice_SAMPLERATE, BOARD_MCK);
00556 
00557     /* Configure DMA */
00558     _ConfigureDma();
00559 
00560     /* USB audio driver initialization */
00561     AUDDSpeakerDriver_Initialize(&auddSpeakerDriverDescriptors);
00562     
00563     // Start USB stack to authorize VBus monitoring
00564     USBD_Connect();
00565     
00566     /* Infinite loop */
00567     while (1) {
00568         if (USBD_GetState() < USBD_STATE_CONFIGURED) {
00569             usbConn = 0;
00570             continue;
00571         }
00572         if (audioOn) {
00573             if(isDacActive == 0) {
00574                 AudioPlayEnable(0);
00575                 printf("audE ");
00576                 isFirstFrame = 1;
00577                 audioOn = 0;
00578             } else {
00579                 if (num != numBuffersToSend) {
00580                     num = numBuffersToSend;
00581                 }
00582                 numDiff = numBuffersToSend - DAC_DELAY;
00583                 if (prevDiff != numDiff) {
00584                     prevDiff = numDiff;
00585                     if (numDiff > 0 && clockAdjust != 1) {
00586                       printf("+");
00587                         /* USB too fast or SSC too slow: faster clock */
00588                         clockAdjust = 1;
00589                         _SyncAdjust(1);
00590                     }
00591                     if (numDiff < 0 && clockAdjust != -1) {
00592                       printf("-");
00593                         /* USB too slow or SSC too fast: slower clock */
00594                         clockAdjust = -1;
00595                         _SyncAdjust(-1);
00596                     }
00597                     if (numDiff == 0 && clockAdjust != 0) {
00598                         clockAdjust = 0;
00599                         _SyncAdjust(0);
00600                     }
00601                 }
00602             }
00603         } else if (isDacActive) {
00604             printf("audS ");
00605             audioOn = 1;
00606         }
00607 
00608         if (usbConn == 0) {
00609             usbConn = 1;
00610             /* Start Reading the incoming audio stream */
00611             AUDDSpeakerDriver_Read(buffers[inBufferIndex],
00612                                 AUDDevice_BYTESPERFRAME,
00613                                 (TransferCallback) FrameReceived,
00614                                 0); // No optional argument
00615         }
00616     }
00617 }
00618 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines