cancel
Showing results for 
Search instead for 
Did you mean: 

Unexpected timing with nops

tompa
Associate III

Hi,

I am confused with pin toggle timings, why I am getting non equal pulse widths like in the picture, I turn off all compiler optimizations and disable all of the interrupts, code is simple pin toggling. My controller is STM32L431 running on 80MHz.

Thank you very much for any advice!

__disable_irq();
  while(1) {
	  GPIOB->BSRR = GPIO_BSRR_BS15;	
	  __NOP();
	  __NOP();
	  GPIOB->BSRR = GPIO_BSRR_BR15;	
	  __NOP();
	  __NOP();
  }

  pic2.png

14 REPLIES 14
PGump.1
Senior III

Hi,

The while(1) is NOT zero overhead. To prove it, try

__disable_irq();
  while(1) {
	  GPIOB->BSRR = GPIO_BSRR_BS15;	
	  __NOP();
	  __NOP();
	  GPIOB->BSRR = GPIO_BSRR_BR15;	
	  __NOP();
	  __NOP();
	  GPIOB->BSRR = GPIO_BSRR_BS15;	
	  __NOP();
	  __NOP();
	  GPIOB->BSRR = GPIO_BSRR_BR15;	
	  __NOP();
	  __NOP();
  }

 

I hope that helps.

Kind regards
Pedro

AI = Artificial Intelligence, NI = No Intelligence, RI = Real Intelligence.

Hi PGump.1

Thank you for your reply, 

unfortunatelly still uneven widths..

gregstm
Senior III

Use a simulator and step through your code at the assembly level, counting the cycles (there should a cycle count visible) - that way you will get to see the "overhead" of the loop. If you want to handcraft a loop with precise timing, you could consider doing it in assembly language.


@tompa wrote: "unfortunatelly still uneven widths.."

I never said it would fix it, it would prove it... Was the proof correct?

Kind regards
Pedro

AI = Artificial Intelligence, NI = No Intelligence, RI = Real Intelligence.

Sampling a Nucleo-746RG @80MHz with a Saleae Logic Pro 8 at 500MS/s

Using the same code as the original post. I did not observe the significant difference in width per the first image of the post. 

STM_Thirty2_0-1730595920264.png

 

If you feel a post has answered your question, please click "Accept as Solution"