[solved] stm32f103 : Unexpected pause in code execution every 1ms - Page 1
Forum section: electronics / general

author: Jaunedeau
author_rank: Regular Contributor
timestamp: May 13, 2023, 07:53:02 am
content: 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 :Code:asm("asm:");asm("movr1, #0x00000001");asm("ldrr2, =0x40010800");asm(".align 4");asm("loop:");asm("strr1, [r2, #20]");asm("strr1, [r2, #16]");asm("strr1, [r2, #20]");asm("strr1, [r2, #16]");asm("strr1, [r2, #20]");asm("strr1, [r2, #16]");asm("strr1, [r2, #20]");asm("strr1, [r2, #16]");asm("bloop");That should just toggle a pin on and off four time, then loop.Here is what I see on the oscilloscope :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 outputRCC_MCO on on PA8 (clock output)SWDIO and SWDCLOCK on PA13 and 148MHz crystal with X9 multiplier to reach nominal 78MHzAll I can see activated in CubeMX is :Serial Wire debugHigh Speed Clock (HSE) set to Crystal/Ceramic oscillatorNVIC only has Code:enabledon things I can't disablethink 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.

author: jnk0le
author_rank: Regular Contributor
timestamp: May 13, 2023, 08:11:58 am
content: Most likely, it's a systick interrupt.

author: AndyC_772
author_rank: Super Contributor
timestamp: May 13, 2023, 08:16:05 am
content: 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.pdfPage 151 - make sure TICKINT = 0

author: Jaunedeau
author_rank: Regular Contributor
timestamp: May 13, 2023, 08:44:50 am
content: Quote from: AndyC_772 on May 13, 2023, 08:16:05 amYes, that was my probleme, thanks !Code:asm("CPSIDif");sovled my problem

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 13, 2023, 10:26:59 am
content: 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.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: May 19, 2023, 05:17:44 pm
content: 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.