I think the problem is, I had originally worked with the ST code for using the codec library, and it's handled differently. What I don't understand is how the I2C knows when it can use the buffer contents, which is probably the whole problem in what I've done here. Unless the main code fills the buffer then just waits for the event to be thrown when it's ready for more, still, thank handoff doesn't make sense to me. Should I have an event loop running somewhere?
In any case, thanks for looking! Coming from Arduiono-land, the STM32F4 has been much more of a challenge, just glad I've made it this far!!
Code: Select all
#include "ch.h"
#include "hal.h"
#include "codec.h"
uint32_t Codec_TIMEOUT_UserCallback(void)
{
port_halt();
return 0;
}
void port_halt(void)
{
port_disable();
palSetPad(GPIOD, 15); // turn on blue LED
while(TRUE)
{
}
}
static Thread* g_pAudioDecoderThread = NULL;
static void I2SDmaTxInterrupt(SPIDriver* spip, uint32_t flags)
{
if(spip == &SPID3)
{
if(flags & STM32_DMA_ISR_HTIF)
{
// let the decoder thread play the next samples
chSysLockFromIsr();
chEvtSignalI(g_pAudioDecoderThread, (eventmask_t)2);
chSysUnlockFromIsr();
}
if(flags & STM32_DMA_ISR_TCIF)
{
// let the decoder thread play the next samples
chSysLockFromIsr();
chEvtSignalI(g_pAudioDecoderThread, (eventmask_t)4);
chSysUnlockFromIsr();
}
}
}
#define AUDIO_BUF_SIZE 4096
static uint16_t audioBuffer[AUDIO_BUF_SIZE] = {0};
static uint16_t counter = 0;
extern void audioGen_runProc(uint16_t* buffer, uint16_t len)
{ //generates the audio and writes into buffer
uint16_t i = 0;
do
{ buffer[i] = counter;
buffer[i+1] = counter;
counter++;
i+=2;
}
while(i<len-1);
}
int main(void) {
halInit();
chSysInit();
palSetPad(GPIOD, GPIOD_LED3); // turn on orange LED
dmaStreamAllocate(SPID3.dmatx,
STM32_SPI_SPI3_IRQ_PRIORITY,
(stm32_dmaisr_t)I2SDmaTxInterrupt,
&SPID3);
uint32_t unDmaBufMode = 0;
g_pAudioDecoderThread = chThdSelf();
EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, 80, (uint32_t)44100);
EVAL_AUDIO_Play((uint16_t*)audioBuffer, AUDIO_BUF_SIZE);
while (TRUE)
{ if(unDmaBufMode == 0)
{ //fill whole buffer - first time through
audioGen_runProc( audioBuffer, AUDIO_BUF_SIZE);
}
else if(unDmaBufMode == 1)
{ //fill first half
audioGen_runProc( audioBuffer, AUDIO_BUF_SIZE/2);
}
else
{ //fill last half
audioGen_runProc( audioBuffer+(AUDIO_BUF_SIZE*sizeof(uint16_t)/2), AUDIO_BUF_SIZE/2);
}
eventmask_t em = chEvtWaitAny((eventmask_t)2 | 4);
if(em & 2)
{ unDmaBufMode = 1;
}
else if(em & 4)
{ unDmaBufMode = 2;
}
}
}
Rick