Skip to content

muhammadrefa/WM8940-stm32-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

195b3b3 · Jan 17, 2023

History

49 Commits
Nov 18, 2022
Jan 12, 2023
Jan 17, 2023
Jan 12, 2023
Jan 9, 2023

Repository files navigation

WM8940 audio codec library for STM32

Useful links:

How to use

Write your implementation to write/read register from the device. For example:

// --- main.c ---

void MyI2C_Write(I2C_HandleTypeDef* i2c_handle, uint8_t i2c_address, uint8_t register, uint16_t value)
{
    // TODO
}

uint16_t MyI2C_Read(I2C_HandleTypeDef* i2c_handle, uint8_t i2c_address, uint8_t register)
{
    // TODO
}
// --- main.h ---

void MyI2C_Write(I2C_HandleTypeDef* i2c_handle, uint8_t i2c_address, uint8_t register, uint16_t value);
uint16_t MyI2C_Read(I2C_HandleTypeDef* i2c_handle, uint8_t i2c_address, uint8_t register);

Edit the wm8940.h to call your implementation

// --- wm8940.h ---

#include "main.h"    // The location of the function prototype for the implementation

#define WM8940_REG_WRITE(handle, reg, val) MyI2C_Write(handle, WM8940_I2C_ADDRESS, reg, val)
#define WM8940_REG_READ(handle, reg) MyI2C_Read(handle, WM8940_I2C_ADDRESS, reg)

Now you can initialize the audio codec, configure the Digital Audio Interface to transmit/receive audio data, and follow the Audio Signal Path from the datasheet.

// --- main.c ---

#include "wm8940.h"

WM8940_t wm8940;

int main(void)
{
    // ...

    wm8940.comm_handle = &hi2c1;
    WM8940_Init(&wm8940);

    // STM32 as I2S Master
    WM8940_Set_Clock(&wm8940, 0, WM8940_BCLKDIV_1, WM8940_MCLKDIV_1, WM8940_CLKSEL_MCLK);
    WM8940_Set_AudioInterfaceFormat(&wm8940, WM8940_AUDIO_IFACE_FMT_I2S, WM8940_AUDIO_IFACE_WL_16BITS);
    WM8940_Set_SampleRate(&wm8940, WM8940_SAMPLERATE_8KHZ);

    // Audio bypass from MICN to Speaker (BTL) and Mono
    WM8940_Set_PGA_Input(&wm8940, WM8940_INPUT_MICN);
    WM8940_Set_PGA_Mute(&wm8940, 1);
    WM8940_Set_PGA_Enable(&wm8940, 1);
    WM8940_Set_Boost_Enable(&wm8940, 1);
    WM8940_Set_MicBias_Enable(&wm8940, 1);
    WM8940_Set_Speaker_Source(&wm8940, WM8940_OUTPUT_FROM_BYPASS);
    WM8940_Set_Speaker_Mute(&wm8940, 0);
    WM8940_Set_Mono_Source(&wm8940, WM8940_OUTPUT_FROM_BYPASS);
    WM8940_Set_Mono_Mute(&wm8940, 0);
    WM8940_Set_Output_Enable(&wm8940, WM8940_OUTPUT_SPK | WM8940_OUTPUT_MONO);

    // ...
}

Implemented (based on device description):

  • Input signal path
    • Microphone inputs
    • Input PGA volume control
    • Auxiliary input
    • Input boost
    • Microphone biasing
  • Analogue to Digital Converter (ADC)
    • High pass filter
    • Notch filter
    • Digital ADC volume control
  • Input limiter/Automatic Level Control (ALC)
    • Operation mode (Normal/Limiter)
    • Attack and decay times
    • Minimum and maximum gain
    • Hold time and ALC level
    • Noise gate
    • ALC zero cross
  • Output signal path
    • Hi-Fi DAC volume control
    • Hi-Fi Digital to Analogue Converter (DAC)
    • Automute
    • DAC output limiter
    • Volume boost
  • Analogue outputs
    • SPKOUTP/SPKOUTN outputs
    • Zero cross timeout
    • Enabling the outputs
    • Unused analogue inputs/outputs (VREF to analogue output resistance/VROI)
  • Output switch
    • Thermal shutdown
  • Digital audio interfaces
    • Set audio format
    • Set word length
    • Set frame clock and BCLK polarity
    • Set LOUTR
    • (ADC & DAC) data L&R swap
    • Clock generation control
  • Audio sample rates
  • Master clock and Phase Locked Loop (PLL)
    • Enable PLL
    • Set PLL power
    • Set PLL frequency
  • Companding
    • (ADC & DAC) Loopback
    • (ADC & DAC) Companding
    • Enable 8-bit word length
  • General Purpose Input/Output
  • Control interface
    • Selection of control mode
    • Auto-incremental write setting
    • Readback
  • Resetting the chip
  • Power management
    • VMID control
    • BIASEN control
  • POP minimisation
    • Power on Bias control (configured on init)
    • VMID soft start (configured on init)
    • Fast VMID discharge

About

WM8940 library for STM32 written in C

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages