Skip to main content
deep_rune
Associate III
July 2, 2020
Question

speed of execution - pin change seems too slow

  • July 2, 2020
  • 10 replies
  • 2383 views

I just got a simple program running on my STM32f0301k6. I am running the clock off a 20MHz crystal and the core is running off the PLL x 2 so everything is running at 40MHz. This means one cycle executes in 25nS, but if I write a loop like

	 GPIOB->ODR = 1<<1;
	 GPIOB->ODR &= ~GPIO_PIN_1;

then i see the pin toggle every 300nS or so. Why is this toggle time such a big difference from 25nS? More confusing to me is that if I change the pin mode to high speed, the toggle time gets much *slower*, measuring about 1.2uS

can anyone explain this to me?

This topic has been closed for replies.

10 replies

TDK
July 2, 2020

When the compiler compiles code, it translates each line of C/C++ code into one or more native Cortex-M0 instructions. Each of those instructions can take multiple cycles. So you should not expect 1 line of C code to execute in a single processor tick. Optimization level can have a big effect on this.

Pin speed isn't going to affect this directly, but it might affect how the compiler does it job which will indirectly affect results.

Using the BSRR register to toggle pins instead of ODR is preferable since it will only affect the pin you want to change.

"If you feel a post has answered your question, please click ""Accept as Solution""."
deep_rune
deep_runeAuthor
Associate III
July 2, 2020

that makes sense. is there a disassembly i can view to see exactly what its doing? I'm not that bothered about the speed but it would be good to understand whats going on

TDK
July 2, 2020

Usually there is a "Show disassembly" or similar option in most IDEs that will show the native instructions.

"If you feel a post has answered your question, please click ""Accept as Solution""."
deep_rune
deep_runeAuthor
Associate III
July 3, 2020

thanks, do you know how to view it inside of stm32cudeIDE?

berendi
Principal
July 3, 2020

Window / show view / disassembly

S.Ma
Principal
July 4, 2020

Is it a learning experiment? anything that requires asic type reactivity should use hw assist instead of time lag and execute critical piece of sw. once you add usb or ethernet, if your io bitbang takes time or is jitter sensitive you may end up in the wrong path to reliable implementation.

deep_rune
deep_runeAuthor
Associate III
July 4, 2020

well i am learning but this is a piece of audio hardware I made. The lower the interrupt time the more 'note perfect' the sound is. So im not using usb or ethernet.

I'm not very experienced so I don't know what you mean by hw assist instead of time lag or 'sw'

Piranha
Principal III
July 4, 2020

For a fast or accurate pin toggling, PWM, beep tones etc. use timer output channel.

deep_rune
deep_runeAuthor
Associate III
July 5, 2020

the signal isn't an audio signal, its just used inside a piece of audio hardware. Unless i misunderstood you. it would be great if there was another, better way to achieve what i want. the MCU takes a rising edge and produces a high or low output, like a shift register, except the length of the register and the values in the shift register can be changed

berendi
Principal
July 5, 2020

I think you should look into the timer synchronization topic in the reference manual.

deep_rune
deep_runeAuthor
Associate III
July 5, 2020

thanks, i will do that. why do you say so? Should i be using a timer rather than an interrupt?

berendi
Principal
July 5, 2020

If the task can be solved by a timer, maybe two timers in a master-slave configuration, or DMA transfers to GPIO registers triggered by a timer, it would be a lot faster.

But I have no idea what exactly do you want to do.

deep_rune
deep_runeAuthor
Associate III
July 5, 2020

I will look into it, although Im not experienced enough to know what you mean at the moment. im trying to make a shift register, which outputs one bit (output pin) and is clocked by a signal (interrupt pin) . The length of the shift register and the values in it can be changed, the clock is unpredictable.