PaulStoffregen / OneWire

Library for Dallas/Maxim 1-Wire Chips
http://www.pjrc.com/teensy/td_libs_OneWire.html
579 stars 382 forks source link

Removed redundant code #11

Open mdaiter opened 8 years ago

mdaiter commented 8 years ago

There was redundant code within the if statement of the WriteBit function. Tried to reduce redundancy for compiler output's sake.

PaulStoffregen commented 8 years ago

Have you tested the timing?

This code is fewer lines of source, and may compile to few bytes, but it does more work within the timing critical sections. The original was designed to push all the extra stuff outside of the interrupt disable regions.

mdaiter commented 8 years ago

Haven't tested the timing due to not having a board. However, it's just a boolean comparison. Would it really affect timing that much?

PaulStoffregen commented 8 years ago

Yes, OneWire is timing critical. Especially on slow 8 bit processors like AVR, these small details matter.

orgua commented 6 years ago

yes, timing is critical, but the master is mostly dictating the timings. and a 1 MHz processor can waste a cycle and just spend 1µs on it. still ok. proposal to stay outside of atomic-code:

    const uint8_t time_high = uint8_t(value ? 10 : 60);
    const uint8_t time_low  = uint8_t(value ? 55 : 5);

    noInterrupts();
    DIRECT_WRITE_LOW(_baseReg, _bitMask);
    DIRECT_MODE_OUTPUT(_baseReg, _bitMask);    // drive output low
    delayMicroseconds(time_high);
    DIRECT_WRITE_HIGH(_baseReg, _bitMask);    // drive output high
    interrupts();
    delayMicroseconds(time_low);