library

Dependents:   Aprendendo Final_v3

Committer:
zigdrix
Date:
Mon Oct 19 22:13:50 2020 +0000
Revision:
4:e746cdd3e76a
Parent:
0:0002c86c2220
Projeto Final PI 1

Who changed what in which revision?

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