ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
one_wire_bus.cpp
Go to the documentation of this file.
1#include "one_wire_bus.h"
3
4namespace esphome::one_wire {
5
6ESPHOME_LOG_TAG(TAG, "one_wire");
7
8static const uint8_t DALLAS_MODEL_DS18S20 = 0x10;
9static const uint8_t DALLAS_MODEL_DS1822 = 0x22;
10static const uint8_t DALLAS_MODEL_DS18B20 = 0x28;
11static const uint8_t DALLAS_MODEL_DS1825 = 0x3B;
12static const uint8_t DALLAS_MODEL_DS28EA00 = 0x42;
13static const uint8_t MAXIM_MODEL_MAX31888 = 0x54;
14
15const uint8_t ONE_WIRE_ROM_SELECT = 0x55;
16const uint8_t ONE_WIRE_ROM_SEARCH = 0xF0;
17
18const std::vector<uint64_t> &OneWireBus::get_devices() { return this->devices_; }
19
21 int res = this->reset_int();
22 if (res == -1) {
23 ESP_LOGE(TAG, "1-wire bus is held low");
24 }
25 return res == 1;
26}
27
28bool IRAM_ATTR OneWireBus::select(uint64_t address) {
29 if (!this->reset_())
30 return false;
31 this->write8(ONE_WIRE_ROM_SELECT);
32 this->write64(address);
33 return true;
34}
35
37 this->devices_.clear();
38
39 this->reset_search();
40 uint64_t address;
41 while (true) {
42 if (!this->reset_()) {
43 // Reset failed or no devices present
44 return;
45 }
46
47 this->write8(ONE_WIRE_ROM_SEARCH);
48 address = this->search_int();
49 if (address == 0)
50 break;
51 auto *address8 = reinterpret_cast<uint8_t *>(&address);
52 if (crc8(address8, 7) != address8[7]) {
53 char hex_buf[17];
54 ESP_LOGW(TAG, "Dallas device 0x%s has invalid CRC.", format_hex_to(hex_buf, address));
55 } else {
56 this->devices_.push_back(address);
57 }
58 }
59}
60
62 if (!this->reset_())
63 return false;
64 this->write8(0xCC); // skip ROM
65 return true;
66}
67
68const LogString *OneWireBus::get_model_str(uint8_t model) {
69 switch (model) {
70 case DALLAS_MODEL_DS18S20:
71 return LOG_STR("DS18S20");
72 case DALLAS_MODEL_DS1822:
73 return LOG_STR("DS1822");
74 case DALLAS_MODEL_DS18B20:
75 return LOG_STR("DS18B20");
76 case DALLAS_MODEL_DS1825:
77 return LOG_STR("DS1825");
78 case DALLAS_MODEL_DS28EA00:
79 return LOG_STR("DS28EA00");
80 case MAXIM_MODEL_MAX31888:
81 return LOG_STR("MAX31888");
82 default:
83 return LOG_STR("Unknown");
84 }
85}
86
87void OneWireBus::dump_devices_(const char *tag) {
88 if (this->devices_.empty()) {
89 ESP_LOGW(tag, " Found no devices!");
90 } else {
91 ESP_LOGCONFIG(tag, " Found devices:");
92 char hex_buf[17]; // uint64_t = 16 hex chars + null
93 for (auto &address : this->devices_) {
94 ESP_LOGCONFIG(tag, " 0x%s (%s)", format_hex_to(hex_buf, address), LOG_STR_ARG(get_model_str(address & 0xff)));
95 }
96 }
97}
98
99} // namespace esphome::one_wire
uint8_t address
Definition bl0906.h:4
virtual void reset_search()=0
Reset the device search.
const std::vector< uint64_t > & get_devices()
Return the list of found devices.
std::vector< uint64_t > devices_
bool skip()
Write a command to the bus that addresses all devices by skipping the ROM.
void dump_devices_(const char *tag)
log the found devices
virtual int reset_int()=0
Bus Reset.
bool reset_()
Reset the bus, should be done before all write operations.
virtual void write64(uint64_t val)=0
Write a 64 bit unsigned integer to the bus. LSB first.
bool select(uint64_t address)
Select a specific address on the bus for the following command.
const LogString * get_model_str(uint8_t model)
Get the description string for this model.
virtual void write8(uint8_t val)=0
Write a word to the bus. LSB first.
void search()
Search for 1-Wire devices on the bus.
virtual uint64_t search_int()=0
Search for a 1-Wire device on the bus. Returns 0 if all devices have been found.
ESPHOME_LOG_TAG(TAG, "one_wire")
const uint8_t ONE_WIRE_ROM_SEARCH
const uint8_t ONE_WIRE_ROM_SELECT
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
Definition helpers.cpp:68
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:428