2023-12-12 12:28 AM - last edited on 2023-12-12 02:19 AM by Sarra.S
So i was trying to generate pwm on all gpio pins of a stm32F103 for my application. I found followed this guide:https://www.hackster.io/javier-munoz-saez/all-pins-as-pwm-at-the-same-time-baremetal-stm32-1df86f
the essence is using the function:
void setSoftPWM(uint16_t pin, uint32_t duty ,uint32_t *softpwmbuffer){ for (uint32_t i = 0; i < lengthSoftPWMbuffer; ++ i) { if(i<duty){//set pin softpwmbuffer[i]&=(uint32_t)~(pin<<16); softpwmbuffer[i]|=(uint32_t)pin; }else{//reset pin softpwmbuffer[i]&=(uint32_t)~(pin); softpwmbuffer[i]|=(uint32_t)pin<<16; } } }
to generate a buffer array of 100 elements which contain the gpio info for the GPIOX_BSRR register depending on the duty cycle. I configure the dma in circular mode and trigger it with a timer update event.
I do the initial configuration using CubeMX and then add the following code to start the dma.
HAL_TIM_Base_Start(&htim1); HAL_TIM_Base_Start(&htim2); HAL_TIM_Base_Start(&htim4); HAL_DMA_Start_IT(&hdma_tim1_up, (uint32_t)&(dataA[0]), (uint32_t)&(GPIOA->BSRR), sizeof(dataA)); HAL_DMA_Start_IT(&hdma_tim2_up, (uint32_t)&(dataB[0]), (uint32_t)&(GPIOB->ODR), sizeof(dataB)); HAL_DMA_Start_IT(&hdma_tim4_up, (uint32_t)&(dataC[0]), (uint32_t)&(GPIOC->BSRR), sizeof(dataC)); //start DMAs __HAL_TIM_ENABLE_DMA(&htim1, TIM_DMA_UPDATE); __HAL_TIM_ENABLE_DMA(&htim2, TIM_DMA_UPDATE); __HAL_TIM_ENABLE_DMA(&htim4, TIM_DMA_UPDATE);
Then i use the setSoftPWM() function to initialize a pwm in different gpio pins like so:
setSoftPWM(GPIO_PIN_0, 90, (uint32_t*)&dataA); //sets gpioA setSoftPWM(GPIO_PIN_0, 40, (uint32_t*)&dataB); //sets gpioB setSoftPWM(GPIO_PIN_13, 60, (uint32_t*)&dataC);//sets gpioC
But when i upload to the MCU i am getting these waveforms on the pins:
PWM at 100% duty cycle
PWM at 0% duty cycle
PWM at 50% duty cycle:
as you can see the pwm is working somewhat but only in a very small part of the waveform. The rest of the waveform has this weird jitter.
I tried a bunch of things but am unable to eliminate the jitter. The random waveforms are completely arbitrary and change with the gpio ports and everytime i reset the MCU.
At first i thought maybe it is a hardware problem so i tried it in a different f103 and a L432kc but am getting the same result.
So its probably a software problem but no matter what i try, cant really explain or eliminate the jitter.
what am i missing here?
Regards:
Okbit
Here is the full code made using stm32CubeIDE
Solved! Go to Solution.
2023-12-12 08:04 AM
> sizeof(dataA)
The function wants the number of transfers, not the number of bytes.
This should be sizeof(dataA) / sizeof(*dataA), or however else you want to write it such that it's the number of uint32_t values in the buffer.
2023-12-12 02:16 AM - edited 2023-12-12 02:18 AM
Hello @Okbit, welcome to ST Community,
I'd highly recommend referring to the TIM example under the STM32CubeF1
The example titled "TIM_DMA" describes how to use DMA with TIMER Update request to transfer Data from memory to TIMER Capture Compare Register. The objective of the example is to configure TIM1 channel 3 to generate a complementary PWM signal. You may take this example as a reference to correctly configure your peripheral.
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.
2023-12-12 08:04 AM
> sizeof(dataA)
The function wants the number of transfers, not the number of bytes.
This should be sizeof(dataA) / sizeof(*dataA), or however else you want to write it such that it's the number of uint32_t values in the buffer.
2023-12-13 02:00 AM
That was it! I never realized it was that simple!
Thank you so much i was stuck on this problem forever.
2023-12-13 02:00 AM
Thats a great resource that i wasn't aware of thanks for the info