2 ******************************************************************************
3 * @file usbd_audio_out_if.c
4 * @author MCD Application Team
7 * @brief This file provides the Audio Out (palyback) interface API.
8 ******************************************************************************
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
22 /* Includes ------------------------------------------------------------------*/
23 #include "usbd_audio_core.h"
24 #include "usbd_audio_out_if.h"
29 /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
34 /** @defgroup usbd_audio_out_if
35 * @brief usbd out interface module
39 /** @defgroup usbd_audio_out_if_Private_TypesDefinitions
47 /** @defgroup usbd_audio_out_if_Private_Defines
55 /** @defgroup usbd_audio_out_if_Private_Macros
63 /** @defgroup usbd_audio_out_if_Private_FunctionPrototypes
66 static uint8_t Init (uint32_t AudioFreq, uint32_t Volume, uint32_t options);
67 static uint8_t DeInit (uint32_t options);
68 static uint8_t AudioCmd (uint8_t* pbuf, uint32_t size, uint8_t cmd);
69 static uint8_t VolumeCtl (uint8_t vol);
70 static uint8_t MuteCtl (uint8_t cmd);
71 static uint8_t PeriodicTC (uint8_t cmd);
72 static uint8_t GetState (void);
78 /** @defgroup usbd_audio_out_if_Private_Variables
81 AUDIO_FOPS_TypeDef AUDIO_OUT_fops =
92 static uint8_t AudioState = AUDIO_STATE_INACTIVE;
98 /** @defgroup usbd_audio_out_if_Private_Functions
104 * Initialize and configures all required resources for audio play function.
105 * @param AudioFreq: Statrtup audio frequency.
106 * @param Volume: Startup volume to be set.
107 * @param options: specific options passed to low layer function.
108 * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else.
110 static uint8_t Init (uint32_t AudioFreq,
114 static uint32_t Initialized = 0;
116 /* Check if the low layer has already been initialized */
117 if (Initialized == 0)
119 /* Call low layer function */
120 if (EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, Volume, AudioFreq) != 0)
122 AudioState = AUDIO_STATE_ERROR;
126 /* Set the Initialization flag to prevent reinitializing the interface again */
130 /* Update the Audio state machine */
131 AudioState = AUDIO_STATE_ACTIVE;
138 * Free all resources used by low layer and stops audio-play function.
139 * @param options: options passed to low layer function.
140 * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else.
142 static uint8_t DeInit (uint32_t options)
144 /* Update the Audio state machine */
145 AudioState = AUDIO_STATE_INACTIVE;
152 * Play, Stop, Pause or Resume current file.
153 * @param pbuf: address from which file shoud be played.
154 * @param size: size of the current buffer/file.
155 * @param cmd: command to be executed, can be AUDIO_CMD_PLAY , AUDIO_CMD_PAUSE,
156 * AUDIO_CMD_RESUME or AUDIO_CMD_STOP.
157 * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else.
159 static uint8_t AudioCmd(uint8_t* pbuf,
163 /* Check the current state */
164 if ((AudioState == AUDIO_STATE_INACTIVE) || (AudioState == AUDIO_STATE_ERROR))
166 AudioState = AUDIO_STATE_ERROR;
172 /* Process the PLAY command ----------------------------*/
174 /* If current state is Active or Stopped */
175 if ((AudioState == AUDIO_STATE_ACTIVE) || \
176 (AudioState == AUDIO_STATE_STOPPED) || \
177 (AudioState == AUDIO_STATE_PLAYING))
179 Audio_MAL_Play((uint32_t)pbuf, (size/2));
180 AudioState = AUDIO_STATE_PLAYING;
183 /* If current state is Paused */
184 else if (AudioState == AUDIO_STATE_PAUSED)
186 if (EVAL_AUDIO_PauseResume(AUDIO_RESUME, (uint32_t)pbuf, (size/2)) != 0)
188 AudioState = AUDIO_STATE_ERROR;
193 AudioState = AUDIO_STATE_PLAYING;
197 else /* Not allowed command */
202 /* Process the STOP command ----------------------------*/
204 if (AudioState != AUDIO_STATE_PLAYING)
206 /* Unsupported command */
209 else if (EVAL_AUDIO_Stop(CODEC_PDWN_SW) != 0)
211 AudioState = AUDIO_STATE_ERROR;
216 AudioState = AUDIO_STATE_STOPPED;
220 /* Process the PAUSE command ---------------------------*/
221 case AUDIO_CMD_PAUSE:
222 if (AudioState != AUDIO_STATE_PLAYING)
224 /* Unsupported command */
227 else if (EVAL_AUDIO_PauseResume(AUDIO_PAUSE, (uint32_t)pbuf, (size/2)) != 0)
229 AudioState = AUDIO_STATE_ERROR;
234 AudioState = AUDIO_STATE_PAUSED;
238 /* Unsupported command ---------------------------------*/
246 * Set the volume level in %
247 * @param vol: volume level to be set in % (from 0% to 100%)
248 * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else.
250 static uint8_t VolumeCtl (uint8_t vol)
252 /* Call low layer volume setting function */
253 if (EVAL_AUDIO_VolumeCtl(vol) != 0)
255 AudioState = AUDIO_STATE_ERROR;
264 * Mute or Unmute the audio current output
265 * @param cmd: can be 0 to unmute, or 1 to mute.
266 * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else.
268 static uint8_t MuteCtl (uint8_t cmd)
270 /* Call low layer mute setting function */
271 if (EVAL_AUDIO_Mute(cmd) != 0)
273 AudioState = AUDIO_STATE_ERROR;
285 * @retval AUDIO_OK if all operations succeed, AUDIO_FAIL else.
287 static uint8_t PeriodicTC (uint8_t cmd)
297 * Return the current state of the audio machine
299 * @retval Current State.
301 static uint8_t GetState (void)
318 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/