DAC on STM32F4 Discovery board...where am I going wrong

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

DAC on STM32F4 Discovery board...where am I going wrong

Post by Rick Burnett »

I've spent a good 12 hours trying to debug what I am doing wrong. From what I can tell from the this forum, the I2C stuff isn't completely implemented with drivers for the included DAC. I found a project here that someone created an MP3 player and for now, I am just trying to create a ramp output, nothing complex (yet). The code compiles fine, but of course, there is no sound out of the port. I am just putting the main.c and stripped out most of the comments to just make it shorter. Does anything pop out as a mistake?

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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: DAC on STM32F4 Discovery board...where am I going wrong

Post by Giovanni »

Hi,

Sadly we don't have yet an officially supported I2S driver (it has been on the to-do list for a while now).

About your code, do you receive the DMA ISR? if not, do you enable the I2S clock in RCC? in halconf.h there are I2S related settings too.

#define STM32_I2SSRC STM32_I2SSRC_CKIN
#define STM32_PLLI2SN_VALUE 192
#define STM32_PLLI2SR_VALUE 5

Read carefully the Reference Manual about the I2S clock source, the settings have not been tested yet.

Giovanni
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: DAC on STM32F4 Discovery board...where am I going wrong

Post by Rick Burnett »

Thanks so much for your help. I am not sure what the DMA ISR is so I'll need to do some more reading on both that and the I2S information in the reference manual. The MP3 player mcuconf.h does not have an I2C section so I am not sure what things have changed since that example was released here. I took the example code that generates ch.elf for the discovery board and migrated everything into it from the mp3 example, getting rid of the things I didn't need (not doing anything with mp3s).

All those defines are in the mcuconf.h, however, the values used in the mp3 player were a little different:

#define STM32_PLLI2SN_VALUE 258
#define STM32_PLLI2SR_VALUE 3

When I run the code, it throws the Codec_TIMEOUT_UserCallback function (which sets the LED blue).

Clearly, I just need to read more about how the I2C works because I am sure it's probably something simple I've neglected to set correctly somewhere, or, I am doing things that are conflicting.

Rick
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: DAC on STM32F4 Discovery board...where am I going wrong

Post by Giovanni »

The DMA ISR is your I2SDmaTxInterrupt() function, it is called when the DMA operation is halfway and again when it is completed.

If it is called then it means that the I2S is actively pulling data through the DMA, an important bit of info.

Giovanni
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: DAC on STM32F4 Discovery board...where am I going wrong

Post by Rick Burnett »

Ahhhh, okay. What I did was set it up to turn on a few of the LEDs inside that function and no, they did not come on. That would lead me to believe that I2C is not actively pulling info.
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: DAC on STM32F4 Discovery board...where am I going wrong

Post by Rick Burnett »

I just wanted to report that I switched to a different method of using I2C from this code example:

https://github.com/abhishek-kakkar/STM3 ... overy_demo

And everything worked. Given I was doing something different (synthesis and not wave file playing) I changed a lot of the code around, but just wanted to pass this example on to others wanting to use the DAC through I2C on the STM32F4 Discovery board.

Rick
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: DAC on STM32F4 Discovery board...where am I going wrong

Post by Giovanni »

You may want to post it in the contributions forum, it is where people search for solutions.

Giovanni
Post Reply