Author Topic: [solved] stm32f103 : Unexpected pause in code execution every 1ms  (Read 2226 times)

0 Members and 1 Guest are viewing this topic.

Offline JaunedeauTopic starter

  • Regular Contributor
  • *
  • Posts: 122
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");
    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 output
  • RCC_MCO on on PA8 (clock output)
  • SWDIO and SWDCLOCK on PA13 and 14
  • 8MHz crystal with X9 multiplier to reach nominal 78MHz



  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]
Code: [Select]
enabled
    on 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.
« Last Edit: May 13, 2023, 09:43:07 am by Jaunedeau »
 

Offline jnk0le

  • Regular Contributor
  • *
  • Posts: 228
  • Country: pl
Re: stm32f103 : Unexpected pause in code execution every 1ms
« Reply #1 on: May 13, 2023, 08:11:58 am »
Most likely, it's a systick interrupt.
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4566
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: stm32f103 : Unexpected pause in code execution every 1ms
« Reply #2 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

Page 151 - make sure TICKINT = 0

Offline JaunedeauTopic starter

  • Regular Contributor
  • *
  • Posts: 122
Re: stm32f103 : Unexpected pause in code execution every 1ms
« Reply #3 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

Page 151 - make sure TICKINT = 0


Yes, that was my probleme, thanks !

Code: [Select]
  asm(" CPSID if"); sovled my problem :)
« Last Edit: May 13, 2023, 09:44:12 am by Jaunedeau »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6928
  • Country: es
Re: [solved] stm32f103 : Unexpected pause in code execution every 1ms
« Reply #4 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.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5386
  • Country: nl
Re: [solved] stm32f103 : Unexpected pause in code execution every 1ms
« Reply #5 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->