cancel
Showing results for 
Search instead for 
Did you mean: 

I am trying to run a BLDC motor with TIM1 on STM32F401RE. I want to use a software commutation event with HAL drivers as is done in the "6step" examples. So far the call back is not working. Please tell me what I am missing. Thanks.

BPetr.1
Associate II

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 //TimHandle.Instance = TIM1;

 HAL_TIMEx_ConfigCommutEvent_IT(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

 /* USER CODE END Init */

/////////////////////////////////////////////////////////////////////////////////////////

/* USER CODE BEGIN 4 */

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim){

  if(htim->Instance == (TIM2)){

    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_14);

    HAL_TIM_GenerateEvent(&htim1, TIM_EventSource_COM);

    //count++;

    //BRG_switch();  //<-this call is working

    }

  //if(htim->Instance == (TIM1)){pwm++;}

}

void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim1){

  count++;

  BRG_switch();    //<-this call is not working!

}

1 ACCEPTED SOLUTION

Accepted Solutions
BPetr.1
Associate II

YEP, sorry, I found the answer. The

 HAL_TIMEx_ConfigCommutEvent_IT(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

HAS TO BE AFTER MX_TIM1_Init();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_TIM1_Init();

 MX_ADC1_Init();

 MX_TIM2_Init();

 /* USER CODE BEGIN 2 */

 //TimHandle.Instance = TIM1;

 HAL_TIM_Base_Start_IT(&htim2);

 HAL_TIM_Base_Start_IT(&htim1);

 HAL_TIM_Base_MspInit(&TimHandle);

 HAL_TIMEx_ConfigCommutEvent_IT(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

  // TIM1->DIER|=TIM_DIER_UIE;

  //TIM1->DIER|=TIM_DIER_COMIE;

  //__HAL_TIM_ENABLE_IT(&htim1, TIM_IT_COM);

View solution in original post

1 REPLY 1
BPetr.1
Associate II

YEP, sorry, I found the answer. The

 HAL_TIMEx_ConfigCommutEvent_IT(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

HAS TO BE AFTER MX_TIM1_Init();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_TIM1_Init();

 MX_ADC1_Init();

 MX_TIM2_Init();

 /* USER CODE BEGIN 2 */

 //TimHandle.Instance = TIM1;

 HAL_TIM_Base_Start_IT(&htim2);

 HAL_TIM_Base_Start_IT(&htim1);

 HAL_TIM_Base_MspInit(&TimHandle);

 HAL_TIMEx_ConfigCommutEvent_IT(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

  // TIM1->DIER|=TIM_DIER_UIE;

  //TIM1->DIER|=TIM_DIER_COMIE;

  //__HAL_TIM_ENABLE_IT(&htim1, TIM_IT_COM);