Hello!
I am trying to port Pazi's original TDA1543A code (https://forum.pjrc.com/threads/29284-Dual-channel-16bit-dac-PT8211/page5?p=141314#post141314) to work with my Teensy 4.0. So far, I have the CLK and FS signals working, but I haven't been so lucky with getting the DATA line to give me any output. I'm pretty new to low level coding like this, and this is what I came up with by looking at the datasheet (the excessive comments are me trying to understand the code). Am I missing anything? Are there obvious mistakes?
output_tda.h:
output_tda.cpp:
Any help would be very much appreciated!
I am trying to port Pazi's original TDA1543A code (https://forum.pjrc.com/threads/29284-Dual-channel-16bit-dac-PT8211/page5?p=141314#post141314) to work with my Teensy 4.0. So far, I have the CLK and FS signals working, but I haven't been so lucky with getting the DATA line to give me any output. I'm pretty new to low level coding like this, and this is what I came up with by looking at the datasheet (the excessive comments are me trying to understand the code). Am I missing anything? Are there obvious mistakes?
output_tda.h:
Code:
/* Audio Library for Teensy 3.X
* Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
*/
#ifndef output_tda_h_
#define output_tda_h_
#include "Arduino.h"
#include "AudioStream.h"
#include "DMAChannel.h"
class AudioOutputTDA : public AudioStream
{
public:
AudioOutputTDA(void) : AudioStream(2, inputQueueArray) { begin(); }
virtual void update(void);
void begin(void);
protected:
static void configureI2S(void);
static audio_block_t *block_left_1st;
static audio_block_t *block_right_1st;
static bool update_responsibility;
static DMAChannel dma;
static void isr(void);
private:
static audio_block_t *block_left_2nd;
static audio_block_t *block_right_2nd;
static uint16_t block_left_offset;
static uint16_t block_right_offset;
audio_block_t *inputQueueArray[2];
static void configureDMA();
static void receiveAudioBlock(audio_block_t*& index,
uint16_t& blockOffset,
audio_block_t*& block_1st,
audio_block_t*& block_2nd);
static uint32_t scaleInputTo32Bit(uint16_t input);
static void copyAndScaleInputToOutput(uint32_t*& dest,
uint16_t& blockOffset,
audio_block_t*& block_1st,
audio_block_t*& block_2nd);
static uint32_t* getDestination();
};
#endif
output_tda.cpp:
Code:
/* Audio Library for Teensy 3.X
*
* TDA1543A DAC interface
*
* Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
* Original Teensy 3.X version by user Pazi
* (https://forum.pjrc.com/threads/29284-Dual-channel-16bit-dac-PT8211/page5?p=141314#post141314)
*
* Adapted for Teensy 4.0 by Daniël Kamp, with help from Wouter Ensink
*
*/
#include "output_tda.h"
#include "utility/imxrt_hw.h"
audio_block_t * AudioOutputTDA::block_left_1st = NULL;
audio_block_t * AudioOutputTDA::block_right_1st = NULL;
audio_block_t * AudioOutputTDA::block_left_2nd = NULL;
audio_block_t * AudioOutputTDA::block_right_2nd = NULL;
uint16_t AudioOutputTDA::block_left_offset = 0;
uint16_t AudioOutputTDA::block_right_offset = 0;
bool AudioOutputTDA::update_responsibility = false;
DMAMEM __attribute__((aligned(32))) static uint32_t i2s_tx_buffer[AUDIO_BLOCK_SAMPLES * 2];
DMAChannel AudioOutputTDA::dma(false);
// ==============================================
// Do setup things
// ==============================================
void AudioOutputTDA::begin(void)
{
// Allocate memory and init DMAChannel
memset(i2s_tx_buffer, 0, sizeof(i2s_tx_buffer));
dma.begin(true);
block_left_1st = NULL;
block_right_1st = NULL;
// Do protocol configuration
configureI2S_2();
// Config pin 7 for use with SAI
CORE_PIN7_CONFIG = 3; //1:TX_DATA0
// Flush cache
arm_dcache_flush_delete(i2s_tx_buffer, sizeof(i2s_tx_buffer));
configureDMA();
// Set enable bits for I2S transmitter and receiver
I2S1_RCSR |= I2S_RCSR_RE;
I2S1_TCSR |= I2S_TCSR_TE | I2S_TCSR_BCE | I2S_TCSR_FRDE;
update_responsibility = update_setup();
// Attach isr() function to DMA interrupt
dma.attachInterrupt(isr);
}
void AudioOutputTDA::configureDMA() {
// Set DMA source address
dma.TCD->SADDR = i2s_tx_buffer;
// Set DMA source offset
dma.TCD->SOFF = 4;
// Set DMA attribute bits
dma.TCD->ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
// Set DMA's number of bytes to transfer per request
dma.TCD->NBYTES_MLNO = 4;
// Set DMA source last address
dma.TCD->SLAST = -sizeof(i2s_tx_buffer);
// Set DMA destination offset
dma.TCD->DOFF = 0;
// Set DMA current major iterator count [6.6.5.28.4, page 166]
dma.TCD->CITER_ELINKNO = sizeof(i2s_tx_buffer) / 4;
// Set address for error handling? AFAIK not applicable right now, might look into this later
dma.TCD->DLASTSGA = 0;
// Set DMA beginning major iterator count [6.6.5.32, page 171]
dma.TCD->BITER_ELINKNO = sizeof(i2s_tx_buffer) / 4;
// Set DMA CSR config bits
dma.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
// Set DMA destination address
dma.TCD->DADDR = (void *)((uint32_t)&I2S1_TDR0);
// Link DMA to SAI1 hardware event
dma.triggerAtHardwareEvent(DMAMUX_SOURCE_SAI1_TX);
// Enable the DMA
dma.enable();
}
void AudioOutputTDA::configureI2S(void) {
CCM_CCGR5 |= CCM_CCGR5_SAI1(CCM_CCGR_ON);
// TODO: understand what this does
//PLL:
int fs = AUDIO_SAMPLE_RATE_EXACT;
// PLL between 27*24 = 648MHz und 54*24=1296MHz
int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
int n2 = 1 + (24000000 * 27) / (fs * 256 * n1);
double C = ((double)fs * 256 * n1 * n2) / 24000000;
int c0 = C;
int c2 = 10000;
int c1 = C * c2 - (c0 * c2);
set_audioClock(c0, c1, c2);
// Clear SAI1_CLK register locations
CCM_CSCMR1 = (CCM_CSCMR1 & ~(CCM_CSCMR1_SAI1_CLK_SEL_MASK))
| CCM_CSCMR1_SAI1_CLK_SEL(2); // (0,1,2): PLL3PFD0, PLL5, PLL4
CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
| CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
| CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
// Select MCLK
IOMUXC_GPR_GPR1 = (IOMUXC_GPR_GPR1 & ~(IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL_MASK))
| (IOMUXC_GPR_GPR1_SAI1_MCLK_DIR | IOMUXC_GPR_GPR1_SAI1_MCLK1_SEL(0));
// TODO: end of confusion
// Don't continue setup sequence if I2S1_TCSR_TE (Transmit Enable) is already set
if (I2S1_TCSR & I2S_TCSR_TE) return;
// Configure BCK and CS pins
CORE_PIN21_CONFIG = 3; //1:RX_BCLK
CORE_PIN20_CONFIG = 3; //1:RX_SYNC
// ==========================================================
// Configure the I2S register
// Datasheet: page 1987 onwards
// ==========================================================
// Setup transmitter
constexpr int bitwidth = 31;
I2S1_TMR = 0;
I2S1_TCR1 = I2S_TCR1_RFW(0);
I2S1_TCR2 = I2S_TCR2_SYNC(0) | I2S_TCR2_BCP | I2S_TCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
I2S1_TCR3 = I2S_TCR3_TCE;
I2S1_TCR4 = I2S_TCR4_FRSZ(1) | I2S_TCR4_SYWD(bitwidth) | I2S_TCR4_MF | I2S_TCR4_FSE | I2S_TCR4_FSP | I2S_TCR4_FSD;
I2S1_TCR5 = I2S_TCR5_WNW(bitwidth) | I2S_TCR5_W0W(bitwidth) | I2S_TCR5_FBT(bitwidth);
// Setup receiver (unnecessary afaik, but the example did it too)
I2S1_RMR = 0;
I2S1_RCR1 = I2S_RCR1_RFW(0);
I2S1_RCR2 = I2S_RCR2_SYNC(1) | I2S_RCR2_BCP | I2S_RCR2_MSEL(1) | I2S_TCR2_BCD | I2S_TCR2_DIV(0);
I2S1_RCR3 = I2S_RCR3_RCE;
I2S1_RCR4 = I2S_RCR4_FRSZ(1) | I2S_RCR4_SYWD(bitwidth) | I2S_RCR4_MF | I2S_RCR4_FSE | I2S_RCR4_FSP | I2S_RCR4_FSD;
I2S1_RCR5 = I2S_RCR5_WNW(bitwidth) | I2S_RCR5_W0W(bitwidth) | I2S_RCR5_FBT(bitwidth);
}
// ==============================================
// Handle incoming audio blocks
// ==============================================
void AudioOutputTDA::update(void) {
// Receive left channel
auto* block_left = receiveReadOnly(0);
receiveAudioBlock(block_left, block_left_offset, block_left_1st, block_left_2nd);
// Receive right channel
auto* block_right = receiveReadOnly(1);
receiveAudioBlock(block_right, block_right_offset, block_right_1st, block_right_2nd);
}
void AudioOutputTDA::receiveAudioBlock(audio_block_t*& block, uint16_t& blockOffset, audio_block_t*& block_1st, audio_block_t*& block_2nd) {
if (block) {
// Deny interrupt requests while buffer is being moved
__disable_irq();
// Move the block into whichever buffer is empty...
if (block_1st == NULL) {
block_1st = block;
blockOffset = 0;
__enable_irq();
} else if (block_2nd == NULL) {
block_2nd = block;
__enable_irq();
}
// ...or shift the buffers and fill the last one
else {
audio_block_t *tmp = block_1st;
block_1st = block_2nd;
block_2nd = block;
blockOffset = 0;
__enable_irq();
release(tmp);
}
}
}
// ==============================================
// Handle outgoing audio blocks
// ==============================================
void AudioOutputTDA::isr(void) {
dma.clearInterrupt();
auto* dest = getDestination();
// Copy and scale left channel
copyAndScaleInputToOutput(dest, block_left_offset, block_left_1st, block_left_2nd);
// Move back to beginning(+1) of buffer
dest -= AUDIO_BLOCK_SAMPLES - 1;
// Copy and scale right channel
copyAndScaleInputToOutput(dest, block_right_offset, block_right_1st, block_right_2nd);
// Serial.println(*dest);
}
uint32_t* AudioOutputTDA::getDestination()
{
const auto saddr = (uint32_t)(dma.TCD->SADDR);
// Fill the part of the buffer that the DMA isn't currently transmitting
if (saddr < (uint32_t)i2s_tx_buffer + sizeof(i2s_tx_buffer) / 2) {
auto* temp = (uint32_t *)&i2s_tx_buffer[AUDIO_BLOCK_SAMPLES];
// TODO: figure out what this does (something IRQ?)
if (AudioOutputTDA::update_responsibility)
AudioStream::update_all();
return temp;
} else {
return (uint32_t *)i2s_tx_buffer;
}
}
uint32_t AudioOutputTDA::scaleInputTo32Bit(uint16_t input)
{
// 0x8000 == 0b1000'0000'0000'0000
// AND-ing with 0x8000 gives us the unsigned 16-bit version of the input
// 0xff000000 == 1111'1111'0000'0000'0000'0000'0000'0000
// 32-bit input == 0000'0000'bbbb'bbbb'bbbb'bbbb'0000'0000
// OR-ing these together gives us the correct format for the TDA1543A chip
return ((uint32_t)input << 9) | ((input & 0x8000) != 0 ? 0xff000000 : 0);
}
void AudioOutputTDA::copyAndScaleInputToOutput(uint32_t*& dest,
uint16_t& blockOffset,
audio_block_t*& block_1st,
audio_block_t*& block_2nd)
{
if (block_1st) {
auto offset = blockOffset;
auto* src = &block_1st->data[offset];
for (int i = 0; i < AUDIO_BLOCK_SAMPLES/2; ++i) {
*dest = scaleInputTo32Bit(*src++);
dest += 2;
}
offset += AUDIO_BLOCK_SAMPLES/2;
if (offset < AUDIO_BLOCK_SAMPLES) {
blockOffset = offset;
} else {
blockOffset = 0;
AudioStream::release(block_1st);
block_1st = block_2nd;
block_2nd = NULL;
}
} else {
for (int i = 0; i < AUDIO_BLOCK_SAMPLES/2; ++i) {
*dest = 0;
dest += 2;
}
}
}
Any help would be very much appreciated!
Last edited: