AnsweredAssumed Answered

How to properly change I2S clock.

Question asked by garberoglio.leon.001 on Oct 6, 2016
Latest reply on Oct 18, 2016 by ForumSTM32.ForumSTM3
Hi, I'm using a STM32F411 on a product that uses I2S to stream some sound to an audio codec.
I use this function to configure oscillator:
void SystemClock_Config(void)
{
 
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
 
    __PWR_CLK_ENABLE();
 
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 6;
    RCC_OscInitStruct.PLL.PLLN = 168;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
    RCC_OscInitStruct.PLL.PLLQ = 7;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
 
    //22050 Hz original del final_09_DPOAE_05_Apexar
//  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
//  PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
//  PeriphClkInitStruct.PLLI2S.PLLI2SM = 12;
//  PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
//  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
 
    //44100Hz
//  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
//  PeriphClkInitStruct.PLLI2S.PLLI2SN = 271;
//  PeriphClkInitStruct.PLLI2S.PLLI2SM = 12;
//  PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
//  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
    /*
     * Esta configuracion es para tener buen % trabajando a 22KHz
     */
//  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
//  PeriphClkInitStruct.PLLI2S.PLLI2SN = 429;
//  PeriphClkInitStruct.PLLI2S.PLLI2SM = 12;
//  PeriphClkInitStruct.PLLI2S.PLLI2SR = 4;
//  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
 
    /*
     * Esta configuracion es para tener buen % trabajando a 96KHz
     */
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
    PeriphClkInitStruct.PLLI2S.PLLI2SN = 344;
    PeriphClkInitStruct.PLLI2S.PLLI2SM = 12;
    PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
    HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
 
    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
    /* SysTick_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

As you can see I have 4 diferent blocks on I2S clock. I need to use 2 of them on my firmware.
I need to know how can I change just I2S clock frecuency. Could I write just this:

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
PeriphClkInitStruct.PLLI2S.PLLI2SN = 344;
PeriphClkInitStruct.PLLI2S.PLLI2SM = 12;
PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

on any part of my code and it will change I2S clock?

Thank

Outcomes