cancel
Showing results for 
Search instead for 
Did you mean: 

The STM32L476 seems stuck at the reset frequency of 4MHZ. How can I switch to use the HSE?

WWhit
Associate II

 I need to run off the HSE and I am not seeing any glaring errors in the STM32 cube generated code; however I am still at 4MHz.

------SNIP--------------------------

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_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 = 1;

      RCC_OscInitStruct.PLL.PLLN = 20;

      RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;

      RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;

      RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;

      HAL_RCC_OscConfig(&RCC_OscInitStruct);

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

     HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);

      PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2

                    |RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_SDMMC1

                    |RCC_PERIPHCLK_ADC;

      PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;

      PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

      PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;

      PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;

      PeriphClkInit.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_PLLSAI1;

      PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSE;

      PeriphClkInit.PLLSAI1.PLLSAI1M = 1;

      PeriphClkInit.PLLSAI1.PLLSAI1N = 8;

      PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;

      PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;

      PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;

      PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK|RCC_PLLSAI1_ADC1CLK;

     HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

       /**Configure the main internal regulator output voltage

       */

      HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

       /**Configure the Systick interrupt time

       */

      HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

       /**Configure the Systick

       */

      HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 }

----END SNIP

3 REPLIES 3

Perhaps the clock just isn't starting. You seem to be ignoring any failure indication returned by HAL_RCC_OscConfig or HAL_RCC_ClockConfig

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
WWhit
Associate II

Well there is no way to currently print out the error -- hence I was "stepping" through and it was returning HAL_OK. Once the system is up and running error checking will prove fruitful. Thanks

I would clear the local structures here.

Enable HSE, but don't use as the primary clock

Inspect RCC registers confirm it is HSE_RDY

Output the HSE clock to the MCO (PA8) pin, review that with a scope

Try running directly from the HSE, not HSE+PLL

Make sure HSE_VALUE matches the value of your clock. If XO source use BYPASS mode.

Use the SWV/SWO debug channel to output diagnostics

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..