commit d9a563ac9fc5711bbaf2aa02645bcb8e20f99f8b Author: cfif Date: Wed Dec 4 13:10:47 2024 +0300 Init diff --git a/AudioCodec.c b/AudioCodec.c new file mode 100644 index 0000000..a68f795 --- /dev/null +++ b/AudioCodec.c @@ -0,0 +1,155 @@ +// +// Created by xemon on 19.10.22. +// +#include "AudioCodec.h" +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/AudioCodec.h b/AudioCodec.h new file mode 100644 index 0000000..99da8b9 --- /dev/null +++ b/AudioCodec.h @@ -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 diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..2097af1 --- /dev/null +++ b/modular.json @@ -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" + ] + } +} \ No newline at end of file