arduino-audio-driver
Loading...
Searching...
No Matches
WM8904 Class Reference

Header only C++ driver for the WM8904 audio codec. More...

#include <WM8904.h>

Inheritance diagram for WM8904:
ZephyrDriverCommon

Public Types

enum  Reg : uint8_t {
  REG_RESET = 0x00 , REG_ANALOG_ADC_0 = 0x0A , REG_POWER_MGMT_0 = 0x0C , REG_POWER_MGMT_2 = 0x0E ,
  REG_POWER_MGMT_3 = 0x0F , REG_POWER_MGMT_6 = 0x12 , REG_CLK_RATES_0 = 0x14 , REG_CLK_RATES_1 = 0x15 ,
  REG_CLK_RATES_2 = 0x16 , REG_AUDIO_IF_0 = 0x18 , REG_AUDIO_IF_1 = 0x19 , REG_AUDIO_IF_2 = 0x1A ,
  REG_AUDIO_IF_3 = 0x1B , REG_DAC_DIGITAL_VOLUME_LEFT = 0x1E , REG_DAC_DIGITAL_VOLUME_RIGHT = 0x1F , REG_DAC_DIG_1 = 0x21 ,
  REG_ADC_DIGITAL_VOLUME_LEFT = 0x24 , REG_ADC_DIGITAL_VOLUME_RIGHT = 0x25 , REG_DAC_DIG_0 = 0x27 , REG_ANALOG_LEFT_IN_0 = 0x2C ,
  REG_ANALOG_RIGHT_IN_0 = 0x2D , REG_ANALOG_LEFT_IN_1 = 0x2E , REG_ANALOG_RIGHT_IN_1 = 0x2F , REG_ANALOG_OUT1_LEFT = 0x39 ,
  REG_ANALOG_OUT1_RIGHT = 0x3A , REG_ANALOG_OUT2_LEFT = 0x3B , REG_ANALOG_OUT2_RIGHT = 0x3C , REG_ANALOG_OUT12_ZC = 0x3D ,
  REG_DC_SERVO_0 = 0x43 , REG_ANALOG_HP_0 = 0x5A , REG_CHRG_PUMP_0 = 0x62 , REG_CLS_W_0 = 0x68 ,
  REG_WRT_SEQUENCER_0 = 0x6C , REG_WRT_SEQUENCER_3 = 0x6F , REG_WRT_SEQUENCER_4 = 0x70 , REG_FLL_CONTROL_1 = 0x74 ,
  REG_FLL_CONTROL_2 = 0x75 , REG_FLL_CONTROL_3 = 0x76 , REG_FLL_CONTROL_4 = 0x77 , REG_FLL_CONTROL_5 = 0x78 ,
  REG_GPIO_CONTROL_1 = 0x79 , REG_GPIO_CONTROL_2 = 0x7A , REG_GPIO_CONTROL_3 = 0x7B , REG_GPIO_CONTROL_4 = 0x7C ,
  REG_FLL_NCO_TEST_0 = 0xF7 , REG_FLL_NCO_TEST_1 = 0xF8
}

Public Member Functions

 WM8904 ()
uint8_t address ()
 Provides the actual I2C address of the codec.
bool applyProperties ()
 Apply the pending volume changes (set VU bit for both output channel pairs).
bool begin (uint32_t sample_rate, uint8_t bits)
 Initialize the codec: soft reset, power up the analog/digital blocks, configure the digital audio interface protocol, sample rate / word size and (optionally) enable the playback and/or capture path.
bool begin (uint32_t sample_rate, uint8_t bits, codec_mode_t mode, i2s_format_t fmt, bool is_master, uint8_t channels) override
bool begin (WM8904Protocol protocol=WM8904Protocol::I2S, uint32_t sample_rate=44100, uint8_t word_size=16, bool playback=true, bool capture=true)
bool configureInput ()
 Configure the default input path: route input 2, default volume, unmuted.
bool configureOutput ()
virtual int getInputVolume ()
virtual int getVolume ()
 Provides the output volume in % (0...100) that was set with setVolume().
i2c_bus_handle_t getWire ()
 Provides the actual I2C communication object.
bool isInputVolumeSupported () override
 Returns true: the input volume is supported.
bool routeInput (uint32_t input, WM8904Channel channel=WM8904Channel::All)
 Route an analog input (1..3) to the given channel's PGA. Only FrontLeft, FrontRight and All are valid channels.
bool setActive (codec_mode_t mode) override
void setAddress (uint8_t addr)
 Defines the I2C address of the codec.
bool setAudioFormat (uint32_t sample_rate, uint8_t word_size=16)
 Configure the sample rate and word size of the digital audio interface. The SYSCLK / fs ratio is determined from the current REG_CLK_RATES_0 setting (assumes MCLK_DIV = 1, i.e. SYSCLK = MCLK / 2, matching the divider written in begin()).
bool setDevices (input_device_t input_device, output_device_t output_device) override
 Stores the output device selection for use by configureOutput().
bool setInputMute (bool mute, WM8904Channel channel=WM8904Channel::All)
 Mute / unmute the input for the given channel(s).
bool setInputVolume (int volume) override
 Sets the input volume in % (0...100) for all channels.
bool setInputVolume (uint8_t volume, WM8904Channel channel=WM8904Channel::All)
 Set the input volume (0..31) for the given channel(s).
bool setMute (bool mute) override
 Mutes/unmutes all outputs.
bool setOutputMute (bool mute, WM8904Channel channel=WM8904Channel::All)
 Mute / unmute the output for the given channel(s).
bool setOutputVolume (uint8_t volume, WM8904Channel channel=WM8904Channel::All)
 Set the output volume (0..63) for the given channel(s).
bool setProtocol (WM8904Protocol protocol)
 Configure the digital audio interface protocol (I2S, LJ, RJ, PCMA/B).
bool setVolume (int volume) override
 Sets the output volume in % (0...100) for all channels.
void setWire (i2c_bus_handle_t w)
 Defines the I2C communication object.
bool softReset ()
 Soft reset of the codec (writes 0x00 to REG_RESET).

Static Public Attributes

static constexpr uint16_t INPUT_VOLUME_DEFAULT = 0b00101
static constexpr uint16_t INPUT_VOLUME_MAX = 0b11111
static constexpr uint16_t INPUT_VOLUME_MIN = 0b00000
static constexpr uint16_t OUTPUT_VOLUME_DEFAULT = 0b101101
static constexpr uint16_t OUTPUT_VOLUME_MAX = 0b111111
static constexpr uint16_t OUTPUT_VOLUME_MIN = 0b000000
static constexpr uint8_t REGMASK_IN_MUTE = 0b10000000
static constexpr uint8_t REGMASK_IN_VOLUME = 0b00011111
static constexpr uint8_t REGMASK_INSEL_CMENA = 0b01000000
static constexpr uint8_t REGMASK_INSEL_IP_SEL_N = 0b00110000
static constexpr uint8_t REGMASK_INSEL_IP_SEL_P = 0b00001100
static constexpr uint8_t REGMASK_INSEL_MODE = 0b00000011
static constexpr uint16_t REGMASK_OUT_MUTE = 0b100000000
static constexpr uint16_t REGMASK_OUT_VOL = 0b000111111
static constexpr uint16_t REGMASK_OUT_VU = 0b010000000
static constexpr uint16_t REGMASK_OUT_ZC = 0b001000000

Protected Member Functions

bool readReg (uint8_t reg, uint8_t &value, bool stopBit=true)
 Reads a single byte from an 8 bit register address.
bool readReg16 (uint8_t reg, uint16_t &value)
 Reads a 16 bit (big endian) value from an 8 bit register address.
bool updateInput (WM8904Channel channel, uint8_t val, uint8_t mask)
bool updateOutput (WM8904Channel channel, uint16_t val, uint16_t mask)
bool updateReg (uint8_t reg, uint8_t mask, uint8_t value)
 Read-Modify-Write of a single byte register.
bool updateReg16 (uint8_t reg, uint16_t mask, uint16_t value)
 Read-Modify-Write of a 16 bit (big endian) register.
bool writeReg (uint8_t reg, uint8_t value)
 Writes a single byte to an 8 bit register address.
bool writeReg16 (uint8_t reg, uint16_t value)
 Writes a 16 bit (big endian) value to an 8 bit register address.

Protected Attributes

uint8_t i2c_addr = 0
int input_volume_percent = 100
 Last input volume (in %) provided to setInputVolume().
output_device_t output_device = DAC_OUTPUT_ALL
 Output device selection set via setDevices(), used by configureOutput().
int volume_percent = 100
 Last volume (in %) provided to setVolume().
i2c_bus_handle_t wire = nullptr

Detailed Description

Header only C++ driver for the WM8904 audio codec.

Provides a soft reset, basic playback/capture configuration, input routing and output/input volume & mute control, ported from the corresponding Zephyr RTOS driver.

Member Enumeration Documentation

◆ Reg

enum Reg : uint8_t
Enumerator
REG_RESET 
REG_ANALOG_ADC_0 
REG_POWER_MGMT_0 
REG_POWER_MGMT_2 
REG_POWER_MGMT_3 
REG_POWER_MGMT_6 
REG_CLK_RATES_0 
REG_CLK_RATES_1 
REG_CLK_RATES_2 
REG_AUDIO_IF_0 
REG_AUDIO_IF_1 
REG_AUDIO_IF_2 
REG_AUDIO_IF_3 
REG_DAC_DIGITAL_VOLUME_LEFT 
REG_DAC_DIGITAL_VOLUME_RIGHT 
REG_DAC_DIG_1 
REG_ADC_DIGITAL_VOLUME_LEFT 
REG_ADC_DIGITAL_VOLUME_RIGHT 
REG_DAC_DIG_0 
REG_ANALOG_LEFT_IN_0 
REG_ANALOG_RIGHT_IN_0 
REG_ANALOG_LEFT_IN_1 
REG_ANALOG_RIGHT_IN_1 
REG_ANALOG_OUT1_LEFT 
REG_ANALOG_OUT1_RIGHT 
REG_ANALOG_OUT2_LEFT 
REG_ANALOG_OUT2_RIGHT 
REG_ANALOG_OUT12_ZC 
REG_DC_SERVO_0 
REG_ANALOG_HP_0 
REG_CHRG_PUMP_0 
REG_CLS_W_0 
REG_WRT_SEQUENCER_0 
REG_WRT_SEQUENCER_3 
REG_WRT_SEQUENCER_4 
REG_FLL_CONTROL_1 
REG_FLL_CONTROL_2 
REG_FLL_CONTROL_3 
REG_FLL_CONTROL_4 
REG_FLL_CONTROL_5 
REG_GPIO_CONTROL_1 
REG_GPIO_CONTROL_2 
REG_GPIO_CONTROL_3 
REG_GPIO_CONTROL_4 
REG_FLL_NCO_TEST_0 
REG_FLL_NCO_TEST_1 

Constructor & Destructor Documentation

◆ WM8904()

WM8904 ( )
inline

Member Function Documentation

◆ address()

uint8_t address ( )
inlineinherited

Provides the actual I2C address of the codec.

◆ applyProperties()

bool applyProperties ( )
inline

Apply the pending volume changes (set VU bit for both output channel pairs).

◆ begin() [1/3]

bool begin ( uint32_t sample_rate,
uint8_t bits )
inline

Initialize the codec: soft reset, power up the analog/digital blocks, configure the digital audio interface protocol, sample rate / word size and (optionally) enable the playback and/or capture path.

Parameters
protocolDigital audio interface protocol
sample_rateI2S sample rate in Hz (e.g. 44100, 48000)
word_sizeI2S word size in bits (16, 20, 24, 32)
playbackEnable & configure the output (headphone/line) path
captureEnable & configure the input path Initializes the codec for I2S with the given sample rate and bits per sample

◆ begin() [2/3]

bool begin ( uint32_t sample_rate,
uint8_t bits,
codec_mode_t mode,
i2s_format_t fmt,
bool is_master,
uint8_t channels )
inlineoverridevirtual

Initializes the codec for I2S with the given sample rate, bits per sample, enabling the playback and/or capture path depending on mode and using the given I2S data format. The is_master and channels parameters are not configurable on this chip and are ignored.

Implements ZephyrDriverCommon.

◆ begin() [3/3]

bool begin ( WM8904Protocol protocol = WM8904Protocol::I2S,
uint32_t sample_rate = 44100,
uint8_t word_size = 16,
bool playback = true,
bool capture = true )
inline

◆ configureInput()

bool configureInput ( )
inline

Configure the default input path: route input 2, default volume, unmuted.

◆ configureOutput()

bool configureOutput ( )
inline

Configure the default output path: default volume, headphone (LINE1) and/or line/speaker (LINE2) output unmuted depending on the output device selected via setDevices()

◆ getInputVolume()

virtual int getInputVolume ( )
inlinevirtualinherited

Provides the input volume in % (0...100) that was set with setInputVolume()

◆ getVolume()

virtual int getVolume ( )
inlinevirtualinherited

Provides the output volume in % (0...100) that was set with setVolume().

◆ getWire()

i2c_bus_handle_t getWire ( )
inlineinherited

Provides the actual I2C communication object.

◆ isInputVolumeSupported()

bool isInputVolumeSupported ( )
inlineoverridevirtual

Returns true: the input volume is supported.

Reimplemented from ZephyrDriverCommon.

◆ readReg()

bool readReg ( uint8_t reg,
uint8_t & value,
bool stopBit = true )
inlineprotectedinherited

Reads a single byte from an 8 bit register address.

◆ readReg16()

bool readReg16 ( uint8_t reg,
uint16_t & value )
inlineprotectedinherited

Reads a 16 bit (big endian) value from an 8 bit register address.

◆ routeInput()

bool routeInput ( uint32_t input,
WM8904Channel channel = WM8904Channel::All )
inline

Route an analog input (1..3) to the given channel's PGA. Only FrontLeft, FrontRight and All are valid channels.

Parameters
inputinput number 1..3

◆ setActive()

bool setActive ( codec_mode_t mode)
inlineoverridevirtual

Activates/deactivates the playback and/or capture path depending on codec_mode_t by muting/unmuting the output and input independently

Reimplemented from ZephyrDriverCommon.

◆ setAddress()

void setAddress ( uint8_t addr)
inlineinherited

Defines the I2C address of the codec.

◆ setAudioFormat()

bool setAudioFormat ( uint32_t sample_rate,
uint8_t word_size = 16 )
inline

Configure the sample rate and word size of the digital audio interface. The SYSCLK / fs ratio is determined from the current REG_CLK_RATES_0 setting (assumes MCLK_DIV = 1, i.e. SYSCLK = MCLK / 2, matching the divider written in begin()).

Parameters
sample_ratesample rate in Hz
word_sizeword size in bits (16, 20, 24, 32)

◆ setDevices()

bool setDevices ( input_device_t input_device,
output_device_t output_device )
inlineoverridevirtual

Stores the output device selection for use by configureOutput().

Reimplemented from ZephyrDriverCommon.

◆ setInputMute()

bool setInputMute ( bool mute,
WM8904Channel channel = WM8904Channel::All )
inline

Mute / unmute the input for the given channel(s).

◆ setInputVolume() [1/2]

bool setInputVolume ( int volume)
inlineoverridevirtual

Sets the input volume in % (0...100) for all channels.

Reimplemented from ZephyrDriverCommon.

◆ setInputVolume() [2/2]

bool setInputVolume ( uint8_t volume,
WM8904Channel channel = WM8904Channel::All )
inline

Set the input volume (0..31) for the given channel(s).

◆ setMute()

bool setMute ( bool mute)
inlineoverridevirtual

Mutes/unmutes all outputs.

Reimplemented from ZephyrDriverCommon.

◆ setOutputMute()

bool setOutputMute ( bool mute,
WM8904Channel channel = WM8904Channel::All )
inline

Mute / unmute the output for the given channel(s).

◆ setOutputVolume()

bool setOutputVolume ( uint8_t volume,
WM8904Channel channel = WM8904Channel::All )
inline

Set the output volume (0..63) for the given channel(s).

◆ setProtocol()

bool setProtocol ( WM8904Protocol protocol)
inline

Configure the digital audio interface protocol (I2S, LJ, RJ, PCMA/B).

◆ setVolume()

bool setVolume ( int volume)
inlineoverridevirtual

Sets the output volume in % (0...100) for all channels.

Reimplemented from ZephyrDriverCommon.

◆ setWire()

void setWire ( i2c_bus_handle_t w)
inlineinherited

Defines the I2C communication object.

◆ softReset()

bool softReset ( )
inline

Soft reset of the codec (writes 0x00 to REG_RESET).

◆ updateInput()

bool updateInput ( WM8904Channel channel,
uint8_t val,
uint8_t mask )
inlineprotected

◆ updateOutput()

bool updateOutput ( WM8904Channel channel,
uint16_t val,
uint16_t mask )
inlineprotected

◆ updateReg()

bool updateReg ( uint8_t reg,
uint8_t mask,
uint8_t value )
inlineprotectedinherited

Read-Modify-Write of a single byte register.

◆ updateReg16()

bool updateReg16 ( uint8_t reg,
uint16_t mask,
uint16_t value )
inlineprotectedinherited

Read-Modify-Write of a 16 bit (big endian) register.

◆ writeReg()

bool writeReg ( uint8_t reg,
uint8_t value )
inlineprotectedinherited

Writes a single byte to an 8 bit register address.

◆ writeReg16()

bool writeReg16 ( uint8_t reg,
uint16_t value )
inlineprotectedinherited

Writes a 16 bit (big endian) value to an 8 bit register address.

Member Data Documentation

◆ i2c_addr

uint8_t i2c_addr = 0
protectedinherited

◆ INPUT_VOLUME_DEFAULT

uint16_t INPUT_VOLUME_DEFAULT = 0b00101
staticconstexpr

◆ INPUT_VOLUME_MAX

uint16_t INPUT_VOLUME_MAX = 0b11111
staticconstexpr

◆ INPUT_VOLUME_MIN

uint16_t INPUT_VOLUME_MIN = 0b00000
staticconstexpr

◆ input_volume_percent

int input_volume_percent = 100
protectedinherited

Last input volume (in %) provided to setInputVolume().

◆ output_device

output_device_t output_device = DAC_OUTPUT_ALL
protected

Output device selection set via setDevices(), used by configureOutput().

◆ OUTPUT_VOLUME_DEFAULT

uint16_t OUTPUT_VOLUME_DEFAULT = 0b101101
staticconstexpr

◆ OUTPUT_VOLUME_MAX

uint16_t OUTPUT_VOLUME_MAX = 0b111111
staticconstexpr

◆ OUTPUT_VOLUME_MIN

uint16_t OUTPUT_VOLUME_MIN = 0b000000
staticconstexpr

◆ REGMASK_IN_MUTE

uint8_t REGMASK_IN_MUTE = 0b10000000
staticconstexpr

◆ REGMASK_IN_VOLUME

uint8_t REGMASK_IN_VOLUME = 0b00011111
staticconstexpr

◆ REGMASK_INSEL_CMENA

uint8_t REGMASK_INSEL_CMENA = 0b01000000
staticconstexpr

◆ REGMASK_INSEL_IP_SEL_N

uint8_t REGMASK_INSEL_IP_SEL_N = 0b00110000
staticconstexpr

◆ REGMASK_INSEL_IP_SEL_P

uint8_t REGMASK_INSEL_IP_SEL_P = 0b00001100
staticconstexpr

◆ REGMASK_INSEL_MODE

uint8_t REGMASK_INSEL_MODE = 0b00000011
staticconstexpr

◆ REGMASK_OUT_MUTE

uint16_t REGMASK_OUT_MUTE = 0b100000000
staticconstexpr

◆ REGMASK_OUT_VOL

uint16_t REGMASK_OUT_VOL = 0b000111111
staticconstexpr

◆ REGMASK_OUT_VU

uint16_t REGMASK_OUT_VU = 0b010000000
staticconstexpr

◆ REGMASK_OUT_ZC

uint16_t REGMASK_OUT_ZC = 0b001000000
staticconstexpr

◆ volume_percent

int volume_percent = 100
protectedinherited

Last volume (in %) provided to setVolume().

◆ wire

i2c_bus_handle_t wire = nullptr
protectedinherited

The documentation for this class was generated from the following file: