CARME-M4 BSP  V1.5
cs42l51.c
Go to the documentation of this file.
1 
74 #ifdef __cplusplus
75  extern "C" {
76 #endif /* __cplusplus */
77 
78 /*----- Header-Files -------------------------------------------------------*/
79 #include <stm32f4xx.h> /* Processor STM32F407IG */
80 #include <i2c.h> /* CARME I2C definitions */
81 #include <i2s.h> /* CARME I2S definitions */
82 #include <cs42l51.h> /* cs42l51 audio codec definitions */
83 
84 /*----- Macros -------------------------------------------------------------*/
85 
86 /*----- Data types ---------------------------------------------------------*/
87 
88 /*----- Function prototypes ------------------------------------------------*/
91 
92 /*----- Data ---------------------------------------------------------------*/
93 
94 /*----- Implementation -----------------------------------------------------*/
104 void CS42L51_WriteReg(uint8_t reg, uint8_t data) {
105 
106  CARME_I2C_Write(CODEC_I2C, CODEC_ADDRESS, (uint8_t) reg, 0, &data, 1);
107 }
108 
117 uint8_t CS42L51_ReadReg(uint8_t reg) {
118 
119  uint8_t data;
120 
121  CARME_I2C_Read(CODEC_I2C, CODEC_ADDRESS, (uint8_t) reg, 0, &data, 1);
122  return data;
123 }
124 
133 uint8_t CS42L51_Init(int8_t Volume) {
134 
137 
138  /* Check if the codec is connected */
139  if (CS42L51_ReadReg(CHIP_ID) != 0xD9) {
140  return 1;
141  }
142 
143  /* Keep Codec powered OFF */
145 
151 
157 
162 
168 
174 
180 
187 
188  /* Set ALCAx and PGAx control */
191 
198 
199  /* Set treble and bass */
201 
202  /* Set the output volume */
205 
214 
221 
229 
235 
236  /* Power down up after initial configure */
238 
247 // CS42L51_WriteReg(BEEP_FREQUENCY_AND_TIMING, 0x0F);
248 // CS42L51_WriteReg(BEEP_OFF_TIME_AND_VOLUME, 0x66);
249 // CS42L51_WriteReg(BEEP_AND_TONE_CONFIGURATION, 0xC0);
250  CS42L51_VolumeOutCtrl(Volume);
251 
252  return 0;
253 }
254 
264 
266 }
267 
276 
277  I2S_InitTypeDef I2S_InitStruct;
278 
279  /* Enable the peripheral clock */
281 
282  /* GPIO configuration */
284 
285  /* Deinitialize and disable the I2S and SPI hardware */
286  SPI_Cmd(CODEC_I2S, DISABLE);
287  I2S_Cmd(CODEC_I2S, DISABLE);
288  I2S_Cmd(CODEC_I2S_EXT, DISABLE);
290 
291  /* CODEC_I2S peripheral configuration */
292  I2S_InitStruct.I2S_AudioFreq = I2S_AudioFreq_48k;
293  I2S_InitStruct.I2S_Standard = I2S_Standard_Phillips;
294  I2S_InitStruct.I2S_DataFormat = I2S_DataFormat_16b;
295  I2S_InitStruct.I2S_CPOL = I2S_CPOL_Low;
296  I2S_InitStruct.I2S_Mode = I2S_Mode_MasterTx;
297  I2S_InitStruct.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
298 
299  /* Configure the I2S */
300  I2S_Init(CODEC_I2S, &I2S_InitStruct);
301  /* Initialize the I2S extended channel for RX */
302  I2S_FullDuplexConfig(CODEC_I2S_EXT, &I2S_InitStruct);
303 
304  I2S_Cmd(CODEC_I2S, ENABLE);
305  I2S_Cmd(CODEC_I2S_EXT, ENABLE);
306 }
307 
315 uint8_t CS42L51_Status(void) {
316 
318 }
319 
337 void CS42L51_VolumeOutCtrl(int8_t Volume) {
338 
339 // uint8_t reg;
340 //
341 // Volume &= 0x07;
342 // Volume <<= 5;
343 // reg = CS42L51_ReadReg(DAC_OUTPUT_CONTROL);
344 // reg &= 0x1F;
345 // reg |= Volume;
346 //
347 // CS42L51_WriteReg(DAC_OUTPUT_CONTROL, reg);
348  if (Volume > 0xE6) {
349  /* Set the Master volume */
350  CS42L51_WriteReg(AOUTA_VOLUME_CONTROL, Volume - 0xE7);
351  CS42L51_WriteReg(AOUTB_VOLUME_CONTROL, Volume - 0xE7);
352  }
353  else {
354  /* Set the Master volume */
355  CS42L51_WriteReg(AOUTA_VOLUME_CONTROL, Volume + 0x19);
356  CS42L51_WriteReg(AOUTB_VOLUME_CONTROL, Volume + 0x19);
357  }
358 }
359 
369 void CS42L51_Mute(uint8_t on) {
370 
371  uint8_t reg;
372 
374 
375  if (on) {
376  reg |= 0x03;
377  }
378  else {
379  reg &= ~0x03;
380  }
381 
383 }
384 
385 #ifdef __cplusplus
386  }
387 #endif /* __cplusplus */
388 
uint16_t I2S_MCLKOutput
void I2S_Cmd(SPI_TypeDef *SPIx, FunctionalState NewState)
Enables or disables the specified SPI peripheral (in I2S mode).
#define CODEC_I2S_CLK
Definition: cs42l51.h:83
#define CODEC_I2C
Definition: cs42l51.h:80
#define PCMB_MIXER_VOLUME_CONTROL
Definition: cs42l51.h:100
void CS42L51_WriteReg(uint8_t reg, uint8_t data)
Write a byte to a register of the cs42l51 codec.
Definition: cs42l51.c:104
#define LIMITER_THRESHOLD
Definition: cs42l51.h:108
#define PCMA_MIXER_VOLUME_CONTROL
Definition: cs42l51.h:99
#define INTERFACE_CONTROL
Definition: cs42l51.h:89
void I2S_Init(SPI_TypeDef *SPIx, I2S_InitTypeDef *I2S_InitStruct)
Initializes the SPIx peripheral according to the specified parameters in the I2S_InitStruct.
uint16_t I2S_Mode
Definition: stm32f4xx_spi.h:94
#define PCM_CHANNEL_MIXER
Definition: cs42l51.h:107
#define MIC_CONTROL
Definition: cs42l51.h:90
#define CODEC_ADDRESS
Definition: cs42l51.h:84
#define LIMITER_ATTACK
Definition: cs42l51.h:110
#define AOUTA_VOLUME_CONTROL
Definition: cs42l51.h:105
uint32_t I2S_AudioFreq
uint8_t CS42L51_Init(int8_t Volume)
Codec CS42L51 low-layer and register initialization.
Definition: cs42l51.c:133
#define MIC_POWER_AND_SPEED
Definition: cs42l51.h:88
void I2S_FullDuplexConfig(SPI_TypeDef *I2Sxext, I2S_InitTypeDef *I2S_InitStruct)
Configures the full duplex mode for the I2Sx peripheral using its extension I2Sxext according to the ...
I2S Init structure definition.
Definition: stm32f4xx_spi.h:91
ERROR_CODES CARME_I2C_Read(I2C_TypeDef *I2Cx, uint8_t addr, uint16_t reg, uint8_t twoByte, uint8_t *pdata, uint16_t count)
Read data from an I2C slave device on a specific register address.
Definition: i2c.c:312
uint8_t CS42L51_Status(void)
Get the status of the cs42l51 codec.
Definition: cs42l51.c:315
void CS42L51_CtrlInterface_Init(void)
Codec CS42L51 ctrl line configuration and register initialization.
Definition: cs42l51.c:263
I2S board support package for the CARME-M4 module.
void CARME_I2C_Init(I2C_TypeDef *I2Cx)
I2C initialization.
Definition: i2c.c:124
void CS42L51_Mute(uint8_t on)
Mute the CS42L51 output.
Definition: cs42l51.c:369
#define CODEC_I2S
Definition: cs42l51.h:81
#define ADC_CONTROL
Definition: cs42l51.h:91
void SPI_I2S_DeInit(SPI_TypeDef *SPIx)
De-initialize the SPIx peripheral registers to their default reset values.
#define ADC_CONFIGURE
Definition: cs42l51.h:92
uint16_t I2S_Standard
Definition: stm32f4xx_spi.h:97
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the Low Speed APB (APB1) peripheral clock.
#define POWER_CONTROL
Definition: cs42l51.h:87
CS42L51 codec support functions.
#define ALCA_AND_PGAA_CONTROL
Definition: cs42l51.h:95
uint16_t I2S_DataFormat
void CARME_I2S_GPIO_Init(void)
I2S GPIO initialization.
Definition: i2s.c:107
ERROR_CODES CARME_I2C_Write(I2C_TypeDef *I2Cx, uint8_t addr, uint16_t reg, uint8_t twoByte, uint8_t *pdata, uint16_t count)
Write data to an I2C slave device on a specific register address.
Definition: i2c.c:226
#define TONE_CONTROL
Definition: cs42l51.h:104
void CS42L51_AudioInterface_Init(void)
Codec CS42L51 audio line configuration.
Definition: cs42l51.c:275
#define LIMITER_RELEASE
Definition: cs42l51.h:109
#define CHIP_ID
Definition: cs42l51.h:86
#define STATUS_REGISTER
Definition: cs42l51.h:115
#define DAC_OUTPUT_CONTROL
Definition: cs42l51.h:93
#define CODEC_I2S_EXT
Definition: cs42l51.h:82
uint8_t CS42L51_ReadReg(uint8_t reg)
Read a byte from a register of the cs42l51 codec.
Definition: cs42l51.c:117
void SPI_Cmd(SPI_TypeDef *SPIx, FunctionalState NewState)
Enables or disables the specified SPI peripheral.
#define ALCB_AND_PGAB_CONTROL
Definition: cs42l51.h:96
#define AOUTB_VOLUME_CONTROL
Definition: cs42l51.h:106
void CS42L51_VolumeOutCtrl(int8_t Volume)
Codec CS42L51 output volume control.
Definition: cs42l51.c:337
I2C board support package for the CARME module.
#define DAC_CONTROL
Definition: cs42l51.h:94