I2S with MAX98357A Breakout From Adafruit
MAX98357A
The IC on this breakout board contains a 3W class D amplifier for driving things like speakers. Unlike other common hobbyist class D amplifiers, this breakout uses an I2S interface rather than a PWM or Analog signal. The board also has several configurable gain settings and a low power shutdown mode.
Link to Buy: https://www.adafruit.com/products/3006
I2S
I2S is a communication bus standard that is very popular for audio devices.
The bus uses three lines: -Serial Data: for audio data -Word Select (WS): selects whether the audio data is for the left or right channel -Clock
The mbed is I2S capable and can operate in either master or slave mode. The also has both a TX and RX capabilities that can be implemented as different instances of the I2S class. This may be useful if the user has a system with both an ADC and DAC as the mbed can interface easily with both. The library written by Giles Barton-Owen is linked below with example code.
Getting the MAX98357A to work with the mbed
The breakout board contains 7 pins that plug into the breadboard and two that go to a speaker. Starting from the left the LRC pin needs to connect to the
Datasheet: https://cdn-shop.adafruit.com/product-files/3006/MAX98357A-MAX98357B.pdf
mbed <-> MAX
- p5(Serial) <-> Din
- p6(WS) <-> LRC
- p7(clock) <-> BCLK
- GND <-> GND
- Vout or Vu<-> Vin
The gain pin can be configured in five different configurations as follows.
Example Code
This code generates a 100Hz ramp wave test tone using the I2S library linked below. There are a few things to note about the library before using it the the breakout board. In the beginning of the cpp file there are a set of definitions that correspond to default modes for the mbed. Some of these must be changed. The mbed should be configured as the Master. It does not matter if the i2s object is configured in stereo or mono mode. If set to stereo the breakout board just averages the left and right channels. If the mbed is receiving data then the i2s.attach() function can be used for an FIFO interrupt.
include the mbed library with this snippet
#include "mbed.h" #include "I2S.h" I2S i2s(I2S_TRANSMIT, p5, p6, p7); Ticker timer; int i = 0; int fs = 32000; int freq = 100; int buff[1]; int bufflen = 1; void isr() { buff[0] = i * (fs/freq); i2s.write(buff, bufflen); i ++; if(i == (fs/freq)) { i = 0; } } int main() { i2s.stereomono(I2S_MONO); i2s.masterslave(I2S_MASTER); i2s.start(); timer.attach(&isr, 1/32000.0); while(1) {} }
Import library
Public Member Functions |
|
I2S (bool rxtx, PinName sd, PinName ws, PinName clk) | |
Create a
I2S
instance.
|
|
I2S (bool rxtx, PinName sd) | |
Create a
I2S
instance: Only with the serial data line set.
|
|
I2S (bool rxtx, PinName sd, PinName ws) | |
Create a
I2S
instance: Only with serial data line and word select.
|
|
I2S (bool rxtx, PinName sd, bool fourwiremode) | |
Create a
I2S
instance: Only with serial data line.
|
|
I2S (bool rxtx, PinName sd, PinName ws, bool fourwiremode) | |
Create a
I2S
instance: Only with serial data line and word select line.
|
|
~I2S () | |
Destroy the
I2S
instance.
|
|
void | write (char buf[], int len) |
Write to the FIFO.
|
|
void | write (int buf[], int len) |
Write to the FIFO.
|
|
int | read () |
Read the FIFOs contents.
|
|
void | read (char buf[], int len) |
Read from the FIFO.
|
|
void | read (int buf[], int len) |
Read from the FIFO.
|
|
int | max_fifo_points () |
Get the maximum number of points of data the FIFO could store.
|
|
void | power (bool pwr) |
Switch the peripheral on and off.
|
|
void | masterslave (bool mastermode) |
Switch the peripheral between master and slave.
|
|
void | wordsize (int words) |
Switch the peripheral between different wordsizes.
|
|
void | mclk_freq (int freq) |
Define the mclk frequency.
|
|
void | frequency (int freq) |
Define the sample rate.
|
|
void | set_interrupt_fifo_level (int level) |
Set the level that the fifo interrupts at.
|
|
int | fifo_level () |
Get the current FIFO level.
|
|
int | fifo_points () |
Get the current number of samples in the FIFO.
|
|
void | stereomono (bool stereomode) |
Set whether the peripheral is in stereo or mono mode: in mono the perifpheral sends out the same data twice.
|
|
void | mute () |
Mute the peripheral.
|
|
void | mute (bool mute_en) |
Set the mute status of the peripheral.
|
|
void | stop () |
Stop the peripheral.
|
|
void | start () |
Start the peripheral.
|
|
bool | setup_ok () |
Check the Clock and Pin setup went according to plan.
|
|
void | attach (void(*fptr)(void)) |
Attach a function to be called when the FIFO triggers.
|
|
template<typename T > | |
void | attach (T *tptr, void(T::*mptr)(void)) |
Attach a member function to be called when the FIFO triggers.
|
Please log in to post comments.