diff --git a/src/BAAudioControlWM8731.cpp b/src/BAAudioControlWM8731.cpp index 6c9b964..c394622 100644 --- a/src/BAAudioControlWM8731.cpp +++ b/src/BAAudioControlWM8731.cpp @@ -103,7 +103,6 @@ void BAAudioControlWM8731::resetInternalReg(void) { BAAudioControlWM8731::BAAudioControlWM8731() { resetInternalReg(); - Wire.begin(); // start the I2C service } BAAudioControlWM8731::~BAAudioControlWM8731() @@ -114,6 +113,9 @@ BAAudioControlWM8731::~BAAudioControlWM8731() void BAAudioControlWM8731::disable(void) { + Serial.println("Disabling codec"); + if (m_wireStarted == false) { Wire.begin(); m_wireStarted = true; } + // set OUTPD to '1' (powerdown), which is bit 4 regArray[WM8731_REG_POWERDOWN] |= 0x10; write(WM8731_REG_POWERDOWN, regArray[WM8731_REG_POWERDOWN]); @@ -130,6 +132,8 @@ void BAAudioControlWM8731::disable(void) void BAAudioControlWM8731::enable(void) { + Serial.println("Enabling codec"); + if (m_wireStarted == false) { Wire.begin(); m_wireStarted = true; } // Sequence from WAN0111.pdf // Begin configuring the codec @@ -173,6 +177,8 @@ void BAAudioControlWM8731::enable(void) regArray[WM8731_REG_POWERDOWN] = 0x02; delay(500); // wait for output to power up + Serial.println("Done codec config"); + delay(100); // wait for mute ramp @@ -315,10 +321,19 @@ void BAAudioControlWM8731::writeI2C(unsigned int addr, unsigned int val) // Low level write control for the codec via the Teensy I2C interface bool BAAudioControlWM8731::write(unsigned int reg, unsigned int val) { - Wire.beginTransmission(WM8731_I2C_ADDR); - Wire.write((reg << 1) | ((val >> 8) & 1)); - Wire.write(val & 0xFF); - Wire.endTransmission(); + bool done = false; + + while (!done) { + Wire.beginTransmission(WM8731_I2C_ADDR); + Wire.write((reg << 1) | ((val >> 8) & 1)); + Wire.write(val & 0xFF); + if (byte error = Wire.endTransmission() ) { + //Serial.println(String("Wire::Error: ") + error + String(" retrying...")); + } else { + done = true; + //Serial.println("Wire::SUCCESS!"); + } + } return true; } diff --git a/src/BAAudioControlWM8731.h b/src/BAAudioControlWM8731.h index 538f557..b01c5a0 100644 --- a/src/BAAudioControlWM8731.h +++ b/src/BAAudioControlWM8731.h @@ -24,8 +24,6 @@ #ifndef INC_BAAUDIOCONTROLWM8731_H_ #define INC_BAAUDIOCONTROLWM8731_H_ -#include - namespace BAGuitar { constexpr int WM8731_NUM_REGS = 10; // Number of registers in the internal shadow array @@ -123,6 +121,8 @@ private: // resets the internal shadow register array void resetInternalReg(void); + bool m_wireStarted = false; + }; } /* namespace BAGuitar */