From ef52d50b7ca104a7e417a136595f2771e48e7b0c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 2 Jun 2025 01:31:56 +0200 Subject: [PATCH] structuring code --- soundcube-i2s-test/codec-registers.h | 110 +++++++++++++++++++++++++++ soundcube-i2s-test/codec.h | 106 -------------------------- 2 files changed, 110 insertions(+), 106 deletions(-) create mode 100644 soundcube-i2s-test/codec-registers.h diff --git a/soundcube-i2s-test/codec-registers.h b/soundcube-i2s-test/codec-registers.h new file mode 100644 index 0000000..5a6fcdf --- /dev/null +++ b/soundcube-i2s-test/codec-registers.h @@ -0,0 +1,110 @@ +#pragma once + +#ifndef DEBUG +#define DEBUG false +#endif + +struct CodecSettings{ + CodecSettings(uint8_t i2c_address, TwoWire *wire) : wire(wire), i2cAddress(i2c_address) {} + TwoWire *wire; + + uint8_t i2cAddress = 0x18; + uint8_t page; + uint8_t reg; + uint8_t len; + + virtual uint8_t get() = 0; + + void write(){ + selectPage(page); + cw(reg, get()); + } + + void read(uint8_t result[]){ + selectPage(page); + cr(reg, result, len); + } + + void selectPage(int page){ + cw(0x00, page); + } + + void cw(unsigned char first, unsigned char second){ + Wire1.beginTransmission(i2cAddress); + Wire1.write(first); + Wire1.write(second); + int result = Wire1.endTransmission(); + if(DEBUG){ + Serial.print(i2cAddress, HEX); + Serial.print(" "); + Serial.print(first, HEX); + Serial.print(" "); + Serial.print(second, HEX); + Serial.print(" : "); + if(result == 0) { + Serial.println("OK"); + } else { + Serial.print("ERROR: "); + Serial.println(result); + } + } + delay(5); + } + + void cr(unsigned char first, uint8_t result[], size_t len){ + Wire1.beginTransmission(i2cAddress); + Wire1.write(first); // set register for read + Wire1.endTransmission(false); // false to not release the line + + Wire1.requestFrom(i2cAddress, len, true); + + Wire1.readBytes(result, len); + + if(DEBUG){ + Serial.print(first, HEX); + Serial.print(" "); + + for (int i = 0; i < len; i++) { + Serial.print(result[i], HEX); + Serial.print(" "); + Serial.println(result[i], BIN); + } + } + } +}; + +struct ClockSettings1 : public CodecSettings{ + ClockSettings1(uint8_t i2c_address, TwoWire *wire) : CodecSettings(i2c_address, wire) {} + + uint8_t page = 0x00; + uint8_t reg = 0x04; + uint8_t len = 1; + + enum PLLRange{ + PLL_HIGH = 0b01000000, + PLL_LOW = 0b00000000 + }; + + enum PLLInputClock{ + PLL_IN_MCLK = 0b00000000, + PLL_IN_BCLK = 0b00000100, + PLL_IN_GPIO = 0b00001000, + PLL_IN_DIN = 0b00001100 + }; + + enum CodecInputClock{ + CODEC_IN_MCLK = 0b00000000, + CODEC_IN_BCLK = 0b00000001, + CODEC_IN_GPIO = 0b00000010, + CODEC_IN_PLL = 0b00000011 + }; + + PLLRange pll_range = PLL_LOW; + PLLInputClock pll_input_clock = PLL_IN_MCLK; + CodecInputClock codec_input_clock = CODEC_IN_MCLK; + + uint8_t get(){ + return pll_range | pll_input_clock | codec_input_clock; + } +}; + diff --git a/soundcube-i2s-test/codec.h b/soundcube-i2s-test/codec.h index 1518f67..c670d10 100644 --- a/soundcube-i2s-test/codec.h +++ b/soundcube-i2s-test/codec.h @@ -2,113 +2,7 @@ #include #include -#ifndef DEBUG -#define DEBUG false -#endif -struct CodecSettings{ - CodecSettings(uint8_t i2c_address, TwoWire *wire) : wire(wire), i2cAddress(i2c_address) {} - TwoWire *wire; - - uint8_t i2cAddress = 0x18; - uint8_t page; - uint8_t reg; - uint8_t len; - - virtual uint8_t get() = 0; - - void write(){ - selectPage(page); - cw(reg, get()); - } - - void read(uint8_t result[]){ - selectPage(page); - cr(reg, result, len); - } - - void selectPage(int page){ - cw(0x00, page); - } - - void cw(unsigned char first, unsigned char second){ - Wire1.beginTransmission(i2cAddress); - Wire1.write(first); - Wire1.write(second); - int result = Wire1.endTransmission(); - if(DEBUG){ - Serial.print(i2cAddress, HEX); - Serial.print(" "); - Serial.print(first, HEX); - Serial.print(" "); - Serial.print(second, HEX); - Serial.print(" : "); - if(result == 0) { - Serial.println("OK"); - } else { - Serial.print("ERROR: "); - Serial.println(result); - } - } - delay(5); - } - - void cr(unsigned char first, uint8_t result[], size_t len){ - Wire1.beginTransmission(i2cAddress); - Wire1.write(first); // set register for read - Wire1.endTransmission(false); // false to not release the line - - Wire1.requestFrom(i2cAddress, len, true); - - Wire1.readBytes(result, len); - - if(DEBUG){ - Serial.print(first, HEX); - Serial.print(" "); - - for (int i = 0; i < len; i++) { - Serial.print(result[i], HEX); - Serial.print(" "); - Serial.println(result[i], BIN); - } - } - } -}; - -struct ClockSettings1 : public CodecSettings{ - ClockSettings1(uint8_t i2c_address, TwoWire *wire) : CodecSettings(i2c_address, wire) {} - - uint8_t page = 0x00; - uint8_t reg = 0x04; - uint8_t len = 1; - - enum PLLRange{ - PLL_HIGH = 0b01000000, - PLL_LOW = 0b00000000 - }; - - enum PLLInputClock{ - PLL_IN_MCLK = 0b00000000, - PLL_IN_BCLK = 0b00000100, - PLL_IN_GPIO = 0b00001000, - PLL_IN_DIN = 0b00001100 - }; - - enum CodecInputClock{ - CODEC_IN_MCLK = 0b00000000, - CODEC_IN_BCLK = 0b00000001, - CODEC_IN_GPIO = 0b00000010, - CODEC_IN_PLL = 0b00000011 - }; - - PLLRange pll_range = PLL_LOW; - PLLInputClock pll_input_clock = PLL_IN_MCLK; - CodecInputClock codec_input_clock = CODEC_IN_MCLK; - - uint8_t get(){ - return pll_range | pll_input_clock | codec_input_clock; - } -}; class TLV320AIC3204_Settings{