Weird Jitter when using DMA to generate PWM in GPIOs
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
