2025-09-29 10:39 PM - last edited on 2025-09-30 12:10 AM by mƎALLEm
Hi everyone.
I am driving a Neopixel LED with a PWM signal in DMA mode using a timer. Even though all variables are the same, my code works with timer 3, but the same code doesn't work with timer 2 with the same timer configuration. Why could this be? I checked for a 16-32 bit difference, but they say it's not related to that. I have an STM32G431 Nucleo board. I tried all pin configurations for timer 2 channel 1, but it didn't work. İf it's normally and ı cant use this for neopixel, I will change timer but I use other timers for different jobs with different frequences. But ı dont thinks so.
2025-09-30 12:11 AM
Hello
check if you have enabled the RCC clock of the timer and the RCC clock of the corresponding GPIOs
2025-09-30 12:17 AM
Bu ı can take basic pwm output from timer 2.
2025-09-30 12:35 AM
And you would need to look up DMA details in the reference manual.
Not every channel/stream can be used with every source/target.
2025-09-30 12:38 AM
My controller is stm32g431kbt6u. Timer 2 and Timer 3 are use APB1 bus. And they have same calibration in cube mx ui. I use same code. Just change &htimx. Code:
unsigned char LED_Data[MAX_LED][4];
unsigned short pwmData[(24*MAX_LED)+50];
unsigned int color;
unsigned char sendflag=0;
int red=255,green=255,blue=255,led=0,delayIndex=100,topLedLimit=100;
....
Other Code Details...
....
void Reset_LEDs(void){
for(unsigned char i=0;i<MAX_LED;i++){
LED_Data[i][0]=0;
LED_Data[i][1]=0;
LED_Data[i][2]=0;
LED_Data[i][3]=0;
}
}
void LED_Set_Data(int LEDnumber, int Red, int Green, int Blue){
if(LEDnumber>0){
LED_Data[LEDnumber][0]=LEDnumber;
LED_Data[LEDnumber][1]=(Green>=0)? Green:0;
LED_Data[LEDnumber][2]=(Red>=0)? Red:0;
LED_Data[LEDnumber][3]=(Blue>=0)? Blue:0;
}
}
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim){
HAL_TIM_PWM_Stop_DMA(&htim2, TIM_CHANNEL_1);
sendflag=1;
}
void WS2812_Write(void){
unsigned int index=0;
for(int i=0;i<MAX_LED;i++){
color=((LED_Data[i][3]<<16)|(LED_Data[i][2]<<8)|(LED_Data[i][1]));
for(int j=23;j>=0;j--){
if(color&(1<<j)){
pwmData[index] = 70;
}else{
pwmData[index] = 30;
}
index++;
}
}
for(int i=0;i<50;i++){
pwmData[index]=0;
index++;
}
HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_1, (uint32_t *)pwmData, index);
while(!sendflag);
sendflag = 0;
}
in Main While:
for(int i=0;i<20;i++){
LED_Set_Data(i,red,green,blue);
}
WS2812_Write();
2025-09-30 12:40 AM
Read out and check/post/compare-to-working content of TIM and relevant DMA registers.
JW
2025-09-30 12:55 AM
I cant see any important detail for this.
2025-09-30 1:23 AM - edited 2025-09-30 1:24 AM
@cereyanmühendisi wrote:my code works with timer 3, but the same code doesn't work with timer 2 with the same timer configuration..
"doesn't work" tells us little.
Do you get no output at all? incorrect timing? or what?
As well as the timer configuration, is your output pin configuration correct?
Show your schematic.
How to write your question to maximize your chances to find a solution
2025-09-30 1:36 AM
Actually ı can take signal. I can see from my ossiloscope. But I guess it's not true. About output pin configutation, I dont now. İts looks like same in Sytem Core-> GPIO.
2025-09-30 1:45 AM
You still haven't shown your schematic.
Have you checked that the Nucleo board doesn't have something else on PA2 ?
@cereyanmühendisi wrote:Actually ı can take signal. I can see from my ossiloscope.
So what is the difference between the working case, and non-working case?
Does the output match the Neopixel specifications?
Please post traces of working & non-working waveforms.
Be sure to use your oscilloscope's screen capture facility - rather than trying to photograph the screen.