cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476 - SAI - I2S Master Receive - MEMS MIC - Clock Problem

ALEXANDRE FAJARDO
Associate
Posted on April 07, 2018 at 08:49

Hello,

I'm having trouble setting up the SAI peripheral clock on an STM32L476RET6 using Stm32CubeMX (Version 4.25.0).

I want to use the SAI2A as Master Receive on a MEMS MIC ICS43434 with I2S with FS = 48KHz. and SCK = 3.072MHz.

FS = MCLK / 256

So I put 12.288 in the field ''To SAI2 MHz'' and press enter but CubeMX suggests 12.235...

12.235 / 256 = 47.79KHz would be a bit out of the way I want. So I put 12.288 * 2 = 24.576 and CubeMX suggests 24,571 which would theoretically give me a FS of 47.99 ... KHz which I think is good but when compiling on Keil (V5.24.2.0) and measure with my oscilloscope:

FS = 58.7KHz insted 47.99 KHz

SCK = 3.74MHz insted 3.07MHz

MCLK = 15Mhz insted 12.285MHz

the microphone responds okay but obviously with an incorrect sampling rate for my application.

I also configured another pin so that I could measure the clock generated by the HSI and measure 16Mhz as well as expected.

on my board I have a crystal of 16MHz but when using it I have the same results as when I use HSI.

I tried varying ''RCC_OscInitStruct.HSICalibrationValue'' but only got +-3KHz approximately.

Anyone have any idea what I might be doing wrong?

0690X0000060ASnQAM.png0690X0000060ASsQAM.png

Here is the code:

/* Includes ------------------------------------------------------------------*/

#include ''main.h''

#include ''stm32l4xx_hal.h''

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

SAI_HandleTypeDef hsai_BlockA2;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_SAI2_Init(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

* @brief The application entry point.

*

* @retval None

*/

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_SAI2_Init();

/* USER CODE BEGIN 2 */

__HAL_SAI_ENABLE(&hsai_BlockA2); 

/* 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;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

RCC_PeriphCLKInitTypeDef PeriphClkInit;

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = 16;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

RCC_OscInitStruct.PLL.PLLN = 43;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;

RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;

RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV4;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

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_2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SAI2;

PeriphClkInit.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLL;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1);

/**Configure the main internal regulator output voltage

*/

if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Configure the Systick interrupt time

*/

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick

*/

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* SAI2 init function */

static void MX_SAI2_Init(void)

{

hsai_BlockA2.Instance = SAI2_Block_A;

hsai_BlockA2.Init.AudioMode = SAI_MODEMASTER_RX;

hsai_BlockA2.Init.Synchro = SAI_ASYNCHRONOUS;

hsai_BlockA2.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;

hsai_BlockA2.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;

hsai_BlockA2.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_EMPTY;

hsai_BlockA2.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_48K;

hsai_BlockA2.Init.SynchroExt = SAI_SYNCEXT_DISABLE;

hsai_BlockA2.Init.MonoStereoMode = SAI_STEREOMODE;

hsai_BlockA2.Init.CompandingMode = SAI_NOCOMPANDING;

if (HAL_SAI_InitProtocol(&hsai_BlockA2, SAI_I2S_STANDARD, SAI_PROTOCOL_DATASIZE_24BIT, 2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

/** Configure pins

PA8 ------> RCC_MCO

*/

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOB_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

/*Configure GPIO pin : PA8 */

GPIO_InitStruct.Pin = GPIO_PIN_8;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

GPIO_InitStruct.Alternate = GPIO_AF0_MCO;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

* @brief This function is executed in case of error occurrence.

* @param file: The file name as string.

* @param line: The line in file as a number.

* @retval None

*/

void _Error_Handler(char *file, int line)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

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,

tex: printf(''Wrong parameters value: file %s on line %d\r\n'', file, line) */

/* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ALEXANDRE FAJARDO
Associate
Posted on April 08, 2018 at 08:39

Hello!

today I successfully achieved Fs = 48KHz through the following clock setting ...

0690X00000604SsQAI.jpg

I tested the STM32L476, STM32L433 and STM32L432 microcontrollers with the same configuration and they all worked.

Strange, I'm using the same value (24.571429 MHz), but it comes from MSI instead of HSI. Should not it work well with HSI? I do not know but I feel better already :))

View solution in original post

4 REPLIES 4
ALEXANDRE FAJARDO
Associate
Posted on April 08, 2018 at 08:39

Hello!

today I successfully achieved Fs = 48KHz through the following clock setting ...

0690X00000604SsQAI.jpg

I tested the STM32L476, STM32L433 and STM32L432 microcontrollers with the same configuration and they all worked.

Strange, I'm using the same value (24.571429 MHz), but it comes from MSI instead of HSI. Should not it work well with HSI? I do not know but I feel better already :))

ajay.sharma
Associate II

Hi ,

Were you able to achieve this?

Need some help on the data interpretation.

How can i verify the data received from mic?

I have master clock set at 2.8Mhz and sampling rate at 44kHz (measured using CRO).

I have the select pin grounded but i think this should not be a problem because it will be just for left / right channel mono.

I have attached my code here.

I am blocked here I am getting data but i think it is not correct as the variation in data is only in 8 bits.

ajay.sharma
Associate II

I am using SPH0645LM4H-B mic sensor

Amel NASRI
ST Employee

Hi @ajay.sharma​ ,

I recommend you to create a new question where you deeply describe your case, what you tried, what you are facing as issue...

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.