STM32L476G-Discovery board drivers V1.0.0

Dependents:   DiscoLogger DISCO_L476VG_GlassLCD DISCO_L476VG_MicrophoneRecorder DISCO_L476VG_UART ... more

Committer:
jeromecoutant
Date:
Tue Sep 24 18:00:58 2019 +0200
Revision:
5:4943b15cce9f
Parent:
1:917af0ca86df
Update BSP files with CubeL4 V1.14.0

Who changed what in which revision?

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