2

Yesterday I played with a SRAM 621024 SRAM 1 Mbit IC and since I use a lot of pins, digitalReads and digitalWrites are needed including shift registers (see Example

Since it took a long time for reading/writing one byte, I checked for faster ways and found some posts, one of them was using digitalWriteFast: Library

However, it did not make any difference... is it possible that the (fast) digitalWrite is already implemented in the default Arduino IDE/library?

Update: I found out I made a stupid mistake and the library was not used.

Now I get different values:

  • Writing 397.880 us per byte
  • Reading 386.524 us per byte

Without the fast library:

  • Writing 502.152 us per byte
  • Reading 483.752 us per byte

Note, there is also a digitalReadFast, but this does not affect the speed.

I did some more testing and found out that a digitalReadFast/digitalWriteFast cost just about 8-12 us, a pinModeFast is 0 us (negligable), however a shiftOut for setting the address cost 120-124 us ... since 3 shifts are needed to set an address, this results in 360-372 us. Subtracting this from the measured times above, writing takes around 25-38 us and reading 15-27us. I possibly can maybe optimize the bit operations a little bit.

However, the end result for using this RAM chip with enough speed is to either change the shift register to a faster one or remove the shift registers at all. This would mean about 30 pins needed for the SRAM only. The conclusion is: better use a 3K256, 23LC1024 or similar variant.

(Note: I tried a ShiftOutFast file), which reduces a shiftout to 36 us but 3 times 36 is still over 100 us, too much to compare to a 3K256/23LC1024 solution.

11
  • 2
    If it made no difference then the chances are the bottleneck isn't the digital pin manipulation.
    – Majenko
    Commented Apr 8, 2017 at 13:05
  • 1
    The current digitalWrite() still does quite a lot of stuff. Commented Apr 8, 2017 at 14:57
  • 2
    Last time digitalWrite() changed was six years ago Commented Apr 8, 2017 at 15:20
  • 1
    I should point out that micros() has a 4 µs resolution, so a difference of less than 4 µs would not be measurable (if that is how you are measuring it).
    – Nick Gammon
    Commented Apr 9, 2017 at 0:03
  • 1
    a pinModeFast is 0 us (negligable) - it may in fact be 3 µs is my point. Unless you did a loop of 1000 of them, written in such a way that the compiler didn't optimize the loop away.
    – Nick Gammon
    Commented Apr 9, 2017 at 0:18

1 Answer 1

4

I just tried to time digitalWrite() against direct port access with the following code:

void setup()
{
    uint16_t overhead, start, end;

    Serial.begin(9600);
    TCCR1A = 0;
    TCCR1B = 1;

    // Time digitalWrite().
    overhead = 7;
    cli();
    start = TCNT1;
    digitalWrite(2, HIGH);
    end = TCNT1;
    sei();
    Serial.print("digitalWrite(): ");
    Serial.print(end - start - overhead);
    Serial.println(" cycles");

    // Time direct port access.
    overhead = 4;
    cli();
    start = TCNT1;
    PORTD &= ~_BV(PD2);
    end = TCNT1;
    sei();
    Serial.print("direct port access: ");
    Serial.print(end - start - overhead);
    Serial.println(" cycles");
}

void loop(){}

The result was:

digitalWrite(): 78 cycles
direct port access: 2 cycles

I used an old Arduino core (1.0.5), but that function has not changed since. You can try with your current core and see.

PS: In this program, the correct value for overhead can only be determined by examining the generated assembly. It's a count of the cycles you are measuring but do not directly belong to the operation you really want to measure.

4
  • WOW! That's a difference! Very interesting to know. Where does all that extra MPU cycle usage come from? Commented Apr 8, 2017 at 17:03
  • 1
    @EnricBlanco: Here is the link to the source code again. Look at the disassembly: saving registers to the stack and restoring them back (20 cycles) table lookups, tests... and 8 cycles of call overhead. Commented Apr 8, 2017 at 18:06
  • is there any related trick like this for the ESP8266?
    – dandavis
    Commented Apr 8, 2017 at 21:31
  • @dandavis: I have no experience on that chip. Commented Apr 8, 2017 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.