EEVblog® Electronics Community Forum
Electronics => Microcontrollers => Topic started by: Jaunedeau on May 13, 2023, 07:53:02 am
-
Hi,
I have a bluepill with an stm32f103. The documentation says it is predictible, but it still has a prefetch queue to avoid waitstates, so I wanted to experiment with this. I just want to play with counting cycles*. But the code stops executing for 600-1000ns every (apparently exactly) 1ms.
I run the following code :
asm("asm:");
asm(" mov r1, #0x00000001");
asm(" ldr r2, =0x40010800");
asm(".align 4");
asm("loop:");
asm(" str r1, [r2, #20]");
asm(" str r1, [r2, #16]");
asm(" str r1, [r2, #20]");
asm(" str r1, [r2, #16]");
asm(" str r1, [r2, #20]");
asm(" str r1, [r2, #16]");
asm(" str r1, [r2, #20]");
asm(" str r1, [r2, #16]");
asm(" b loop");
That should just toggle a pin on and off four time, then loop.
Here is what I see on the oscilloscope :
(https://www.eevblog.com/forum/microcontrollers/stm32f103-unexpected-pause-in-code-execution-every-1ms/?action=dlattach;attach=1781723)
The pause seems to be of repetable length, but it's about half long if I compile in release instead of debug.
I have not made reproductible tests yet, but it seem to appear at random place in my loop (you see on the picture that after the long pause, I don't always have the same number of 0 or 1). Anyway it constantly repeat 1ms before the trigerring one.
Also depending on what I write before the loop, it seems stall during either a 1 or a 0 on the output pin. I can flash several times with the same firmware and it will apparently always be the same, but if I change the code before the loop it might change (even though I aligned the code before)
I have a very basic CubeMX new project, with :
- PA0 to PA7 and PC13 set to output
- RCC_MCO on on PA8 (clock output)
- SWDIO and SWDCLOCK on PA13 and 14
- 8MHz crystal with X9 multiplier to reach nominal 78MHz
(https://www.eevblog.com/forum/microcontrollers/stm32f103-unexpected-pause-in-code-execution-every-1ms/?action=dlattach;attach=1781717)
All I can see activated in CubeMX is :
- Serial Wire debug
- High Speed Clock (HSE) set to Crystal/Ceramic oscillator
[li]NVIC only has [/li][/list]enabledon things I can't disable[/li]
think I let everything else disabled (no watchdog timer).
I use a cheap STLinkV2 clone for programming.
Source code attached.
Any idea what is happening ?
Thank you,
J.
* : Just for fun, don't judge me. I can count cycles on 6502 where you often need it, I just wanted to experiment with next difficulty level.
-
Most likely, it's a systick interrupt.
-
SysTick interrupt, probably. Can you turn that off and see what happens?
https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf (https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf)
Page 151 - make sure TICKINT = 0
-
SysTick interrupt, probably. Can you turn that off and see what happens?
https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf (https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf)
Page 151 - make sure TICKINT = 0
Yes, that was my probleme, thanks !
asm(" CPSID if"); sovled my problem :)
-
Don't disable systick when using HAL, lots of functions rely on it for timeout management.
You'll get the code stuck in lots of places.
-
Indeed, temporarily disabling the systick timer is a nice diagnostics tool, but you should not do it lightly.
See it as part of the learning curve, and learn to write your programs in a robust way, so they can tolerate timing variations caused by ISR's.