SAMV71 Xplained Ultra Software Package 1.4

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(uint32_t channel, void* pArg)
00263 {
00264     sXdmad *pDmad = &dmad;
00265     Xdmac *pXdmac = pDmad->pXdmacs;
00266     pArg = pArg; /*dummy */
00267     if (numBuffersToSend == 0) {
00268         /* End of transmission */
00269         isDacActive = 0;
00270         return;
00271     }
00272     /* Load next buffer */
00273    
00274     XDMAC_SetSourceAddr(pXdmac, sscDmaTxChannel,  (uint32_t) buffers[outBufferIndex]);
00275     XDMAC_SetMicroblockControl(pXdmac, sscDmaTxChannel, bufferSizes[outBufferIndex]);
00276     outBufferIndex = (outBufferIndex + 1) % BUFFER_NUMBER;
00277     numBuffersToSend --;
00278     memory_sync();
00279     XDMAD_StartTransfer( pDmad, sscDmaTxChannel );
00280 }
00281 
00282 /**
00283  * \brief DMA driver configuration
00284  */
00285 static void _ConfigureDma( void )
00286 {
00287     sXdmad *pDmad = &dmad;
00288     /* Driver initialize */
00289     XDMAD_Initialize( pDmad, 0 );
00290     /* IRQ configure */
00291     NVIC_ClearPendingIRQ(XDMAC_IRQn);
00292     NVIC_SetPriority(XDMAC_IRQn, 1);
00293     NVIC_EnableIRQ(XDMAC_IRQn);
00294 
00295     /* Allocate DMA channels for SSC */
00296     sscDmaTxChannel = XDMAD_AllocateChannel( pDmad, XDMAD_TRANSFER_MEMORY, ID_SSC);
00297     if (sscDmaTxChannel == XDMAD_ALLOC_FAILED ) {
00298         printf("xDMA channel allocation error\n\r");
00299         while(1);
00300     }
00301    
00302     /* Set TX callback */
00303     XDMAD_SetCallback(pDmad, sscDmaTxChannel,(XdmadTransferCallback)_SscTxCallback, 0);
00304     XDMAD_PrepareChannel(pDmad, sscDmaTxChannel );
00305 }
00306 
00307 /**
00308  * Enable/Disable audio channels
00309  */
00310 static void AudioPlayEnable(uint8_t enable)
00311 {
00312     if (enable == 1) {
00313         SSC_EnableTransmitter(SSC);
00314     } else if (enable == 0) {
00315         SSC_DisableTransmitter(SSC);
00316     }
00317 }
00318 
00319 /**
00320  * Adjust codec for sync
00321  * \param adjust Sync case: default(0)/faster(>0)/slower(<0)
00322  */
00323 static void _SyncAdjust(int32_t adjust)
00324 {
00325     if (adjust > 0) {
00326         /* Fractional multiply for FLL_K, Fref = 0x8000 (1/2) */
00327         WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_FLL_CRTL3, 0xFF00);
00328         /* FLL_GAIN=0, FLL_N=187 */
00329         return;
00330     }
00331     if (adjust < 0) {
00332         /* Fractional multiply for FLL_K, Fref = 0x8000 (1/2) */
00333         WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_FLL_CRTL3, 0x5000);
00334         /* FLL_GAIN=0, FLL_N=187 */
00335         return;
00336     }
00337     /* Default: 32K -> 48K*256, FLL: 32768*187.5/16/8
00338      */
00339     /* FLL_FRATIO=4 (/16), FLL_OUTDIV= 7 (/8) */
00340     /* Fractional multiply for FLL_K, Fref = 0x8000 (1/2) */
00341     WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_FLL_CRTL3, 0x8000 + 0x3000);
00342     /* FLL_GAIN=0, FLL_N=187 */
00343     return; 
00344 }
00345 
00346 /**
00347  * Configure the TWI and DACC for audio output.
00348  * \param sampleRate Audio sample rate.
00349  * \param mck        MCK frequency.
00350  */
00351 static void _ConfigureAudioPlay(uint32_t sampleRate, uint32_t mck)
00352 {
00353     /* -- Pins Configuration -- */
00354     PIO_Configure(pinsAudio, PIO_LISTSIZE(pinsAudio));
00355 
00356     /* -- SSC Configuration -- */
00357     sampleRate = sampleRate; /*dummy */
00358     SSC_Configure(SSC,0, mck);
00359     SSC_DisableTransmitter(SSC);
00360     SSC_ConfigureTransmitter(SSC, I2S_SLAVE_TX_SETTING,I2S_SLAVE_TX_FRM_SETTING);
00361     SSC_ConfigureReceiver(SSC,I2S_SLAVE_RX_SETTING,I2S_SLAVE_RX_FRM_SETTING);
00362     
00363     /* Enable TWI peripheral clock */
00364     PMC_EnablePeripheral(ID_TWIHS0);
00365     /* Configure and enable the TWI (required for accessing the DAC) */
00366     TWI_ConfigureMaster(TWIHS0, TWI_CLOCK, mck);
00367     TWID_Initialize(&twid, TWIHS0);
00368     
00369     /* Initialize the audio DAC
00370      */
00371 
00372     WM8904_Write(&twid, WM8904_SLAVE_ADDRESS, WM8904_REG_RESET, 0);
00373     Wait(100);
00374     /* WM8904 as master */
00375     if(WM8904_Read(&twid, WM8904_SLAVE_ADDRESS, 0)!=0x8904) {
00376           printf("WM8904 not found!\n\r");
00377           while(1);
00378     }
00379     WM8904_Init(&twid, WM8904_SLAVE_ADDRESS,PMC_MCKR_CSS_SLOW_CLK);
00380     _SyncAdjust(0);
00381     PMC_ConfigurePCK2(PMC_MCKR_CSS_SLOW_CLK, PMC_MCKR_PRES_CLK_1 );
00382     /* Mute */
00383     AudioPlayEnable(0);
00384 }
00385 
00386 /**
00387  *  Invoked when a frame has been received.
00388  */
00389 static void FrameReceived(uint32_t unused,
00390                         uint8_t status,
00391                         uint32_t transferred,
00392                         uint32_t remaining)
00393 {
00394     unused = unused; /*dummy */
00395     remaining = remaining; /*dummy */
00396     if (status == USBD_STATUS_SUCCESS) {
00397         bufferSizes[inBufferIndex] = transferred / AUDDevice_BYTESPERSAMPLE;
00398         inBufferIndex = (inBufferIndex + 1) % BUFFER_NUMBER;
00399         numBuffersToSend++;
00400         /* Start DAc transmission if necessary */
00401         if (!isDacActive) {
00402             dacDelay = DAC_DELAY;
00403             isDacActive = 1;
00404         } else if (dacDelay > 0) {
00405             /* Wait until a few buffers have been received */
00406             dacDelay--;
00407             } else if (isFirstFrame) {
00408                 /* Start sending buffers */
00409                 isFirstFrame = 0;
00410                 AudioPlayEnable(1);
00411                 _SscDma(&(SSC->SSC_THR), sscDmaTxChannel, 
00412                         buffers[outBufferIndex], bufferSizes[outBufferIndex]);
00413                 outBufferIndex = (outBufferIndex + 1) % BUFFER_NUMBER;
00414                 numBuffersToSend --;
00415             }
00416         } else if (status == USBD_STATUS_ABORTED) {
00417             /* Error , ABORT, add NULL buffer */
00418             bufferSizes[inBufferIndex] = 0;
00419         } else {
00420     /* Packet is discarded */
00421     }
00422     /* Receive next packet */
00423     AUDDSpeakerDriver_Read(buffers[inBufferIndex],
00424                            AUDDevice_BYTESPERFRAME,
00425                            (TransferCallback) FrameReceived,
00426                            0); // No optional argument
00427 }
00428 
00429 /*----------------------------------------------------------------------------
00430  *         Callbacks re-implementation
00431  *----------------------------------------------------------------------------*/
00432 /**
00433  * Invoked when the configuration of the device changes. Parse used endpoints.
00434  * \param cfgnum New configuration number.
00435  */
00436 void USBDDriverCallbacks_ConfigurationChanged(unsigned char cfgnum)
00437 {
00438     AUDDSpeakerDriver_ConfigurationChangeHandler(cfgnum);
00439 }
00440 
00441 /**
00442  * Invoked whenever the active setting of an interface is changed by the
00443  * host. Reset streaming interface.
00444  * \param interface Interface number.
00445  * \param setting Newly active setting.
00446  */
00447 void USBDDriverCallbacks_InterfaceSettingChanged(unsigned char interface,
00448                                              unsigned char setting)
00449 {
00450     AUDDSpeakerDriver_InterfaceSettingChangedHandler(interface, setting);
00451 }
00452 
00453 /**
00454  *  Invoked whenever a SETUP request is received from the host. Forwards the
00455  *  request to the standard handler.
00456  */
00457 void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
00458 {
00459     AUDDSpeakerDriver_RequestHandler(request);
00460 }
00461 
00462 /**
00463  *  Invoked when an audio channel get muted or unmuted. Mutes/unmutes the
00464  *  channel at the DAC level.
00465  *  \param channel  Channel number that changed.
00466  *  \param muted    Indicates the new mute status of the channel.
00467  */
00468 void AUDDSpeakerDriver_MuteChanged(uint8_t channel, uint8_t muted)
00469 {
00470     /* Speaker Master channel */
00471     if (channel == AUDDSpeakerDriver_MASTERCHANNEL) {
00472         if (muted) {
00473             AudioPlayEnable(0);
00474             TRACE_WARNING("MuteMaster ");
00475         } else {
00476             TRACE_INFO("UnmuteMaster ");
00477             AudioPlayEnable(1);
00478         }
00479     }
00480 }
00481 
00482 /**
00483  *  Invoked when an audio streaming interface setting changed. Actually control
00484  *  streaming rate.
00485  *  \param newSetting  New stream (interface) setting.
00486  */
00487 void AUDDSpeakerDriver_StreamSettingChanged(uint8_t newSetting)
00488 {
00489     if (newSetting) {
00490         LED_Set(USBD_LEDOTHER);
00491         numBuffersToSend = 0;
00492     } else {
00493         LED_Clear(USBD_LEDOTHER);
00494     }
00495 }
00496 
00497 /**
00498  * Configure clock settings for USB device
00499  */
00500 static void _ConfigureUsbhs(void)
00501 {
00502     /* UTMI parallel mode, High/Full/Low Speed */
00503     /* UUSBCK not used in this configuration (High Speed) */
00504     PMC->PMC_SCDR = PMC_SCDR_USBCLK;
00505     /* USB clock register: USB Clock Input is UTMI PLL */
00506     PMC->PMC_USB = PMC_USB_USBS;
00507     /* Enable peripheral clock for USBHS */
00508     PMC_EnablePeripheral(ID_USBHS);
00509     USBHS->USBHS_CTRL = USBHS_CTRL_UIMOD_DEVICE;
00510     /* Enable PLL 480 MHz */
00511     PMC->CKGR_UCKR = CKGR_UCKR_UPLLEN | CKGR_UCKR_UPLLCOUNT(0xF);
00512     /* Wait that PLL is considered locked by the PMC */
00513     while( !(PMC->PMC_SR & PMC_SR_LOCKU) );
00514     /* IRQ */
00515     NVIC_EnableIRQ(USBHS_IRQn) ;
00516 }
00517 
00518 /*----------------------------------------------------------------------------
00519  *         Exported functions
00520  *----------------------------------------------------------------------------*/
00521 /**
00522  *  \brief usb_audio_speaker Application entry point.
00523  *
00524  *  Starts the driver and waits for an audio input stream to forward to the DAC.
00525  */
00526 int main(void)
00527 {
00528     volatile uint8_t usbConn = 0;
00529     volatile uint8_t audioOn = 0;
00530     volatile uint32_t num = 0;
00531     int32_t  numDiff = 0, prevDiff = 0;
00532     int8_t   clockAdjust = 0;
00533 
00534     /* Disable watchdog */
00535     WDT_Disable( WDT );
00536 
00537     printf("-- USB Device Audio Speaker Example %s --\n\r", SOFTPACK_VERSION);
00538     printf("-- %s\n\r", BOARD_NAME);
00539     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00540     
00541     SCB_EnableICache();
00542     SCB_EnableDCache();
00543 
00544     /* LED */
00545     LED_Configure(LED_YELLOW0);
00546 
00547     TimeTick_Configure();
00548     /* Interrupt priority */
00549     NVIC_SetPriority( USBHS_IRQn, 2 );
00550 
00551     /* Initialize all USB power (off) */
00552     _ConfigureUsbhs();
00553     /* Configure Audio */
00554     _ConfigureAudioPlay(AUDDevice_SAMPLERATE, BOARD_MCK);
00555 
00556     /* Configure DMA */
00557     _ConfigureDma();
00558 
00559     /* USB audio driver initialization */
00560     AUDDSpeakerDriver_Initialize(&auddSpeakerDriverDescriptors);
00561     
00562     // Start USB stack to authorize VBus monitoring
00563     USBD_Connect();
00564     
00565     /* Infinite loop */
00566     while (1) {
00567         if (USBD_GetState() < USBD_STATE_CONFIGURED) {
00568             usbConn = 0;
00569             continue;
00570         }
00571         if (audioOn) {
00572             if(isDacActive == 0) {
00573                 AudioPlayEnable(0);
00574                 printf("audE ");
00575                 isFirstFrame = 1;
00576                 audioOn = 0;
00577             } else {
00578                 if (num != numBuffersToSend) {
00579                     num = numBuffersToSend;
00580                 }
00581                 numDiff = numBuffersToSend - DAC_DELAY;
00582                 if (prevDiff != numDiff) {
00583                     prevDiff = numDiff;
00584                     if (numDiff > 0 && clockAdjust != 1) {
00585                       printf("+");
00586                         /* USB too fast or SSC too slow: faster clock */
00587                         clockAdjust = 1;
00588                         _SyncAdjust(1);
00589                     }
00590                     if (numDiff < 0 && clockAdjust != -1) {
00591                       printf("-");
00592                         /* USB too slow or SSC too fast: slower clock */
00593                         clockAdjust = -1;
00594                         _SyncAdjust(-1);
00595                     }
00596                     if (numDiff == 0 && clockAdjust != 0) {
00597                         clockAdjust = 0;
00598                         _SyncAdjust(0);
00599                     }
00600                 }
00601             }
00602         } else if (isDacActive) {
00603             printf("audS ");
00604             audioOn = 1;
00605         }
00606 
00607         if (usbConn == 0) {
00608             usbConn = 1;
00609             /* Start Reading the incoming audio stream */
00610             AUDDSpeakerDriver_Read(buffers[inBufferIndex],
00611                                 AUDDevice_BYTESPERFRAME,
00612                                 (TransferCallback) FrameReceived,
00613                                 0); // No optional argument
00614         }
00615     }
00616 }
00617 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines