Revo onewire library, changes to work on FRDM-K22
Fork of OneWire by
Diff: OneWire.cpp
- Revision:
- 6:28e60d774095
- Parent:
- 5:45f3eb39b00c
- Child:
- 7:a42114ac702f
--- a/OneWire.cpp Sat Jan 07 11:20:08 2017 +0000 +++ b/OneWire.cpp Sat Sep 09 09:10:49 2017 +0000 @@ -129,30 +129,76 @@ // the bus to come high, if it doesn't then it is broken or shorted // and we return a 0; // -// Returns 1 if a device asserted a presence pulse, 0 otherwise. +// Returns ONEWIRE_OK if a device asserted a presence pulse, ONEWIRE_ERROR otherwise. // -uint8_t OneWire::reset(void) + + +uint8_t OneWire::init_sequence() { - uint8_t r; + uint16_t retries = 20000; + + /*if( reset(false) == ONEWIRE_ERROR ) + return ONEWIRE_ERROR;*/ + + reset(); + + //wire.input(); should be input coming out of reset + wait_us(80); + + /* uint8_t result = wire.read_bit(); + + if ( result == 0 ) + { + while( result == 0) + { + result = wire.read_bit(); + + if (--retries == 0) { + return ONEWIRE_NO_RESPONSE; + } + } + + return ONEWIRE_OK; + } + + return ONEWIRE_NO_RESPONSE;*/ + + + return ONEWIRE_OK; +} + +uint8_t OneWire::reset( bool force/* = true*/ ) +{ uint8_t retries = 125; - wire.input(); + /*wire.input(); // wait until the wire is high... just in case do { if (--retries == 0) { - return 0; + if( force ) + break; + else + return ONEWIRE_ERROR; } wait_us(2); - } while (wire.read() != 1); + } while (wire.read() != 1);*/ wire.output(); wire = 0; - wait_us(480); + wait_us(500); wire.input(); + wait_us(70); - r = !wire.read(); - wait_us(410); - return r; + uint8_t err = wire; //just do the reset, use init_seq function to do more + + wait_us(240); + if ( wire == 0 ) { // short circuit + printf("OneWire::reset SHORT_CIRCUIT\r\n"); + return ONEWIRE_ERROR_SHORT_CIRCUIT; + } + + printf("OneWire::reset OK\r\n"); + return ONEWIRE_OK; } // @@ -161,7 +207,8 @@ // void OneWire::write_bit(uint8_t v) { - wire.output(); + wire.output(); + if (v & 1) { wire = 0; // drive output low wait_us(10); @@ -185,11 +232,11 @@ wire.output(); wire = 0; - //wait_us(1); + wait_us(2); //modified to meet ray's values wire.input(); - wait_us(5); - r = wire.read(); - wait_us(54); + wait_us(14); + r = wire; + wait_us(45); return r; } @@ -202,9 +249,11 @@ // void OneWire::write(uint8_t v, uint8_t power /* = 0 */) { uint8_t bitMask; + + printf("OneWire::write => %#x\r\n", v); for (bitMask = 0x01; bitMask; bitMask <<= 1) { - OneWire::write_bit( (bitMask & v)?1:0); + OneWire::write_bit( (bitMask & v)?1:0); } if ( !power) { wire.input(); @@ -212,11 +261,15 @@ } void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) { - for (uint16_t i = 0 ; i < count ; i++) - write(buf[i]); - if (!power) { + + for (uint16_t i = 0 ; i < count ; i++) + { + write(buf[i]); + } + + if (!power) { wire.input(); - } + } } //