2020-07-02 09:00 AM
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?
2020-07-02 09:14 AM
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.
2020-07-02 09:25 AM
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
2020-07-02 09:33 AM
Usually there is a "Show disassembly" or similar option in most IDEs that will show the native instructions.
2020-07-03 07:24 AM
thanks, do you know how to view it inside of stm32cudeIDE?
2020-07-03 09:43 AM
Window / show view / disassembly
2020-07-03 09:26 PM
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.
2020-07-04 03:43 AM
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'
2020-07-04 03:07 PM
For a fast or accurate pin toggling, PWM, beep tones etc. use timer output channel.
2020-07-05 01:37 AM
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