SAMV71 Xplained Ultra Software Package 1.4

AUDDSpeakerDriver.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2008, 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 
00030 /** \file
00031  * \addtogroup usbd_audio_speaker
00032  *@{
00033  */
00034 
00035 /*------------------------------------------------------------------------------
00036  *         Headers
00037  *------------------------------------------------------------------------------*/
00038 
00039 #include <AUDDSpeakerDriver.h>
00040 
00041 #include <AUDDSpeakerPhone.h>
00042 
00043 #include <USBLib_Trace.h>
00044 
00045 #include <AUDRequests.h>
00046 
00047 #include "USBD.h"
00048 #include <USBD_HAL.h>
00049 #include <USBDDriver.h>
00050 
00051 /*----------------------------------------------------------------------------
00052  *         Internal types
00053  *----------------------------------------------------------------------------*/
00054 
00055 /**
00056  * \brief Audio speaker driver struct.
00057  */
00058 typedef struct _AUDDSpeakerDriver {
00059     /** Speaker & Phone function */
00060     AUDDSpeakerPhone fun;
00061     /** Stream instance for speaker */
00062     AUDDStream  speaker;
00063     /** Array for storing the current setting of each interface */
00064     uint8_t     bAltInterfaces[AUDDSpeakerDriver_NUMINTERFACES];
00065 } AUDDSpeakerDriver;
00066 
00067 /*----------------------------------------------------------------------------
00068  *         Internal variables
00069  *----------------------------------------------------------------------------*/
00070 
00071 /** Global USB audio speaker driver instance. */
00072 static AUDDSpeakerDriver auddSpeakerDriver;
00073 
00074 /*----------------------------------------------------------------------------
00075  *         Dummy callbacks
00076  *----------------------------------------------------------------------------*/
00077 
00078 /*----------------------------------------------------------------------------
00079  *         Internal functions
00080  *----------------------------------------------------------------------------*/
00081 
00082 /**
00083  * Callback triggerred after the mute or volume status of the channel has been
00084  * changed.
00085  * \param ec        Event code.
00086  * \param channel   Channel number.
00087  * \param pArg      Pointer to AUDDStream instance.
00088  */
00089 static void AUDDSpeaker_EventCallback(uint32_t ec,
00090                                       uint8_t channel,
00091                                       AUDDStream *pArg)
00092 {
00093     if (ec == AUDD_EC_MuteChanged) {
00094         if (AUDDSpeakerDriver_MuteChanged != NULL)
00095             AUDDSpeakerDriver_MuteChanged(channel, pArg->bmMute);
00096     }
00097     else if (ec == AUDD_EC_VolumeChanged) {
00098         /* Not supported now */
00099     }
00100 }
00101 
00102 /*----------------------------------------------------------------------------
00103  *         Exported functions
00104  *----------------------------------------------------------------------------*/
00105 
00106 /**
00107  * Initializes an USB audio speaker device driver, as well as the underlying
00108  * USB controller.
00109  */
00110 void AUDDSpeakerDriver_Initialize(const USBDDriverDescriptors *pDescriptors)
00111 {
00112     AUDDSpeakerDriver *pAudd = &auddSpeakerDriver;
00113     AUDDSpeakerPhone *pAudf  = &pAudd->fun;
00114     AUDDStream *pAuds = &pAudd->speaker;
00115     USBDDriver *pUsbd = USBD_GetDriver();
00116 
00117     AUDDSpeakerPhone_InitializeStream(
00118         pAuds, AUDDSpeakerDriver_NUMCHANNELS, 0,
00119         (AUDDStreamEventCallback)AUDDSpeaker_EventCallback, pAuds);
00120 
00121     AUDDSpeakerPhone_Initialize(
00122         pAudf, pUsbd, pAuds, 0);
00123 
00124     /* Initialize the USB driver */
00125     USBDDriver_Initialize(pUsbd,
00126                           pDescriptors,
00127                           pAudd->bAltInterfaces);
00128     USBD_Init();
00129 }
00130 
00131 /**
00132  * Invoked whenever the active configuration of device is changed by the
00133  * host.
00134  * \param cfgnum Configuration number.
00135  */
00136 void AUDDSpeakerDriver_ConfigurationChangeHandler(uint8_t cfgnum)
00137 {
00138     AUDDSpeakerDriver *pAudd = &auddSpeakerDriver;
00139     AUDDSpeakerPhone *pAudf = &pAudd->fun;
00140     const USBDDriverDescriptors *pDescriptors = pAudf->pUsbd->pDescriptors;
00141     USBConfigurationDescriptor *pDesc;
00142 
00143     if (cfgnum > 0) {
00144 
00145         /* Parse endpoints for data & notification */
00146         if (USBD_HAL_IsHighSpeed() && pDescriptors->pHsConfiguration)
00147             pDesc = (USBConfigurationDescriptor*)pDescriptors->pHsConfiguration;
00148         else
00149             pDesc = (USBConfigurationDescriptor*)pDescriptors->pFsConfiguration;
00150 
00151         AUDDSpeakerPhone_ParseInterfaces(pAudf,
00152                                          (USBGenericDescriptor*)pDesc,
00153                                          pDesc->wTotalLength);
00154     }
00155 }
00156 
00157 /**
00158  * Invoked whenever the active setting of an interface is changed by the
00159  * host. Changes the status of the third LED accordingly.
00160  * \param interface Interface number.
00161  * \param setting Newly active setting.
00162  */
00163 void AUDDSpeakerDriver_InterfaceSettingChangedHandler(uint8_t interface,
00164                                                       uint8_t setting)
00165 {
00166     AUDDSpeakerDriver *pSpeakerd = &auddSpeakerDriver;
00167     AUDDSpeakerPhone  *pAudf     = &pSpeakerd->fun;
00168 
00169     if (setting == 0) {
00170         AUDDSpeakerPhone_CloseStream(pAudf, interface);
00171     }
00172 
00173     if (NULL != AUDDSpeakerDriver_StreamSettingChanged)
00174         AUDDSpeakerDriver_StreamSettingChanged(setting);
00175 }
00176 
00177 /**
00178  * Handles audio-specific USB requests sent by the host, and forwards
00179  * standard ones to the USB device driver.
00180  * \param request Pointer to a USBGenericRequest instance.
00181  */
00182 void AUDDSpeakerDriver_RequestHandler(const USBGenericRequest *request)
00183 {
00184     AUDDSpeakerDriver *pAudd = &auddSpeakerDriver;
00185     AUDDSpeakerPhone *pAudf  = &pAudd->fun;
00186     USBDDriver *pUsbd = pAudf->pUsbd;
00187 
00188     TRACE_INFO_WP("NewReq ");
00189 
00190     /* Handle Audio Class requests */
00191     if (AUDDSpeakerPhone_RequestHandler(pAudf, request) == USBRC_SUCCESS) {
00192         return;
00193     }
00194 
00195     /* Handle STD requests */
00196     if (USBGenericRequest_GetType(request) == USBGenericRequest_STANDARD) {
00197 
00198         USBDDriver_RequestHandler(pUsbd, request);
00199     }
00200     /* Unsupported request */
00201     else {
00202 
00203         TRACE_WARNING(
00204           "AUDDSpeakerDriver_RequestHandler: Unsupported request (%d,%x)\n\r",
00205           USBGenericRequest_GetType(request),
00206           USBGenericRequest_GetRequest(request));
00207         USBD_Stall(0);
00208     }
00209 }
00210 
00211 /**
00212  * Reads incoming audio data sent by the USB host into the provided
00213  * buffer. When the transfer is complete, an optional callback function is
00214  * invoked.
00215  * \param buffer Pointer to the data storage buffer.
00216  * \param length Size of the buffer in bytes.
00217  * \param callback Optional callback function.
00218  * \param argument Optional argument to the callback function.
00219  * \return USBD_STATUS_SUCCESS if the transfer is started successfully;
00220  *         otherwise an error code.
00221  */
00222 uint8_t AUDDSpeakerDriver_Read(void *buffer,
00223                                uint32_t length,
00224                                TransferCallback callback,
00225                                void *argument)
00226 {
00227     AUDDSpeakerDriver *pAudd = &auddSpeakerDriver;
00228     AUDDSpeakerPhone *pAudf  = &pAudd->fun;
00229     return USBD_Read(pAudf->pSpeaker->bEndpointOut,
00230                      buffer, length,
00231                      callback, argument);
00232 }
00233 
00234 /**@}*/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines