AnsweredAssumed Answered

Clock settings in CubeMX 4.6 doesn't work for F072

Question asked by we.jesper on Mar 22, 2015
Latest reply on Mar 30, 2015 by Amel N
I am using CubeMX 4.6.0 with STM32Cube FW 1.2.1 for a STM32F072 chip.

I am having trouble getting the clock settings code to work properly.
I have activated some ADC features in CubeMX, which turns on the HSI14 clock. This is fine. But it mixes HSI14 with the system clock in a strange way.

If I start with selecting HSI as the clock source in CubeMX, the following code is generated:

void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.HSI14CalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
 
  __SYSCFG_CLK_ENABLE();
}

This code fails at the HAL_RCC_OscConfig() call, the CPU dies and has to be full-chip erased to start communicating again.

If I remove the RCC_OSCILLATORTYPE_HSI14 argument to OscillatorType, the problem disappears and the code works just fine. So I suspect this is an error in the CubeMX generator???

Now if I select HSI48 as the System Clock in CubeMX, the following code is generated:

void SystemClock_Config(void) {
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI14|RCC_OSCILLATORTYPE_HSI48;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  RCC_OscInitStruct.HSI14CalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
 
  __SYSCFG_CLK_ENABLE();
}

Again I remove RCC_OSCILLATORTYPE_HSI14 from OscillatorType, but now this doesn't help, the CPU still fails at the HAL_RCC_OscConfig() call. So now should I suspect a firmware problem???

The specific code which fails in the 48MHz case is the __HAL_RCC_HSI48_ENABLE() macro in stm32f0xx_hal_rcc_ex.c:498 in HAL_RCC_OscConfig(). I can single step up to this point, but when uit is run the CPU loses contact with the debugger.

Outcomes