updated wait_ms(ms) to thread_sleep_for(ms) for Mbed OS 6

Committer:
Jerome Coutant
Date:
Tue May 16 10:50:26 2017 +0200
Revision:
2:123b894b49dd
Child:
3:3cdfcc4f7c9d
STM32Cube_FW_BSP_DISCO_F469NI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jerome Coutant 2:123b894b49dd 1 /**
Jerome Coutant 2:123b894b49dd 2 ******************************************************************************
Jerome Coutant 2:123b894b49dd 3 * @file cs43l22.c
Jerome Coutant 2:123b894b49dd 4 * @author MCD Application Team
Jerome Coutant 2:123b894b49dd 5 * @version V2.0.2
Jerome Coutant 2:123b894b49dd 6 * @date 06-October-2015
Jerome Coutant 2:123b894b49dd 7 * @brief This file provides the CS43L22 Audio Codec driver.
Jerome Coutant 2:123b894b49dd 8 ******************************************************************************
Jerome Coutant 2:123b894b49dd 9 * @attention
Jerome Coutant 2:123b894b49dd 10 *
Jerome Coutant 2:123b894b49dd 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Jerome Coutant 2:123b894b49dd 12 *
Jerome Coutant 2:123b894b49dd 13 * Redistribution and use in source and binary forms, with or without modification,
Jerome Coutant 2:123b894b49dd 14 * are permitted provided that the following conditions are met:
Jerome Coutant 2:123b894b49dd 15 * 1. Redistributions of source code must retain the above copyright notice,
Jerome Coutant 2:123b894b49dd 16 * this list of conditions and the following disclaimer.
Jerome Coutant 2:123b894b49dd 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
Jerome Coutant 2:123b894b49dd 18 * this list of conditions and the following disclaimer in the documentation
Jerome Coutant 2:123b894b49dd 19 * and/or other materials provided with the distribution.
Jerome Coutant 2:123b894b49dd 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Jerome Coutant 2:123b894b49dd 21 * may be used to endorse or promote products derived from this software
Jerome Coutant 2:123b894b49dd 22 * without specific prior written permission.
Jerome Coutant 2:123b894b49dd 23 *
Jerome Coutant 2:123b894b49dd 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Jerome Coutant 2:123b894b49dd 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Jerome Coutant 2:123b894b49dd 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Jerome Coutant 2:123b894b49dd 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Jerome Coutant 2:123b894b49dd 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Jerome Coutant 2:123b894b49dd 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Jerome Coutant 2:123b894b49dd 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Jerome Coutant 2:123b894b49dd 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Jerome Coutant 2:123b894b49dd 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Jerome Coutant 2:123b894b49dd 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jerome Coutant 2:123b894b49dd 34 *
Jerome Coutant 2:123b894b49dd 35 ******************************************************************************
Jerome Coutant 2:123b894b49dd 36 */
Jerome Coutant 2:123b894b49dd 37
Jerome Coutant 2:123b894b49dd 38 /* Includes ------------------------------------------------------------------*/
Jerome Coutant 2:123b894b49dd 39 #include "cs43l22.h"
Jerome Coutant 2:123b894b49dd 40
Jerome Coutant 2:123b894b49dd 41 /** @addtogroup BSP
Jerome Coutant 2:123b894b49dd 42 * @{
Jerome Coutant 2:123b894b49dd 43 */
Jerome Coutant 2:123b894b49dd 44
Jerome Coutant 2:123b894b49dd 45 /** @addtogroup Components
Jerome Coutant 2:123b894b49dd 46 * @{
Jerome Coutant 2:123b894b49dd 47 */
Jerome Coutant 2:123b894b49dd 48
Jerome Coutant 2:123b894b49dd 49 /** @addtogroup CS43L22
Jerome Coutant 2:123b894b49dd 50 * @brief This file provides a set of functions needed to drive the
Jerome Coutant 2:123b894b49dd 51 * CS43L22 audio codec.
Jerome Coutant 2:123b894b49dd 52 * @{
Jerome Coutant 2:123b894b49dd 53 */
Jerome Coutant 2:123b894b49dd 54
Jerome Coutant 2:123b894b49dd 55 /** @defgroup CS43L22_Private_Types
Jerome Coutant 2:123b894b49dd 56 * @{
Jerome Coutant 2:123b894b49dd 57 */
Jerome Coutant 2:123b894b49dd 58
Jerome Coutant 2:123b894b49dd 59 /**
Jerome Coutant 2:123b894b49dd 60 * @}
Jerome Coutant 2:123b894b49dd 61 */
Jerome Coutant 2:123b894b49dd 62
Jerome Coutant 2:123b894b49dd 63 /** @defgroup CS43L22_Private_Defines
Jerome Coutant 2:123b894b49dd 64 * @{
Jerome Coutant 2:123b894b49dd 65 */
Jerome Coutant 2:123b894b49dd 66 #define VOLUME_CONVERT(Volume) (((Volume) > 100)? 100:((uint8_t)(((Volume) * 255) / 100)))
Jerome Coutant 2:123b894b49dd 67 /* Uncomment this line to enable verifying data sent to codec after each write
Jerome Coutant 2:123b894b49dd 68 operation (for debug purpose) */
Jerome Coutant 2:123b894b49dd 69 #if !defined (VERIFY_WRITTENDATA)
Jerome Coutant 2:123b894b49dd 70 /* #define VERIFY_WRITTENDATA */
Jerome Coutant 2:123b894b49dd 71 #endif /* VERIFY_WRITTENDATA */
Jerome Coutant 2:123b894b49dd 72 /**
Jerome Coutant 2:123b894b49dd 73 * @}
Jerome Coutant 2:123b894b49dd 74 */
Jerome Coutant 2:123b894b49dd 75
Jerome Coutant 2:123b894b49dd 76 /** @defgroup CS43L22_Private_Macros
Jerome Coutant 2:123b894b49dd 77 * @{
Jerome Coutant 2:123b894b49dd 78 */
Jerome Coutant 2:123b894b49dd 79
Jerome Coutant 2:123b894b49dd 80 /**
Jerome Coutant 2:123b894b49dd 81 * @}
Jerome Coutant 2:123b894b49dd 82 */
Jerome Coutant 2:123b894b49dd 83
Jerome Coutant 2:123b894b49dd 84 /** @defgroup CS43L22_Private_Variables
Jerome Coutant 2:123b894b49dd 85 * @{
Jerome Coutant 2:123b894b49dd 86 */
Jerome Coutant 2:123b894b49dd 87
Jerome Coutant 2:123b894b49dd 88 /* Audio codec driver structure initialization */
Jerome Coutant 2:123b894b49dd 89 AUDIO_DrvTypeDef cs43l22_drv =
Jerome Coutant 2:123b894b49dd 90 {
Jerome Coutant 2:123b894b49dd 91 cs43l22_Init,
Jerome Coutant 2:123b894b49dd 92 cs43l22_DeInit,
Jerome Coutant 2:123b894b49dd 93 cs43l22_ReadID,
Jerome Coutant 2:123b894b49dd 94
Jerome Coutant 2:123b894b49dd 95 cs43l22_Play,
Jerome Coutant 2:123b894b49dd 96 cs43l22_Pause,
Jerome Coutant 2:123b894b49dd 97 cs43l22_Resume,
Jerome Coutant 2:123b894b49dd 98 cs43l22_Stop,
Jerome Coutant 2:123b894b49dd 99
Jerome Coutant 2:123b894b49dd 100 cs43l22_SetFrequency,
Jerome Coutant 2:123b894b49dd 101 cs43l22_SetVolume,
Jerome Coutant 2:123b894b49dd 102 cs43l22_SetMute,
Jerome Coutant 2:123b894b49dd 103 cs43l22_SetOutputMode,
Jerome Coutant 2:123b894b49dd 104 cs43l22_Reset,
Jerome Coutant 2:123b894b49dd 105 };
Jerome Coutant 2:123b894b49dd 106
Jerome Coutant 2:123b894b49dd 107 static uint8_t Is_cs43l22_Stop = 1;
Jerome Coutant 2:123b894b49dd 108
Jerome Coutant 2:123b894b49dd 109 volatile uint8_t OutputDev = 0;
Jerome Coutant 2:123b894b49dd 110
Jerome Coutant 2:123b894b49dd 111 /**
Jerome Coutant 2:123b894b49dd 112 * @}
Jerome Coutant 2:123b894b49dd 113 */
Jerome Coutant 2:123b894b49dd 114
Jerome Coutant 2:123b894b49dd 115 /** @defgroup CS43L22_Function_Prototypes
Jerome Coutant 2:123b894b49dd 116 * @{
Jerome Coutant 2:123b894b49dd 117 */
Jerome Coutant 2:123b894b49dd 118 static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
Jerome Coutant 2:123b894b49dd 119 /**
Jerome Coutant 2:123b894b49dd 120 * @}
Jerome Coutant 2:123b894b49dd 121 */
Jerome Coutant 2:123b894b49dd 122
Jerome Coutant 2:123b894b49dd 123 /** @defgroup CS43L22_Private_Functions
Jerome Coutant 2:123b894b49dd 124 * @{
Jerome Coutant 2:123b894b49dd 125 */
Jerome Coutant 2:123b894b49dd 126
Jerome Coutant 2:123b894b49dd 127 /**
Jerome Coutant 2:123b894b49dd 128 * @brief Initializes the audio codec and the control interface.
Jerome Coutant 2:123b894b49dd 129 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 130 * @param OutputDevice: can be OUTPUT_DEVICE_SPEAKER, OUTPUT_DEVICE_HEADPHONE,
Jerome Coutant 2:123b894b49dd 131 * OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO .
Jerome Coutant 2:123b894b49dd 132 * @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
Jerome Coutant 2:123b894b49dd 133 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 134 */
Jerome Coutant 2:123b894b49dd 135 uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
Jerome Coutant 2:123b894b49dd 136 {
Jerome Coutant 2:123b894b49dd 137 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 138
Jerome Coutant 2:123b894b49dd 139 /* Initialize the Control interface of the Audio Codec */
Jerome Coutant 2:123b894b49dd 140 AUDIO_IO_Init();
Jerome Coutant 2:123b894b49dd 141
Jerome Coutant 2:123b894b49dd 142 /* Keep Codec powered OFF */
Jerome Coutant 2:123b894b49dd 143 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x01);
Jerome Coutant 2:123b894b49dd 144
Jerome Coutant 2:123b894b49dd 145 /*Save Output device for mute ON/OFF procedure*/
Jerome Coutant 2:123b894b49dd 146 switch (OutputDevice)
Jerome Coutant 2:123b894b49dd 147 {
Jerome Coutant 2:123b894b49dd 148 case OUTPUT_DEVICE_SPEAKER:
Jerome Coutant 2:123b894b49dd 149 OutputDev = 0xFA;
Jerome Coutant 2:123b894b49dd 150 break;
Jerome Coutant 2:123b894b49dd 151
Jerome Coutant 2:123b894b49dd 152 case OUTPUT_DEVICE_HEADPHONE:
Jerome Coutant 2:123b894b49dd 153 OutputDev = 0xAF;
Jerome Coutant 2:123b894b49dd 154 break;
Jerome Coutant 2:123b894b49dd 155
Jerome Coutant 2:123b894b49dd 156 case OUTPUT_DEVICE_BOTH:
Jerome Coutant 2:123b894b49dd 157 OutputDev = 0xAA;
Jerome Coutant 2:123b894b49dd 158 break;
Jerome Coutant 2:123b894b49dd 159
Jerome Coutant 2:123b894b49dd 160 case OUTPUT_DEVICE_AUTO:
Jerome Coutant 2:123b894b49dd 161 OutputDev = 0x05;
Jerome Coutant 2:123b894b49dd 162 break;
Jerome Coutant 2:123b894b49dd 163
Jerome Coutant 2:123b894b49dd 164 default:
Jerome Coutant 2:123b894b49dd 165 OutputDev = 0x05;
Jerome Coutant 2:123b894b49dd 166 break;
Jerome Coutant 2:123b894b49dd 167 }
Jerome Coutant 2:123b894b49dd 168
Jerome Coutant 2:123b894b49dd 169 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev);
Jerome Coutant 2:123b894b49dd 170
Jerome Coutant 2:123b894b49dd 171 /* Clock configuration: Auto detection */
Jerome Coutant 2:123b894b49dd 172 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_CLOCKING_CTL, 0x81);
Jerome Coutant 2:123b894b49dd 173
Jerome Coutant 2:123b894b49dd 174 /* Set the Slave Mode and the audio Standard */
Jerome Coutant 2:123b894b49dd 175 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_INTERFACE_CTL1, CODEC_STANDARD);
Jerome Coutant 2:123b894b49dd 176
Jerome Coutant 2:123b894b49dd 177 /* Set the Master volume */
Jerome Coutant 2:123b894b49dd 178 counter += cs43l22_SetVolume(DeviceAddr, Volume);
Jerome Coutant 2:123b894b49dd 179
Jerome Coutant 2:123b894b49dd 180 /* If the Speaker is enabled, set the Mono mode and volume attenuation level */
Jerome Coutant 2:123b894b49dd 181 if(OutputDevice != OUTPUT_DEVICE_HEADPHONE)
Jerome Coutant 2:123b894b49dd 182 {
Jerome Coutant 2:123b894b49dd 183 /* Set the Speaker Mono mode */
Jerome Coutant 2:123b894b49dd 184 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PLAYBACK_CTL2, 0x06);
Jerome Coutant 2:123b894b49dd 185
Jerome Coutant 2:123b894b49dd 186 /* Set the Speaker attenuation level */
Jerome Coutant 2:123b894b49dd 187 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_SPEAKER_A_VOL, 0x00);
Jerome Coutant 2:123b894b49dd 188 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_SPEAKER_B_VOL, 0x00);
Jerome Coutant 2:123b894b49dd 189 }
Jerome Coutant 2:123b894b49dd 190
Jerome Coutant 2:123b894b49dd 191 /* Additional configuration for the CODEC. These configurations are done to reduce
Jerome Coutant 2:123b894b49dd 192 the time needed for the Codec to power off. If these configurations are removed,
Jerome Coutant 2:123b894b49dd 193 then a long delay should be added between powering off the Codec and switching
Jerome Coutant 2:123b894b49dd 194 off the I2S peripheral MCLK clock (which is the operating clock for Codec).
Jerome Coutant 2:123b894b49dd 195 If this delay is not inserted, then the codec will not shut down properly and
Jerome Coutant 2:123b894b49dd 196 it results in high noise after shut down. */
Jerome Coutant 2:123b894b49dd 197
Jerome Coutant 2:123b894b49dd 198 /* Disable the analog soft ramp */
Jerome Coutant 2:123b894b49dd 199 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_ANALOG_ZC_SR_SETT, 0x00);
Jerome Coutant 2:123b894b49dd 200 /* Disable the digital soft ramp */
Jerome Coutant 2:123b894b49dd 201 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x04);
Jerome Coutant 2:123b894b49dd 202 /* Disable the limiter attack level */
Jerome Coutant 2:123b894b49dd 203 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_LIMIT_CTL1, 0x00);
Jerome Coutant 2:123b894b49dd 204 /* Adjust Bass and Treble levels */
Jerome Coutant 2:123b894b49dd 205 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_TONE_CTL, 0x0F);
Jerome Coutant 2:123b894b49dd 206 /* Adjust PCM volume level */
Jerome Coutant 2:123b894b49dd 207 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PCMA_VOL, 0x0A);
Jerome Coutant 2:123b894b49dd 208 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_PCMB_VOL, 0x0A);
Jerome Coutant 2:123b894b49dd 209
Jerome Coutant 2:123b894b49dd 210 /* Return communication control value */
Jerome Coutant 2:123b894b49dd 211 return counter;
Jerome Coutant 2:123b894b49dd 212 }
Jerome Coutant 2:123b894b49dd 213
Jerome Coutant 2:123b894b49dd 214 /**
Jerome Coutant 2:123b894b49dd 215 * @brief Deinitializes the audio codec.
Jerome Coutant 2:123b894b49dd 216 * @param None
Jerome Coutant 2:123b894b49dd 217 * @retval None
Jerome Coutant 2:123b894b49dd 218 */
Jerome Coutant 2:123b894b49dd 219 void cs43l22_DeInit(void)
Jerome Coutant 2:123b894b49dd 220 {
Jerome Coutant 2:123b894b49dd 221 /* Deinitialize Audio Codec interface */
Jerome Coutant 2:123b894b49dd 222 AUDIO_IO_DeInit();
Jerome Coutant 2:123b894b49dd 223 }
Jerome Coutant 2:123b894b49dd 224
Jerome Coutant 2:123b894b49dd 225 /**
Jerome Coutant 2:123b894b49dd 226 * @brief Get the CS43L22 ID.
Jerome Coutant 2:123b894b49dd 227 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 228 * @retval The CS43L22 ID
Jerome Coutant 2:123b894b49dd 229 */
Jerome Coutant 2:123b894b49dd 230 uint32_t cs43l22_ReadID(uint16_t DeviceAddr)
Jerome Coutant 2:123b894b49dd 231 {
Jerome Coutant 2:123b894b49dd 232 uint8_t Value;
Jerome Coutant 2:123b894b49dd 233 /* Initialize the Control interface of the Audio Codec */
Jerome Coutant 2:123b894b49dd 234 AUDIO_IO_Init();
Jerome Coutant 2:123b894b49dd 235
Jerome Coutant 2:123b894b49dd 236 Value = AUDIO_IO_Read(DeviceAddr, CS43L22_CHIPID_ADDR);
Jerome Coutant 2:123b894b49dd 237 Value = (Value & CS43L22_ID_MASK);
Jerome Coutant 2:123b894b49dd 238
Jerome Coutant 2:123b894b49dd 239 return((uint32_t) Value);
Jerome Coutant 2:123b894b49dd 240 }
Jerome Coutant 2:123b894b49dd 241
Jerome Coutant 2:123b894b49dd 242 /**
Jerome Coutant 2:123b894b49dd 243 * @brief Start the audio Codec play feature.
Jerome Coutant 2:123b894b49dd 244 * @note For this codec no Play options are required.
Jerome Coutant 2:123b894b49dd 245 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 246 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 247 */
Jerome Coutant 2:123b894b49dd 248 uint32_t cs43l22_Play(uint16_t DeviceAddr, uint16_t* pBuffer, uint16_t Size)
Jerome Coutant 2:123b894b49dd 249 {
Jerome Coutant 2:123b894b49dd 250 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 251
Jerome Coutant 2:123b894b49dd 252 if(Is_cs43l22_Stop == 1)
Jerome Coutant 2:123b894b49dd 253 {
Jerome Coutant 2:123b894b49dd 254 /* Enable the digital soft ramp */
Jerome Coutant 2:123b894b49dd 255 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x06);
Jerome Coutant 2:123b894b49dd 256
Jerome Coutant 2:123b894b49dd 257 /* Enable Output device */
Jerome Coutant 2:123b894b49dd 258 counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_OFF);
Jerome Coutant 2:123b894b49dd 259
Jerome Coutant 2:123b894b49dd 260 /* Power on the Codec */
Jerome Coutant 2:123b894b49dd 261 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9E);
Jerome Coutant 2:123b894b49dd 262 Is_cs43l22_Stop = 0;
Jerome Coutant 2:123b894b49dd 263 }
Jerome Coutant 2:123b894b49dd 264
Jerome Coutant 2:123b894b49dd 265 /* Return communication control value */
Jerome Coutant 2:123b894b49dd 266 return counter;
Jerome Coutant 2:123b894b49dd 267 }
Jerome Coutant 2:123b894b49dd 268
Jerome Coutant 2:123b894b49dd 269 /**
Jerome Coutant 2:123b894b49dd 270 * @brief Pauses playing on the audio codec.
Jerome Coutant 2:123b894b49dd 271 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 272 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 273 */
Jerome Coutant 2:123b894b49dd 274 uint32_t cs43l22_Pause(uint16_t DeviceAddr)
Jerome Coutant 2:123b894b49dd 275 {
Jerome Coutant 2:123b894b49dd 276 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 277
Jerome Coutant 2:123b894b49dd 278 /* Pause the audio file playing */
Jerome Coutant 2:123b894b49dd 279 /* Mute the output first */
Jerome Coutant 2:123b894b49dd 280 counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_ON);
Jerome Coutant 2:123b894b49dd 281
Jerome Coutant 2:123b894b49dd 282 /* Put the Codec in Power save mode */
Jerome Coutant 2:123b894b49dd 283 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x01);
Jerome Coutant 2:123b894b49dd 284
Jerome Coutant 2:123b894b49dd 285 return counter;
Jerome Coutant 2:123b894b49dd 286 }
Jerome Coutant 2:123b894b49dd 287
Jerome Coutant 2:123b894b49dd 288 /**
Jerome Coutant 2:123b894b49dd 289 * @brief Resumes playing on the audio codec.
Jerome Coutant 2:123b894b49dd 290 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 291 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 292 */
Jerome Coutant 2:123b894b49dd 293 uint32_t cs43l22_Resume(uint16_t DeviceAddr)
Jerome Coutant 2:123b894b49dd 294 {
Jerome Coutant 2:123b894b49dd 295 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 296 volatile uint32_t index = 0x00;
Jerome Coutant 2:123b894b49dd 297 /* Resumes the audio file playing */
Jerome Coutant 2:123b894b49dd 298 /* Unmute the output first */
Jerome Coutant 2:123b894b49dd 299 counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_OFF);
Jerome Coutant 2:123b894b49dd 300
Jerome Coutant 2:123b894b49dd 301 for(index = 0x00; index < 0xFF; index++);
Jerome Coutant 2:123b894b49dd 302
Jerome Coutant 2:123b894b49dd 303 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev);
Jerome Coutant 2:123b894b49dd 304
Jerome Coutant 2:123b894b49dd 305 /* Exit the Power save mode */
Jerome Coutant 2:123b894b49dd 306 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9E);
Jerome Coutant 2:123b894b49dd 307
Jerome Coutant 2:123b894b49dd 308 return counter;
Jerome Coutant 2:123b894b49dd 309 }
Jerome Coutant 2:123b894b49dd 310
Jerome Coutant 2:123b894b49dd 311 /**
Jerome Coutant 2:123b894b49dd 312 * @brief Stops audio Codec playing. It powers down the codec.
Jerome Coutant 2:123b894b49dd 313 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 314 * @param CodecPdwnMode: selects the power down mode.
Jerome Coutant 2:123b894b49dd 315 * - CODEC_PDWN_HW: Physically power down the codec. When resuming from this
Jerome Coutant 2:123b894b49dd 316 * mode, the codec is set to default configuration
Jerome Coutant 2:123b894b49dd 317 * (user should re-Initialize the codec in order to
Jerome Coutant 2:123b894b49dd 318 * play again the audio stream).
Jerome Coutant 2:123b894b49dd 319 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 320 */
Jerome Coutant 2:123b894b49dd 321 uint32_t cs43l22_Stop(uint16_t DeviceAddr, uint32_t CodecPdwnMode)
Jerome Coutant 2:123b894b49dd 322 {
Jerome Coutant 2:123b894b49dd 323 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 324
Jerome Coutant 2:123b894b49dd 325 /* Mute the output first */
Jerome Coutant 2:123b894b49dd 326 counter += cs43l22_SetMute(DeviceAddr, AUDIO_MUTE_ON);
Jerome Coutant 2:123b894b49dd 327
Jerome Coutant 2:123b894b49dd 328 /* Disable the digital soft ramp */
Jerome Coutant 2:123b894b49dd 329 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MISC_CTL, 0x04);
Jerome Coutant 2:123b894b49dd 330
Jerome Coutant 2:123b894b49dd 331 /* Power down the DAC and the speaker (PMDAC and PMSPK bits)*/
Jerome Coutant 2:123b894b49dd 332 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL1, 0x9F);
Jerome Coutant 2:123b894b49dd 333
Jerome Coutant 2:123b894b49dd 334 Is_cs43l22_Stop = 1;
Jerome Coutant 2:123b894b49dd 335 return counter;
Jerome Coutant 2:123b894b49dd 336 }
Jerome Coutant 2:123b894b49dd 337
Jerome Coutant 2:123b894b49dd 338 /**
Jerome Coutant 2:123b894b49dd 339 * @brief Sets higher or lower the codec volume level.
Jerome Coutant 2:123b894b49dd 340 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 341 * @param Volume: a byte value from 0 to 255 (refer to codec registers
Jerome Coutant 2:123b894b49dd 342 * description for more details).
Jerome Coutant 2:123b894b49dd 343 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 344 */
Jerome Coutant 2:123b894b49dd 345 uint32_t cs43l22_SetVolume(uint16_t DeviceAddr, uint8_t Volume)
Jerome Coutant 2:123b894b49dd 346 {
Jerome Coutant 2:123b894b49dd 347 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 348 uint8_t convertedvol = VOLUME_CONVERT(Volume);
Jerome Coutant 2:123b894b49dd 349
Jerome Coutant 2:123b894b49dd 350 if(Volume > 0xE6)
Jerome Coutant 2:123b894b49dd 351 {
Jerome Coutant 2:123b894b49dd 352 /* Set the Master volume */
Jerome Coutant 2:123b894b49dd 353 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_A_VOL, convertedvol - 0xE7);
Jerome Coutant 2:123b894b49dd 354 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_B_VOL, convertedvol - 0xE7);
Jerome Coutant 2:123b894b49dd 355 }
Jerome Coutant 2:123b894b49dd 356 else
Jerome Coutant 2:123b894b49dd 357 {
Jerome Coutant 2:123b894b49dd 358 /* Set the Master volume */
Jerome Coutant 2:123b894b49dd 359 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_A_VOL, convertedvol + 0x19);
Jerome Coutant 2:123b894b49dd 360 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_MASTER_B_VOL, convertedvol + 0x19);
Jerome Coutant 2:123b894b49dd 361 }
Jerome Coutant 2:123b894b49dd 362
Jerome Coutant 2:123b894b49dd 363 return counter;
Jerome Coutant 2:123b894b49dd 364 }
Jerome Coutant 2:123b894b49dd 365
Jerome Coutant 2:123b894b49dd 366 /**
Jerome Coutant 2:123b894b49dd 367 * @brief Sets new frequency.
Jerome Coutant 2:123b894b49dd 368 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 369 * @param AudioFreq: Audio frequency used to play the audio stream.
Jerome Coutant 2:123b894b49dd 370 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 371 */
Jerome Coutant 2:123b894b49dd 372 uint32_t cs43l22_SetFrequency(uint16_t DeviceAddr, uint32_t AudioFreq)
Jerome Coutant 2:123b894b49dd 373 {
Jerome Coutant 2:123b894b49dd 374 return 0;
Jerome Coutant 2:123b894b49dd 375 }
Jerome Coutant 2:123b894b49dd 376
Jerome Coutant 2:123b894b49dd 377 /**
Jerome Coutant 2:123b894b49dd 378 * @brief Enables or disables the mute feature on the audio codec.
Jerome Coutant 2:123b894b49dd 379 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 380 * @param Cmd: AUDIO_MUTE_ON to enable the mute or AUDIO_MUTE_OFF to disable the
Jerome Coutant 2:123b894b49dd 381 * mute mode.
Jerome Coutant 2:123b894b49dd 382 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 383 */
Jerome Coutant 2:123b894b49dd 384 uint32_t cs43l22_SetMute(uint16_t DeviceAddr, uint32_t Cmd)
Jerome Coutant 2:123b894b49dd 385 {
Jerome Coutant 2:123b894b49dd 386 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 387
Jerome Coutant 2:123b894b49dd 388 /* Set the Mute mode */
Jerome Coutant 2:123b894b49dd 389 if(Cmd == AUDIO_MUTE_ON)
Jerome Coutant 2:123b894b49dd 390 {
Jerome Coutant 2:123b894b49dd 391 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xFF);
Jerome Coutant 2:123b894b49dd 392 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_A_VOL, 0x01);
Jerome Coutant 2:123b894b49dd 393 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_B_VOL, 0x01);
Jerome Coutant 2:123b894b49dd 394 }
Jerome Coutant 2:123b894b49dd 395 else /* AUDIO_MUTE_OFF Disable the Mute */
Jerome Coutant 2:123b894b49dd 396 {
Jerome Coutant 2:123b894b49dd 397 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_A_VOL, 0x00);
Jerome Coutant 2:123b894b49dd 398 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_HEADPHONE_B_VOL, 0x00);
Jerome Coutant 2:123b894b49dd 399 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, OutputDev);
Jerome Coutant 2:123b894b49dd 400 }
Jerome Coutant 2:123b894b49dd 401 return counter;
Jerome Coutant 2:123b894b49dd 402 }
Jerome Coutant 2:123b894b49dd 403
Jerome Coutant 2:123b894b49dd 404 /**
Jerome Coutant 2:123b894b49dd 405 * @brief Switch dynamically (while audio file is played) the output target
Jerome Coutant 2:123b894b49dd 406 * (speaker or headphone).
Jerome Coutant 2:123b894b49dd 407 * @note This function modifies a global variable of the audio codec driver: OutputDev.
Jerome Coutant 2:123b894b49dd 408 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 409 * @param Output: specifies the audio output target: OUTPUT_DEVICE_SPEAKER,
Jerome Coutant 2:123b894b49dd 410 * OUTPUT_DEVICE_HEADPHONE, OUTPUT_DEVICE_BOTH or OUTPUT_DEVICE_AUTO
Jerome Coutant 2:123b894b49dd 411 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 412 */
Jerome Coutant 2:123b894b49dd 413 uint32_t cs43l22_SetOutputMode(uint16_t DeviceAddr, uint8_t Output)
Jerome Coutant 2:123b894b49dd 414 {
Jerome Coutant 2:123b894b49dd 415 uint32_t counter = 0;
Jerome Coutant 2:123b894b49dd 416
Jerome Coutant 2:123b894b49dd 417 switch (Output)
Jerome Coutant 2:123b894b49dd 418 {
Jerome Coutant 2:123b894b49dd 419 case OUTPUT_DEVICE_SPEAKER:
Jerome Coutant 2:123b894b49dd 420 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xFA); /* SPK always ON & HP always OFF */
Jerome Coutant 2:123b894b49dd 421 OutputDev = 0xFA;
Jerome Coutant 2:123b894b49dd 422 break;
Jerome Coutant 2:123b894b49dd 423
Jerome Coutant 2:123b894b49dd 424 case OUTPUT_DEVICE_HEADPHONE:
Jerome Coutant 2:123b894b49dd 425 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xAF); /* SPK always OFF & HP always ON */
Jerome Coutant 2:123b894b49dd 426 OutputDev = 0xAF;
Jerome Coutant 2:123b894b49dd 427 break;
Jerome Coutant 2:123b894b49dd 428
Jerome Coutant 2:123b894b49dd 429 case OUTPUT_DEVICE_BOTH:
Jerome Coutant 2:123b894b49dd 430 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0xAA); /* SPK always ON & HP always ON */
Jerome Coutant 2:123b894b49dd 431 OutputDev = 0xAA;
Jerome Coutant 2:123b894b49dd 432 break;
Jerome Coutant 2:123b894b49dd 433
Jerome Coutant 2:123b894b49dd 434 case OUTPUT_DEVICE_AUTO:
Jerome Coutant 2:123b894b49dd 435 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0x05); /* Detect the HP or the SPK automatically */
Jerome Coutant 2:123b894b49dd 436 OutputDev = 0x05;
Jerome Coutant 2:123b894b49dd 437 break;
Jerome Coutant 2:123b894b49dd 438
Jerome Coutant 2:123b894b49dd 439 default:
Jerome Coutant 2:123b894b49dd 440 counter += CODEC_IO_Write(DeviceAddr, CS43L22_REG_POWER_CTL2, 0x05); /* Detect the HP or the SPK automatically */
Jerome Coutant 2:123b894b49dd 441 OutputDev = 0x05;
Jerome Coutant 2:123b894b49dd 442 break;
Jerome Coutant 2:123b894b49dd 443 }
Jerome Coutant 2:123b894b49dd 444 return counter;
Jerome Coutant 2:123b894b49dd 445 }
Jerome Coutant 2:123b894b49dd 446
Jerome Coutant 2:123b894b49dd 447 /**
Jerome Coutant 2:123b894b49dd 448 * @brief Resets cs43l22 registers.
Jerome Coutant 2:123b894b49dd 449 * @param DeviceAddr: Device address on communication Bus.
Jerome Coutant 2:123b894b49dd 450 * @retval 0 if correct communication, else wrong communication
Jerome Coutant 2:123b894b49dd 451 */
Jerome Coutant 2:123b894b49dd 452 uint32_t cs43l22_Reset(uint16_t DeviceAddr)
Jerome Coutant 2:123b894b49dd 453 {
Jerome Coutant 2:123b894b49dd 454 return 0;
Jerome Coutant 2:123b894b49dd 455 }
Jerome Coutant 2:123b894b49dd 456
Jerome Coutant 2:123b894b49dd 457 /**
Jerome Coutant 2:123b894b49dd 458 * @brief Writes/Read a single data.
Jerome Coutant 2:123b894b49dd 459 * @param Addr: I2C address
Jerome Coutant 2:123b894b49dd 460 * @param Reg: Reg address
Jerome Coutant 2:123b894b49dd 461 * @param Value: Data to be written
Jerome Coutant 2:123b894b49dd 462 * @retval None
Jerome Coutant 2:123b894b49dd 463 */
Jerome Coutant 2:123b894b49dd 464 static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
Jerome Coutant 2:123b894b49dd 465 {
Jerome Coutant 2:123b894b49dd 466 uint32_t result = 0;
Jerome Coutant 2:123b894b49dd 467
Jerome Coutant 2:123b894b49dd 468 AUDIO_IO_Write(Addr, Reg, Value);
Jerome Coutant 2:123b894b49dd 469
Jerome Coutant 2:123b894b49dd 470 #ifdef VERIFY_WRITTENDATA
Jerome Coutant 2:123b894b49dd 471 /* Verify that the data has been correctly written */
Jerome Coutant 2:123b894b49dd 472 result = (AUDIO_IO_Read(Addr, Reg) == Value)? 0:1;
Jerome Coutant 2:123b894b49dd 473 #endif /* VERIFY_WRITTENDATA */
Jerome Coutant 2:123b894b49dd 474
Jerome Coutant 2:123b894b49dd 475 return result;
Jerome Coutant 2:123b894b49dd 476 }
Jerome Coutant 2:123b894b49dd 477
Jerome Coutant 2:123b894b49dd 478 /**
Jerome Coutant 2:123b894b49dd 479 * @}
Jerome Coutant 2:123b894b49dd 480 */
Jerome Coutant 2:123b894b49dd 481
Jerome Coutant 2:123b894b49dd 482 /**
Jerome Coutant 2:123b894b49dd 483 * @}
Jerome Coutant 2:123b894b49dd 484 */
Jerome Coutant 2:123b894b49dd 485
Jerome Coutant 2:123b894b49dd 486 /**
Jerome Coutant 2:123b894b49dd 487 * @}
Jerome Coutant 2:123b894b49dd 488 */
Jerome Coutant 2:123b894b49dd 489
Jerome Coutant 2:123b894b49dd 490 /**
Jerome Coutant 2:123b894b49dd 491 * @}
Jerome Coutant 2:123b894b49dd 492 */
Jerome Coutant 2:123b894b49dd 493
Jerome Coutant 2:123b894b49dd 494 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/