AnsweredAssumed Answered

Understanding TIM DMA_Handle_index

Question asked by Matti Sillanpää on Jun 27, 2017
Latest reply on Jun 29, 2017 by Matti Sillanpää

Hi.

 

 

I'm trying to use DMA to  drive PWM output for led effects. MCU write seems to be OK, however there's few very weird consepts related to DMA configuration I don't seem to be able to wrap my head around.

 

void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* TIMx clock enable */

/* Enable DMA2 clock */
DMAx_CLK_ENABLE

/*##-3- Configure the DMA stream ###########################################*/
/* Set the parameters to be configured */
hdma_tim.Init.Channel = DMA_CHANNEL_CC3;
hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim.Init.MemInc = DMA_MINC_ENABLE;
hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_tim.Init.Mode = DMA_NORMAL;
hdma_tim.Init.Priority = DMA_PRIORITY_HIGH;
/* hdma_tim.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tim.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;*/
/* hdma_tim.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_tim.Init.PeriphBurst = DMA_PBURST_SINGLE; */
/* Set hdma_tim instance */
hdma_tim.Instance = TIMx_CC3_DMA_STREAM;

/* Link hdma_tim to hdma[TIM_DMA_ID_UPDATE] update mode? */
__HAL_LINKDMA(htim, hdma[TIM_DMA_ID_UPDATE], hdma_tim);

/* Initialize TIMx DMA handle */
HAL_DMA_Init(htim->hdma[TIM_DMA_ID_UPDATE]);

/*##-4- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt */
HAL_NVIC_SetPriority(TIMx_DMA_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIMx_DMA_IRQn);
}

The link/initialize DMA uses the hdma array inside the TIM_HandleTypeDef. In the struct definition there's comment that array should be accessed via DMA_Handle_index. These are:

/** @defgroup DMA_Handle_index DMA Handle index
* @{
*/
#define TIM_DMA_ID_UPDATE ((uint16_t) 0x0000U) /*!< Index of the DMA handle used for Update DMA requests */
#define TIM_DMA_ID_CC1 ((uint16_t) 0x0001U) /*!< Index of the DMA handle used for Capture/Compare 1 DMA requests */
#define TIM_DMA_ID_CC2 ((uint16_t) 0x0002U) /*!< Index of the DMA handle used for Capture/Compare 2 DMA requests */
#define TIM_DMA_ID_CC3 ((uint16_t) 0x0003U) /*!< Index of the DMA handle used for Capture/Compare 3 DMA requests */
#define TIM_DMA_ID_CC4 ((uint16_t) 0x0004U) /*!< Index of the DMA handle used for Capture/Compare 4 DMA requests */
#define TIM_DMA_ID_COMMUTATION ((uint16_t) 0x0005U) /*!< Index of the DMA handle used for Commutation DMA requests */
#define TIM_DMA_ID_TRIGGER ((uint16_t) 0x0006U) /*!< Index of the DMA handle used for Trigger DMA requests */

I've tried to link this info somehow to registers in reference manual for both DMA and TIM2, however I cannot make any sense to this. What should I use? What are these?

 

I'm using TIM2 channel 2 so it should be DMA1 stream 6. The used GPIO pin is PA1.

Outcomes