Skip to main content
Visitor II
July 30, 2025
Question

TIM2 and TIM8 sync issue with DMA in circular mode

  • July 30, 2025
  • 4 replies
  • 449 views

I AM USING STM32F446RCT6 MICROCONTROLLER 

HAL_TIM_OC_Start_IT(&htim8, TIM_CHANNEL_2);

HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_4, (uint32_t *)gPWMData1, gPWMData1LastIdx); // trans is generated

HAL_DMA_Start_IT(&hdma_tim8_up, (uint32_t)&gPWMData2[row], (uint32_t)&(GPIOB->BSRR), gPWMData2LastIdx);

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)

{

if (htim == &htim2) {

if(circular==0)

{

HAL_TIM_PWM_Stop_DMA(&htim2, TIM_CHANNEL_4);{
 

IT WORKS IN NORMAL DMA MODE BUT NOT IN CIRCULAR DMA SYNCING TIM2 AND TIM8 IS QUITE DIFFICULT HOW TO DO IT?

 

BECAUSE AFTER STOPPING GAP ARISES WHICH IS THE ISSUE CONTINUOUS COMMUNICATION REQUIRED

htim2.Instance = TIM2;
htim2.Init.Prescaler = 1-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 50-1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload =TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
 Error_Handler();
}

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

if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)

{

Error_Handler();

}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;//TIM_TRGO_ENABLE;//

sMasterConfig.MasterSlaveMode =TIM_MASTERSLAVEMODE_ENABLE;//IM_MASTERSLAVEMODE_ENABLE;

if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

static void MX_TIM8_Init(void)

{



/* USER CODE BEGIN TIM8_Init 0 */



/* USER CODE END TIM8_Init 0 */



TIM_SlaveConfigTypeDef sSlaveConfig = {0};

TIM_MasterConfigTypeDef sMasterConfig = {0};

TIM_OC_InitTypeDef sConfigOC = {0};

TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};



/* USER CODE BEGIN TIM8_Init 1 */



/* USER CODE END TIM8_Init 1 */

htim8.Instance = TIM8;

htim8.Init.Prescaler = 0;

htim8.Init.CounterMode = TIM_COUNTERMODE_UP;

htim8.Init.Period = 50-1;

htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim8.Init.RepetitionCounter = 0;

htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

if (HAL_TIM_Base_Init(&htim8) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_OC_Init(&htim8) != HAL_OK)

{

Error_Handler();

}



sSlaveConfig.SlaveMode =TIM_SLAVEMODE_TRIGGER;

sSlaveConfig.InputTrigger = TIM_TS_ITR1; // ITR1 ← TIM2

if (HAL_TIM_SlaveConfigSynchro(&htim8, &sSlaveConfig) != HAL_OK)

{

Error_Handler();

}

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



sConfigOC.OCMode = TIM_OCMODE_TOGGLE;

sConfigOC.Pulse = 0;

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_OC_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

{

Error_Handler();

}

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();

}

/* USER CODE BEGIN TIM8_Init 2 */



/* USER CODE END TIM8_Init 2 */

HAL_TIM_MspPostInit(&htim8);
}

MGOEL1_0-1753871531615.bmp

MGOEL1_1-1753871592244.bmp


Edited to apply source code formatting - please see How to insert source code for future reference.

    This topic has been closed for replies.

    4 replies

    Super User
    July 30, 2025

    If you don't want a gap in the PWM, don't stop the timer.

    What waveform are you trying to achieve?

     

    MGOEL.1Author
    Visitor II
    July 30, 2025

    MGOEL1_0-1753886067750.bmp

    waveform achieved through normal dma with start,then stop in pulsecallback then wait for completion but require it to be continuous as stopping dma gives latency which we need to remove above waveform required in continuation without any delay

     

    MGOEL.1Author
    Visitor II
    July 30, 2025

    MGOEL1_1-1753886208380.bmp

    OK NORMAL.BMP

    Super User
    July 30, 2025

    If your PWM signal requires the CPU to do stuff in order to get the waveform you want, you'll need to ensure those priorities are higher (numerically lower) than competing interrupts. Sounds like the USB priority is equal or higher.

    MGOEL.1Author
    Visitor II
    July 30, 2025

    priority i have kept high for tim2 and tim8 I stop tim2 dma then  wait for its pulseback to complete then only tim8 and tim2 sync otherwise in circular mode it doesn't.but stopping and starting dma introduce latency 

    Super User
    July 30, 2025

    You're going to need to present the problem much more clearly. What waveform are you expecting? Consider NOT starting/stopping the timers if you don't want latency to be introduced. Find a way to get the waveform without relying on the CPU. Almost certainly there is a way. Many slave timer options available.

    MGOEL.1Author
    Visitor II
    July 31, 2025

    correct pulse1correct pulse1out of syncout of synccorrect pulse2correct pulse2

    MGOEL.1Author
    Visitor II
    July 31, 2025

    i am inserting photos but it is not attacorrect pulsecorrect pulse

     

    correct pulse1correct pulse1

     

    ching why?