AnsweredAssumed Answered

Cube MX Timer init Error on STM32F050R8

Question asked by Leonardo Volponi on Apr 4, 2017
Latest reply on Apr 4, 2017 by Khouloud G

Using the attached cube project TestSTM32F051R8_TimError.ioc

 

When I generate the source codes the CubeMx version  4.20.0 produces the following initialization code for the timers (I used only TIM1 and TIM3) but the error is exactly the same for both of them.

 

 

..............

void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;

htim1.Instance = TIM1;
htim1.Init.Prescaler = 4800;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;


htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;  <--- Error this field is not defined in the structure and also the TIM_AUTORELOAD_PRELOAD_ENABLE is not defined anywhere in the HAL libraries.

 


if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}

.....................

 

 

Just a suggestion for the CubeMX development team:

It sould be a good idea to accept user's code just before calling the HAL_ functions, I mean something like this:

 

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;

 

/* USER CODE BEGIN HAL_RCC_OscConfig */     <-- Begin of the added User code section

   The user can write its own code here and can be saved between code regenerations....     

 

   This way can be avoided the problem to adjust everytime the generated code in the actual version of the CubeMx that 

   sometimes (in my case always) fail to create initialization code for the clock system for the STM32F407 with external  crystal oscillator.

 

/* USER CODE END HAL_RCC_OscConfig */         <-- End of the added User code section


if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

 

other example  should be:

 

htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;

 

/* USER CODE BEGIN HAL_TIM_Base_Init( &htim1) */   <-- Begin of the added User code section

 

    User code can be written here..... and the CubeMX will keep it between code regeneration

    and this will permit to create particular initialization code not directly available from the various CubeMx options.

    just for example I can add a statement like this: htim1.Init.ClockDivision = VariableClockDivisionValue;

       

/* USER CODE END HAL_TIM_Base_Init( &htim1) */       <-- End of the added User code section


if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}

 

 

Thank you very much for this forum !

 

Best regards to all !

Leonardo.

Outcomes