#include "GDSP_Codec.h" #include "main.h" HAL_StatusTypeDef GDSP_Codec_Init(GDSP_Codec *codec, I2C_HandleTypeDef *I2Chandle, GPIO_TypeDef *nrstPinBank, uint16_t nrstPin){ codec->I2Chandle = I2Chandle; codec->nrstPinBank = nrstPinBank; codec->nrstPin = nrstPin; GDSP_Codec_Reset(); HAL_StatusTypeDef status; status = GDSP_Codec_WriteRegister(codec, 0x01, 0x01); //Software Reset HAL_Delay(10); status = GDSP_Codec_WriteRegister(codec, 0x03, 0x10); //Disable PLL, 2 = 4. MCLK = 256 * 48Khz, fs(ref) = MCLK / (128 * Q) status = GDSP_Codec_WriteRegister(codec, 0x07, 0x08); //fs(ref) = 48khz, left DAC data path plays left-channel input data, right DAC data path is muted status = GDSP_Codec_WriteRegister(codec, 0x0F, 0x00); //Un-mute left ADC PGA status = GDSP_Codec_WriteRegister(codec, 0x10, 0x80); //Mute right ADC PGA status = GDSP_Codec_WriteRegister(codec, 0x11, 0x0F); //Connect MIC2L/LINE2L to left ADC PGA (@0db gain), do not connect MIC2R/LINE2R to left ADC PGA status = GDSP_Codec_WriteRegister(codec, 0x13, 0x7C); //Power up left ADC status = GDSP_Codec_WriteRegister(codec, 0x25, 0x80); //Power up left DAC status = GDSP_Codec_WriteRegister(codec, 0x28, 0x80); //Output common mode voltage = 1,65V (= VCC / 2) status = GDSP_Codec_WriteRegister(codec, 0x2B, 0x00); //Unmute left DAC channel status = GDSP_Codec_WriteRegister(codec, 0x52, 0x80); //Route left DAC to left line output status = GDSP_Codec_WriteRegister(codec, 0x56, 0x0B); //Unmute left line output status = GDSP_Codec_WriteRegister(codec, 0x65, 0x01); //CODEC_CLKIN uses CLKDIV_OUT return status; } void GDSP_Codec_Reset(){ HAL_GPIO_WritePin(CODEC_NRST_GPIO_Port, CODEC_NRST_Pin, GPIO_PIN_RESET); HAL_Delay(25); HAL_GPIO_WritePin(CODEC_NRST_GPIO_Port, CODEC_NRST_Pin, GPIO_PIN_SET); HAL_Delay(25); } HAL_StatusTypeDef GDSP_Codec_WriteRegister(GDSP_Codec *codec, uint8_t regAddr, uint8_t val){ HAL_StatusTypeDef status = HAL_I2C_Mem_Write(codec->I2Chandle, GDSP_CODEC_I2C_ADDR, regAddr, I2C_MEMADD_SIZE_8BIT, &val, 1, HAL_MAX_DELAY); return status; } HAL_StatusTypeDef GDSP_Codec_ReadRegister(GDSP_Codec *codec, uint8_t regAddr, uint8_t val){ HAL_StatusTypeDef status = HAL_I2C_Mem_Read(codec->I2Chandle, GDSP_CODEC_I2C_ADDR, regAddr, I2C_MEMADD_SIZE_8BIT, &val, 1, HAL_MAX_DELAY); return status; }