AnsweredAssumed Answered

STM32F429-Generating variable PWM using DMA

Question asked by Sukhdeep Singh on Jun 7, 2017
Latest reply on May 3, 2018 by ferrario.fabio.001

Hi,

     Variable PWM is not working. So i looked at the example code provided in "STM32Cube_FW_F4_V1.16.0\Projects\STM324x9I_EVAL\Examples\TIM\TIM_DMA"    

 

Based on example, modified code generated using cubeMx.

 

Please can anyone tell me the

1) What is Repetition counter(htim8.Init.RepetitionCounter) ? how it effects timer and PWM?

2) Where is the mistake in below code?

 

In main.c

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    lowPass();
    MX_GPIO_Init();
    MX_DMA_Init();
    MX_TIM8_Init();
    while(1){
      if(htim8.State==HAL_TIM_STATE_READY){
            HAL_TIM_PWM_Start_DMA(&htim8,TIM_CHANNEL_3,(uint32_t *)&SineLookupTable[0],PWM_SZ);
       }
    }
}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base){
     __HAL_RCC_TIM8_CLK_ENABLE();
   hdma_tim8_ch3.Instance = DMA2_Stream4;
    hdma_tim8_ch3.Init.Channel = DMA_CHANNEL_7;
    hdma_tim8_ch3.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_tim8_ch3.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_tim8_ch3.Init.MemInc = DMA_MINC_ENABLE;
    hdma_tim8_ch3.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_tim8_ch3.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    hdma_tim8_ch3.Init.Mode = DMA_CIRCULAR;
    hdma_tim8_ch3.Init.Priority = DMA_PRIORITY_LOW;
    hdma_tim8_ch3.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_tim8_ch3) != HAL_OK) {
       _Error_Handler(__FILE__, __LINE__);
    }
   __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_CC3],hdma_tim8_ch3);
   /* TIM8 interrupt Init */
    HAL_NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn);
    HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn);
    HAL_NVIC_SetPriority(TIM8_TRG_COM_TIM14_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM8_TRG_COM_TIM14_IRQn);
    HAL_NVIC_SetPriority(TIM8_CC_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM8_CC_IRQn);
}
static void MX_TIM8_Init(void){
   TIM_ClockConfigTypeDef sClockSourceConfig;
   TIM_MasterConfigTypeDef sMasterConfig;
   TIM_OC_InitTypeDef sConfigOC;
   TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
   htim8.Instance = TIM8;
   htim8.Init.Prescaler = 100;
   htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
   htim8.Init.Period = TIM8_IR_CNT_VAL;
   htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
   htim8.Init.RepetitionCounter = PWM_SZ;
  if (HAL_TIM_Base_Init(&htim8) != HAL_OK) {
      _Error_Handler(__FILE__, __LINE__);
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK){
     _Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_PWM_Init(&htim8) != HAL_OK){
    _Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK){
   _Error_Handler(__FILE__, __LINE__);
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = TIM8_IR_DUTY_VAL;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_3) != HAL_OK){
   _Error_Handler(__FILE__, __LINE__);
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK){
    _Error_Handler(__FILE__, __LINE__);
}
HAL_TIM_MspPostInit(&htim8);
}

In stm32f4xx_it.c

     

 #define PWM_SZ 100
       uint32_t SineLookupTable[PWM_SZ];
       const float Angle_Step_Rate=1.8;

      void waveform(void){
             uint8_t tmp;
             for(tmp=0;tmp<PWM_SZ;tmp++){
                SineLookupTable[tmp] = (TIM8_IR_CNT_VAL*(tmp+1))/PWM_SZ;
       }
   }


   void DMA2_Stream4_IRQHandler(void){
      HAL_DMA_IRQHandler(&hdma_tim8_ch3);
   }

Outcomes