Skip to main content
ADado.1
Associate
July 27, 2022
Question

STM32F401CCU PCM5102A Sample Rate deviation

  • July 27, 2022
  • 6 replies
  • 1529 views

Hi All, I'm using an STM32F401CCU with a TI PCM5102A DAC. I've set up a simple DMA (code attached) and a naïve square wave function. My issue is that the generated square wave is at the 'wrong' frequency. In the code I am generating a 440 hz (or multiple) square wave at a sample rate of 32khz (set in the I2S section of the IOC).

I am using an accumulator to either set a double variable to low or high whether it is above or below 0.5. This value is then multiplied by 32767 and then added to an entry in a signed 16 bit array.

I2S peripheral clock is at 128 MHZ. I seem to be getting however around 374Hz indicating perhaps that the time increment isn't correct (and then the sample rate is not) any help much appreciated, am new to this

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file : main.c
 * @brief : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2022 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "math.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
 
#define SAMPLE_RATE 32000
 
 
 
 
 
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
I2S_HandleTypeDef hi2s3;
DMA_HandleTypeDef hdma_spi3_tx;
 
/* USER CODE BEGIN PV */
int16_t splx[16];
 
 
int16_t sample;
 
double T;
double Ts = 0.00003125;
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2S3_Init(void);
static void MX_DMA_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
 * @brief The application entry point.
 * @retval int
 */
int main(void)
{
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */
 
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* USER CODE BEGIN Init */
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_I2S3_Init();
 
 /* USER CODE BEGIN 2 */
 
HAL_I2S_Transmit_DMA(&hi2s3, (uint16_t*)splx,8);
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 }
 
 
 /* USER CODE END 3 */
}
 
/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
 /** Macro to configure the PLL multiplication factor
 */
 __HAL_RCC_PLL_PLLM_CONFIG(8);
 /** Macro to configure the PLL clock source
 */
 __HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSI);
 /** Configure the main internal regulator output voltage
 */
 __HAL_RCC_PWR_CLK_ENABLE();
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 Error_Handler();
 }
}
 
/**
 * @brief I2S3 Initialization Function
 * @param None
 * @retval None
 */
static void MX_I2S3_Init(void)
{
 
 /* USER CODE BEGIN I2S3_Init 0 */
 
 /* USER CODE END I2S3_Init 0 */
 
 /* USER CODE BEGIN I2S3_Init 1 */
 
 /* USER CODE END I2S3_Init 1 */
 hi2s3.Instance = SPI3;
 hi2s3.Init.Mode = I2S_MODE_MASTER_TX;
 hi2s3.Init.Standard = I2S_STANDARD_PHILIPS;
 hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;
 hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
 hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_32K;
 hi2s3.Init.CPOL = I2S_CPOL_LOW;
 hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
 hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;
 if (HAL_I2S_Init(&hi2s3) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN I2S3_Init 2 */
 
 /* USER CODE END I2S3_Init 2 */
 
}
 
/**
 * Enable DMA controller clock
 */
static void MX_DMA_Init(void)
{
 
 /* DMA controller clock enable */
 __HAL_RCC_DMA1_CLK_ENABLE();
 
 /* DMA interrupt init */
 /* DMA1_Stream5_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
 
}
 
/**
 * @brief GPIO Initialization Function
 * @param None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
 
 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOC_CLK_ENABLE();
 __HAL_RCC_GPIOH_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 __HAL_RCC_GPIOB_CLK_ENABLE();
 
}
 
/* USER CODE BEGIN 4 */
int16_t outputBuffer(){
 
#define TWOPI 6.28318530718
 
 
 
double sampledouble = 0;
 
 
T = T + (Ts*440);
if (T >= 1.0){
 
	T = 0;
}
 
 
 
 
if (T < 0.5){
 
	sampledouble = -0.8;
}
 
 
if (T >= 0.5){
 
	sampledouble = 0.8;
}
 
 
 
//Normalise
 
 
 
sample = sampledouble * 32767;
return sample;
 
}
 
 
 
 
void HAL_I2S_TxHalfCpltCallback (I2S_HandleTypeDef * hi2s){
 
 
 
splx[0] = outputBuffer();
splx[1] = splx[0];
splx[2] = outputBuffer();
splx[3] = splx[2];
splx[4] = outputBuffer();
splx[5] = splx[4];
splx[6] = outputBuffer();
splx[7] = splx[6];
 
 
}
 
 
 
void HAL_I2S_TxCpltCallback (I2S_HandleTypeDef * hi2s){
 
 
 
	splx[8] = outputBuffer();
	splx[9] = splx[8];
	splx[10]= outputBuffer();
	splx[11]=splx[10];
	splx[12]=outputBuffer();
	splx[13]= splx[12];
	splx[14]= outputBuffer();
	splx[15]= splx[14];
 
 
 
 
 
 
 
 
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* USER CODE END 4 */
 
/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
 /* User can add his own implementation to report the HAL error return state */
 __disable_irq();
 while (1)
 {
 }
 /* USER CODE END Error_Handler_Debug */
}
 
#ifdef USE_FULL_ASSERT
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * @param file: pointer to the source file name
 * @param line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
 /* USER CODE BEGIN 6 */
 /* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
 

This topic has been closed for replies.

6 replies

Piranha
Principal III
July 27, 2022

Format the code sanely, edit out all the junk and post only the relevant code in forum.

The square wave period is: 32000 / 440 = 73 samples. Toggle the value +/-32767 for every 36 samples and that's it. Or a 36/37 samples to get closer to the exact frequency. It's a basic math!

ADado.1
ADado.1Author
Associate
July 27, 2022

Not really helpful. My math is fine, I've described what the problem is and it's not to do with the methodology around generating a square wave. Not sure if you're showing off or something

Piranha
Principal III
July 27, 2022

> I2S peripheral clock is at 128 MHZ.

Not possible with HSI as a SYSCLK source. And your MCU has a maximum frequency of 84 MHz. You are clicking CubeMX and that tool shows clocks with a plain numbers in GUI interface even for those, who don't read the documentation!

P.S. You do understand that Cortex-M4 has a single precision FPU and therefore your double type variables are calculated in software with a relatively heavy processing?

ADado.1
ADado.1Author
Associate
July 27, 2022

The PLL is generating it from the HSI

Piranha
Principal III
July 27, 2022

OK, indeed it seems to be possible. Anyway double-check if there are no some additional limitations for APB/I2S clock ratio.

And read and check the actual registers for all the clocks, especially the dividers, which are calculated from the I2S_AUDIOFREQ_32K setting. ST's code cannot be trusted on anything...