Author Topic: STM32 + CS4344 I2S audio DAC  (Read 786 times)

0 Members and 2 Guests are viewing this topic.

Offline gab_22Topic starter

  • Contributor
  • Posts: 10
  • Country: fr
STM32 + CS4344 I2S audio DAC
« on: January 10, 2024, 09:31:18 pm »
Hi everyone!

I'm trying to understand how to use the I2S feature of STM32 cores, in particular, on the STM32F303RE and a low-cost CS4344-based dac board (this one eBay auction: #174915064977 ).

My first goal is to play a sine wave through I2S fed by the DMA channel.

Here, in the attachment, are the configurations for the I2S module and the DMA

And here, is my code

Code: [Select]
  /* USER CODE BEGIN 2 */
  HAL_I2S_Init(&hi2s2);
  HAL_I2S_Transmit_DMA(&hi2s2, (uint16_t*)sine2496, 512);
  /* USER CODE END 2 */

Code: [Select]
/* USER CODE BEGIN 0 */
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
{
HAL_I2S_Transmit_DMA(&hi2s2, (uint16_t*)sine2496, 512);
}
/* USER CODE END 0 */

Code: [Select]
const uint16_t sine2496[] = {

    16383,
    16383,
    16785,
    16785,
    17187,
    17187,
                    ....

The samples of the sine table (despite its name it's a 16bits/48kHz sampling rate computed table) are duplicated to have the same signal on both stereo channels.

But the sound at the output is not a sine wave. Fortunately, I have a logical analyzer and I sampled that :

Marker 4: Frame one (see the table above) expected value on both channels
Marker 6: Unexpected values. I expected 16785 on the left and right channels.
Marker 7: Correct values but wrong place (expected at marker 6)
Marker 8: Unexpected values. I expected 17187 on the left and right channels.
Marker 9: Correct values but wrong place (expected at marker 8 )
etc

I don't understand why I have these unexpected values instead of the correct values.

The I2S signal frequency seems ok, I just need to check the MCLK with an oscilloscope because of the measured min/max/mean frequencies on the screenshot.

Do you have any ideas?

Thank you



 

Offline mskeete

  • Contributor
  • Posts: 33
  • Country: gb
Re: STM32 + CS4344 I2S audio DAC
« Reply #1 on: January 11, 2024, 04:33:29 pm »
Have you tried setting the data width to "half word" instead of byte?
 
The following users thanked this post: gab_22

Offline gab_22Topic starter

  • Contributor
  • Posts: 10
  • Country: fr
Re: STM32 + CS4344 I2S audio DAC
« Reply #2 on: January 11, 2024, 08:02:01 pm »
Yes, and also with "word" setting. But the output is still not a sine wave. :(
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5929
  • Country: es
Re: STM32 + CS4344 I2S audio DAC
« Reply #3 on: January 12, 2024, 12:32:56 am »
DMA configuration is wrong, width must be half-word, both src and dst.
Also once the DMA ends, no more data is streamed through i2s, so you'll get "silence" chunks between each reloading, distorting the output.
Set DMA to circular mode and it'll keep going forever without needing that ISR (Remove HAL_I2S_TxCpltCallback), the DMA will reset itself to the beginning of the buffer when done.

Remember HAL_I2S_Transmit_DMA size is number of dma transfers, not bytes.
With DMA in half-word, sine2496 must have 512 samples here:
HAL_I2S_Transmit_DMA(&hi2s2, (uint16_t*)sine2496, 512);

If playing a signal, use half-transfer and transfer complete ISRs to update each half of the buffer, this way you' won't run into buffer underrun problems, if you get clipping noises the buffer might be too small, causing underrun.
I played with this during the pandemic, check:
https://github.com/deividAlfa/STM32F411-Black-pill-USB-WAV-MP3-player
« Last Edit: January 12, 2024, 01:05:05 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: gab_22


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf