Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: How to count time when interrupts are disabled?

  1. #1

    How to count time when interrupts are disabled?

    I have code like this
    before = millis()
    leds.show() (using adafruit neopixel or FastLED libraries)
    Serial.println(millis() - before)

    Because both libraries when they talk to neopixels, have to disable interrupts for some amount of time, millis() does not increment, and therefore I get the wrong value back.
    Using micros() is supposed to use CPU counters, but does not work better in my testing.

    Is there another way to get time elapsed in that case? (if possible in a way that works on other processors too)

    Thanks.

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,665
    Code:
        //Enable ARM-M4 Cycle Counter
        ARM_DEMCR |= ARM_DEMCR_TRCENA;
        ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
    then
    Code:
    unsigned cycles = ARM_DWT_CYCCNT;
    gives you the cpu-cycles spent. This works on any ARM Cortex M (and I think on other ARMs, too).

    This counter really counts cycles, so, for 120MHZ, it will increase 120000000 times every second and quickly overflow.
    Last edited by Frank B; 04-21-2017 at 09:13 PM.

  3. #3
    Thank you for the tip. Once read, can I reset the counter?

    Thanks
    Marc

  4. #4
    Senior Member
    Join Date
    Jan 2013
    Posts
    843
    Quote Originally Posted by Frank B View Post
    Code:
    unsigned cycles = ARM_DWT_CYCCNT;
    gives you the cpu-cycles spent. This works on any ARM Cortex M (and I think on other ARMs, too).
    Not quite, not all Cortex M0 have it - Teensy LC doesn't. Teensy 3.x does.

  5. #5
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,665
    No.
    Even it was possible - if you return from your "reset counter" function, it will again have several dozen counts... ;-)
    So why should you want to do that ? Resetting is not the usual way to work with such counters.. and it can interfere with other code that uses this counter (for example, the audio-library)
    Normally, you use a difference.

    aka

    cyles = ARM_DWT_CYCCNT;
    /* dowhateveryouwanthere */
    spentcycles= ARM_DWT_CYCCNT - cycles;
    Last edited by Frank B; 04-21-2017 at 09:35 PM.

  6. #6
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,665
    ok. but M3, M4 and M7 have it.

  7. #7
    Senior Member
    Join Date
    Jan 2013
    Posts
    843
    Quote Originally Posted by marcmerlin View Post
    Thank you for the tip. Once read, can I reset the counter?
    Look at elapsedMillis.h as example for how to get a re-settable version.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •