Page 1 of 1

SPI configuration for SPDiF, STM32F4

Posted: Tue Dec 25, 2012 10:29 pm
by cruz_91
Hi everyone,
I'm new here. I choose Chibios for my university project, because I want to use SDIO. I was very excited when file operations started working:) In future I want to write driver for chibios for nokia 3510i color LCD.

In my project I want to use SPDiF interface ( http://en.wikipedia.org/wiki/S/PDIF ) to send music to amplifier. I though about SPI to blink led, because it fits good.
In simple terms 16bit sample is convert into 32bit BMC ( http://en.wikipedia.org/wiki/Biphase_mark_code ) code and now this code can be divide into 2 parts and sends via SPI (uses 16bit transfer configuration)

now is my question. I need to send samples with some sampling frequency (48kHz or 44,1kHz) and I don't know what kind of mechanism I can use to send data with stable frequency. I though about timer interrupt or use sleep in thread but maybe exists better solutions.

I must send data with rate ~2,8224Mbps in 44.1kHz sampling rate

Re: SPI configuration for SPDiF, STM32F4

Posted: Wed Dec 26, 2012 9:23 am
by Giovanni
Hi,

You can use the GPT driver to generate callbacks with that frequency (or close enough) then start a SPI transfer from the callback.

Giovanni

Re: SPI configuration for SPDiF, STM32F4

Posted: Wed Dec 26, 2012 11:09 am
by cruz_91
Hi,
During breakfast i've though about I2S. What do you think about it? I read that I2S is still under dev, but maybe I can use interrupt from this source? (if only I2S generate interrupt from clock ticks),
Today if i'll find some time i'll try GPT.
thanks!

Re: SPI configuration for SPDiF, STM32F4

Posted: Wed Dec 26, 2012 11:18 am
by Giovanni
Well, you could use an I2S to generate interrupts at the right frequency but I never tried it.

Giovanni

Re: SPI configuration for SPDiF, STM32F4

Posted: Thu Dec 27, 2012 10:51 pm
by cruz_91
I've tried to use GPT with SPI but everytime my soft jail in SPI function...
I don't know what's wrong...
but good news is, 3510i lcd works:)

in mcuconf.h I set to true values:

Code: Select all

#define STM32_GPT_USE_TIM1                  TRUE
#define STM32_GPT_USE_TIM1                  TRUE


in halconf.h

Code: Select all

#define HAL_USE_GPT                 TRUE



spdif spi function

Code: Select all

const uint8_t samples[4]={0xff,0xf0,0x0,0xff};
void spdifloop()
{
      spiSelectI(&SPID2);
      spiSend(&SPID2, 4, samples);
      spiUnselect(&SPID2);
}



config and callback

Code: Select all

/*
 * GPT1  callback.
 */
static void gpt1cb(GPTDriver *gptp)
{

    palSetPad(GPIOD, GPIOD_LED3);
    //spdifloop();

    palClearPad(GPIOD, GPIOD_LED3);
  }


/*GPT configuration*/
static GPTConfig gpt1cfg =
{
  200000,    /* timer clock.*/
  gpt1cb        /* Timer callback.*/
};


And init timer in main

Code: Select all

   /**
    * Init timer
    */
   gptStart(&GPTD1, &gpt1cfg);
   gptStartContinuous(&GPTD1, 10);


if I uncomment spdifloop() diode (GPIOD_LED3) lights and soft stop at spiSelect.

here my spi configuration:

Code: Select all

static const SPIConfig spi2cfg = {NULL,
/* HW dependent part.*/
GPIOB, 12, SPI_CR1_DFF };


and main code

Code: Select all

/*
    * Init SPI
    */
   spiStart(&SPID2, &spi2cfg);

   palSetPadMode(GPIOB, 12, PAL_MODE_ALTERNATE(5) |
         PAL_STM32_OSPEED_HIGHEST); /* NSS.     */
   palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) |
         PAL_STM32_OSPEED_HIGHEST); /* MOSI.    */


in function like this calling in shell everything is allright and spdif diode sends bytes:

Code: Select all

void cmd_spitest(BaseSequentialStream *chp, int argc, char *argv[]) {
   (void) argc;
   (void) argv;
   uint32_t i = 0;
   chprintf(chp, "Trying to send data via SPI... ");
   chThdSleepMilliseconds(100);
   for(i=0;i<1000000;++i)
   {
   spdifloop();}
   chprintf(chp, "sent\r\n");

}


I use chibios 2.5.2 with STM32F4 Discovery board

Thanks for your help
Karol cruz_91

Re: SPI configuration for SPDiF, STM32F4

Posted: Fri Dec 28, 2012 8:44 am
by Giovanni
Hi,

From callbacks you can ONLY call I-Class functions, spiSend() is not, it is a synchronous function that suspends until the transfer is complete, from a callback (called from an ISR) you cannot suspend.

You should use spiStartSendI() which starts an asynchronous transfer and terminates with a callback.

in the F4-Discovery demo there is an example of an SPI transfer started from a callback.

Giovanni