You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
10 KiB
345 lines
10 KiB
7 years ago
|
/*
|
||
7 years ago
|
* BAAudioControlWM8731.cpp
|
||
7 years ago
|
*
|
||
|
* Created on: May 22, 2017
|
||
|
* Author: slascos
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.*
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
#include <Wire.h>
|
||
|
#include "BAAudioControlWM8731.h"
|
||
|
|
||
|
namespace BAGuitar {
|
||
|
|
||
7 years ago
|
// use const instead of define for proper scoping
|
||
|
constexpr int WM8731_I2C_ADDR = 0x1A;
|
||
|
|
||
|
// The WM8731 register map
|
||
|
constexpr int WM8731_REG_LLINEIN = 0;
|
||
|
constexpr int WM8731_REG_RLINEIN = 1;
|
||
|
constexpr int WM8731_REG_LHEADOUT = 2;
|
||
|
constexpr int WM8731_REG_RHEADOUT = 3;
|
||
|
constexpr int WM8731_REG_ANALOG =4;
|
||
|
constexpr int WM8731_REG_DIGITAL = 5;
|
||
|
constexpr int WM8731_REG_POWERDOWN = 6;
|
||
|
constexpr int WM8731_REG_INTERFACE = 7;
|
||
|
constexpr int WM8731_REG_SAMPLING = 8;
|
||
|
constexpr int WM8731_REG_ACTIVE = 9;
|
||
|
constexpr int WM8731_REG_RESET = 15;
|
||
|
|
||
|
// Register Masks and Shifts
|
||
|
// Register 0
|
||
|
constexpr int WM8731_LEFT_INPUT_GAIN_ADDR = 0;
|
||
|
constexpr int WM8731_LEFT_INPUT_GAIN_MASK = 0x1F;
|
||
|
constexpr int WM8731_LEFT_INPUT_GAIN_SHIFT = 0;
|
||
|
constexpr int WM8731_LEFT_INPUT_MUTE_ADDR = 0;
|
||
|
constexpr int WM8731_LEFT_INPUT_MUTE_MASK = 0x80;
|
||
|
constexpr int WM8731_LEFT_INPUT_MUTE_SHIFT = 7;
|
||
|
constexpr int WM8731_LINK_LEFT_RIGHT_IN_ADDR = 0;
|
||
|
constexpr int WM8731_LINK_LEFT_RIGHT_IN_MASK = 0x100;
|
||
|
constexpr int WM8731_LINK_LEFT_RIGHT_IN_SHIFT = 8;
|
||
|
// Register 1
|
||
|
constexpr int WM8731_RIGHT_INPUT_GAIN_ADDR = 1;
|
||
|
constexpr int WM8731_RIGHT_INPUT_GAIN_MASK = 0x1F;
|
||
|
constexpr int WM8731_RIGHT_INPUT_GAIN_SHIFT = 0;
|
||
|
constexpr int WM8731_RIGHT_INPUT_MUTE_ADDR = 1;
|
||
|
constexpr int WM8731_RIGHT_INPUT_MUTE_MASK = 0x80;
|
||
|
constexpr int WM8731_RIGHT_INPUT_MUTE_SHIFT = 7;
|
||
|
constexpr int WM8731_LINK_RIGHT_LEFT_IN_ADDR = 1;
|
||
|
constexpr int WM8731_LINK_RIGHT_LEFT_IN_MASK = 0x100;
|
||
|
constexpr int WM8731_LINK_RIGHT_LEFT_IN_SHIFT = 8;
|
||
|
// Register 4
|
||
|
constexpr int WM8731_ADC_BYPASS_ADDR = 4;
|
||
|
constexpr int WM8731_ADC_BYPASS_MASK = 0x8;
|
||
|
constexpr int WM8731_ADC_BYPASS_SHIFT = 3;
|
||
|
constexpr int WM8731_DAC_SELECT_ADDR = 4;
|
||
|
constexpr int WM8731_DAC_SELECT_MASK = 0x10;
|
||
|
constexpr int WM8731_DAC_SELECT_SHIFT = 4;
|
||
|
// Register 5
|
||
|
constexpr int WM8731_DAC_MUTE_ADDR = 5;
|
||
|
constexpr int WM8731_DAC_MUTE_MASK = 0x8;
|
||
|
constexpr int WM8731_DAC_MUTE_SHIFT = 3;
|
||
|
constexpr int WM8731_HPF_DISABLE_ADDR = 5;
|
||
|
constexpr int WM8731_HPF_DISABLE_MASK = 0x1;
|
||
|
constexpr int WM8731_HPF_DISABLE_SHIFT = 0;
|
||
7 years ago
|
// Register 7
|
||
|
constexpr int WM8731_LRSWAP_ADDR = 5;
|
||
|
constexpr int WM8731_LRSWAP_MASK = 0x20;
|
||
|
constexpr int WM8731_LRSWAPE_SHIFT = 5;
|
||
7 years ago
|
|
||
|
// Register 9
|
||
|
constexpr int WM8731_ACTIVATE_ADDR = 9;
|
||
|
constexpr int WM8731_ACTIVATE_MASK = 0x1;
|
||
|
|
||
|
|
||
|
// Reset the internal shadow register array to match
|
||
|
// the reset state of the codec.
|
||
7 years ago
|
void BAAudioControlWM8731::resetInternalReg(void) {
|
||
|
// Set to reset state
|
||
|
regArray[0] = 0x97;
|
||
|
regArray[1] = 0x97;
|
||
|
regArray[2] = 0x79;
|
||
|
regArray[3] = 0x79;
|
||
|
regArray[4] = 0x0a;
|
||
|
regArray[5] = 0x8;
|
||
|
regArray[6] = 0x9f;
|
||
|
regArray[7] = 0xa;
|
||
|
regArray[8] = 0;
|
||
|
regArray[9] = 0;
|
||
|
}
|
||
|
|
||
|
BAAudioControlWM8731::BAAudioControlWM8731()
|
||
|
{
|
||
|
resetInternalReg();
|
||
|
}
|
||
|
|
||
|
BAAudioControlWM8731::~BAAudioControlWM8731()
|
||
|
{
|
||
|
}
|
||
|
|
||
7 years ago
|
// Powerdown and disable the codec
|
||
7 years ago
|
void BAAudioControlWM8731::disable(void)
|
||
|
{
|
||
|
|
||
7 years ago
|
//Serial.println("Disabling codec");
|
||
7 years ago
|
if (m_wireStarted == false) { Wire.begin(); m_wireStarted = true; }
|
||
|
|
||
7 years ago
|
// set OUTPD to '1' (powerdown), which is bit 4
|
||
|
regArray[WM8731_REG_POWERDOWN] |= 0x10;
|
||
|
write(WM8731_REG_POWERDOWN, regArray[WM8731_REG_POWERDOWN]);
|
||
|
delay(100); // wait for power down
|
||
|
|
||
|
// power down the rest of the supplies
|
||
|
write(WM8731_REG_POWERDOWN, 0x9f); // complete codec powerdown
|
||
|
delay(100);
|
||
|
|
||
|
resetCodec();
|
||
|
}
|
||
|
|
||
7 years ago
|
// Powerup and unmute the codec
|
||
|
void BAAudioControlWM8731::enable(void)
|
||
7 years ago
|
{
|
||
|
|
||
7 years ago
|
disable(); // disable first in case it was already powered up
|
||
|
|
||
7 years ago
|
//Serial.println("Enabling codec");
|
||
7 years ago
|
if (m_wireStarted == false) { Wire.begin(); m_wireStarted = true; }
|
||
7 years ago
|
// Sequence from WAN0111.pdf
|
||
|
|
||
|
// Begin configuring the codec
|
||
|
resetCodec();
|
||
|
delay(100); // wait for reset
|
||
|
|
||
|
// Power up all domains except OUTPD and microphone
|
||
|
regArray[WM8731_REG_POWERDOWN] = 0x12;
|
||
|
write(WM8731_REG_POWERDOWN, regArray[WM8731_REG_POWERDOWN]);
|
||
|
delay(100); // wait for codec powerup
|
||
|
|
||
|
|
||
|
setAdcBypass(false); // causes a slight click
|
||
|
setDacSelect(true);
|
||
|
setHPFDisable(true);
|
||
|
setLeftInputGain(0x17); // default input gain
|
||
|
setRightInputGain(0x17);
|
||
|
setLeftInMute(false); // no input mute
|
||
|
setRightInMute(false);
|
||
|
setDacMute(false); // unmute the DAC
|
||
|
|
||
|
// mute the headphone outputs
|
||
|
write(WM8731_REG_LHEADOUT, 0x00); // volume off
|
||
|
regArray[WM8731_REG_LHEADOUT] = 0x00;
|
||
|
write(WM8731_REG_RHEADOUT, 0x00);
|
||
|
regArray[WM8731_REG_RHEADOUT] = 0x00;
|
||
|
|
||
|
/// Configure the audio interface
|
||
|
write(WM8731_REG_INTERFACE, 0x02); // I2S, 16 bit, MCLK slave
|
||
|
regArray[WM8731_REG_INTERFACE] = 0x2;
|
||
|
|
||
|
write(WM8731_REG_SAMPLING, 0x20); // 256*Fs, 44.1 kHz, MCLK/1
|
||
|
regArray[WM8731_REG_SAMPLING] = 0x20;
|
||
|
delay(100); // wait for interface config
|
||
|
|
||
|
// Activate the audio interface
|
||
|
setActivate(true);
|
||
|
delay(100);
|
||
|
|
||
|
write(WM8731_REG_POWERDOWN, 0x02); // power up outputs
|
||
|
regArray[WM8731_REG_POWERDOWN] = 0x02;
|
||
|
delay(500); // wait for output to power up
|
||
|
|
||
7 years ago
|
//Serial.println("Done codec config");
|
||
7 years ago
|
|
||
7 years ago
|
|
||
|
delay(100); // wait for mute ramp
|
||
|
|
||
|
}
|
||
|
|
||
7 years ago
|
// Set the PGA gain on the Left channel
|
||
7 years ago
|
void BAAudioControlWM8731::setLeftInputGain(int val)
|
||
|
{
|
||
|
regArray[WM8731_LEFT_INPUT_GAIN_ADDR] &= ~WM8731_LEFT_INPUT_GAIN_MASK;
|
||
|
regArray[WM8731_LEFT_INPUT_GAIN_ADDR] |=
|
||
|
((val << WM8731_LEFT_INPUT_GAIN_SHIFT) & WM8731_LEFT_INPUT_GAIN_MASK);
|
||
|
write(WM8731_LEFT_INPUT_GAIN_ADDR, regArray[WM8731_LEFT_INPUT_GAIN_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Mute control on the ADC Left channel
|
||
7 years ago
|
void BAAudioControlWM8731::setLeftInMute(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_LEFT_INPUT_MUTE_ADDR] |= WM8731_LEFT_INPUT_MUTE_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_LEFT_INPUT_MUTE_ADDR] &= ~WM8731_LEFT_INPUT_MUTE_MASK;
|
||
|
}
|
||
|
write(WM8731_LEFT_INPUT_MUTE_ADDR, regArray[WM8731_LEFT_INPUT_MUTE_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Link the gain/mute controls for Left and Right channels
|
||
7 years ago
|
void BAAudioControlWM8731::setLinkLeftRightIn(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_LINK_LEFT_RIGHT_IN_ADDR] |= WM8731_LINK_LEFT_RIGHT_IN_MASK;
|
||
|
regArray[WM8731_LINK_RIGHT_LEFT_IN_ADDR] |= WM8731_LINK_RIGHT_LEFT_IN_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_LINK_LEFT_RIGHT_IN_ADDR] &= ~WM8731_LINK_LEFT_RIGHT_IN_MASK;
|
||
|
regArray[WM8731_LINK_RIGHT_LEFT_IN_ADDR] &= ~WM8731_LINK_RIGHT_LEFT_IN_MASK;
|
||
|
}
|
||
|
write(WM8731_LINK_LEFT_RIGHT_IN_ADDR, regArray[WM8731_LINK_LEFT_RIGHT_IN_ADDR]);
|
||
|
write(WM8731_LINK_RIGHT_LEFT_IN_ADDR, regArray[WM8731_LINK_RIGHT_LEFT_IN_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Set the PGA input gain on the Right channel
|
||
7 years ago
|
void BAAudioControlWM8731::setRightInputGain(int val)
|
||
|
{
|
||
|
regArray[WM8731_RIGHT_INPUT_GAIN_ADDR] &= ~WM8731_RIGHT_INPUT_GAIN_MASK;
|
||
|
regArray[WM8731_RIGHT_INPUT_GAIN_ADDR] |=
|
||
|
((val << WM8731_RIGHT_INPUT_GAIN_SHIFT) & WM8731_RIGHT_INPUT_GAIN_MASK);
|
||
|
write(WM8731_RIGHT_INPUT_GAIN_ADDR, regArray[WM8731_RIGHT_INPUT_GAIN_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Mute control on the input ADC right channel
|
||
7 years ago
|
void BAAudioControlWM8731::setRightInMute(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_RIGHT_INPUT_MUTE_ADDR] |= WM8731_RIGHT_INPUT_MUTE_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_RIGHT_INPUT_MUTE_ADDR] &= ~WM8731_RIGHT_INPUT_MUTE_MASK;
|
||
|
}
|
||
|
write(WM8731_RIGHT_INPUT_MUTE_ADDR, regArray[WM8731_RIGHT_INPUT_MUTE_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Left/right swap control
|
||
|
void BAAudioControlWM8731::setLeftRightSwap(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_LRSWAP_ADDR] |= WM8731_LRSWAP_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_LRSWAP_ADDR] &= ~WM8731_LRSWAP_MASK;
|
||
|
}
|
||
|
write(WM8731_LRSWAP_ADDR, regArray[WM8731_LRSWAP_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Dac output mute control
|
||
7 years ago
|
void BAAudioControlWM8731::setDacMute(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_DAC_MUTE_ADDR] |= WM8731_DAC_MUTE_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_DAC_MUTE_ADDR] &= ~WM8731_DAC_MUTE_MASK;
|
||
|
}
|
||
|
write(WM8731_DAC_MUTE_ADDR, regArray[WM8731_DAC_MUTE_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Switches the DAC audio in/out of the output path
|
||
7 years ago
|
void BAAudioControlWM8731::setDacSelect(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_DAC_SELECT_ADDR] |= WM8731_DAC_SELECT_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_DAC_SELECT_ADDR] &= ~WM8731_DAC_SELECT_MASK;
|
||
|
}
|
||
|
write(WM8731_DAC_SELECT_ADDR, regArray[WM8731_DAC_SELECT_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Bypass sends the ADC input audio (analog) directly to analog output stage
|
||
|
// bypassing all digital processing
|
||
7 years ago
|
void BAAudioControlWM8731::setAdcBypass(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_ADC_BYPASS_ADDR] |= WM8731_ADC_BYPASS_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_ADC_BYPASS_ADDR] &= ~WM8731_ADC_BYPASS_MASK;
|
||
|
}
|
||
|
write(WM8731_ADC_BYPASS_ADDR, regArray[WM8731_ADC_BYPASS_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Enable/disable the dynamic HPF (recommended, it creates noise)
|
||
7 years ago
|
void BAAudioControlWM8731::setHPFDisable(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
regArray[WM8731_HPF_DISABLE_ADDR] |= WM8731_HPF_DISABLE_MASK;
|
||
|
} else {
|
||
|
regArray[WM8731_HPF_DISABLE_ADDR] &= ~WM8731_HPF_DISABLE_MASK;
|
||
|
}
|
||
|
write(WM8731_HPF_DISABLE_ADDR, regArray[WM8731_HPF_DISABLE_ADDR]);
|
||
|
}
|
||
|
|
||
7 years ago
|
// Activate/deactive the I2S audio interface
|
||
7 years ago
|
void BAAudioControlWM8731::setActivate(bool val)
|
||
|
{
|
||
|
if (val) {
|
||
|
write(WM8731_ACTIVATE_ADDR, WM8731_ACTIVATE_MASK);
|
||
|
} else {
|
||
|
write(WM8731_ACTIVATE_ADDR, 0);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
7 years ago
|
// Trigger the on-chip codec reset
|
||
7 years ago
|
void BAAudioControlWM8731::resetCodec(void)
|
||
|
{
|
||
|
write(WM8731_REG_RESET, 0x0);
|
||
|
resetInternalReg();
|
||
|
}
|
||
|
|
||
7 years ago
|
// Direct write control to the codec
|
||
7 years ago
|
bool BAAudioControlWM8731::writeI2C(unsigned int addr, unsigned int val)
|
||
7 years ago
|
{
|
||
7 years ago
|
return write(addr, val);
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// Low level write control for the codec via the Teensy I2C interface
|
||
7 years ago
|
bool BAAudioControlWM8731::write(unsigned int reg, unsigned int val)
|
||
|
{
|
||
7 years ago
|
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() ) {
|
||
7 years ago
|
(void)error; // supress warning about unused variable
|
||
7 years ago
|
//Serial.println(String("Wire::Error: ") + error + String(" retrying..."));
|
||
|
} else {
|
||
|
done = true;
|
||
|
//Serial.println("Wire::SUCCESS!");
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
} /* namespace BAGuitar */
|