Skip to main content
thannara123
Associate III
August 21, 2024
Question

Change Polarity of PWM with DMA in circular mode

  • August 21, 2024
  • 2 replies
  • 1110 views

Hello Experts ,

I am generating a two-Channel PWM with complementary channels also 

 

 

HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)sin_table, 20);//low
HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_2, (uint32_t *)sin_table, 20); //high
HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_2);

 

 

I am using DMA in circular mode .

I require when the total complete transfer occur the Polarity of the above channel ie Channel 1 and channel should or change . 

 

@BarryWhit 

@MM..1 

@Sarra.S 

 

 

 

void PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{ if (htim->hdma[TIM_DMA_ID_CC1] != NULL && htim->hdma[TIM_DMA_ID_CC1] == &hdma_tim1_ch1)
 {
 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

 if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
 {
 HAL_TIM_PWM_Stop_DMA(&htim1, TIM_CHANNEL_1);
 HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
 // Stop PWM on CH2 and CH2N before toggling polarity
 HAL_TIM_PWM_Stop_DMA(&htim1, TIM_CHANNEL_2);
 HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
 // Toggle the polarity bits for CH1 and CH1N
 htim1.Instance->CCER ^= (TIM_CCER_CC1P | TIM_CCER_CC1NP);
 // Toggle the polarity bits for CH2 and CH2N
 htim1.Instance->CCER ^= (TIM_CCER_CC2P | TIM_CCER_CC2NP);
HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t *)sin_table, 200);
	 HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_1);
	 HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_2, (uint32_t *)sin_table, 200);
	 HAL_TIMEx_PWMN_Start_IT(&htim1, TIM_CHANNEL_2);

 

 

I tried the following way , After trigger the callback it stops the PWM and not start again ?

How can obtain it ?

 

2 replies

Andrew Neil
Super User
August 21, 2024
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
MM..1
Chief III
August 21, 2024

Forget to use HAL for continuos operations. Maybe with right usage half and full complete callbacks and rewrite arrays for DMA. For continuos PWM never break TIM setup only change CCR values...