TLV320 arduino and cp examples

Examples for the TLV320DAC3100 breakout guide
This commit is contained in:
Liz 2025-03-31 16:41:55 -04:00
parent 923b6f24ce
commit 5a20f1cd72
8 changed files with 4683 additions and 1 deletions

View file

@ -0,0 +1,144 @@
// SPDX-FileCopyrightText: 2023 Ladyada for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/*
This example plays a 'raw' PCM file from memory to I2S
*/
#include <Adafruit_TLV320DAC3100.h>
#include <I2S.h>
#include <math.h>
#include "startup.h" // audio file in flash
Adafruit_TLV320DAC3100 codec; // Create codec object
// Create the I2S port using a PIO state machine
I2S i2s(OUTPUT);
// GPIO pin numbers on Feather RP2040
#define pBCLK D9 // BITCLOCK
#define pWS D10 // LRCLOCK
#define pDOUT D11 // DATA
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("I2S playback demo");
}
void loop() {
}
void setup1() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("\n\nTLV320DAC3100 Sine Tone Test");
// Start I2C communication with codec
Serial.println("Initializing codec...");
if (!codec.begin()) {
Serial.println("Failed to initialize codec!");
}
codec.reset();
// Step 1: Set codec interface to I2S with 16-bit data
Serial.println("Configuring codec interface...");
if (!codec.setCodecInterface(TLV320DAC3100_FORMAT_I2S, TLV320DAC3100_DATA_LEN_16)) {
Serial.println("Failed to configure codec interface!");
}
// Step 2: Configure clock - using PLL with BCLK as input
Serial.println("Configuring codec clocks...");
if (!codec.setCodecClockInput(TLV320DAC3100_CODEC_CLKIN_PLL) ||
!codec.setPLLClockInput(TLV320DAC3100_PLL_CLKIN_BCLK)) {
Serial.println("Failed to configure codec clocks!");
}
// Step 3: Set up PLL - these values work well for 44.1kHz sample rate
if (!codec.setPLLValues(1, 1, 8, 0)) {
Serial.println("Failed to configure PLL values!");
}
// Step 4: Configure DAC dividers
if (!codec.setNDAC(true, 8) ||
!codec.setMDAC(true, 2) ||
!codec.setDOSR(128)) {
Serial.println("Failed to configure DAC dividers!");
}
// Step 5: Power up PLL
if (!codec.powerPLL(true)) {
Serial.println("Failed to power up PLL!");
}
// Step 6: Configure DAC path - power up both left and right DACs
Serial.println("Configuring DAC path...");
if (!codec.setDACDataPath(true, true,
TLV320_DAC_PATH_NORMAL,
TLV320_DAC_PATH_NORMAL,
TLV320_VOLUME_STEP_1SAMPLE)) {
Serial.println("Failed to configure DAC data path!");
}
// Step 7: Route DAC output to headphone
if (!codec.configureAnalogInputs(TLV320_DAC_ROUTE_MIXER, // Left DAC to mixer
TLV320_DAC_ROUTE_MIXER, // Right DAC to mixer
false, false, false, // No AIN routing
false)) { // No HPL->HPR
Serial.println("Failed to configure DAC routing!");
}
// Step 8: Unmute DAC and set volume (higher for testing)
Serial.println("Setting DAC volume...");
if (!codec.setDACVolumeControl(
false, false, TLV320_VOL_INDEPENDENT) || // Unmute both channels
!codec.setChannelVolume(false, 18) || // Left DAC +0dB
!codec.setChannelVolume(true, 18)) { // Right DAC +0dB
Serial.println("Failed to configure DAC volume control!");
}
if (!codec.setChannelVolume(false, 12.0) ||
!codec.setChannelVolume(true, 12.0)) {
Serial.println("Failed to set DAC channel volumes!");
}
if (!codec.enableSpeaker(true) || // Dis/Enable speaker amp
!codec.configureSPK_PGA(TLV320_SPK_GAIN_6DB, // Set gain to 6dB
true) || // Unmute
!codec.setSPKVolume(true, 0)) { // Enable and set volume to 0dB
Serial.println("Failed to configure speaker output!");
}
// Initialize I2S peripheral
Serial.println("Initializing I2S...");
i2s.setBCLK(pBCLK);
i2s.setDATA(pDOUT);
i2s.setBitsPerSample(16);
}
void loop1() {
// the main loop will tell us when it wants us to play!
play_i2s(startupAudioData, sizeof(startupAudioData), startupSampleRate);
delay(1000);
}
void play_i2s(const uint8_t *data, uint32_t len, uint32_t rate) {
// start I2S at the sample rate with 16-bits per sample
if (!i2s.begin(rate)) {
Serial.println("Failed to initialize I2S!");
delay(500);
i2s.end();
return;
}
for(uint32_t i=0; i<len; i++) {
uint16_t sample = (uint16_t)data[i] << 6; // our data is 10 bit but we want 16 bit so we add some gain
// write the same sample twice, once for left and once for the right channel
i2s.write(sample);
i2s.write(sample);
}
i2s.end();
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,33 @@
# SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import audiobusio
import audiocore
import board
import adafruit_tlv320
i2c = board.I2C()
dac = adafruit_tlv320.TLV320DAC3100(i2c)
# set sample rate & bit depth, use bclk
dac.configure_clocks(sample_rate=44100, bit_depth=16)
# use headphones
dac.headphone_output = True
dac.headphone_volume = -15 # dB
# or use speaker
# dac.speaker_output = True
# dac.speaker_volume = -10 # dB
audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
with open("StreetChicken.wav", "rb") as wave_file:
wav = audiocore.WaveFile(wave_file)
print("Playing wav file!")
audio.play(wav)
while audio.playing:
pass
print("Done!")

View file

@ -0,0 +1,128 @@
#include <Adafruit_TLV320DAC3100.h>
#include <I2S.h>
#include <math.h>
Adafruit_TLV320DAC3100 codec; // Create codec object
#define pBCLK D9 // BITCLOCK - I2S clock
#define pWS D10 // LRCLOCK - Word select
#define pDOUT D11 // DATA - I2S data
// Create I2S port
I2S i2s(OUTPUT);
const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 500; // amplitude of square wave
const int sampleRate = 16000; // 16 KHz is a good quality
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
int16_t sample = amplitude; // current sample value
int count = 0;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("\n\nTLV320DAC3100 Sine Tone Test");
// Start I2C communication with codec
Serial.println("Initializing codec...");
if (!codec.begin()) {
Serial.println("Failed to initialize codec!");
}
codec.reset();
// Step 1: Set codec interface to I2S with 16-bit data
Serial.println("Configuring codec interface...");
if (!codec.setCodecInterface(TLV320DAC3100_FORMAT_I2S, TLV320DAC3100_DATA_LEN_16)) {
Serial.println("Failed to configure codec interface!");
}
// Step 2: Configure clock - using PLL with BCLK as input
Serial.println("Configuring codec clocks...");
if (!codec.setCodecClockInput(TLV320DAC3100_CODEC_CLKIN_PLL) ||
!codec.setPLLClockInput(TLV320DAC3100_PLL_CLKIN_BCLK)) {
Serial.println("Failed to configure codec clocks!");
}
// Step 3: Set up PLL - these values work well for 44.1kHz sample rate
if (!codec.setPLLValues(1, 1, 8, 0)) {
Serial.println("Failed to configure PLL values!");
}
// Step 4: Configure DAC dividers
if (!codec.setNDAC(true, 8) ||
!codec.setMDAC(true, 2) ||
!codec.setDOSR(128)) {
Serial.println("Failed to configure DAC dividers!");
}
// Step 5: Power up PLL
if (!codec.powerPLL(true)) {
Serial.println("Failed to power up PLL!");
}
// Step 6: Configure DAC path - power up both left and right DACs
Serial.println("Configuring DAC path...");
if (!codec.setDACDataPath(true, true,
TLV320_DAC_PATH_NORMAL,
TLV320_DAC_PATH_NORMAL,
TLV320_VOLUME_STEP_1SAMPLE)) {
Serial.println("Failed to configure DAC data path!");
}
// Step 7: Route DAC output to headphone
if (!codec.configureAnalogInputs(TLV320_DAC_ROUTE_MIXER, // Left DAC to mixer
TLV320_DAC_ROUTE_MIXER, // Right DAC to mixer
false, false, false, // No AIN routing
false)) { // No HPL->HPR
Serial.println("Failed to configure DAC routing!");
}
// Step 8: Unmute DAC and set volume (higher for testing)
Serial.println("Setting DAC volume...");
if (!codec.setDACVolumeControl(
false, false, TLV320_VOL_INDEPENDENT) || // Unmute both channels
!codec.setChannelVolume(false, 18) || // Left DAC +0dB
!codec.setChannelVolume(true, 18)) { // Right DAC +0dB
Serial.println("Failed to configure DAC volume control!");
}
if (!codec.setChannelVolume(false, 12.0) ||
!codec.setChannelVolume(true, 12.0)) {
Serial.println("Failed to set DAC channel volumes!");
}
if (!codec.enableSpeaker(true) || // Dis/Enable speaker amp
!codec.configureSPK_PGA(TLV320_SPK_GAIN_6DB, // Set gain to 6dB
true) || // Unmute
!codec.setSPKVolume(true, 0)) { // Enable and set volume to 0dB
Serial.println("Failed to configure speaker output!");
}
// Initialize I2S peripheral
Serial.println("Initializing I2S...");
i2s.setBCLK(pBCLK);
i2s.setDATA(pDOUT);
i2s.setBitsPerSample(16);
// Start I2S at the sample rate
if (!i2s.begin(sampleRate)) {
Serial.println("Failed to initialize I2S!");
}
}
void loop() {
if (count % halfWavelength == 0) {
// invert the sample every half wavelength count multiple to generate square wave
sample = -1 * sample;
}
// write the same sample twice, once for left and once for the right channel
i2s.write(sample);
i2s.write(sample);
// increment the counter for the next sample
count++;
}

View file

@ -1 +1 @@
depends=Adafruit ILI9341, Adafruit BusIO, SD, Adafruit NeoPixel, Adafruit VS1053 Library, Adafruit BluefruitLE nRF51, Adafruit seesaw Library, Ethernet, Stepper, Adafruit IO Arduino, FastLED, Adafruit LiquidCrystal, Adafruit SoftServo, TinyWireM, Adafruit AM radio library, WaveHC, Adafruit LED Backpack Library, MAX31850 OneWire, Adafruit VC0706 Serial Camera Library, RTClib, Adafruit SleepyDog Library, Adafruit Thermal Printer Library, Adafruit Zero I2S Library, Adafruit EPD, Adafruit SSD1351 library, Adafruit FONA Library, Adafruit Motor Shield V2 Library, Adafruit NeoMatrix, Adafruit Soundboard library, Adafruit Circuit Playground, ArduinoJson, Adafruit TCS34725, Adafruit Pixie, Adafruit GPS Library, TinyGPS, WiFi101, Adafruit DotStar, Adafruit Si7021 Library, Adafruit WS2801 Library, Mouse, Keyboard, Time, IRremote, Adafruit LSM9DS0 Library, Adafruit Arcada Library, MIDIUSB, PubSubClient, Adafruit LIS2MDL, Adafruit NeoPXL8, Adafruit MCP23017 Arduino Library, Adafruit MLX90640, LiquidCrystal, Adafruit NeoTrellis M4 Library, RGB matrix Panel, Adafruit MLX90614 Library, Adafruit RGB LCD Shield Library, MAX6675 library, Adafruit MP3, Adafruit Keypad, Adafruit Arcada GifDecoder, Keypad, Neosegment, Encoder, Adafruit TiCoServo, Adafruit Trellis Library, FauxmoESP, Adafruit LSM303 Accel, Adafruit LSM303DLH Mag, Adafruit LSM303DLHC, CapacitiveSensor, Adafruit Zero PDM Library, Adafruit DMA neopixel library, elapsedMillis, DST RTC, Adafruit SHARP Memory Display, Adafruit SPIFlash, BSEC Software Library, WiiChuck, Adafruit DPS310, Adafruit AHTX0, RotaryEncoder, Adafruit MCP9808 Library, LSM303, Adafruit Protomatter, Adafruit IS31FL3741 Library, Sensirion I2C SCD4x, Adafruit TestBed, Bounce2, Adafruit AHRS, Adafruit DRV2605 Library, STM32duino VL53L4CD, PicoDVI - Adafruit Fork, Adafruit MMA8451 Library, Adafruit TSC2007, GFX Library for Arduino, Adafruit PyCamera Library, Adafruit ADG72x, Adafruit BNO055, Adafruit SHT4x Library, Adafruit VCNL4200 Library, Adafruit GC9A01A, Adafruit DVI HSTX
depends=Adafruit ILI9341, Adafruit BusIO, SD, Adafruit NeoPixel, Adafruit VS1053 Library, Adafruit BluefruitLE nRF51, Adafruit seesaw Library, Ethernet, Stepper, Adafruit IO Arduino, FastLED, Adafruit LiquidCrystal, Adafruit SoftServo, TinyWireM, Adafruit AM radio library, WaveHC, Adafruit LED Backpack Library, MAX31850 OneWire, Adafruit VC0706 Serial Camera Library, RTClib, Adafruit SleepyDog Library, Adafruit Thermal Printer Library, Adafruit Zero I2S Library, Adafruit EPD, Adafruit SSD1351 library, Adafruit FONA Library, Adafruit Motor Shield V2 Library, Adafruit NeoMatrix, Adafruit Soundboard library, Adafruit Circuit Playground, ArduinoJson, Adafruit TCS34725, Adafruit Pixie, Adafruit GPS Library, TinyGPS, WiFi101, Adafruit DotStar, Adafruit Si7021 Library, Adafruit WS2801 Library, Mouse, Keyboard, Time, IRremote, Adafruit LSM9DS0 Library, Adafruit Arcada Library, MIDIUSB, PubSubClient, Adafruit LIS2MDL, Adafruit NeoPXL8, Adafruit MCP23017 Arduino Library, Adafruit MLX90640, LiquidCrystal, Adafruit NeoTrellis M4 Library, RGB matrix Panel, Adafruit MLX90614 Library, Adafruit RGB LCD Shield Library, MAX6675 library, Adafruit MP3, Adafruit Keypad, Adafruit Arcada GifDecoder, Keypad, Neosegment, Encoder, Adafruit TiCoServo, Adafruit Trellis Library, FauxmoESP, Adafruit LSM303 Accel, Adafruit LSM303DLH Mag, Adafruit LSM303DLHC, CapacitiveSensor, Adafruit Zero PDM Library, Adafruit DMA neopixel library, elapsedMillis, DST RTC, Adafruit SHARP Memory Display, Adafruit SPIFlash, BSEC Software Library, WiiChuck, Adafruit DPS310, Adafruit AHTX0, RotaryEncoder, Adafruit MCP9808 Library, LSM303, Adafruit Protomatter, Adafruit IS31FL3741 Library, Sensirion I2C SCD4x, Adafruit TestBed, Bounce2, Adafruit AHRS, Adafruit DRV2605 Library, STM32duino VL53L4CD, PicoDVI - Adafruit Fork, Adafruit MMA8451 Library, Adafruit TSC2007, GFX Library for Arduino, Adafruit PyCamera Library, Adafruit ADG72x, Adafruit BNO055, Adafruit SHT4x Library, Adafruit VCNL4200 Library, Adafruit GC9A01A, Adafruit DVI HSTX, Adafruit TLV320 I2S