cancel
Showing results for 
Search instead for 
Did you mean: 

How to use DMA to change PWM frequency?

Caraffa
Associate II

Hi! I know overall idea of DMA and i need to use it but i have no idea how to do it.

I found one post with same question but i didn't found any helpful info for me. I'm not advanced in case of using registers. I used them to directly change CCR and ARR sometimes, so I need a direct answer.

Edit: It is worth mentioning that I use CubeIDE

Thanks for help

11 REPLIES 11

Make TIMx_ARR to be the target of the DMA transfer on the peripheral side. Make sure ARR preload is switched on.

JW

Could you explain it further, especially first part? How i can make TIMx_ARR target of the DMA transfer? ​

You write it in the Peripheral address register of DMA channel/stream, while the channel/stream is set to transfer memory-to-peripheral.

Read the DMA chapter first. You may also want to read the Timer cookbook AN4776. And next time start with stating which STM32 are you using.

JW

I'm using stm32f407g

Javier1
Principal

I use here a DMA(triggered with a timer)---->PWM(timer) to create a neopixel LED driver, maybe you can fish something out, specially the DMA+PWMtest.

The mcu shouldnt matter as long as you have DMA and TIM available to use.

I tested this with stm32f103 and stm32f7

You use HAL_TIM_PWM_Start_DMA function but if i understand it correctly, it can access only TIMx_CCRx registry but i need TIMx_ARR

what if you try this inside HAL_TIM_PWM_Start_DMA()

//instead of this:
      if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length) != HAL_OK)
      {
 
//do this :
 
      if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length) != HAL_OK)
      {

It does not work, just returns HAL_ERROR. Standard version is working correctly.

Also your example is not correct, when you are calling the function you should put uint32_t pointer as pData, not uint8_t. When i changed that it was working back again.

sorry to hear that...

fyi: is not my example, i just copy pasted, this code comes from the HAL_TIM_PWM_Start_DMA API provided with the HAL libraries maintained by STM

maybe your DMA configuration is set up as Byte instead of word.

ill look into that next time i get into that project, thanks