STM32F4 Discovery - How to set up SPI3 for I2S

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

Moderators: RoccoMarco, lbednarz, tfAteba

watz
Posts: 8
Joined: Mon Mar 12, 2012 9:19 pm

STM32F4 Discovery - How to set up SPI3 for I2S

Post by watz »

Hi :-)

I'm quite new to STM32 and ChibiOS and trying to hack that board to play MP3s from SD. I can sucessfully read and decode MP3s from an SD Card attached to SPI1. Because streaming the PCM data to the onboard DAC is not as trivial as I thought, I simply added all absolutely necessary party of STs "Audio_playback_and_record" example, hoping they will not conflict with ChibiOS too much.

Additionally, I changed the following lines in mcuconf.h:

#define STM32_I2SSRC STM32_I2SSRC_PLLI2S
#define STM32_PLLI2SN_VALUE 258
#define STM32_PLLI2SR_VALUE 3

After that, I actually get audio noise from the speakers if I continously write PCM data to the DAC using STs EVAL_AUDIO_Play() method.
The most obiously problem left now is: How can I configure SPI3 to I2S mode in ChibiOS to actually get the ChibiOS IRQ handler for SPI3 called when the its outgoing DMA buffer gets empty? The IRQ handler in the ST code ("SPI3_IRQHandler") is obiously not called since that part of the ST library is not initialized (I don't think that would be a good idea). I believe I somehow would have to let ChibiOS initialize SPI3 using spiStart().

Edit: OK it looks like a DMA IRQ handler is set up by the SD code, but also not called. I should be able to setup this in ChibiOS somehow....need to look at some examples.

Has anyone any hints or maybe even example code for using the STM32 I2S interface with ChibiOS ?

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

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by Giovanni »

Hi Watz,

The plan is to add a new DAC driver supporting I2S and internal DACs during the the 2.5.x development branch, you can find the topics in the development forum.

I think it would be possible to hack the current SPI driver to do I2S but I never tried it. You would not have to serve the interrupt yourself, the driver would invoke a callback when the transmit buffer is empty. The main problem I see is that peripheral initialization would be a bit different because the I2S uses a different clock source if I remember well, there is also an I2S-specific register that the SPI driver does not touch.

Giovanni
watz
Posts: 8
Joined: Mon Mar 12, 2012 9:19 pm

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by watz »

Good news.

About the hacking....I do believe I need to change the SPI3 setup for I2S and extend spi_lld_serve_tx_interrupt() for checking half/full flags and calling back the user code just like adc_lld_serve_rx_interrupt(), which seems to do the same thing on receiving data. Right ?

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

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by Giovanni »

Correct, the DAC/I2S would work as a reverse ADC, a circular buffer with a callback invoked for half/full transmission, this is how I planned to do it.

BTW, I am working on the "official" DAC driver abstraction, the first code will be committed soon on the repository.

Giovanni
watz
Posts: 8
Joined: Mon Mar 12, 2012 9:19 pm

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by watz »

I'm looking forward to some official DAC interface in ChibiOS :-)
I just noticed that, in my case, spi_lld_serve_tx_interrupt() is already called for SPI3 with flags=HTIF0 | TCIF0 | FEIF0 (half transfer + transfer complete + fifo underrun). Seems like a good start to me.
watz
Posts: 8
Joined: Mon Mar 12, 2012 9:19 pm

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by watz »

Woohooo! I can't believe it....implemented the TC IRQ handling in spi_lld_serve_tx_interrupt() using my own double buffer algo (DMA circle not use yet) and it f***ing worked on the first run :D
MP3s sound really good but only half as fast as they should...I guess I2S is using the wrong sample rate somewhere.

But hey, it works :lol: :lol: :lol: :lol: :lol: :lol:
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by Giovanni »

Good work :-)

Giovanni
watz
Posts: 8
Joined: Mon Mar 12, 2012 9:19 pm

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by watz »

Found it! The reason why it would play back too slow is this little bug in hal_lld.c:

#if STM32_ACTIVATE_PLLI2S
/* PLLI2S activation.*/
// RCC->PLLI2SCFGR = STM32_PLLI2SR_VALUE | STM32_PLLI2SN_VALUE;
RCC->PLLI2SCFGR = STM32_PLLI2SR | STM32_PLLI2SN;
RCC->CR |= RCC_CR_PLLI2SON;
while (!(RCC->CR & RCC_CR_PLLI2SRDY))
; /* Waits until PLLI2S is stable. */
#endif

This bug actually messed the i2s clock calculation in the ST lib. It actually does a division by zero....which actually doesn't throw an exception or similar but simply makes the result of the calculation zero.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by Giovanni »

Hi,

Thanks for finding, I'll open a ticket about this.

Giovanni
watz
Posts: 8
Joined: Mon Mar 12, 2012 9:19 pm

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Post by watz »

Now the fun part begins...cleaning up the hacking mess, removing as much from the ST lib as possible...

I wonder....would that DAC abstraction layer also do the I2S initialization work (= all in one solution) ?
Post Reply