|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745 |
-
-
- #include "control_tlv320aic3206.h"
- #include <Wire.h>
-
-
-
- #define AIC3206_I2C_ADDR 0x18
-
-
- #ifndef AIC_FS
- # define AIC_FS 44100UL
- #endif
-
- #define AIC_BITS 16
-
-
- #define AIC_I2S_SLAVE 1
- #if AIC_I2S_SLAVE
-
- # define AIC_CLK_DIR 0
- #else
-
- # define AIC_CLK_DIR 0x0C
- #endif
-
-
-
-
-
-
-
-
- #if AIC_FS == 44100
-
-
-
-
-
-
-
- #define PLL_J 8
- #define PLL_D 0
-
-
-
-
-
- #if AIC_BITS == 16
- #define BCLK_N 8
- #elif AIC_BITS == 32
- #define BCLK_N 4
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #define DOSR 128
- #define NDAC 2
- #define MDAC 8
-
- #define AOSR 128
- #define NADC 2
- #define MADC 8
-
-
- #define PRB_P 1
- #define PRB_R 1
-
- #endif
-
-
-
-
-
- #define AIC3206_MICPGA_LEFT_POSITIVE_REG 0x0134
- #define AIC3206_MICPGA_LEFT_NEGATIVE_REG 0x0136
- #define AIC3206_MICPGA_RIGHT_POSITIVE_REG 0x0137
- #define AIC3206_MICPGA_RIGHT_NEGATIVE_REG 0x0139
-
- #define AIC3206_MIC_ROUTING_POSITIVE_IN1 0b11000000
- #define AIC3206_MIC_ROUTING_POSITIVE_IN2 0b00110000
- #define AIC3206_MIC_ROUTING_POSITIVE_IN3 0b00001100
- #define AIC3206_MIC_ROUTING_POSITIVE_REVERSE 0b00000011
-
- #define AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L 0b11000000
- #define AIC3206_MIC_ROUTING_NEGATIVE_IN2_REVERSE 0b00110000
- #define AIC3206_MIC_ROUTING_NEGATIVE_IN3_REVERSE 0b00001100
- #define AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM2L 0b00000011
-
- #define AIC3206_MIC_ROUTING_RESISTANCE_10k 0b01010101
- #define AIC3206_MIC_ROUTING_RESISTANCE_20k 0b10101010
- #define AIC3206_MIC_ROUTING_RESISTANCE_40k 0b11111111
- #define AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT AIC3206_MIC_ROUTING_RESISTANCE_10k
-
- #define AIC3206_MICPGA_LEFT_VOLUME_REG 0x013B
- #define AIC3206_MICPGA_RIGHT_VOLUME_REG 0x013C
-
- #define AIC3206_MICPGA_VOLUME_ENABLE 0x00
-
- #define AIC3206_MIC_BIAS_REG 0x0133
- #define AIC3206_MIC_BIAS_POWER_ON 0x40
- #define AIC3206_MIC_BIAS_POWER_OFF 0x00
- #define AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_1_25 0x00
- #define AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_1_7 0x01
- #define AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_2_5 0x10
- #define AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_VSUPPLY 0x11
-
- #define AIC3206_ADC_PROCESSING_BLOCK_REG 0x003d
-
- #define AIC3206_ADC_CHANNEL_POWER_REG 0x0051
- #define AIC3206_ADC_CHANNELS_ON 0b11000000
-
- #define AIC3206_ADC_MUTE_REG 0x0052
- #define AIC3206_ADC_UNMUTE 0x00
-
- bool AudioControlTLV320AIC3206::enable(void)
- {
- delay(100);
-
-
- Wire.begin();
- delay(5);
-
-
-
-
- #define RESET_PIN (resetPinAIC)
- pinMode(RESET_PIN,OUTPUT);
- digitalWrite(RESET_PIN,HIGH);delay(50);
- digitalWrite(RESET_PIN,LOW);delay(50);
- digitalWrite(RESET_PIN,HIGH);delay(50);
-
- aic_reset(); delay(100);
- aic_init(); delay(100);
- aic_initADC(); delay(100);
- aic_initDAC(); delay(100);
-
- aic_readPage(0, 27);
-
- if (debugToSerial) Serial.println("TLV320 enable done");
-
- return true;
-
- }
-
- bool AudioControlTLV320AIC3206::disable(void) {
- return true;
- }
-
-
- bool AudioControlTLV320AIC3206::inputLevel(float volume) {
- return false;
- }
-
- bool AudioControlTLV320AIC3206::inputSelect(int n) {
- if (n == AIC3206_INPUT_IN1) {
- aic_writeAddress(AIC3206_MICPGA_LEFT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN1 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_LEFT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN1 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
-
- setMicBias(AIC3206_MIC_BIAS_OFF);
-
- if (debugToSerial) Serial.println("Set Audio Input to IN1");
- return true;
- } else if (n == AIC3206_INPUT_IN3_MICBIAS) {
- aic_writeAddress(AIC3206_MICPGA_LEFT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN3 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_LEFT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN3 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
-
- setMicBias(AIC3206_DEFAULT_MIC_BIAS);
-
- if (debugToSerial) Serial.println("Set Audio Input to IN3, BIAS SET TO DEFAULT 2.5V");
- return true;
- } else if (n == AIC3206_INPUT_IN3) {
- aic_writeAddress(AIC3206_MICPGA_LEFT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN3 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_LEFT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN3 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
-
- setMicBias(AIC3206_MIC_BIAS_OFF);
-
- if (debugToSerial) Serial.println("Set Audio Input to IN3, BIAS OFF");
- return true;
- } else if (n == AIC3206_INPUT_IN2) {
- aic_writeAddress(AIC3206_MICPGA_LEFT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN2 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_LEFT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN2 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
-
- setMicBias(AIC3206_MIC_BIAS_OFF);
- if (debugToSerial) Serial.println("Set Audio Input to IN2, BIAS OFF");
-
- return true;
- }
- Serial.print("controlTLV320AIC3206: ERROR: Unable to Select Input - Value not supported: ");
- Serial.println(n);
- return false;
- }
-
- bool AudioControlTLV320AIC3206::setMicBias(int n) {
- if (n == AIC3206_MIC_BIAS_1_25) {
- aic_writeAddress(AIC3206_MIC_BIAS_REG, AIC3206_MIC_BIAS_POWER_ON | AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_1_25);
- return true;
- } else if (n == AIC3206_MIC_BIAS_1_7) {
- aic_writeAddress(AIC3206_MIC_BIAS_REG, AIC3206_MIC_BIAS_POWER_ON | AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_1_7);
- return true;
- } else if (n == AIC3206_MIC_BIAS_2_5) {
- aic_writeAddress(AIC3206_MIC_BIAS_REG, AIC3206_MIC_BIAS_POWER_ON | AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_2_5);
- return true;
- } else if (n == AIC3206_MIC_BIAS_VSUPPLY) {
- aic_writeAddress(AIC3206_MIC_BIAS_REG, AIC3206_MIC_BIAS_POWER_ON | AIC3206_MIC_BIAS_OUTPUT_VOLTAGE_VSUPPLY);
- return true;
- } else if (n == AIC3206_MIC_BIAS_OFF) {
- aic_writeAddress(AIC3206_MIC_BIAS_REG, AIC3206_MIC_BIAS_POWER_OFF);
- return true;
- }
- Serial.print("controlTLV320AIC3206: ERROR: Unable to set MIC BIAS - Value not supported: ");
- Serial.println(n);
- return false;
- }
-
- void AudioControlTLV320AIC3206::aic_reset() {
- if (debugToSerial) Serial.println("INFO: Reseting AIC");
- aic_writePage(0x00, 0x01, 0x01);
-
-
- delay(10);
- }
-
-
-
-
-
-
-
-
- void AudioControlTLV320AIC3206::aic_initADC() {
- if (debugToSerial) Serial.println("INFO: Initializing AIC ADC");
- aic_writeAddress(AIC3206_ADC_PROCESSING_BLOCK_REG, PRB_R);
- aic_writePage(1, 61, 0);
- aic_writePage(1, 71, 0b00110001);
- aic_writeAddress(AIC3206_MIC_BIAS_REG, AIC3206_MIC_BIAS_POWER_ON | AIC3206_MIC_BIAS_2_5);
-
- aic_writeAddress(AIC3206_MICPGA_LEFT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN2 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_LEFT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_POSITIVE_REG, AIC3206_MIC_ROUTING_POSITIVE_IN2 & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_NEGATIVE_REG, AIC3206_MIC_ROUTING_NEGATIVE_CM_TO_CM1L & AIC3206_MIC_ROUTING_RESISTANCE_DEFAULT);
-
- aic_writeAddress(AIC3206_MICPGA_LEFT_VOLUME_REG, AIC3206_MICPGA_VOLUME_ENABLE);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_VOLUME_REG, AIC3206_MICPGA_VOLUME_ENABLE);
-
- aic_writeAddress(AIC3206_ADC_MUTE_REG, AIC3206_ADC_UNMUTE);
- aic_writeAddress(AIC3206_ADC_CHANNEL_POWER_REG, AIC3206_ADC_CHANNELS_ON);
- }
-
-
- bool AudioControlTLV320AIC3206::setInputGain_dB(float volume) {
- if (volume < 0.0) {
- volume = 0.0;
- Serial.println("controlTLV320AIC3206: WARNING: Attempting to set MIC volume outside range");
- }
- if (volume > 47.5) {
- volume = 47.5;
- Serial.println("controlTLV320AIC3206: WARNING: Attempting to set MIC volume outside range");
- }
-
- volume = volume * 2.0;
- int8_t volume_int = (int8_t) (round(volume));
-
- if (debugToSerial) {
- Serial.print("INFO: Setting MIC volume to ");
- Serial.print(volume, 1);
- Serial.print(". Converted to volume map => ");
- Serial.println(volume_int);
- }
-
- aic_writeAddress(AIC3206_MICPGA_LEFT_VOLUME_REG, AIC3206_MICPGA_VOLUME_ENABLE | volume_int);
- aic_writeAddress(AIC3206_MICPGA_RIGHT_VOLUME_REG, AIC3206_MICPGA_VOLUME_ENABLE | volume_int);
- return true;
- }
-
-
- #define AIC3206_DAC_PROCESSING_BLOCK_REG 0x003c
- #define AIC3206_DAC_VOLUME_LEFT_REG 0x0041
- #define AIC3206_DAC_VOLUME_RIGHT_REG 0x0042
-
-
-
-
- bool AudioControlTLV320AIC3206::volume(float volume) {
- volume = max(0.0, min(1.0, volume));
- float vol_dB = -58.f + (15.0 - (-58.0f)) * volume;
- volume_dB(vol_dB);
- return true;
- }
-
- bool AudioControlTLV320AIC3206::enableAutoMuteDAC(bool enable, uint8_t mute_delay_code=7) {
- if (enable) {
- mute_delay_code = max(0,min(mute_delay_code,7));
- if (mute_delay_code == 0) enable = false;
- } else {
- mute_delay_code = 0;
- }
- uint8_t val = aic_readPage(0,64);
- val = val & 0b10001111;
- val = val | (mute_delay_code << 4);
- aic_writePage(0,64,val);
- return enable;
- }
-
-
- bool AudioControlTLV320AIC3206::volume_dB(float volume) {
-
-
- if (volume > 24.0) {
- volume = 24.0;
- Serial.println("controlTLV320AIC3206: WARNING: Attempting to set DAC Volume outside range");
- }
- if (volume < -63.5) {
- volume = -63.5;
- Serial.println("controlTLV320AIC3206: WARNING: Attempting to set DAC Volume outside range");
- }
-
- volume = volume * 2.0;
- int8_t volume_int = (int8_t) (round(volume));
-
- if (debugToSerial) {
- Serial.print("controlTLV320AIC3206: Setting DAC volume to ");
- Serial.print(volume, 1);
- Serial.print(". Converted to volume map => ");
- Serial.println(volume_int);
- }
-
- aic_writeAddress(AIC3206_DAC_VOLUME_RIGHT_REG, volume_int);
- aic_writeAddress(AIC3206_DAC_VOLUME_LEFT_REG, volume_int);
- return true;
- }
-
- void AudioControlTLV320AIC3206::aic_initDAC() {
- if (debugToSerial) Serial.println("controlTLV320AIC3206: Initializing AIC DAC");
- outputSelect(AIC3206_OUTPUT_HEADPHONE_JACK_OUT);
- }
-
- bool AudioControlTLV320AIC3206::outputSelect(int n) {
-
-
-
-
- aic_writeAddress(AIC3206_DAC_PROCESSING_BLOCK_REG, PRB_P);
-
-
- aic_writePage(1, 16, 0b01000000);
- aic_writePage(1, 17, 0b01000000);
- aic_writePage(1, 18, 0b01000000);
- aic_writePage(1, 19, 0b01000000);
- aic_writePage(0, 63, 0);
- aic_writePage(1, 9, 0);
-
- aic_writePage(1,12,0);
- aic_writePage(1,13,0);
- aic_writePage(1,14,0);
- aic_writePage(1,15,0);
-
-
- if (n == AIC3206_OUTPUT_HEADPHONE_JACK_OUT) {
-
-
-
-
- aic_writePage(1, 12, 0b00001000);
- aic_writePage(1, 13, 0b00001000);
- aic_writePage(0, 63, 0xD6);
- aic_writePage(1, 16, 0);
- aic_writePage(1, 17, 0);
- aic_writePage(1, 9, 0x30);
- delay(100);
- aic_writeAddress(AIC3206_DAC_VOLUME_LEFT_REG, 0);
- aic_writeAddress(AIC3206_DAC_VOLUME_RIGHT_REG, 0);
- aic_writePage(0, 64, 0);
-
- if (debugToSerial) Serial.println("controlTLV320AIC3206: Set Audio Output to Headphone Jack");
- return true;
- } else if (n == AIC3206_OUTPUT_LINE_OUT) {
-
-
- aic_writePage(1, 14, 0b00001000);
- aic_writePage(1, 15, 0b00001000);
- aic_writePage(0, 63, 0xD6);
- aic_writePage(1, 18, 0);
- aic_writePage(1, 19, 0);
- aic_writePage(1, 9, 0b00001100);
- delay(100);
- aic_writeAddress(AIC3206_DAC_VOLUME_LEFT_REG, 0);
- aic_writeAddress(AIC3206_DAC_VOLUME_RIGHT_REG, 0);
- aic_writePage(0, 64, 0);
-
- if (debugToSerial) Serial.println("controlTLV320AIC3206: Set Audio Output to Line Out");
- return true;
- } else if (n == AIC3206_OUTPUT_HEADPHONE_AND_LINE_OUT) {
- aic_writePage(1, 12, 0b00001000);
- aic_writePage(1, 13, 0b00001000);
- aic_writePage(1, 14, 0b00001000);
- aic_writePage(1, 15, 0b00001000);
-
- aic_writePage(0, 63, 0xD6);
- aic_writePage(1, 18, 0);
- aic_writePage(1, 19, 0);
- aic_writePage(1, 16, 0);
- aic_writePage(1, 17, 0);
-
- aic_writePage(1, 9, 0b00111100);
-
- delay(100);
- aic_writeAddress(AIC3206_DAC_VOLUME_LEFT_REG, 0);
- aic_writeAddress(AIC3206_DAC_VOLUME_RIGHT_REG, 0);
- aic_writePage(0, 64, 0);
-
- if (debugToSerial) Serial.println("controlTLV320AIC3206: Set Audio Output to Headphone Jack and Line out");
- return true;
- }
- Serial.print("controlTLV320AIC3206: ERROR: Unable to Select Output - Value not supported: ");
- Serial.println(n);
- return false;
- }
-
-
- void AudioControlTLV320AIC3206::aic_init() {
- if (debugToSerial) Serial.println("controlTLV320AIC3206: Initializing AIC");
-
-
- aic_writePage(0, 4, 3);
- aic_writePage(0, 5, (PLL_J != 0 ? 0x91 : 0x11));
- aic_writePage(0, 6, PLL_J);
- aic_writePage(0, 7, PLL_D >> 8);
- aic_writePage(0, 8, PLL_D &0xFF);
-
-
- aic_writePage(0, 11, 0x80 | NDAC);
- aic_writePage(0, 12, 0x80 | MDAC);
- aic_writePage(0, 13, 0);
- aic_writePage(0, 14, DOSR);
-
-
- aic_writePage(0, 18, 0x80 | NADC);
- aic_writePage(0, 19, 0x80 | MADC);
- aic_writePage(0, 20, AOSR);
- aic_writePage(0, 30, 0x80 | BCLK_N);
-
-
- aic_writePage(1, 0x01, 8);
- aic_writePage(1, 0x02, 0);
- aic_writePage(1, 0x7B, 1);
- aic_writePage(1, 0x7C, 6);
- aic_writePage(1, 0x01, 10);
- aic_writePage(1, 0x0A, 0);
- aic_writePage(1, 0x47, 0x31);
- aic_writePage(1, 0x7D, 0x53);
-
-
-
- aic_writePage(0, 27, 0x01 | AIC_CLK_DIR | (AIC_BITS == 32 ? 0x30 : 0));
-
- }
-
- unsigned int AudioControlTLV320AIC3206::aic_readPage(uint8_t page, uint8_t reg)
- {
- unsigned int val;
- if (aic_goToPage(page)) {
- Wire.beginTransmission(AIC3206_I2C_ADDR);
- Wire.write(reg);
- unsigned int result = Wire.endTransmission();
- if (result != 0) {
- Serial.print("controlTLV320AIC3206: ERROR: Read Page. Page: ");Serial.print(page);
- Serial.print(" Reg: ");Serial.print(reg);
- Serial.print(". Received Error During Read Page: ");
- Serial.println(result);
- val = 300 + result;
- return val;
- }
- if (Wire.requestFrom(AIC3206_I2C_ADDR, 1) < 1) {
- Serial.print("controlTLV320AIC3206: ERROR: Read Page. Page: ");Serial.print(page);
- Serial.print(" Reg: ");Serial.print(reg);
- Serial.println(". Nothing to return");
- val = 400;
- return val;
- }
- if (Wire.available() >= 1) {
- uint16_t val = Wire.read();
- if (debugToSerial) {
- Serial.print("controlTLV320AIC3206: Read Page. Page: ");Serial.print(page);
- Serial.print(" Reg: ");Serial.print(reg);
- Serial.print(". Received: ");
- Serial.println(val, HEX);
- }
- return val;
- }
- } else {
- Serial.print("controlTLV320AIC3206: INFO: Read Page. Page: ");Serial.print(page);
- Serial.print(" Reg: ");Serial.print(reg);
- Serial.println(". Failed to go to read page. Could not go there.");
- val = 500;
- return val;
- }
- val = 600;
- return val;
- }
-
- bool AudioControlTLV320AIC3206::aic_writeAddress(uint16_t address, uint8_t val) {
- uint8_t reg = (uint8_t) (address & 0xFF);
- uint8_t page = (uint8_t) ((address >> 8) & 0xFF);
-
- return aic_writePage(page, reg, val);
- }
-
- bool AudioControlTLV320AIC3206::aic_writePage(uint8_t page, uint8_t reg, uint8_t val) {
- if (debugToSerial) {
- Serial.print("controlTLV320AIC3206: Write Page. Page: ");Serial.print(page);
- Serial.print(" Reg: ");Serial.print(reg);
- Serial.print(" Val: ");Serial.println(val);
- }
- if (aic_goToPage(page)) {
- Wire.beginTransmission(AIC3206_I2C_ADDR);
- Wire.write(reg);delay(10);
- Wire.write(val);delay(10);
- uint8_t result = Wire.endTransmission();
- if (result == 0) return true;
- else {
- Serial.print("controlTLV320AIC3206: Received Error During writePage(): Error = ");
- Serial.println(result);
- }
- }
- return false;
- }
-
- bool AudioControlTLV320AIC3206::aic_goToPage(byte page) {
- Wire.beginTransmission(AIC3206_I2C_ADDR);
- Wire.write(0x00); delay(10);
- Wire.write(page); delay(10);
- byte result = Wire.endTransmission();
- if (result != 0) {
- Serial.print("controlTLV320AIC3206: Received Error During goToPage(): Error = ");
- Serial.println(result);
- if (result == 2) {
-
-
- } else if (result == 3) {
-
-
- }
- return false;
- }
- return true;
- }
-
- bool AudioControlTLV320AIC3206::updateInputBasedOnMicDetect(int setting) {
-
- int curMicDetVal = readMicDetect();
- if (curMicDetVal != prevMicDetVal) {
- if (curMicDetVal) {
-
- inputSelect(setting);
- } else {
-
- inputSelect(AIC3206_INPUT_IN2);
- }
- }
- prevMicDetVal = curMicDetVal;
- return (bool)curMicDetVal;
- }
- bool AudioControlTLV320AIC3206::enableMicDetect(bool state) {
-
- byte curVal = aic_readPage(0,67);
- byte newVal = curVal;
- if (state) {
-
- newVal = 0b111010111 & newVal;
- newVal = 0b10000000 | curVal;
- aic_writePage(0,67,newVal);
- } else {
-
- newVal = 0b01111111 & newVal;
- aic_writePage(0,67,newVal);
- }
- return state;
- }
- int AudioControlTLV320AIC3206::readMicDetect(void) {
-
- byte curVal = aic_readPage(0,46);
- curVal = (curVal & 0b00010000);
- curVal = (curVal != 0);
- return curVal;
- }
-
- void computeFirstOrderHPCoeff_F32(float cutoff_Hz, float fs_Hz, float *coeff) {
-
-
-
-
-
- const float pi = 3.141592653589793;
- float T = 1.0f/fs_Hz;
- float w = cutoff_Hz * 2.0 * pi;
- float A = 1.0f / (tan( (w*T) / 2.0));
- coeff[0] = A / (1.0 + A);
- coeff[1] = -coeff[0];
- coeff[2] = (1.0 - A) / (1.0 + A);
- coeff[2] = -coeff[2];
- }
- #define CONST_2_31_m1 (2147483647)
- void computeFirstOrderHPCoeff_i32(float cutoff_Hz, float fs_Hz, int32_t *coeff) {
- float coeff_f32[3];
- computeFirstOrderHPCoeff_F32(cutoff_Hz,fs_Hz,coeff_f32);
- for (int i=0; i<3; i++) {
-
- coeff_f32[i] *= (float)CONST_2_31_m1;
-
-
- coeff[i] = (int32_t)coeff_f32[i];
- }
- }
-
- void AudioControlTLV320AIC3206::setHPFonADC(bool enable, float cutoff_Hz, float fs_Hz) {
-
- uint32_t coeff[3];
- if (enable) {
- HP_cutoff_Hz = cutoff_Hz;
- sample_rate_Hz = fs_Hz;
- computeFirstOrderHPCoeff_i32(cutoff_Hz,fs_Hz,(int32_t *)coeff);
-
-
-
-
-
-
-
-
- } else {
-
- HP_cutoff_Hz = cutoff_Hz;
-
-
- coeff[0] = 0x7FFFFFFF; coeff[1] = 0; coeff[2]=0;
- }
-
- setIIRCoeffOnADC(AIC3206_BOTH_CHAN, coeff);
- }
-
-
-
- void AudioControlTLV320AIC3206::setIIRCoeffOnADC(int chan, uint32_t *coeff) {
-
-
- uint32_t prev_state = aic_readPage(0x00,0x51);
- aic_writePage(0x00,0x51,prev_state & (0b00111111));
-
- if (chan == AIC3206_BOTH_CHAN) {
- setIIRCoeffOnADC_Left(coeff);
- setIIRCoeffOnADC_Right(coeff);
- } else if (chan == AIC3206_LEFT_CHAN) {
- setIIRCoeffOnADC_Left(coeff);
- } else {
- setIIRCoeffOnADC_Right(coeff);
- }
-
-
- aic_writePage(0x00,0x51,prev_state);
- }
-
- void AudioControlTLV320AIC3206::setIIRCoeffOnADC_Left(uint32_t *coeff) {
- int page;
- uint32_t c;
-
-
-
-
- page = 8;
- c = coeff[0];
- aic_writePage(page,24,(uint8_t)(c>>24));
- aic_writePage(page,25,(uint8_t)(c>>16));
- aic_writePage(page,26,(uint8_t)(c>>8));
-
-
-
- c = coeff[1];
- aic_writePage(page,28,(uint8_t)(c>>24));
- aic_writePage(page,29,(uint8_t)(c>>16));
- aic_writePage(page,30,(uint8_t)(c>>8));
-
-
- c = coeff[2];
- aic_writePage(page,32,(uint8_t)(c>>24));
- aic_writePage(page,33,(uint8_t)(c>>16));
- aic_writePage(page,34,(uint8_t)(c>>9));
- }
- void AudioControlTLV320AIC3206::setIIRCoeffOnADC_Right(uint32_t *coeff) {
- int page;
- uint32_t c;
-
-
-
-
- page = 9;
- c = coeff[0];
- aic_writePage(page,32,(uint8_t)(c>>24));
- aic_writePage(page,33,(uint8_t)(c>>16));
- aic_writePage(page,34,(uint8_t)(c>>8));
-
-
- c = coeff[1];
- aic_writePage(page,36,(uint8_t)(c>>24));
- aic_writePage(page,37,(uint8_t)(c>>16));
- aic_writePage(page,38,(uint8_t)(c>>8));
-
-
- c = coeff[2];;
- aic_writePage(page,40,(uint8_t)(c>>24));
- aic_writePage(page,41,(uint8_t)(c>>16));
- aic_writePage(page,42,(uint8_t)(c>>8));
-
- }
-
|