EEVblog® Electronics Community Forum

Electronics => Microcontrollers => Topic started by: Jaunedeau on May 13, 2023, 07:53:02 am

Title: [solved] stm32f103 : Unexpected pause in code execution every 1ms
Post 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 :
Code: [Select]
  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");
(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 :

(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 :
[li]NVIC only has [/li][/list]
Code: [Select]
enabledthink 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.
Title: Re: stm32f103 : Unexpected pause in code execution every 1ms
Post by: jnk0le on May 13, 2023, 08:11:58 am
Most likely, it's a systick interrupt.
Title: Re: stm32f103 : Unexpected pause in code execution every 1ms
Post by: AndyC_772 on May 13, 2023, 08:16:05 am
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
Title: Re: stm32f103 : Unexpected pause in code execution every 1ms
Post by: Jaunedeau on May 13, 2023, 08:44:50 am
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 !

Code: [Select]
  asm(" CPSID if"); sovled my problem :)
Title: Re: [solved] stm32f103 : Unexpected pause in code execution every 1ms
Post by: DavidAlfa on May 13, 2023, 10:26:59 am
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.
Title: Re: [solved] stm32f103 : Unexpected pause in code execution every 1ms
Post by: Doctorandus_P on May 19, 2023, 05:17:44 pm
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.