cancel
Showing results for 
Search instead for 
Did you mean: 

speed of execution - pin change seems too slow

deep_rune
Associate III

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?

13 REPLIES 13
TDK
Guru

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
Associate III

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
Guru

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
Associate III

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

berendi
Principal

Window / show view / disassembly

S.Ma
Principal

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
Associate III

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
Chief II

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

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