ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
gpio_one_wire.cpp
Go to the documentation of this file.
1#include "gpio_one_wire.h"
3#include "esphome/core/log.h"
4
5namespace esphome::gpio {
6
7ESPHOME_LOG_TAG(TAG, "gpio.one_wire");
8
10 this->t_pin_->setup();
12 // clear bus with 480µs high, otherwise initial reset in search might fail
13 this->pin_.digital_write(true);
16 this->search();
17}
18
20 ESP_LOGCONFIG(TAG, "GPIO 1-wire bus:");
21 LOG_PIN(" Pin: ", this->t_pin_);
22 this->dump_devices_(TAG);
23}
24
25// Lock only what must not stretch; the ESP8266 Wi-Fi interrupt handler asserts (hardware WDT) when held off ~1 ms.
26// A longer gap between slots is within spec.
27int HOT IRAM_ATTR GPIOOneWireBus::reset_int() {
28 // See reset here:
29 // https://www.maximintegrated.com/en/design/technical-documents/app-notes/1/126.html
30 // Wait for communication to clear (delay G)
32 uint8_t retries = 125;
33 do {
34 if (--retries == 0)
35 return -1;
37 } while (!this->pin_.digital_read());
38
39 bool r = false;
40 uint32_t start;
41 {
42 // Locked from the reset pulse through presence detection (at most about 780 us): a reset pulse stretched past
43 // 960 us power-resets a parasite-powered sensor
45 // Send 480µs LOW TX reset pulse (drive bus low, delay H)
46 this->pin_.digital_write(false);
49
50 // Release the bus, delay I
52 start = micros();
54
55 while (micros() - start < 300) {
56 // sample bus, 0=device(s) present, 1=no device present
57 r = !this->pin_.digital_read();
58 if (r)
59 break;
61 }
62 }
63
64 // delay J: finish the 480us slot, but never spin if it already elapsed (unsigned wrap)
65 uint32_t elapsed = micros() - start;
66 if (elapsed < 480)
67 delayMicroseconds(480 - elapsed);
68 this->pin_.digital_write(true);
70 return r ? 1 : 0;
71}
72
73void HOT IRAM_ATTR GPIOOneWireBus::write_bit_(bool bit) {
74 // from datasheet:
75 // write 0 low time: t_low0: min=60µs, max=120µs
76 // write 1 low time: t_low1: min=1µs, max=15µs
77 // time slot: t_slot: min=60µs, max=120µs
78 // recovery time: t_rec: min=1µs
79 // ds18b20 appears to read the bus after roughly 14µs
80 uint32_t delay0 = bit ? 6 : 60;
81 uint32_t delay1 = bit ? 64 : 10;
82
83 {
85 // drive bus low
86 this->pin_.digital_write(false);
87 // delay A/C
88 delayMicroseconds(delay0);
89 // release bus
90 this->pin_.digital_write(true);
91 }
92 // delay B/D
93 delayMicroseconds(delay1);
94}
95
96bool HOT IRAM_ATTR GPIOOneWireBus::read_bit_() {
97 bool r;
98 {
100 // drive bus low
101 this->pin_.digital_write(false);
102
103 // datasheet says >= 1µs
105
106 // release bus, delay E
108
110 // sample bus to read bit from peer
111 r = this->pin_.digital_read();
112 }
113
114 // read slot is at least 60µs
116
117 this->pin_.digital_write(true);
119 return r;
120}
121
122void IRAM_ATTR GPIOOneWireBus::write8(uint8_t val) {
123 for (uint8_t i = 0; i < 8; i++) {
124 this->write_bit_(bool((1u << i) & val));
125 }
126}
127
128void IRAM_ATTR GPIOOneWireBus::write64(uint64_t val) {
129 for (uint8_t i = 0; i < 64; i++) {
130 this->write_bit_(bool((1ULL << i) & val));
131 }
132}
133
134uint8_t IRAM_ATTR GPIOOneWireBus::read8() {
135 uint8_t ret = 0;
136 for (uint8_t i = 0; i < 8; i++)
137 ret |= (uint8_t(this->read_bit_()) << i);
138 return ret;
139}
140
141uint64_t IRAM_ATTR GPIOOneWireBus::read64() {
142 uint64_t ret = 0;
143 for (uint8_t i = 0; i < 64; i++) {
144 ret |= (uint64_t(this->read_bit_()) << i);
145 }
146 return ret;
147}
148
150 this->last_discrepancy_ = 0;
151 this->last_device_flag_ = false;
152 this->address_ = 0;
153}
154
155uint64_t IRAM_ATTR GPIOOneWireBus::search_int() {
156 if (this->last_device_flag_)
157 return 0u;
158
159 uint8_t last_zero = 0;
160 uint64_t bit_mask = 1;
161 uint64_t address = this->address_;
162
163 // Initiate search
164 for (int bit_number = 1; bit_number <= 64; bit_number++, bit_mask <<= 1) {
165 // read bit
166 bool id_bit = this->read_bit_();
167 // read its complement
168 bool cmp_id_bit = this->read_bit_();
169
170 if (id_bit && cmp_id_bit) {
171 // No devices participating in search
172 return 0;
173 }
174
175 bool branch;
176
177 if (id_bit != cmp_id_bit) {
178 // only chose one branch, the other one doesn't have any devices.
179 branch = id_bit;
180 } else {
181 // there are devices with both 0s and 1s at this bit
182 if (bit_number < this->last_discrepancy_) {
183 branch = (address & bit_mask) > 0;
184 } else {
185 branch = bit_number == this->last_discrepancy_;
186 }
187
188 if (!branch) {
189 last_zero = bit_number;
190 }
191 }
192
193 if (branch) {
194 address |= bit_mask;
195 } else {
196 address &= ~bit_mask;
197 }
198
199 // choose/announce branch
200 this->write_bit_(branch);
201 }
202
203 this->last_discrepancy_ = last_zero;
204 if (this->last_discrepancy_ == 0) {
205 // we're at root and have no choices left, so this was the last one.
206 this->last_device_flag_ = true;
207 }
208
209 this->address_ = address;
210 return address;
211}
212
213} // namespace esphome::gpio
void digital_write(bool value)
Definition gpio.cpp:234
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:246
Helper class to disable interrupts.
Definition helpers.h:2251
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:195
void write64(uint64_t val) override
void write8(uint8_t val) override
void dump_devices_(const char *tag)
log the found devices
void search()
Search for 1-Wire devices on the bus.
int ret
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "gpio.binary_sensor")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
static void uint32_t
SemaphoreHandle_t lock