0

My initial project, generated on CubeMX was working on 32MHz. Since I am trying to implement an ADC with 80MHz I decided to increase the System Clock to 100MHz. The changes I made in the source are in the main.c:

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage 
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 12;
  RCC_OscInitStruct.PLL.PLLN = 50;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** 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_1) != HAL_OK)
  {
    Error_Handler();
  }


  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_USB
                              |RCC_PERIPHCLK_ADC;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSE;
  PeriphClkInit.PLLSAI1.PLLSAI1M = 6;
  PeriphClkInit.PLLSAI1.PLLSAI1N = 20;
  PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }


}

I just changed PLLM and PLLN to adjust to 100MHz. Now when I debug the program in the SysTick register I see that:

SYST_CSR = 7 SYST_RVR = 99999

So I think the ticks are correctly showing 100MHz. I am not sure how correct is that, but let's say for a simple line to execute:

i_flag=1;

it takes 7 cycles according to the SYST_CVR->CURRENT.

Considering some timing issues with the ADC, I suppose I did not set correctly the System clock and it is still 32MHz. Any ideas if my code and checks are correct?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
D. K.
  • 63
  • 1
  • 5
  • 2
    On most MCUs, you have to configure flash wait states when you go beyond ~50MHz. I don't see this in your code, but maybe it is handled by hardware or drivers? Are wait states needed on this part and who's responsible for setting them? – Lundin Aug 27 '20 at 06:36
  • 2
    (if you get wait states wrong, the code typically just goes haywire immediately though, the program hanging or going run-away) – Lundin Aug 27 '20 at 06:38
  • Thank you for your lead! I had no idea about it. Already set it to 4 WS since this is the value for my processor. Just inserted it in the end of SystemClock_Config(). I am still not sure if everything works correctly. Now the above command takes 10 cycles to run. I may restore the original 32MHz or will redo CubeMX with clean 100MHz setup. – D. K. Aug 28 '20 at 12:50
  • Unfortunately these bloatware libs from the vendor are mostly harmful. Instead I'd recommend to look for source code (at Github etc) which doesn't use the ST HAL but performs the clock setup manually. There might also be app notes from ST. If everything else fails, there's always the manual. – Lundin Aug 28 '20 at 12:57

0 Answers0