2019-09-06 04:27 AM
Hello,
i read the ST instructions and looked in the EVAL Board example. There they used a audio jack via SPI but in want to play the file direct on DAC.
First to check if the SD Card works i write and read a file to it (works). Then I call the "play_sound" Funktion.
My problems to solve:
Settings of SD Card and DACs
void play_sound(void)
{
uint32_t WaveDataLength=0;
/* Get Data from SD */
res=f_open(&myFile, "open_sound_mono.wav", FA_READ); // Don't work. SD Card is already mounted and TXT File write and read worked properly
res=f_lseek(&myFile, f_size(&myFile));
res=f_read (&myFile, buffer1, _MAX_SS, &BytesRead);
res=f_read (&myFile, buffer2, _MAX_SS, &BytesRead);
res=f_close(&myFile);
/* Start playing wave */
// Play buffer1 on DAC0 HOWTO ???
buffer_switch = 1;
while((WaveDataLength != 0) )
{
/* wait for DMA transfert complete */
if(buffer_switch == 0)
{
/* Play data from buffer1 */
// Play buffer1 on DAC0 HOWTO ???
/* Store data in buffer2 */
f_read (&myFile , buffer2, _MAX_SS, &BytesRead);
buffer_switch = 1;
}
else
{
/* Play data from buffer2 */
// Play buffer2 on DAC0 HOWTO ???
/* Store data in buffer1 */
f_read (&myFile, buffer1, _MAX_SS, &BytesRead);
buffer_switch = 0;
}
}
// Wiedergabe Stoppen
// WavePlayerStop ???
WaveDataLength = 0;
}
void DAC_setup()
{
HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
//HAL_DAC_Start_DMA(&hdac, DAC1_CHANNEL_1 , pData, Length, Alignment)
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)AUDIO_SAMPLE, 32, DAC_ALIGN_12B_R); //Test with AUDIO_SAMPLE from EVAL Board sample code
}
2019-09-06 04:36 AM
Going to be hard to read any data beyond the end of the file.
2019-09-06 05:05 AM
I gues you mean I have to read the file step by step 512byts to the end?
2019-09-06 05:44 AM
I solved the problem of the output
HAL_TIM_Base_Start(&htim6);
HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)AUDIO_SAMPLE, 32, DAC_ALIGN_12B_R);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_2, (uint32_t*)sine_wave_array, 32, DAC_ALIGN_12B_R);
now I only need to replace the array with the data of the wave file. Do you have a nice idee for this?
2019-09-06 06:01 AM
Better when i close my file at the end :D
2019-09-06 08:29 AM
FATFS *fs; /* Ponter to the filesystem object */
fs = malloc(sizeof (FATFS));
res=f_mount(fs ,"", 1);
if(res!=FR_OK)
{
__NOP();
}
res = f_open(&myFile, "close_sound_mono.wav", FA_OPEN_EXISTING | FA_READ); --> Why this does not work
if(res!=FR_OK)
__NOP();
res=f_open(&myFile, "test.txt", FA_WRITE | FA_CREATE_ALWAYS); --> this work
if(res!=FR_OK)
{
__NOP();
}
2019-09-06 09:02 AM
now I can open my wave file. And my function looks like that. ;)
Problem now. DAC only keeps the first value.
void play_sound(void)
{
res = f_open(&myAudioFile, "SOUND.WAV", FA_READ); //FA_OPEN_EXISTING |
if(res!=FR_OK)
__NOP();
WaveDataLength=f_size(&myAudioFile);
while((WaveDataLength != 0) )
{
// wait for DMA transfert complete
if(buffer_switch == 0)
{
// Play data from buffer1
// Play buffer1 on DAC0 HOWTO ???
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)buffer1, 32, DAC_ALIGN_12B_R);
f_lseek(&myAudioFile, f_tell(&myAudioFile)+_MAX_SS);//move 4096 bits
// Store data in buffer2
f_read (&myAudioFile , buffer2, _MAX_SS, &BytesRead);
WaveDataLength=WaveDataLength-_MAX_SS;
buffer_switch = 1;
}
else
{
// Play data from buffer2
// Play buffer2 on DAC0 HOWTO ???
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)buffer2, 32, DAC_ALIGN_12B_R);
f_lseek(&myAudioFile, f_tell(&myAudioFile)+_MAX_SS);//move 4096 bits
// Store data in buffer1
f_read (&myAudioFile, buffer1, _MAX_SS, &BytesRead);
WaveDataLength=WaveDataLength-_MAX_SS;
buffer_switch = 0;
}
}
WaveDataLength = 0;
__NOP();
res = f_close(&myAudioFile);
}
2019-09-06 09:21 AM
Okay File is loaded step by step to DAC but no proper sound, may the speed is wrong?
CPU Frq = 72MHz
static void MX_TIM6_Init(void) // Trigger for DAC DMA
{
/* USER CODE BEGIN TIM6_Init 0 */
/* USER CODE END TIM6_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 0;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 32768;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM6_Init 2 */
/* USER CODE END TIM6_Init 2 */
}