cancel
Showing results for 
Search instead for 
Did you mean: 

N DMA Transfers based on a timer trigger

Ahmed Tolba
Associate II

I'm trying to use timer 8, stm32f429I, as advanced timer, to do N DMA transfers, that copies data from portD to memory and it does trigger on PC6 after triggering, an N DMA transfers should be shot I have tried, but now it triggers only DMA transfer, only one PC6 falling edge but it doesn't do N reptition If I set reptition counter to N, the trigger is not working any more

 

 

static void MX_DMA_Init(void)

{



/* DMA controller clock enable */

 __HAL_RCC_DMA2_CLK_ENABLE();



/* DMA interrupt init */

/* DMA2_Stream7_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 0, 0);

 HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);



}

 

static void MX_TIM8_Init(void)

{



/* USER CODE BEGIN TIM8_Init 0 */



/* USER CODE END TIM8_Init 0 */



TIM_ClockConfigTypeDef sClockSourceConfig = {0};

TIM_SlaveConfigTypeDef sSlaveConfig = {0};

TIM_MasterConfigTypeDef sMasterConfig = {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 = 65535;

 htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim8.Init.RepetitionCounter = 0;

 htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;



if (HAL_TIM_Base_Init(&htim8) != HAL_OK)

 {

 Error_Handler();

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)

 {

 Error_Handler();

 }

 sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;

 sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;

 sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_FALLING;

 sSlaveConfig.TriggerFilter = 0;

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

 {

 Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

 {

 Error_Handler();

 }

/* USER CODE BEGIN TIM8_Init 2 */



/* USER CODE END TIM8_Init 2 */



}

 

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

if(htim_base->Instance==TIM8)

 {

/* USER CODE BEGIN TIM8_MspInit 0 */



/* USER CODE END TIM8_MspInit 0 */

/* Peripheral clock enable */

 __HAL_RCC_TIM8_CLK_ENABLE();



 __HAL_RCC_GPIOC_CLK_ENABLE();

/**TIM8 GPIO Configuration

 PC6 ------> TIM8_CH1

 */

 GPIO_InitStruct.Pin = GPIO_PIN_6;

 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 GPIO_InitStruct.Alternate = GPIO_AF3_TIM8;

 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);



/* TIM8 DMA Init */

/* TIM8_CH4_TRIG_COM Init */

 hdma_tim8_ch4_trig_com.Instance = DMA2_Stream7;

 hdma_tim8_ch4_trig_com.Init.Channel = DMA_CHANNEL_7;

 hdma_tim8_ch4_trig_com.Init.Direction = DMA_PERIPH_TO_MEMORY;

 hdma_tim8_ch4_trig_com.Init.PeriphInc = DMA_PINC_DISABLE;

 hdma_tim8_ch4_trig_com.Init.MemInc = DMA_MINC_ENABLE;

 hdma_tim8_ch4_trig_com.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;

 hdma_tim8_ch4_trig_com.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;

 hdma_tim8_ch4_trig_com.Init.Mode = DMA_NORMAL;

 hdma_tim8_ch4_trig_com.Init.Priority = DMA_PRIORITY_HIGH;

 hdma_tim8_ch4_trig_com.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

if (HAL_DMA_Init(&hdma_tim8_ch4_trig_com) != HAL_OK)

 {

 Error_Handler();

 }



/* Several peripheral DMA handle pointers point to the same DMA handle.

 Be aware that there is only one stream to perform all the requested DMAs. */

//__HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_CC4],hdma_tim8_ch4_trig_com);

 __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_TRIGGER],hdma_tim8_ch4_trig_com);

//__HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_COMMUTATION],hdma_tim8_ch4_trig_com);

 HAL_DMA_RegisterCallback(&hdma_tim8_ch4_trig_com,HAL_DMA_XFER_CPLT_CB_ID,HAL_DMA_TransferComplete);



/* TIM8 interrupt Init */

 HAL_NVIC_SetPriority(TIM8_TRG_COM_TIM14_IRQn, 0, 0);

 HAL_NVIC_EnableIRQ(TIM8_TRG_COM_TIM14_IRQn);

/* USER CODE BEGIN TIM8_MspInit 1 */

 HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, 0, 0);

 HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn);



/* USER CODE END TIM8_MspInit 1 */

 }



}

 

 __HAL_TIM_ENABLE_IT(&htim8, TIM_IT_TRIGGER);

/* Enable the TIMX OC channel */

 TIM_CCxChannelCmd(htim8.Instance, TIMx_CHANNEL_OC, TIM_CCx_ENABLE);



/* Enable the TIMx IC channel */

 TIM_CCxChannelCmd(htim8.Instance, TIMx_CHANNEL_IC, TIM_CCx_ENABLE);



void TIM8_TRG_COM_TIM14_IRQHandler(void)

{

/* USER CODE BEGIN TIM8_TRG_COM_TIM14_IRQn 0 */



/* USER CODE END TIM8_TRG_COM_TIM14_IRQn 0 */

HAL_TIM_IRQHandler(&htim8);

/* USER CODE BEGIN TIM8_TRG_COM_TIM14_IRQn 1 */



/* USER CODE END TIM8_TRG_COM_TIM14_IRQn 1 */

}



void TIM8_Update_Callback(void) {



 HAL_DMA_Start_IT(&hdma_tim8_ch4_trig_com,(uint32_t)&(GPIOD->IDR),(uint32_t)current_dma_buffer,sizeof(current_dma_buffer));



}

void DMA2_Stream7_IRQHandler(void)

{

/* USER CODE BEGIN DMA2_Stream7_IRQn 0 */



/* USER CODE END DMA2_Stream7_IRQn 0 */

 HAL_DMA_IRQHandler(&hdma_tim8_ch4_trig_com);

/* USER CODE BEGIN DMA2_Stream7_IRQn 1 */



/* USER CODE END DMA2_Stream7_IRQn 1 */

}

 

 

 

before main loop:

 

__HAL_TIM_ENABLE_DMA(&htim8, TIM_DMA_TRIGGER);

__HAL_TIM_ENABLE_DMA(&htim8, TIM_DMA_CC4);

HAL_TIM_Base_Start_IT(&htim8);

 

1 REPLY 1
Sarra.S
ST Employee

Hello @Ahmed Tolba, sorry for the delayed answer,

You need to set up the timer to generate a trigger output (TRGO) on each timer event

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;

and enable the DMA request from the timer update event

__HAL_TIM_ENABLE_DMA(&htim8, TIM_DMA_UPDATE);

 and then start the timer with HAL_TIM_Base_Start_IT(&htim8);

and DMA with HAL_DMA_Start_IT

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.