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

Thread: Is this a valid method to time code execution?

  1. #1
    Senior Member
    Join Date
    Nov 2015
    Location
    Cold hollow VT
    Posts
    148

    Is this a valid method to time code execution?

    I'm trying to make an I2C decoder that uses the PORTx_PCRn register to create DMA / IRQ requests as was described in this POST.

    I'm using a TEENSY 3.2 running at 96MHz and would like to be sure that my routines are not taking too long to execute. For now that is trying to keep within 10uS clock cycles. I'm doing this in my code to see how long it takes to execute. I'm viewing the pins with a scope to measure the time it takes.


    Code:
    if (p1==0x20000)  {digitalWrite(2,HIGH); busData[ptr] |= 0x00000000; busData[ptr] = busData[ptr] << 1;  bitCntr++; digitalWrite(2,LOW);} 
    if (p1==0x30000)  {digitalWrite(3,HIGH); busData[ptr] |= 0x00000001; busData[ptr] = busData[ptr] << 1;  bitCntr++; digitalWrite(3,LOW);}
    I'm assuming the code executes linearly and does not overlap and the time the designated pin is high is approximately how long the code within takes to execute.

    Is this a valid assumption?

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,178
    That snippet leaves out some context - but it seems to look like a valid attempt to change a pin before and after executing 'some code' and should generally be executed in order it is programmed.

    One note of course is the time for conditional "if(p1==??)" testing which would not be included. So if this snippet is all part of the code() being examined perhaps adding digitalWriteFast(4, ??) HIGH and LOW around BOTH of those lines would give a true measure?

    Another that toggling the pin adds time - and using digitalWriteFast() is indeed faster to do the same thing on Teensy than digitalWrite() when using 'constant' values for the pin as shown. This will result in better scope feedback with less overhead.

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,409
    As @defragster mentioned, yes I do that type of debugging all of the time with my logic analyzers.
    Not sure how much of a delay it will show up given only a a couple of assignments between setting high and low.
    As he mentioned using digitalWriteFast(2, HIGH) and the like boils down to a simple register assignment: (Pin2 High) CORE_PIN2_PORTSET = CORE_PIN2_BITMASK;

    When I am doing timings, it is often over a bit large time spans, so dealing with how long does it take for the pin level to rise and fall does not impact me.
    So it has been awhile since I worried about the fastest speed for the pins and if you get better results if the port is configured differently.
    That is we define: *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
    For output on T3.x. MUX(1) is IO pin. But I don't remember of SRE or DSE is the best settings for as fast as possible...

Posting Permissions

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