ESPHome 2025.5.0
Loading...
Searching...
No Matches
one_wire_bus.cpp
Go to the documentation of this file.
1#include "one_wire_bus.h"
3
4namespace esphome {
5namespace one_wire {
6
7static const char *const TAG = "one_wire";
8
9static const uint8_t DALLAS_MODEL_DS18S20 = 0x10;
10static const uint8_t DALLAS_MODEL_DS1822 = 0x22;
11static const uint8_t DALLAS_MODEL_DS18B20 = 0x28;
12static const uint8_t DALLAS_MODEL_DS1825 = 0x3B;
13static const uint8_t DALLAS_MODEL_DS28EA00 = 0x42;
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 return res == 1;
25}
26
27bool IRAM_ATTR OneWireBus::select(uint64_t address) {
28 if (!this->reset_())
29 return false;
30 this->write8(ONE_WIRE_ROM_SELECT);
31 this->write64(address);
32 return true;
33}
34
36 this->devices_.clear();
37
38 this->reset_search();
39 uint64_t address;
40 while (true) {
41 if (!this->reset_()) {
42 // Reset failed or no devices present
43 return;
44 }
45
46 this->write8(ONE_WIRE_ROM_SEARCH);
47 address = this->search_int();
48 if (address == 0)
49 break;
50 auto *address8 = reinterpret_cast<uint8_t *>(&address);
51 if (crc8(address8, 7) != address8[7]) {
52 ESP_LOGW(TAG, "Dallas device 0x%s has invalid CRC.", format_hex(address).c_str());
53 } else {
54 this->devices_.push_back(address);
55 }
56 }
57}
58
60 this->write8(0xCC); // skip ROM
61}
62
63const LogString *OneWireBus::get_model_str(uint8_t model) {
64 switch (model) {
65 case DALLAS_MODEL_DS18S20:
66 return LOG_STR("DS18S20");
67 case DALLAS_MODEL_DS1822:
68 return LOG_STR("DS1822");
69 case DALLAS_MODEL_DS18B20:
70 return LOG_STR("DS18B20");
71 case DALLAS_MODEL_DS1825:
72 return LOG_STR("DS1825");
73 case DALLAS_MODEL_DS28EA00:
74 return LOG_STR("DS28EA00");
75 default:
76 return LOG_STR("Unknown");
77 }
78}
79
80void OneWireBus::dump_devices_(const char *tag) {
81 if (this->devices_.empty()) {
82 ESP_LOGW(tag, " Found no devices!");
83 } else {
84 ESP_LOGCONFIG(tag, " Found devices:");
85 for (auto &address : this->devices_) {
86 ESP_LOGCONFIG(tag, " 0x%s (%s)", format_hex(address).c_str(), LOG_STR_ARG(get_model_str(address & 0xff)));
87 }
88 }
89}
90
91} // namespace one_wire
92} // namespace esphome
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_
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.
void skip()
Write a command to the bus that addresses all devices by skipping the ROM.
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.
const uint8_t ONE_WIRE_ROM_SEARCH
const uint8_t ONE_WIRE_ROM_SELECT
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t crc8(const uint8_t *data, uint8_t len)
Calculate a CRC-8 checksum of data with size len using the CRC-8-Dallas/Maxim polynomial.
Definition helpers.cpp:96
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition helpers.cpp:360