1

I need to help with this assembly delay function, Can any one help me calculate how much second delay is for this assembly delay function? It's coding for STM32L152-DISCOVERY board, here is the its page: http://www.st.com/web/en/catalog/tools/FM116/SC959/SS1532/LN1848/PF258515

Delay       PROC
            push {r1}
            LDR r1, = 0x1000
again1      NOP
            NOP
            subs r1, #1
            bne again1
            pop {r1}
            bx lr
            ENDP
user3435575
  • 31
  • 1
  • 8
  • 2
    Doesn't that processor have 8 timers? That's how you do it, with a timer. Ideally, you'll have a regular timer interrupt which maintains a seconds and/or milliseconds counter. A simpler way will be to sit there and examine a free run counter value, but that's nearly as bad as your approach. If you are going to program an MCU you'll need to master basic timer control. – Weather Vane Sep 13 '15 at 21:37
  • I'm not sure about how many timer it has in the processor, is there any formula that we can use to calculate the delay time for assembly? – user3435575 Sep 13 '15 at 22:14
  • You lookup the clock rate for the STM32L152-DISCOVERY board, then you lookup the number of clock cycles each of the instructions will take (account for the fact there is a loop). Once you have the number of clock cycles you can then compute how long the delay is based on the clock rate and number of clock cycles the delay function takes. – Michael Petch Sep 13 '15 at 22:31
  • Since I think this is homework, I won't show how I arrived at it but based on Arm Cortex-M3 timings on a 25mhz system it is about 1ms delay and about .77ms delay at 32mhz. Given how close it came at 25mhz to 1ms, I'd venture to guess the loop was created originally for that clock speed – Michael Petch Sep 14 '15 at 00:25
  • Please don't use the CPU to try to create a time delay, that sort of thing only works on trivial devices. ARM warn you that `nop` may be skipped by the processor and consume no time at all and that's before we even get into DMA requiring the bus... – Andy Brown Sep 22 '15 at 08:11

1 Answers1

1

First, you need to determine the core freqency. By default, in STM32L152 core clocked from MSI with 2.097 MHz (see Reference manual, RM0038). But firmware can override this settings: look at standard function SystemInit().

Second, using this table Cortex M3 instruction summary, calculate total number of MCU cycles. Note, that branch instruction takes more than one cycle, and its exact value not defined.

As a result, divide numer of cycles to a frequency (in Hz) - you should get about 12 milliseconds, if my assumtions about system clock were correct.

But don't use this code in real systems - this delays not predictable, it depends on flash memory accelerator settings and DMA activity. Use timer delays instead.

Alexey Esaulenko
  • 499
  • 2
  • 10