This commit is contained in:
cfif 2024-12-04 13:10:47 +03:00
commit d9a563ac9f
3 changed files with 205 additions and 0 deletions

155
AudioCodec.c Normal file
View File

@ -0,0 +1,155 @@
//
// Created by xemon on 19.10.22.
//
#include "AudioCodec.h"
#include <max9860_regs.h>
#include <AsciiStringAssmeblingUtils.h>
#include <AsciiStringParsingUtils.h>
bool AudioCodecInitInterface(tAudioCodec *env);
bool AudioCodecTurnOff(tAudioCodec *env);
bool AudioCodecTurnOn(tAudioCodec *env);
bool AudioCodecInvalidateSettings(tAudioCodec *env);
#define IF_NOT_OK_I2C_RETURN(VALUE) if(!VALUE) return false;
void AudioCodecInit(
tAudioCodec *env,
tI2cIO *codecIO,
MAX9860_ComplexAudioConfig *config
) {
env->codecIO = codecIO;
env->config = config;
}
bool AudioCodecEnable(tAudioCodec *env) {
// return true;
IF_NOT_OK_I2C_RETURN(AudioCodecTurnOff(env))
IF_NOT_OK_I2C_RETURN (AudioCodecInitInterface(env))
IF_NOT_OK_I2C_RETURN(AudioCodecInvalidateSettings(env))
IF_NOT_OK_I2C_RETURN(AudioCodecTurnOn(env))
// char asciiSettings[] = "00000000109E3F0400000633004A0000008A";
// char asciiSettings[] = "00109000040000048c20340000008a";
// uint8_t byteSettings[sizeof(asciiSettings) - 1 / 2];
// size_t dataLen = iAsciiStringParseHexBytes(byteSettings, asciiSettings, sizeof(asciiSettings) - 1);
// uint16_t res = I2cWrite(env->codecIO, MAX9860_ADDRESS_WRITE, byteSettings, dataLen, MAX9860_IO_TIMEOUT) == dataLen;
// return res;
return true;
}
bool AudioCodecTurnOff(tAudioCodec *env) {
MAX9860_PowerManagementState MAX9860_POWER = {
.FullPowerOn=false,
.DAC_Enabled=false,
.ADC_EnabledLeft=false,
.ADC_EnabledRight=false,
};
return xAudioCodecSetPowerManagement(env->codecIO, MAX9860_POWER);
}
bool AudioCodecTurnOn(tAudioCodec *env) {
MAX9860_PowerManagementState MAX9860_POWER = (MAX9860_PowerManagementState) {
.FullPowerOn=true,
.DAC_Enabled=true,
.ADC_EnabledLeft=true,
.ADC_EnabledRight=false,
};
return xAudioCodecSetPowerManagement(env->codecIO, MAX9860_POWER);
}
bool AudioCodecInitInterface(tAudioCodec *env) {
// IF_NOT_OK_I2C_RETURN(AudioCodecTurnOff(env))
// uint8_t AUD_SET[15];
// vConvertAsciiToBytes((unsigned char *) "031120000C0044063320200000D58A", 30, (char *) AUD_SET);
//// "03 - 11 2000 - 0C 00 - 44063320200000D58A"
//// I2cIOResult codecSetRes =
// env->audioCodecIO->write(0x20, AUD_SET, 15, 1000);
// vMainModesArbiterInitMax9860(env);
MAX9860_ClocksState MAX9860_CLOCK = {
.MCLK_TO_PCLK_Prescaler=MAX9860_PRESCALER_MCLK_10_TO_20_MHZ,
.IntegerClockMode=MAX9860_ICM_NORMAL,
.EXACTLY_16KHZ=false,
//LRCLK for mclk 12.288mhz
.PLL_Enable=true,
.LRCLK_Driver=0x1000,// 8khz
// .LRCLK_Driver=0x2000,//16khz
};
IF_NOT_OK_I2C_RETURN(xAudioCodecSetClock(env->codecIO, MAX9860_CLOCK))
MAX9860_AudioInterfaceState MAX9860_AUDIO_INTERFACE = {
.MasterMode = false,
.LRCLK_Invert = false,
.DAC_BCLK_Invert = true,
.DAC_DelayMode = false,
.SDOUT_HighImpedanceMode = false,
.TDM_ModeSelect = true,
.ADC_BCLK_Invert = false,
.ADC_Delay_Mode= false,
.StereoEnable= false,
.BCLK_Select= 0b000,
};
IF_NOT_OK_I2C_RETURN(xAudioCodecSetInterface(env->codecIO, MAX9860_AUDIO_INTERFACE))
return true;
}
bool AudioCodecInvalidateSettings(tAudioCodec *env) {
IF_NOT_OK_I2C_RETURN(
xAudioCodecSetDigitalFilters(
env->codecIO,
env->config->filters
)
)
IF_NOT_OK_I2C_RETURN(
xAudioCodecSetDigitalLevelControls(
env->codecIO,
env->config->levelControl
)
)
IF_NOT_OK_I2C_RETURN(
xAudioCodecSetMicrophoneInput(
env->codecIO,
env->config->microphoneGains
)
)
IF_NOT_OK_I2C_RETURN(
xAudioCodecSetAutomaticGainControlAndNoiseGate(
env->codecIO,
env->config->autoGainAndNoiseGate
)
)
return true;
}
bool AudioCodecTestStatus(tAudioCodec *env) {
uint8_t status[3];
uint8_t read = xAudioCodecReadStatus(env->codecIO, status);
return read;
}

28
AudioCodec.h Normal file
View File

@ -0,0 +1,28 @@
//
// Created by xemon on 19.10.22.
//
#ifndef UVEOS_ON_NATION_AUDIOCODEC_H
#define UVEOS_ON_NATION_AUDIOCODEC_H
#include "I2cIO.h"
#include "max9860.h"
typedef struct {
tI2cIO *codecIO;
MAX9860_ComplexAudioConfig *config;
} tAudioCodec;
void AudioCodecInit(
tAudioCodec *env,
tI2cIO *codecIO,
MAX9860_ComplexAudioConfig *config
);
bool AudioCodecEnable(tAudioCodec *env);
bool AudioCodecInvalidateSettings(tAudioCodec *env);
bool AudioCodecTestStatus(tAudioCodec *env);
#endif //UVEOS_ON_NATION_AUDIOCODEC_H

22
modular.json Normal file
View File

@ -0,0 +1,22 @@
{
"dep": [
{
"type": "git",
"provider": "NAVIGATOR_UVEOS_NATION_TELIT",
"repo": "AudioCodec_MAX9860"
},
{
"type": "git",
"provider": "NAVIGATOR_UVEOS_NATION_TELIT",
"repo": "UveosOnNation_VEGA_I2cPorts"
}
],
"cmake": {
"inc_dirs": [
"./"
],
"srcs": [
"./**.c"
]
}
}