36 #include "freertos/FreeRTOS.h"
37 #include "freertos/task.h"
39 #include "sdkconfig.h"
40 #include "driver/gpio.h"
45 static const char *
TAG = CONFIG_OWB_TAG;
59 ESP_LOGE(
TAG,
"bus is not initialised");
64 ESP_LOGE(
TAG,
"bus is NULL");
75 static uint8_t _calc_crc(uint8_t crc, uint8_t data)
78 static const uint8_t table[256] = {
79 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
80 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
81 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
82 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
83 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
84 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
85 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
86 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
87 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
88 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
89 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
90 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
91 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
92 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
93 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
94 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
96 return table[crc ^ data];
99 static uint8_t _calc_crc_block(uint8_t crc,
const uint8_t *buffer,
size_t len)
103 crc = _calc_crc(crc, *buffer++);
104 ESP_LOGD(
TAG,
"buffer 0x%02x, crc 0x%02x, len %d", (uint8_t) * (buffer - 1), (
int)crc, (
int)len);
118 int id_bit_number = 1;
120 int rom_byte_number = 0;
122 uint8_t cmp_id_bit = 0;
123 uint8_t rom_byte_mask = 1;
124 uint8_t search_direction = 0;
125 bool search_result =
false;
151 id_bit = cmp_id_bit = 0;
158 if (id_bit && cmp_id_bit)
165 if (id_bit != cmp_id_bit)
167 search_direction = (id_bit) ? 1 : 0;
173 if (id_bit_number < state->last_discrepancy)
175 search_direction = ((state->
rom_code.
bytes[rom_byte_number] & rom_byte_mask) > 0);
184 if (search_direction == 0)
186 last_zero = id_bit_number;
198 if (search_direction == 1)
216 if (rom_byte_mask == 0)
223 }
while (rom_byte_number < 8);
226 if (!((id_bit_number < 65) || (crc8 != 0)))
237 search_result =
true;
247 search_result =
false;
252 *is_found = search_result;
284 else if (!_is_init(bus))
307 else if (!_is_init(bus))
330 else if (!_is_init(bus))
341 ESP_LOGW(
TAG,
"Strong pull-up GPIO set with parasitic-power disabled");
344 gpio_pad_select_gpio(gpio);
345 gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
367 if (!bus || !rom_code)
371 else if (!_is_init(bus))
389 ESP_LOGE(
TAG,
"CRC failed");
404 ESP_LOGD(
TAG,
"rom_code %s", rom_code_s);
409 ESP_LOGE(
TAG,
"ds18b20 device not responding");
421 if (!bus || !is_present)
425 else if (!_is_init(bus))
433 .last_discrepancy = 64,
434 .last_device_flag =
false,
437 bool is_found =
false;
438 _search(bus, &state, &is_found);
442 for (
int i = 0; i <
sizeof(state.
rom_code.
bytes) && result; ++i)
449 ESP_LOGD(
TAG,
"state.last_discrepancy %d, state.last_device_flag %d, is_found %d",
452 ESP_LOGD(
TAG,
"rom code %sfound", result ?
"" :
"not ");
453 *is_present = result;
464 if (!bus || !a_device_present)
468 else if (!_is_init(bus))
474 status = bus->
driver->
reset(bus, a_device_present);
488 else if (!_is_init(bus))
495 ESP_LOGD(
TAG,
"owb_read_bit: %02x", *out);
510 else if (!_is_init(bus))
517 ESP_LOGD(
TAG,
"owb_read_byte: %02x", *out);
532 else if (!_is_init(bus))
538 for (
int i = 0; i < len; ++i)
545 ESP_LOGD(
TAG,
"owb_read_bytes, len %d:", len);
546 ESP_LOG_BUFFER_HEX_LEVEL(
TAG, buffer, len, ESP_LOG_DEBUG);
562 else if (!_is_init(bus))
568 ESP_LOGD(
TAG,
"owb_write_bit: %02x", bit);
584 else if (!_is_init(bus))
590 ESP_LOGD(
TAG,
"owb_write_byte: %02x", data);
606 else if (!_is_init(bus))
612 ESP_LOGD(
TAG,
"owb_write_bytes, len %d:", len);
613 ESP_LOG_BUFFER_HEX_LEVEL(
TAG, buffer, len, ESP_LOG_DEBUG);
615 for (
int i = 0; i < len; i++)
634 else if (!_is_init(bus))
649 return _calc_crc(crc, data);
654 return _calc_crc_block(crc, data, len);
662 if (!bus || !state || !found_device)
666 else if (!_is_init(bus))
676 _search(bus, state, &result);
679 *found_device = result;
690 if (!bus || !state || !found_device)
694 else if (!_is_init(bus))
700 _search(bus, state, &result);
703 *found_device = result;
711 for (
int i =
sizeof(rom_code.
bytes) - 1; i >= 0; i--)
713 sprintf(buffer,
"%02x", rom_code.
bytes[i]);
727 else if (!_is_init(bus))
736 ESP_LOGD(
TAG,
"strong pullup GPIO %d", enable);
owb_status owb_set_strong_pullup(const OneWireBus *bus, bool enable)
Enable or disable the strong-pullup GPIO, if configured.
owb_status owb_read_byte(const OneWireBus *bus, uint8_t *out)
Read a single byte from the 1-Wire bus.
uint8_t owb_crc8_byte(uint8_t crc, uint8_t data)
1-Wire 8-bit CRC lookup.
owb_status owb_read_rom(const OneWireBus *bus, OneWireBus_ROMCode *rom_code)
Read ROM code from device - only works when there is a single device on the bus.
owb_status owb_use_parasitic_power(OneWireBus *bus, bool use_parasitic_power)
Enable or disable use of parasitic power on the One Wire Bus. This affects how devices signal on the ...
uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t *data, size_t len)
1-Wire 8-bit CRC lookup with accumulation over a block of bytes.
owb_status owb_write_rom_code(const OneWireBus *bus, OneWireBus_ROMCode rom_code)
Write a ROM code to the 1-Wire bus ensuring LSB is sent first.
owb_status owb_write_bytes(const OneWireBus *bus, const uint8_t *buffer, size_t len)
Write a number of bytes to the 1-Wire bus.
char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char *buffer, size_t len)
Create a string representation of a ROM code, most significant byte (CRC8) first.
owb_status owb_read_bytes(const OneWireBus *bus, uint8_t *buffer, unsigned int len)
Read a number of bytes from the 1-Wire bus.
owb_status owb_use_crc(OneWireBus *bus, bool use_crc)
Enable or disable use of CRC checks on device communications.
owb_status owb_read_bit(const OneWireBus *bus, uint8_t *out)
Read a single bit from the 1-Wire bus.
owb_status owb_uninitialize(OneWireBus *bus)
call to release resources after completing use of the OneWireBus
owb_status owb_write_byte(const OneWireBus *bus, uint8_t data)
Write a single byte to the 1-Wire bus.
owb_status owb_search_next(const OneWireBus *bus, OneWireBus_SearchState *state, bool *found_device)
Locates the next device on the 1-Wire bus, if present, starting from the provided state....
owb_status owb_use_strong_pullup_gpio(OneWireBus *bus, gpio_num_t gpio)
Enable or disable use of extra GPIO to activate strong pull-up circuit. This only has effect if paras...
owb_status owb_search_first(const OneWireBus *bus, OneWireBus_SearchState *state, bool *found_device)
Locates the first device on the 1-Wire bus, if present.
owb_status owb_reset(const OneWireBus *bus, bool *a_device_present)
Reset the 1-Wire bus.
owb_status owb_write_bit(const OneWireBus *bus, const uint8_t bit)
Write a bit to the 1-Wire bus.
owb_status owb_verify_rom(const OneWireBus *bus, OneWireBus_ROMCode rom_code, bool *is_present)
Verify the device specified by ROM code is present.
Interface definitions for the 1-Wire bus component.
#define OWB_ROM_READ
Read device ROM (single device on bus only)
owb_status
Represents the result of OWB API functions.
@ OWB_STATUS_PARAMETER_NULL
Function was passed a null pointer.
@ OWB_STATUS_CRC_FAILED
CRC failed on data received from a device or devices.
@ OWB_STATUS_OK
Operation succeeded.
@ OWB_STATUS_NOT_SET
A status value has not been set.
@ OWB_STATUS_NOT_INITIALIZED
Function was passed an uninitialised variable.
@ OWB_STATUS_DEVICE_NOT_RESPONDING
No response received from the addressed device or devices.
#define OWB_ROM_CODE_STRING_LENGTH
Typical length of OneWire bus ROM ID as ASCII hex string, including null terminator.
#define OWB_ROM_SEARCH
Perform Search ROM cycle to identify devices on the bus.
#define GPIO_NUM_NC
ESP-IDF prior to v4.x does not define GPIO_NUM_NC.
Interface definitions for the ESP32 GPIO driver used to communicate with devices on the One Wire Bus.
Represents the state of a device search on the 1-Wire bus.
int last_family_discrepancy
Bit index that identifies the last discrepancy within the first 8-bit family code of the ROM code.
int last_device_flag
Flag to indicate previous search was the last device detected.
OneWireBus_ROMCode rom_code
Device ROM code.
int last_discrepancy
Bit index that identifies from which bit the next search discrepancy check should start.
Structure containing 1-Wire bus information relevant to a single instance.
gpio_num_t strong_pullup_gpio
Set if an external strong pull-up circuit is required.
bool use_parasitic_power
True if parasitic-powered devices are expected on the bus.
const struct owb_driver * driver
Pointer to hardware driver instance.
bool use_crc
True if CRC checks are to be used when retrieving information from a device on the bus.
owb_status(* read_bits)(const OneWireBus *bus, uint8_t *in, int number_of_bits_to_read)
owb_status(* uninitialize)(const OneWireBus *bus)
owb_status(* write_bits)(const OneWireBus *bus, uint8_t out, int number_of_bits_to_write)
owb_status(* reset)(const OneWireBus *bus, bool *is_present)
Represents a 1-Wire ROM Code. This is a sequence of eight bytes, where the first byte is the family n...
uint8_t bytes[8]
Provides raw byte access.