Skip to main content
Associate
October 18, 2023
Question

TIMER Control

  • October 18, 2023
  • 8 replies
  • 5403 views

Hello

 

I am designing a controller that will drive a 3 phase BLDC 0.5kW motor. I choose STM32G474VET3 to generate the signaling with external gate driver and some beefy MOSFETs for that.

 

The MCU has advanced timer controller, and for my application i went with TIM20, ch1, ch2 and ch4. I see they can generate "PWM Generation CH CHN". How ever i dont see how to generate those with proper phase shift for a 3phase motor.

 

Can you please provide me some guide lines how to generate 3 phase pwm that is properly phase shifted. Which settings in the Cube i should look in to. If you have any app note i take a look it would be appreciated. 

 

Thank you 

 

Mirza

This topic has been closed for replies.

8 replies

Fred V.
ST Employee
October 19, 2023

Hello @Mirza-AEC@Mirza-AEC

Did you give a try to the ST Motor Control SDK? Even though it may not generate a code for the exact configuration you are designing, the generated code will show you how to generate three/six, properly phase-shifted signals to drive the phases of your motor.

Is it indeed what you are looking for: generating three sine voltage references (or SVPWM voltage references) spread 120° apart and turning them into PWM duty cycles and their complementaries to feed the six inputs of the drivers? 

Best Regards,

FredV

 

Mirza-AECAuthor
Associate
October 22, 2023

Hello

 

thank you very much for your suggestions. I have NUCLEO-IHM03 with NUCLEO- IHM16M1 and motor GBM2804H-100T. Using ST Motor Controller SDK and instructions i was able to generate the project and make my motor spin. I was very helpful and insightful. 

 

However i am looking for a 6-step commutation settings for TIMER control. Before i proceed with ordering my PCB i would like to know if a single TIMER (TIM20 in my case) using CH1, 2 and 4 can generate the needed signals for driving my motor. I also implemented HALL sensors as feedback and BEMF (voltage and current). I know that STM has also support for HALL sensor direct integration in TIMER control. Some code/video instructions would be much appreciated for this.

 

As i mentioned, i am a bit confused if a single TIMER can be used to generate 3 aligned/synced channels for 6-step commutation.

 

Also, if you can maybe suggested .... for 6 step commutation algorithm is it better to generate PWM or OC type control

 

I watched your online videos in regards with TIMER and PWM. I was also very educational. Still missing though is the 6-step commutation, with HALL sensor implementation.

 

Thank you

SRedd.5
Senior III
October 22, 2023

I am not sure i can understand your question completely but in workbench you have this configuration

SRedd5_0-1697955081348.pngSRedd5_1-1697955119013.pngSRedd5_2-1697955155835.png

I think you can use the above configuration to generate 6 step commutation using Hall sensor.

Mirza-AECAuthor
Associate
October 23, 2023

Hello

 

the reason why TIM20 is not supported is because STM32G431 doesnt have TIM20. The MCU i want to use STM32G474VET3 has TIM20. 

 

I have followed the NUCLEO-IMH03 motor workbench. Now i want to replicate PWM generation on the NUCLEO-IMH03. I use TIM8 CH1,2, and 3 as PWM CH and CHN. Timer TIM8 CH4 i set as PWM No output generation. I copy all the settings from motor workbench settings. After generate code i add in the main function

 

 

 

/* USER CODE BEGIN 2 */
 HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1);
 HAL_TIM_OC_Start_IT(&htim8, TIM_CHANNEL_1);
 HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_2);
 HAL_TIM_OC_Start_IT(&htim8, TIM_CHANNEL_2);
 HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_3);
 HAL_TIM_OC_Start_IT(&htim8, TIM_CHANNEL_3);
 HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_4);
 /* USER CODE END 2 */

 

 

however after i compile and download the code there is no output. I read the RM0440 and on page 1081 there is a table 252. I am not sure i understand the table.

MirzaAEC_0-1698049655228.png

I am saying that i dont understand the table since i choose TIM8 in Trigger Mode and ITR8 as trigger source. I looked on internet regarding "TIMx internal trigger connection" and they are a  bit different, much easier to understand than one in RM0440. 

 

So for my configuration on NUCLEO-IHM03, using TIM8 CH1, CH2 and CH3 as output and CH4 as trigger (i assume) how should i read the table 252. What should be my trigger source?

 

Thank you

Mirza-AECAuthor
Associate
October 26, 2023

Hello

 

i was wondering if someone can help me understand table 252 from RM0440

 

Thank you

SRedd.5
Senior III
October 26, 2023

According to me you can TIM8 for Ch1, Ch2, Ch3 and for trigger source you can use ITR1 which is tim2_trgo but only thing is you have to start the timer2 then you will get the PWM in my understanding.  

Mirza-AECAuthor
Associate
October 26, 2023

Hello

 

thank you very much for the response. I did as you have mentioned.

 

If you could maybe just write few words how did you come to this conclusion. For example what is first column TIMx? And how did you chose timer2? I imagine that is TIM2? Could you elaborate a bit?

SRedd.5
Senior III
October 26, 2023

But do you get the PWM outputs?

Mirza-AECAuthor
Associate
October 26, 2023

I have made a small test bench setup to test this. I have a NUCLEO-STM32G432RBT. I also have a BLDC with hall sensors attached to it. Currently BLDC is not driven by anything ... the phase connections are unconnected. I attached a drill machine to the shaft of the bldc and made it spin. On the scope i measure the 3 hall sensors and they are as they should be as the shaft is turning. 

I am trying to implement what is described in RM0440, on p1159 sec 28.3.29. I have connected the 3 hall sensors to the NUCLEO on Timer2 pins (A0,A1, B10). As the drill machine is turning the hall sensors generate the pulses and drive the NUCLEO. I have configured TIMER8 to be in slave mode, with trigger ITR1 and source.   

BLOCK Diagram.png

HALL_TIMER_Settings.png

PWM_TIMER_Settings.png

PWM_COUNTER_Settings.png

After CubeIDE generates the code i add following

// ENABLE Hall
__HAL_TIM_ENABLE_IT(&htim2,TIM_IT_TRIGGER); 
__HAL_TIM_ENABLE_IT(&htim2,TIM_IT_UPDATE); 
HAL_TIMEx_HallSensor_Start(&htim2);
HAL_TIM_TriggerCallback(&htim2); 

// START Commutation events
HAL_TIMEx_ConfigCommutationEvent(&htim8, TIM_TS_ITR1, TIM_COMMUTATION_SOFTWARE); 
HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_3);

I am then just connecting my oscilloscope to the NUCLEO to see if PWM are generated and they are NOT. I know i should also enable the Inverted pins, but for now i would be happy just with 3 pins. Any suggestions on what to do?

 

Mirza

SRedd.5
Senior III
October 27, 2023
cedric H
Technical Moderator
October 31, 2023

Could you check that the bit 15 (Master Output Enable) of register TIM8_BDTR (TIMx break and dead-time register) is correctly set. 

Do not hesitate to dump all the registers of your timer, I will try to give you some clues.

Cedric

Mirza-AECAuthor
Associate
November 2, 2023

Hello

 

so i have taken your advice. But i wanted to start a bit more simple. On  my P-NUCLEO-IMH03 I implemented my hall sensors on TIM2. The STM32G431 is running on 90Mhz so i first scale down with pre-scaler 900-1, then counter period 10000-1. In the main.c i add lines

 

__HAL_TIM_ENABLE_IT(&htim2,TIM_IT_TRIGGER);
 __HAL_TIM_ENABLE_IT(&htim2,TIM_IT_UPDATE);
 HAL_TIMEx_HallSensor_Start_IT(&htim2);

I also implemented Callback function to read hall sensor state

 

void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
{
	state = get_hall_state();
}

uint8_t get_hall_state(void)
{
	uint8_t _state = 0;

	if(HAL_GPIO_ReadPin(HALLA_GPIO_Port, HALLA_Pin) != GPIO_PIN_RESET)
	{
		_state |= 0x01U << 0;
	}
	if(HAL_GPIO_ReadPin(HALLB_GPIO_Port, HALLB_Pin) != GPIO_PIN_RESET)
	{
		_state |= 0x01U << 1;
	}
	if(HAL_GPIO_ReadPin(HALLC_GPIO_Port, HALLC_Pin) != GPIO_PIN_RESET)
	{
		_state |= 0x01U << 2;
	}

	return _state;
}

 

i can compile this with no problem and download to mt STM32G4. I then set a break point once get_hall_sensor is called just to see if the "HAL_TIM_TriggerCallback" is executed. It never is. Just to se my TIM2 settings i have put a brak point before while(1). These are the settings

 

MirzaAEC_0-1698897682480.png

 

In the ioc, i have enabeled TIM2 global interrupt (NVIC settings).  

What am i doing wrong so that TriggerCallback is never called?

Thank you