Dallas' 1-Wire bus protocol library
Dependents: DS1825 DISCO-F746-Dessiccateur-V1 watersenor_and_temp_code DS1820 ... more
Diff: OneWire.cpp
- Revision:
- 15:7f7759008807
- Parent:
- 14:12b783661ff4
- Child:
- 16:4c3edd30ad6e
--- a/OneWire.cpp Sun Jul 19 19:27:09 2020 +0000 +++ b/OneWire.cpp Mon Jul 20 08:09:13 2020 +0000 @@ -124,10 +124,25 @@ * @param * @retval */ -OneWire::OneWire(PinName pin) : - DigitalInOut(pin) +OneWire::OneWire(PinName pin, int sample_point_us /* = 13 */) : + DigitalInOut(pin), + _sample_point_us(sample_point_us) { + Timer timer; + MODE(); // set mode to either OpenDrain for STM or PullUp for others + + // Measure bus transition time from ouput to input + timer.reset(); + OUTPUT(); // set as output + WRITE(0); // pull the line down + timer.start(); + INPUT(); // set as input (and release the bus) + timer.stop(); + _out_to_in_transition_us = timer.read_us(); + + MBED_ASSERT(_out_to_in_transition_us < _sample_point_us); + INIT_WAIT; #if ONEWIRE_SEARCH reset_search(); @@ -187,23 +202,14 @@ */ uint8_t OneWire::read_bit(void) { - const int SAMPLE_POINT = 10; uint8_t r; - int t; OUTPUT(); WRITE(0); - timer.start(); INPUT(); - t = timer.read_us(); - if (t < SAMPLE_POINT) - WAIT_US(SAMPLE_POINT - t); + wait_us(_sample_point_us - _out_to_in_transition_us); // wait till sample point r = READ(); - timer.stop(); WAIT_US(55); - //printf("t = %d\r\n", t); - //printf("t1 = %d\r\n", timer.read_us() - t); - timer.reset(); return r; }