2023-10-08 04:46 AM
Hi @ST Community ,
I struggle to configure the clock speed to the frequency I desire.
The chip I am using is STM32C011F6U and the code I use as first function in main() is the following:
uint16_t cr = RCC->CR;
uint16_t cal = RCC->ICSCR;
uint16_t cfgr = RCC->CFGR;
//Reset Clock divider to 1 (full 48MHz)
cr &= ~(RCC_CR_HSIDIV_0 | RCC_CR_HSIDIV_1 | RCC_CR_HSIDIV_2);
cr |= (1U << 11);
cr |= (1U << 12);
cr |= (1U << 13);
RCC->CR = cr;
//Enable HSI48 Clock
RCC->CR |= RCC_CR_HSION;
//Wait for HSI to be ready
while((RCC->CR &= RCC_CR_HSIRDY) == 0u);
//Default HSI calibration trimming value
cal &= ~RCC_ICSCR_HSITRIM;
cal |= RCC_ICSCR_HSITRIM_6;
RCC->ICSCR = cal;
//Set APB divider to 8
cfgr &= ~RCC_CFGR_PPRE;
cfgr |= (RCC_CFGR_PPRE_2 | RCC_CFGR_PPRE_1);
RCC->CFGR = cfgr;
//Set AHB divider to 1
cfgr &= ~RCC_CFGR_HPRE;
RCC->CFGR = cfgr;
However the above doesn't seem to have any effect and the clock frequency never change. I have also tried to output the SYSCLK using the MCO pins.
What am I doing wrong or missing?
Thanks!
Solved! Go to Solution.
2023-10-08 04:57 AM
make a project with CubeMX , set clock tree as you want it, generate code -> and look there, what you should write
2023-10-08 04:57 AM
make a project with CubeMX , set clock tree as you want it, generate code -> and look there, what you should write
2023-10-10 07:49 PM
the normal clock configuration is as below in our example and CubeMX, you also can get the official example project in ST official website.
/** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** 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.HSIDiv = RCC_HSI_DIV1; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; 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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { Error_Handler(); } }