I'm using the STM32F3Discovery board and codec CS5343 to implement this project. It's almost completed, but the output is not smooth. It is looking like a step deformation.
Example: 100 Hz sine wave (the result of processed, two's complement and shift one bit)
Try to use the logic analyzer to retrieve the I²S signal at the same time, but the result is smooth and pure. And different from the output of data of I²S DMA via CDC.
Why are the results different? I think the results for both should be the same.
Raw data: Left (retrieved by the logic analyzer). Right (output of USB CDC)
I'm trying to change the configuration of STM32 I²S, but the result is not different. The output signal also has a step formation.
File main.c
uint16_t SignalTmp[32] = {0x00};
uint8_t BufSize = 4;
uint32_t lSample = 0, rSample = 0;
uint8_t FLAG_half = 0, FLAG_comp = 0;
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_SPI1_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_I2S2_Init();
MX_UART4_Init();
MX_USART2_UART_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_Base_Start_IT(&htim4);
HAL_I2S_Receive_DMA(&hi2s2, (uint16_t *)&SignalTmp[0], BufSize);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (HAL_GPIO_ReadPin(BT_KEY_GPIO_Port, BT_KEY_Pin) == 1)
ButtonPressed = 1;
if (ButtonPressed)
{
if (!TransferFlag)
{
HAL_TIM_Base_Start_IT(&htim2);
HAL_GPIO_WritePin(LD7_GPIO_Port, LD7_Pin, GPIO_PIN_SET);
}
else
{
HAL_TIM_Base_Stop_IT(&htim2);
HAL_GPIO_WritePin(LD7_GPIO_Port, LD7_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
}
TransferFlag ^= 1;
ButtonPressed = 0;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
memcpy(&lSample, &SignalTmp[0], 4);
memcpy(&rSample, &SignalTmp[2], 4);
FLAG_half = 1; // Fill buffer half
}
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
{
memcpy(&lSample, &SignalTmp[4], 4);
memcpy(&rSample, &SignalTmp[6], 4);
FLAG_comp = 1;
}
File i2s.c
void MX_I2S2_Init(void)
{
hi2s2.Instance = SPI2;
hi2s2.Init.Mode = I2S_MODE_MASTER_RX;
hi2s2.Init.Standard = I2S_STANDARD_PHILIPS;
hi2s2.Init.DataFormat = I2S_DATAFORMAT_24B;
hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;
hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_48K;
hi2s2.Init.CPOL = I2S_CPOL_HIGH;
hi2s2.Init.ClockSource = I2S_CLOCK_SYSCLK;
hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;
if (HAL_I2S_Init(&hi2s2) != HAL_OK)
{
Error_Handler();
}
}
void HAL_I2S_MspInit(I2S_HandleTypeDef *i2sHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (i2sHandle->Instance == SPI2)
{
/* USER CODE BEGIN SPI2_MspInit 0 */
/* USER CODE END SPI2_MspInit 0 */
/* I2S2 clock enable */
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/** I2S2 GPIO Configuration
PB12 ------> I2S2_WS
PB13 ------> I2S2_CK
PB14 ------> I2S2_ext_SD
PB15 ------> I2S2_SD
PC6 ------> I2S2_MCK
*/
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI2;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* I2S2 DMA Init */
/* SPI2_RX Init */
hdma_spi2_rx.Instance = DMA1_Channel4;
hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_spi2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_spi2_rx.Init.Mode = DMA_CIRCULAR;
hdma_spi2_rx.Init.Priority = DMA_PRIORITY_HIGH;
if (HAL_DMA_Init(&hdma_spi2_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(i2sHandle, hdmarx, hdma_spi2_rx);
/* SPI2_TX Init */
hdma_spi2_tx.Instance = DMA1_Channel5;
hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_spi2_tx.Init.Mode = DMA_CIRCULAR;
hdma_spi2_tx.Init.Priority = DMA_PRIORITY_HIGH;
if (HAL_DMA_Init(&hdma_spi2_tx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(i2sHandle, hdmatx, hdma_spi2_tx);
/* I2S2 interrupt Init */
HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SPI2_IRQn);
/* USER CODE BEGIN SPI2_MspInit 1 */
/* USER CODE END SPI2_MspInit 1 */
}
}
File stm32f3xx_it.c
uint8_t ABuf[64] = {0x00};
uint8_t BBuf[64] = {0x00};
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
if (TX_Flag)
{
if (NextBuf)
CDC_Transmit_FS(&ABuf, 64);
else
CDC_Transmit_FS(&BBuf, 64);
TX_Flag = 0;
}
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
/* USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}
/**
* @brief This function handles TIM3 global interrupt.
*/
void TIM3_IRQHandler(void)
{
/* USER CODE BEGIN TIM3_IRQn 0 */
#if 1
#ifdef SIMULATOR
SignalAvg = GenerateSignal();
#else
if (!NextBuf)
{
memcpy(&ABuf[txidx * 4], &lSample, 4);
txidx++;
memcpy(&ABuf[txidx * 4], &rSample, 4);
txidx++;
}
else
{
memcpy(&BBuf[txidx * 4], &lSample, 4);
txidx++;
memcpy(&BBuf[txidx * 4], &rSample, 4);
txidx++;
}
if (txidx >= 16)
{
NextBuf ^= 1;
TX_Flag = 1;
txidx = 0;
}
#endif
#endif
/* USER CODE END TIM3_IRQn 0 */
HAL_TIM_IRQHandler(&htim3);
/* USER CODE BEGIN TIM3_IRQn 1 */
/* USER CODE END TIM3_IRQn 1 */
}
Link to completed code on GitHub
The result data folder includes three files.
- record_2022_07_19_05-32-45.txt --> the signal data of output of USB CDC, a point data use 4 bytes and the sequence is Left channel, Right channel, Left channel, Right channel...
- Logic_R-1kHzSin_L-GND.csv --> the retrieve signal data from the i2s interface via the logic analyzer.
- drawout7.m --> the data conversion for the output of USB CDC, transfer the data to value (two's complement and shift one bit)