cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Problem

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.

13 REPLIES 13
mƎALLEm
ST Employee

Hello

check if you have enabled the RCC clock of the timer and the RCC clock of the corresponding GPIOs

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.

Bu ı can take basic pwm output from timer 2.

Ozone
Principal II

And you would need to look up DMA details in the reference manual.
Not every channel/stream can be used with every source/target.

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();
waclawek.jan
Super User

Read out and check/post/compare-to-working content of TIM and relevant DMA registers.

JW

I cant see any important detail for this. 

Andrew Neil
Super User

@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

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

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. 

cereyanmhendisi_0-1759221138325.png

cereyanmhendisi_1-1759221193986.png

 

cereyanmhendisi_2-1759221222882.png

cereyanmhendisi_3-1759221274114.png

cereyanmhendisi_4-1759221306235.png

cereyanmhendisi_5-1759221330568.png

 

 

 

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.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.