Associate III
May 13, 2020
Solved
Problem on I2S output using HAL
- May 13, 2020
- 5 replies
- 3469 views
Hi,
I wrote a simplest program (below) to send sine wave data to PCM5102A (DAC) via I2S.
However, I found only L-ch data is valid and R-ch has no data.
Analogue (DAC) output of L-ch is somewhat sine wave, but it is sometimes de-formed. And if I look it closer, I found some noise on the signal. (below captured screen shot)
I use HAL and STM32F401. And I2S settings are half-duplex master transmit (only).
Kindly enlighten me what is wrong with my program.
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_I2S2_Init();
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET); // LED Off
int16_t Wave_Data[n_data * 2];
for (int i = 0; i < n_data; i++) {
Wave_Data[i * 2] = (int16_t) (sin(2. * M_PI * 8. * i / 1000.) * 50) ; // L-ch
Wave_Data[i * 2 + 1] = (int16_t) (sin(2. * M_PI * 10. * i / 1000.) * 50) ; // R-ch
}
while (1) {
if (Wave_buf_empty == 1) {
while ((&hi2s2)->State != HAL_I2S_STATE_READY)
;
if (HAL_I2S_Transmit_DMA(&hi2s2, (uint16_t*) Wave_Data,
(uint16_t) n_data * 2) != HAL_OK) {
Error_Handler();
}
Wave_buf_empty = 0 ;
}
}
}
//////////// Call Back ///////////////
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) {
Wave_buf_empty = 1;
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); // check if DMA is working
}Screen capture of USB scope.
