ESP32 and WM8731

Artheist
Posts: 1
Joined: Sat May 15, 2021 8:50 pm

ESP32 and WM8731

Postby Artheist » Sat May 15, 2021 8:55 pm

Hi,

I would like to be able to control a WM8731 audio codec chip with my ESP32 however I found only few resources to do so.
I know this chip requires to be wired for both I2S and I2C but I do not quite understand how to achieve this.
I would be very grateful if anyone could give me some pointers on how to achieve such goal.

Thanks

@.

danyo93
Posts: 10
Joined: Sat May 15, 2021 5:56 pm

Re: ESP32 and WM8731

Postby danyo93 » Sat May 29, 2021 11:28 am

I'm using the WM8731 in a project at the moment. But I'm not really sure what your question is exactly. Do you have problems understanding the electrical part of wiring up i2c and i2s or the software part?

For a start, i'd get one of the eval boards for the wm8731, e.g. here: https://www.digikey.com/en/products/det ... 06/4495592 and stack it on a bread board together with the ESP32 dev board. You can power the codec from the 3.3V regulator output and just wire up the rest as mentioned in the eval boards data sheet. The data sheet of the wm8731 explains what you have to do on the software side of things. I2S and I2C apis are very well documented in the IDF docs and the corresponding code examples. I wrote my own library for the wm8731 for use with esp-idf, but you can also find a few c-libraries here and there on github to give you a starting point.

As you use ESP-ADF I'd start taking a look at the audio_hal library, which is the template interface for the esxxx codec drivers (those are the ones used on the lyraT and kaluga boards). When you get how the code is structured, you can easily create and add a driver for the wm8731. The advantage of this approach is that you won't have to take care of the lower level i2s implementation, since you can use i2s_stream and the audio pipeline to read/write and process audio.

Aeonian
Posts: 1
Joined: Tue Jun 01, 2021 3:48 am

Re: ESP32 and WM8731

Postby Aeonian » Tue Jun 01, 2021 3:56 am

Good day,

I bought WM8731 audio codec and I am having a hard time finding which pin should I put the i2s and i2c pin.

Can someone help me to figure out which pin?

Thank so much in advance :) :)

danyo93
Posts: 10
Joined: Sat May 15, 2021 5:56 pm

Re: ESP32 and WM8731

Postby danyo93 » Wed Jun 02, 2021 10:31 am

The pins used for I2S and I2C can be configured in software. The ESP32 has a multiplexer that can route a peripheral to almost any physical pin of the module you're using. There's a few pins that can't/shouldn't be used because they're being used e.g. for communication with the internal SPI flash or are bootstrapped. The datasheet of your respective module will tell you which pins those are.

The detailed information on software configuration for any peripheral can be found in the api docs:
IDF:https://docs.espressif.com/projects/esp ... index.html
ADF: https://docs.espressif.com/projects/esp ... index.html

If you're using ESP-IDF along with one of the eval boards, take a look in the respective documentation of that board (data sheet or schematic) to see how the pins of the on-board ESP32-module are routed on the PCB. Board docs are provided here: https://www.espressif.com/en/products/devkits.

Since you asked this question in the ADF-forum, here is a blueprint of how to use ESP-ADF with a custom board/custom codec setup (!! imho this is more advanced and you'll probably be better off using ESP-IDF at first !!):

If you're using ESP-ADF along with one of the Audio-dev boards, just take a look at the board_pins_config.c and of your respective board here: https://github.com/espressif/esp-adf/tr ... udio_board. For the most natural use of ADF along with the WM8731 you will have to write your own board-library and add a driver for the wm8731 to the audio_hal driver lib. Take a look at the esxxx libs already provided in the ADF's audio_hal component here https://github.com/espressif/esp-adf/tr ... hal/driver. Those are the codecs used on the Audio-dev boards, which ESP-ADF natively supports. You'll have to add a folder for the WM8731 and implement those same functions. Use the WM8731 data sheet and/or search online for existing libraries to do that.

After you have successfully implemented the driver, you will then have to write your own board driver, where you specify and initialize the peripherals that your board is using. Again, take look at the source code of already existing boards in the audio_board component of ESP-ADF (https://github.com/espressif/esp-adf/tr ... udio_board) and it will be no rocket science. You will initialize the codec by populating an instance of the audio_hal_func_t-struct defined in audio_hal.h with the pointers to the functions you have implemented in your codec driver. This is basically a C-version of a C++ virtual base class:

Code: Select all

typedef struct audio_hal {
    esp_err_t (*audio_codec_initialize)(audio_hal_codec_config_t *codec_cfg);                                /*!< initialize codec */
    esp_err_t (*audio_codec_deinitialize)(void);                                                             /*!< deinitialize codec */
    esp_err_t (*audio_codec_ctrl)(audio_hal_codec_mode_t mode, audio_hal_ctrl_t ctrl_state);                 /*!< control codec mode and state */
    esp_err_t (*audio_codec_config_iface)(audio_hal_codec_mode_t mode, audio_hal_codec_i2s_iface_t *iface);  /*!< configure i2s interface */
    esp_err_t (*audio_codec_set_mute) (bool mute);                                                           /*!< set codec mute */
    esp_err_t (*audio_codec_set_volume)(int volume);                                                         /*!< set codec volume */
    esp_err_t (*audio_codec_get_volume)(int *volume);                                                        /*!< get codec volume */
    xSemaphoreHandle audio_hal_lock;                                                                         /*!< semaphore of codec */
    void *handle;                                                                                            /*!< handle of audio codec */
} audio_hal_func_t;
Finally, you will now have to add the paths of your newly written libraries to the make-files/CMakeLists of the components. Don't forget to also add a build flag for your board driver in the Make-File of the audio_component. You will 'tell' the linker and compiler which board driver/codec to include by defining the according build-flag in the SDKconfig file of your project.

I think using those resources you should be able to hook up your (any) codec in no time. I know in the beginning it's a lot of reading, but unfortunately, no one else can't do it for you. However, if you're having trouble with a specific aspect of wiring things up or the software configuration, of course feel free to ask.

Happy coding!

Who is online

Users browsing this forum: No registered users and 35 guests