13 changed files with 0 additions and 4511 deletions
@ -1,21 +0,0 @@ |
|||
MIT License |
|||
|
|||
Copyright (c) 2017 David Antliff |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
|||
@ -1,9 +0,0 @@ |
|||
#
|
|||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
|||
# project subdirectory.
|
|||
#
|
|||
|
|||
PROJECT_NAME := ds18b20 |
|||
|
|||
include $(IDF_PATH)/make/project.mk |
|||
|
|||
@ -1,60 +0,0 @@ |
|||
# ESP32-DS18B20 |
|||
|
|||
## Introduction |
|||
|
|||
This is a ESP32-compatible C library for the Maxim Integrated DS18B20 Programmable Resolution 1-Wire Digital Thermometer device. |
|||
|
|||
It supports multiple devices on the same 1-Wire bus. |
|||
|
|||
It is written and tested for the [ESP-IDF](https://github.com/espressif/esp-idf) environment, using the xtensa-esp32-elf toolchain (gcc version 5.2.0). |
|||
|
|||
## Features |
|||
|
|||
This library includes: |
|||
|
|||
* External power supply mode (parasitic mode not yet supported). |
|||
* Static (stack-based) or dynamic (malloc-based) memory model. |
|||
* No globals - support any number of 1-Wire buses simultaneously. |
|||
* 1-Wire device detection and validation, including search for multiple devices on a single bus. |
|||
* Addressing optimisation for a single (solo) device on a bus. |
|||
* 1-Wire bus operations including multi-byte read and write operations. |
|||
* CRC checks on ROM code and temperature data. |
|||
* Programmable temperature measurement resolution (9, 10, 11 or 12-bit resolution). |
|||
* Temperature conversion and retrieval. |
|||
* Separation of conversion and temperature retrieval to allow for simultaneous conversion across multiple devices. |
|||
|
|||
## Documentation |
|||
|
|||
Automatically generated API documentation (doxygen) is available [here](https://davidantliff.github.io/ESP32-DS18B20/index.html). |
|||
|
|||
## Source Code |
|||
|
|||
The source is available from [GitHub](https://www.github.com/DavidAntliff/ESP32-DS18B20). |
|||
|
|||
## License |
|||
|
|||
The code in this project is licensed under the MIT license - see LICENSE for details. |
|||
|
|||
## Links |
|||
|
|||
* [DS18B20 Datasheet](http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf) |
|||
* [1-Wire Communication Through Software](https://www.maximintegrated.com/en/app-notes/index.mvp/id/126) |
|||
* [1-Wire Search Algorithm](https://www.maximintegrated.com/en/app-notes/index.mvp/id/187) |
|||
* [Espressif IoT Development Framework for ESP32](https://github.com/espressif/esp-idf) |
|||
|
|||
## Acknowledgements |
|||
|
|||
Parts of this code are based on references provided to the public domain by Maxim Integrated. |
|||
|
|||
"1-Wire" is a registered trademark of Maxim Integrated. |
|||
|
|||
## Roadmap |
|||
|
|||
The following features are anticipated but not yet implemented: |
|||
|
|||
* Documented examples. |
|||
* Concurrency support (multiple tasks accessing devices on the same bus). |
|||
* Alarm support. |
|||
* EEPROM support. |
|||
* Parasitic power support. |
|||
* FreeRTOS event-based example. |
|||
@ -1,3 +0,0 @@ |
|||
html/ |
|||
latex/ |
|||
|
|||
File diff suppressed because it is too large
@ -1,4 +0,0 @@ |
|||
# Getting Started |
|||
|
|||
TODO... |
|||
|
|||
@ -1,5 +0,0 @@ |
|||
#
|
|||
# "main" pseudo-component makefile.
|
|||
#
|
|||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
|||
|
|||
@ -1,470 +0,0 @@ |
|||
/*
|
|||
* MIT License |
|||
* |
|||
* Copyright (c) 2017 David Antliff |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in all |
|||
* copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
* SOFTWARE. |
|||
*/ |
|||
|
|||
/**
|
|||
* @file ds18b20.c |
|||
* |
|||
* Resolution is cached in the DS18B20_Info object to avoid querying the hardware |
|||
* every time a temperature conversion is required. However this can result in the |
|||
* cached value becoming inconsistent with the hardware value, so care must be taken. |
|||
* |
|||
*/ |
|||
|
|||
#include <stddef.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <stdbool.h> |
|||
#include <stdint.h> |
|||
#include <math.h> |
|||
|
|||
#include "freertos/FreeRTOS.h" |
|||
#include "freertos/task.h" |
|||
#include "driver/gpio.h" |
|||
#include "esp_system.h" |
|||
#include "esp_log.h" |
|||
#include "sdkconfig.h" |
|||
|
|||
#include "ds18b20.h" |
|||
#include "owb.h" |
|||
|
|||
static const char * TAG = "ds18b20"; |
|||
static const int T_CONV = 750; // maximum conversion time at 12-bit resolution in milliseconds
|
|||
|
|||
// Function commands
|
|||
#define DS18B20_FUNCTION_TEMP_CONVERT 0x44 |
|||
#define DS18B20_FUNCTION_SCRATCHPAD_WRITE 0x4E |
|||
#define DS18B20_FUNCTION_SCRATCHPAD_READ 0xBE |
|||
#define DS18B20_FUNCTION_SCRATCHPAD_COPY 0x48 |
|||
#define DS18B20_FUNCTION_EEPROM_RECALL 0xB8 |
|||
#define DS18B20_FUNCTION_POWER_SUPPLY_READ 0xB4 |
|||
|
|||
/// @cond ignore
|
|||
typedef struct |
|||
{ |
|||
uint8_t temperature[2]; // [0] is LSB, [1] is MSB
|
|||
uint8_t trigger_high; |
|||
uint8_t trigger_low; |
|||
uint8_t configuration; |
|||
uint8_t reserved[3]; |
|||
uint8_t crc; |
|||
} Scratchpad; |
|||
/// @endcond ignore
|
|||
|
|||
static void _init(DS18B20_Info * ds18b20_info, OneWireBus * bus) |
|||
{ |
|||
if (ds18b20_info != NULL) |
|||
{ |
|||
ds18b20_info->bus = bus; |
|||
memset(&ds18b20_info->rom_code, 0, sizeof(ds18b20_info->rom_code)); |
|||
ds18b20_info->use_crc = false; |
|||
ds18b20_info->resolution = DS18B20_RESOLUTION_INVALID; |
|||
ds18b20_info->solo = false; // assume multiple devices unless told otherwise
|
|||
ds18b20_info->init = true; |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20_info is NULL"); |
|||
} |
|||
} |
|||
|
|||
static bool _is_init(const DS18B20_Info * ds18b20_info) |
|||
{ |
|||
bool ok = false; |
|||
if (ds18b20_info != NULL) |
|||
{ |
|||
if (ds18b20_info->init) |
|||
{ |
|||
// OK
|
|||
ok = true; |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20_info is not initialised"); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20_info is NULL"); |
|||
} |
|||
return ok; |
|||
} |
|||
|
|||
static bool _address_device(const DS18B20_Info * ds18b20_info) |
|||
{ |
|||
bool present = false; |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
present = owb_reset(ds18b20_info->bus); |
|||
if (present) |
|||
{ |
|||
if (ds18b20_info->solo) |
|||
{ |
|||
// if there's only one device on the bus, we can skip
|
|||
// sending the ROM code and instruct it directly
|
|||
owb_write_byte(ds18b20_info->bus, OWB_ROM_SKIP); |
|||
} |
|||
else |
|||
{ |
|||
// if there are multiple devices on the bus, a Match ROM command
|
|||
// must be issued to address a specific slave
|
|||
owb_write_byte(ds18b20_info->bus, OWB_ROM_MATCH); |
|||
owb_write_rom_code(ds18b20_info->bus, ds18b20_info->rom_code); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20 device not responding"); |
|||
} |
|||
} |
|||
return present; |
|||
} |
|||
|
|||
static bool _check_resolution(DS18B20_RESOLUTION resolution) |
|||
{ |
|||
return (resolution >= DS18B20_RESOLUTION_9_BIT) && (resolution <= DS18B20_RESOLUTION_12_BIT); |
|||
} |
|||
|
|||
static float _wait_for_conversion(DS18B20_RESOLUTION resolution) |
|||
{ |
|||
float elapsed_time = 0.0f; |
|||
if (_check_resolution(resolution)) |
|||
{ |
|||
int divisor = 1 << (DS18B20_RESOLUTION_12_BIT - resolution); |
|||
ESP_LOGD(TAG, "divisor %d", divisor); |
|||
float max_conversion_time = (float)T_CONV / (float)divisor; |
|||
int ticks = ceil(max_conversion_time / portTICK_PERIOD_MS); |
|||
ESP_LOGD(TAG, "wait for conversion: %.3f ms, %d ticks", max_conversion_time, ticks); |
|||
|
|||
// wait at least this maximum conversion time
|
|||
vTaskDelay(ticks); |
|||
|
|||
// TODO: measure elapsed time more accurately
|
|||
elapsed_time = ticks * portTICK_PERIOD_MS; |
|||
} |
|||
return elapsed_time; |
|||
} |
|||
|
|||
static float _decode_temp(uint8_t lsb, uint8_t msb, DS18B20_RESOLUTION resolution) |
|||
{ |
|||
float result = 0.0f; |
|||
if (_check_resolution(resolution)) |
|||
{ |
|||
// masks to remove undefined bits from result
|
|||
static const uint8_t lsb_mask[4] = { ~0x03, ~0x02, ~0x01, ~0x00 }; |
|||
uint8_t lsb_masked = lsb_mask[resolution - DS18B20_RESOLUTION_9_BIT] & lsb; |
|||
int16_t raw = (msb << 8) | lsb_masked; |
|||
result = raw / 16.0f; |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "Unsupported resolution %d", resolution); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
static size_t _min(size_t x, size_t y) |
|||
{ |
|||
return x > y ? y : x; |
|||
} |
|||
|
|||
static Scratchpad _read_scratchpad(const DS18B20_Info * ds18b20_info, size_t count) |
|||
{ |
|||
count = _min(sizeof(Scratchpad), count); // avoid overflow
|
|||
Scratchpad scratchpad = {0}; |
|||
ESP_LOGD(TAG, "scratchpad read %d bytes: ", count); |
|||
if (_address_device(ds18b20_info)) |
|||
{ |
|||
owb_write_byte(ds18b20_info->bus, DS18B20_FUNCTION_SCRATCHPAD_READ); |
|||
owb_read_bytes(ds18b20_info->bus, (uint8_t *)&scratchpad, count); |
|||
esp_log_buffer_hex(TAG, &scratchpad, count); |
|||
} |
|||
return scratchpad; |
|||
} |
|||
|
|||
static bool _write_scratchpad(const DS18B20_Info * ds18b20_info, const Scratchpad * scratchpad, bool verify) |
|||
{ |
|||
bool result = false; |
|||
// Only bytes 2, 3 and 4 (trigger and configuration) can be written.
|
|||
// All three bytes MUST be written before the next reset to avoid corruption.
|
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
if (_address_device(ds18b20_info)) |
|||
{ |
|||
owb_write_byte(ds18b20_info->bus, DS18B20_FUNCTION_SCRATCHPAD_WRITE); |
|||
owb_write_bytes(ds18b20_info->bus, (uint8_t *)&scratchpad->trigger_high, 3); |
|||
ESP_LOGD(TAG, "scratchpad write 3 bytes:"); |
|||
esp_log_buffer_hex(TAG, &scratchpad->trigger_high, 3); |
|||
result = true; |
|||
|
|||
if (verify) |
|||
{ |
|||
Scratchpad read = _read_scratchpad(ds18b20_info, offsetof(Scratchpad, configuration) + 1); |
|||
if (memcmp(&scratchpad->trigger_high, &read.trigger_high, 3) != 0) |
|||
{ |
|||
ESP_LOGE(TAG, "scratchpad verify failed: " |
|||
"wrote {0x%02x, 0x%02x, 0x%02x}, " |
|||
"read {0x%02x, 0x%02x, 0x%02x}", |
|||
scratchpad->trigger_high, scratchpad->trigger_low, scratchpad->configuration, |
|||
read.trigger_high, read.trigger_low, read.configuration); |
|||
result = false; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
|
|||
// Public API
|
|||
|
|||
DS18B20_Info * ds18b20_malloc(void) |
|||
{ |
|||
DS18B20_Info * ds18b20_info = malloc(sizeof(*ds18b20_info)); |
|||
if (ds18b20_info != NULL) |
|||
{ |
|||
memset(ds18b20_info, 0, sizeof(*ds18b20_info)); |
|||
ESP_LOGD(TAG, "malloc %p", ds18b20_info); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "malloc failed"); |
|||
} |
|||
|
|||
return ds18b20_info; |
|||
} |
|||
|
|||
void ds18b20_free(DS18B20_Info ** ds18b20_info) |
|||
{ |
|||
if (ds18b20_info != NULL && (*ds18b20_info != NULL)) |
|||
{ |
|||
ESP_LOGD(TAG, "free %p", *ds18b20_info); |
|||
free(*ds18b20_info); |
|||
*ds18b20_info = NULL; |
|||
} |
|||
} |
|||
|
|||
void ds18b20_init(DS18B20_Info * ds18b20_info, OneWireBus * bus, OneWireBus_ROMCode rom_code) |
|||
{ |
|||
if (ds18b20_info != NULL) |
|||
{ |
|||
_init(ds18b20_info, bus); |
|||
ds18b20_info->rom_code = rom_code; |
|||
|
|||
// read current resolution from device as it may not be power-on or factory default
|
|||
ds18b20_info->resolution = ds18b20_read_resolution(ds18b20_info); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20_info is NULL"); |
|||
} |
|||
} |
|||
|
|||
void ds18b20_init_solo(DS18B20_Info * ds18b20_info, OneWireBus * bus) |
|||
{ |
|||
if (ds18b20_info != NULL) |
|||
{ |
|||
_init(ds18b20_info, bus); |
|||
ds18b20_info->solo = true; |
|||
|
|||
// read current resolution from device as it may not be power-on or factory default
|
|||
ds18b20_info->resolution = ds18b20_read_resolution(ds18b20_info); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20_info is NULL"); |
|||
} |
|||
} |
|||
|
|||
void ds18b20_use_crc(DS18B20_Info * ds18b20_info, bool use_crc) |
|||
{ |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
ds18b20_info->use_crc = use_crc; |
|||
ESP_LOGD(TAG, "use_crc %d", ds18b20_info->use_crc); |
|||
} |
|||
} |
|||
|
|||
bool ds18b20_set_resolution(DS18B20_Info * ds18b20_info, DS18B20_RESOLUTION resolution) |
|||
{ |
|||
bool result = false; |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
if (_check_resolution(ds18b20_info->resolution)) |
|||
{ |
|||
// read scratchpad up to and including configuration register
|
|||
Scratchpad scratchpad = _read_scratchpad(ds18b20_info, |
|||
offsetof(Scratchpad, configuration) - offsetof(Scratchpad, temperature) + 1); |
|||
|
|||
// modify configuration register to set resolution
|
|||
uint8_t value = (((resolution - 1) & 0x03) << 5) | 0x1f; |
|||
scratchpad.configuration = value; |
|||
ESP_LOGD(TAG, "configuration value 0x%02x", value); |
|||
|
|||
// write bytes 2, 3 and 4 of scratchpad
|
|||
result = _write_scratchpad(ds18b20_info, &scratchpad, /* verify */ true); |
|||
if (result) |
|||
{ |
|||
ds18b20_info->resolution = resolution; |
|||
ESP_LOGI(TAG, "Resolution set to %d bits", (int)resolution); |
|||
} |
|||
else |
|||
{ |
|||
// Resolution change failed - update the info resolution with the value read from configuration
|
|||
ds18b20_info->resolution = ds18b20_read_resolution(ds18b20_info); |
|||
ESP_LOGW(TAG, "Resolution consistency lost - refreshed from device: %d", ds18b20_info->resolution); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "Unsupported resolution %d", resolution); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
DS18B20_RESOLUTION ds18b20_read_resolution(DS18B20_Info * ds18b20_info) |
|||
{ |
|||
DS18B20_RESOLUTION resolution = DS18B20_RESOLUTION_INVALID; |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
// read scratchpad up to and including configuration register
|
|||
Scratchpad scratchpad = _read_scratchpad(ds18b20_info, |
|||
offsetof(Scratchpad, configuration) - offsetof(Scratchpad, temperature) + 1); |
|||
|
|||
resolution = ((scratchpad.configuration >> 5) & 0x03) + DS18B20_RESOLUTION_9_BIT; |
|||
if (!_check_resolution(resolution)) |
|||
{ |
|||
ESP_LOGE(TAG, "invalid resolution read from device: 0x%02x", scratchpad.configuration); |
|||
resolution = DS18B20_RESOLUTION_INVALID; |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGI(TAG, "Resolution read as %d", resolution); |
|||
} |
|||
} |
|||
return resolution; |
|||
} |
|||
|
|||
bool ds18b20_convert(const DS18B20_Info * ds18b20_info) |
|||
{ |
|||
bool result = false; |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
OneWireBus * bus = ds18b20_info->bus; |
|||
if (_address_device(ds18b20_info)) |
|||
{ |
|||
// initiate a temperature measurement
|
|||
owb_write_byte(bus, DS18B20_FUNCTION_TEMP_CONVERT); |
|||
result = true; |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20 device not responding"); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
void ds18b20_convert_all(const OneWireBus * bus) |
|||
{ |
|||
owb_reset(bus); |
|||
owb_write_byte(bus, OWB_ROM_SKIP); |
|||
owb_write_byte(bus, DS18B20_FUNCTION_TEMP_CONVERT); |
|||
} |
|||
|
|||
float ds18b20_wait_for_conversion(const DS18B20_Info * ds18b20_info) |
|||
{ |
|||
float elapsed_time = 0.0f; |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
elapsed_time = _wait_for_conversion(ds18b20_info->resolution); |
|||
} |
|||
return elapsed_time; |
|||
} |
|||
|
|||
float ds18b20_read_temp(const DS18B20_Info * ds18b20_info) |
|||
{ |
|||
float temp = 0.0f; |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
OneWireBus * bus = ds18b20_info->bus; |
|||
if (_address_device(ds18b20_info)) |
|||
{ |
|||
// read measurement
|
|||
_address_device(ds18b20_info); |
|||
owb_write_byte(bus, DS18B20_FUNCTION_SCRATCHPAD_READ); |
|||
|
|||
uint8_t temp_LSB = 0; |
|||
uint8_t temp_MSB = 0; |
|||
if (!ds18b20_info->use_crc) |
|||
{ |
|||
// Without CRC:
|
|||
temp_LSB = owb_read_byte(bus); |
|||
temp_MSB = owb_read_byte(bus); |
|||
owb_reset(bus); // terminate early
|
|||
} |
|||
else |
|||
{ |
|||
// with CRC:
|
|||
uint8_t buffer[9]; |
|||
owb_read_bytes(bus, buffer, 9); |
|||
|
|||
temp_LSB = buffer[0]; |
|||
temp_MSB = buffer[1]; |
|||
|
|||
if (owb_crc8_bytes(0, buffer, 9) != 0) |
|||
{ |
|||
ESP_LOGE(TAG, "CRC failed"); |
|||
temp_LSB = temp_MSB = 0; |
|||
} |
|||
} |
|||
|
|||
ESP_LOGD(TAG, "temp_LSB 0x%02x, temp_MSB 0x%02x", temp_LSB, temp_MSB); |
|||
temp = _decode_temp(temp_LSB, temp_MSB, ds18b20_info->resolution); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20 device not responding"); |
|||
} |
|||
} |
|||
return temp; |
|||
} |
|||
|
|||
float ds18b20_convert_and_read_temp(const DS18B20_Info * ds18b20_info) |
|||
{ |
|||
float temp = 0.0f; |
|||
if (_is_init(ds18b20_info)) |
|||
{ |
|||
if (ds18b20_convert(ds18b20_info)) |
|||
{ |
|||
// wait at least maximum conversion time
|
|||
_wait_for_conversion(ds18b20_info->resolution); |
|||
|
|||
temp = ds18b20_read_temp(ds18b20_info); |
|||
} |
|||
} |
|||
return temp; |
|||
} |
|||
|
|||
|
|||
@ -1,181 +0,0 @@ |
|||
/*
|
|||
* MIT License |
|||
* |
|||
* Copyright (c) 2017 David Antliff |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in all |
|||
* copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
* SOFTWARE. |
|||
*/ |
|||
|
|||
/**
|
|||
* @file ds18b20.h |
|||
* @brief Interface definitions for the Maxim Integrated DS18B20 Programmable |
|||
* Resolution 1-Wire Digital Thermometer device. |
|||
* |
|||
* This component provides structures and functions that are useful for communicating |
|||
* with DS18B20 devices connected via a Maxim Integrated 1-Wire® bus. |
|||
*/ |
|||
|
|||
#ifndef DS18B20_H |
|||
#define DS18B20_H |
|||
|
|||
#include "owb.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
/**
|
|||
* @brief Symbols for the supported temperature resolution of the device. |
|||
*/ |
|||
typedef enum |
|||
{ |
|||
DS18B20_RESOLUTION_INVALID = -1, ///< Invalid resolution
|
|||
DS18B20_RESOLUTION_9_BIT = 9, ///< 9-bit resolution, LSB bits 2,1,0 undefined
|
|||
DS18B20_RESOLUTION_10_BIT = 10, ///< 10-bit resolution, LSB bits 1,0 undefined
|
|||
DS18B20_RESOLUTION_11_BIT = 11, ///< 11-bit resolution, LSB bit 0 undefined
|
|||
DS18B20_RESOLUTION_12_BIT = 12, ///< 12-bit resolution (default)
|
|||
} DS18B20_RESOLUTION; |
|||
|
|||
/**
|
|||
* @brief Structure containing information related to a single DS18B20 device connected |
|||
* via a 1-Wire bus. |
|||
*/ |
|||
typedef struct |
|||
{ |
|||
bool init; ///< True if struct has been initialised, otherwise false
|
|||
bool solo; ///< True if device is intended to be the only one connected to the bus, otherwise false
|
|||
bool use_crc; ///< True if CRC checks are to be used when retrieving information from a device on the bus
|
|||
OneWireBus * bus; ///< Pointer to 1-Wire bus information relevant to this device
|
|||
OneWireBus_ROMCode rom_code; ///< The ROM code used to address this device on the bus
|
|||
DS18B20_RESOLUTION resolution; ///< Temperature measurement resolution per reading
|
|||
} DS18B20_Info; |
|||
|
|||
/**
|
|||
* @brief Construct a new device info instance. |
|||
* New instance should be initialised before calling other functions. |
|||
* @return Pointer to new device info instance, or NULL if it cannot be created. |
|||
*/ |
|||
DS18B20_Info * ds18b20_malloc(void); |
|||
|
|||
/**
|
|||
* @brief Delete an existing device info instance. |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
* @param[in,out] ds18b20_info Pointer to device info instance that will be freed and set to NULL. |
|||
*/ |
|||
void ds18b20_free(DS18B20_Info ** ds18b20_info); |
|||
|
|||
/**
|
|||
* @brief Initialise a device info instance with the specified GPIO. |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
* @param[in] bus Pointer to initialised 1-Wire bus instance. |
|||
* @param[in] rom_code Device-specific ROM code to identify a device on the bus. |
|||
*/ |
|||
void ds18b20_init(DS18B20_Info * ds18b20_info, OneWireBus * bus, OneWireBus_ROMCode rom_code); |
|||
|
|||
/**
|
|||
* @brief Initialise a device info instance with the specified GPIO as a solo device on the bus. |
|||
* |
|||
* This is subject to the requirement that this device is the ONLY device on the bus. |
|||
* This allows for faster commands to be used without ROM code addressing. |
|||
* |
|||
* NOTE: if additional devices are added to the bus, operation will cease to work correctly. |
|||
* |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
* @param[in] bus Pointer to initialised 1-Wire bus instance. |
|||
* @param[in] rom_code Device-specific ROM code to identify a device on the bus. |
|||
*/ |
|||
void ds18b20_init_solo(DS18B20_Info * ds18b20_info, OneWireBus * bus); |
|||
|
|||
/**
|
|||
* @brief Enable or disable use of CRC checks on device communications. |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
* @param[in] use_crc True to enable CRC checks, false to disable. |
|||
*/ |
|||
void ds18b20_use_crc(DS18B20_Info * ds18b20_info, bool use_crc); |
|||
|
|||
/**
|
|||
* @brief Set temperature measurement resolution. |
|||
* |
|||
* This programs the hardware to the specified resolution and sets the cached value to be the same. |
|||
* If the program fails, the value currently in hardware is used to refresh the cache. |
|||
* |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
* @param[in] resolution Selected resolution. |
|||
* @return True if successful, otherwise false. |
|||
*/ |
|||
bool ds18b20_set_resolution(DS18B20_Info * ds18b20_info, DS18B20_RESOLUTION resolution); |
|||
|
|||
/**
|
|||
* @brief Update and return the current temperature measurement resolution from the device. |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
* @return The currently configured temperature measurement resolution. |
|||
*/ |
|||
DS18B20_RESOLUTION ds18b20_read_resolution(DS18B20_Info * ds18b20_info); |
|||
|
|||
/**
|
|||
* @brief Read 64-bit ROM code from device - only works when there is a single device on the bus. |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
* @return The 64-bit value read from the device's ROM. |
|||
*/ |
|||
OneWireBus_ROMCode ds18b20_read_rom(DS18B20_Info * ds18b20_info); |
|||
|
|||
/**
|
|||
* @brief Start a temperature measurement conversion on a single device. |
|||
* @param[in] ds18b20_info Pointer to device info instance. |
|||
*/ |
|||
bool ds18b20_convert(const DS18B20_Info * ds18b20_info); |
|||
|
|||
/**
|
|||
* @brief Start temperature conversion on all connected devices. |
|||
* |
|||
* This should be followed by a sufficient delay to ensure all devices complete |
|||
* their conversion before the measurements are read. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
*/ |
|||
void ds18b20_convert_all(const OneWireBus * bus); |
|||
|
|||
/**
|
|||
* @brief Wait for the maximum conversion time according to the current resolution of the device. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @return An estimate of the time elapsed, in milliseconds. Actual elapsed time may be greater. |
|||
*/ |
|||
float ds18b20_wait_for_conversion(const DS18B20_Info * ds18b20_info); |
|||
|
|||
/**
|
|||
* @brief Read last temperature measurement from device. |
|||
* |
|||
* This is typically called after ds18b20_start_mass_conversion(), provided enough time |
|||
* has elapsed to ensure that all devices have completed their conversions. |
|||
* @param[in] ds18b20_info Pointer to device info instance. Must be initialised first. |
|||
* @return The measurement value returned by the device, in degrees Celsius. |
|||
*/ |
|||
float ds18b20_read_temp(const DS18B20_Info * ds18b20_info); |
|||
|
|||
/**
|
|||
* @brief Convert, wait and read current temperature from device. |
|||
* @param[in] ds18b20_info Pointer to device info instance. Must be initialised first. |
|||
* @return The measurement value returned by the device, in degrees Celsius. |
|||
*/ |
|||
float ds18b20_convert_and_read_temp(const DS18B20_Info * ds18b20_info); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif // DS18B20_H
|
|||
@ -1,179 +0,0 @@ |
|||
/*
|
|||
* MIT License |
|||
* |
|||
* Copyright (c) 2017 David Antliff |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in all |
|||
* copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
* SOFTWARE. |
|||
*/ |
|||
|
|||
#include <inttypes.h> |
|||
|
|||
#include "freertos/FreeRTOS.h" |
|||
#include "freertos/task.h" |
|||
#include "esp_system.h" |
|||
#include "esp_log.h" |
|||
|
|||
// Uncomment to enable static (stack-based) allocation of instances and avoid malloc/free.
|
|||
//#define USE_STATIC 1
|
|||
|
|||
#include "owb.h" |
|||
#include "ds18b20.h" |
|||
|
|||
|
|||
#define GPIO_DS18B20_0 (GPIO_NUM_5) |
|||
#define MAX_DEVICES (8) |
|||
#define DS18B20_RESOLUTION (DS18B20_RESOLUTION_11_BIT) |
|||
|
|||
void app_main() |
|||
{ |
|||
esp_log_level_set("*", ESP_LOG_INFO); |
|||
|
|||
// Create a 1-Wire bus
|
|||
#ifdef USE_STATIC |
|||
OneWireBus owb_static; // static allocation
|
|||
OneWireBus * owb = &owb_static; |
|||
#else |
|||
OneWireBus * owb = owb_malloc(); // heap allocation
|
|||
#endif |
|||
|
|||
owb_init(owb, GPIO_DS18B20_0); |
|||
owb_use_crc(owb, true); // enable CRC check for ROM code
|
|||
|
|||
// find all connected devices
|
|||
printf("Find devices:\n"); |
|||
OneWireBus_ROMCode device_rom_codes[MAX_DEVICES] = {0}; |
|||
int num_devices = 0; |
|||
OneWireBus_SearchState search_state = {0}; |
|||
bool found = owb_search_first(owb, &search_state); |
|||
while (found) |
|||
{ |
|||
char rom_code_s[17]; |
|||
owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s)); |
|||
printf(" %d : %s\n", num_devices, rom_code_s); |
|||
device_rom_codes[num_devices] = search_state.rom_code; |
|||
++num_devices; |
|||
found = owb_search_next(owb, &search_state); |
|||
} |
|||
|
|||
//uint64_t rom_code = 0x0001162e87ccee28; // pink
|
|||
//uint64_t rom_code = 0xf402162c6149ee28; // green
|
|||
//uint64_t rom_code = 0x1502162ca5b2ee28; // orange
|
|||
//uint64_t rom_code = owb_read_rom(owb);
|
|||
|
|||
// known ROM codes (LSB first):
|
|||
OneWireBus_ROMCode known_device = { |
|||
.fields.family = { 0x28 }, |
|||
.fields.serial_number = { 0xee, 0xcc, 0x87, 0x2e, 0x16, 0x01 }, |
|||
.fields.crc = { 0x00 }, |
|||
}; |
|||
char rom_code_s[17]; |
|||
owb_string_from_rom_code(known_device, rom_code_s, sizeof(rom_code_s)); |
|||
printf("Device %s is %s\n", rom_code_s, owb_verify_rom(owb, known_device) ? "present" : "not present"); |
|||
|
|||
// Create a DS18B20 device on the 1-Wire bus
|
|||
#ifdef USE_STATIC |
|||
DS18B20_Info devices_static[MAX_DEVICES] = {0}; |
|||
DS18B20_Info * devices[MAX_DEVICES] = {0}; |
|||
for (int i = 0; i < MAX_DEVICES; ++i) |
|||
{ |
|||
devices[i] = &(devices_static[i]); |
|||
} |
|||
#else |
|||
DS18B20_Info * devices[MAX_DEVICES] = {0}; |
|||
#endif |
|||
|
|||
for (int i = 0; i < num_devices; ++i) |
|||
{ |
|||
#ifdef USE_STATIC |
|||
DS18B20_Info * ds18b20_info = devices[i]; |
|||
#else |
|||
DS18B20_Info * ds18b20_info = ds18b20_malloc(); // heap allocation
|
|||
devices[i] = ds18b20_info; |
|||
#endif |
|||
if (num_devices == 1) |
|||
{ |
|||
printf("Single device optimisations enabled\n"); |
|||
ds18b20_init_solo(ds18b20_info, owb); // only one device on bus
|
|||
} |
|||
else |
|||
{ |
|||
ds18b20_init(ds18b20_info, owb, device_rom_codes[i]); // associate with bus and device
|
|||
} |
|||
ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings
|
|||
ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION); |
|||
} |
|||
|
|||
// // read temperatures from all sensors sequentially
|
|||
// while (1)
|
|||
// {
|
|||
// printf("\nTemperature readings (degrees C):\n");
|
|||
// for (int i = 0; i < num_devices; ++i)
|
|||
// {
|
|||
// float temp = ds18b20_get_temp(devices[i]);
|
|||
// printf(" %d: %.3f\n", i, temp);
|
|||
// }
|
|||
// vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|||
// }
|
|||
|
|||
// read temperatures more efficiently by starting conversions on all devices at the same time
|
|||
if (num_devices > 0) |
|||
{ |
|||
while (1) |
|||
{ |
|||
TickType_t start_ticks = xTaskGetTickCount(); |
|||
|
|||
ds18b20_convert_all(owb); |
|||
|
|||
// in this application all devices use the same resolution,
|
|||
// so use the first device to determine the delay
|
|||
ds18b20_wait_for_conversion(devices[0]); |
|||
|
|||
// read the results immediately after conversion otherwise it may fail
|
|||
// (using printf before reading may take too long)
|
|||
float temps[MAX_DEVICES] = { 0 }; |
|||
for (int i = 0; i < num_devices; ++i) |
|||
{ |
|||
temps[i] = ds18b20_read_temp(devices[i]); |
|||
} |
|||
|
|||
// print results in a separate loop, after all have been read
|
|||
printf("\nTemperature readings (degrees C):\n"); |
|||
for (int i = 0; i < num_devices; ++i) |
|||
{ |
|||
printf(" %d: %.1f\n", i, temps[i]); |
|||
} |
|||
|
|||
// make up delay to approximately 1 second per measurement
|
|||
vTaskDelay(1000 / portTICK_PERIOD_MS - (xTaskGetTickCount() - start_ticks)); |
|||
} |
|||
} |
|||
|
|||
#ifndef USE_STATIC |
|||
// clean up dynamically allocated data
|
|||
for (int i = 0; i < num_devices; ++i) |
|||
{ |
|||
ds18b20_free(&devices[i]); |
|||
} |
|||
owb_free(&owb); |
|||
#endif |
|||
|
|||
printf("Restarting now.\n"); |
|||
fflush(stdout); |
|||
esp_restart(); |
|||
} |
|||
@ -1,587 +0,0 @@ |
|||
/*
|
|||
* MIT License |
|||
* |
|||
* Copyright (c) 2017 David Antliff |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in all |
|||
* copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
* SOFTWARE. |
|||
*/ |
|||
|
|||
/**
|
|||
* @file owb.c |
|||
*/ |
|||
|
|||
#include <stddef.h> |
|||
#include <stdbool.h> |
|||
#include <inttypes.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
|
|||
#include "esp_log.h" |
|||
#include "sdkconfig.h" |
|||
#include "driver/gpio.h" |
|||
|
|||
#include "owb.h" |
|||
|
|||
static const char * TAG = "owb"; |
|||
|
|||
/// @cond ignore
|
|||
struct _OneWireBus_Timing |
|||
{ |
|||
uint32_t A, B, C, D, E, F, G, H, I, J; |
|||
}; |
|||
//// @endcond
|
|||
|
|||
// 1-Wire timing delays (standard) in microseconds.
|
|||
// Labels and values are from https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
|
|||
static const struct _OneWireBus_Timing _StandardTiming = { |
|||
6, // A - read/write "1" master pull DQ low duration
|
|||
64, // B - write "0" master pull DQ low duration
|
|||
60, // C - write "1" master pull DQ high duration
|
|||
10, // D - write "0" master pull DQ high duration
|
|||
9, // E - read master pull DQ high duration
|
|||
55, // F - complete read timeslot + 10ms recovery
|
|||
0, // G - wait before reset
|
|||
480, // H - master pull DQ low duration
|
|||
70, // I - master pull DQ high duration
|
|||
410, // J - complete presence timeslot + recovery
|
|||
}; |
|||
|
|||
static void _us_delay(uint32_t time_us) |
|||
{ |
|||
ets_delay_us(time_us); |
|||
} |
|||
|
|||
bool _is_init(const OneWireBus * bus) |
|||
{ |
|||
bool ok = false; |
|||
if (bus != NULL) |
|||
{ |
|||
if (bus->init) |
|||
{ |
|||
// OK
|
|||
ok = true; |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "bus is not initialised"); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "bus is NULL"); |
|||
} |
|||
return ok; |
|||
} |
|||
|
|||
/**
|
|||
* @brief Generate a 1-Wire reset (initialization). |
|||
* @param[in] bus Initialised bus instance. |
|||
* @return true if device is present, otherwise false. |
|||
*/ |
|||
static bool _reset(const OneWireBus * bus) |
|||
{ |
|||
bool present = false; |
|||
if (_is_init(bus)) |
|||
{ |
|||
gpio_set_direction(bus->gpio, GPIO_MODE_OUTPUT); |
|||
|
|||
_us_delay(bus->timing->G); |
|||
gpio_set_level(bus->gpio, 0); // Drive DQ low
|
|||
_us_delay(bus->timing->H); |
|||
gpio_set_level(bus->gpio, 1); // Release the bus
|
|||
_us_delay(bus->timing->I); |
|||
|
|||
gpio_set_direction(bus->gpio, GPIO_MODE_INPUT); |
|||
int level1 = gpio_get_level(bus->gpio); |
|||
_us_delay(bus->timing->J); // Complete the reset sequence recovery
|
|||
int level2 = gpio_get_level(bus->gpio); |
|||
|
|||
present = (level1 == 0) && (level2 == 1); // Sample for presence pulse from slave
|
|||
ESP_LOGD(TAG, "reset: level1 0x%x, level2 0x%x, present %d", level1, level2, present); |
|||
} |
|||
return present; |
|||
} |
|||
|
|||
/**
|
|||
* @brief Send a 1-Wire write bit, with recovery time. |
|||
* @param[in] bus Initialised bus instance. |
|||
* @param[in] bit The value to send. |
|||
*/ |
|||
static void _write_bit(const OneWireBus * bus, int bit) |
|||
{ |
|||
if (_is_init(bus)) |
|||
{ |
|||
int delay1 = bit ? bus->timing->A : bus->timing->C; |
|||
int delay2 = bit ? bus->timing->B : bus->timing->D; |
|||
gpio_set_direction(bus->gpio, GPIO_MODE_OUTPUT); |
|||
gpio_set_level(bus->gpio, 0); // Drive DQ low
|
|||
_us_delay(delay1); |
|||
gpio_set_level(bus->gpio, 1); // Release the bus
|
|||
_us_delay(delay2); |
|||
} |
|||
} |
|||
|
|||
/**
|
|||
* @brief Read a bit from the 1-Wire bus and return the value, with recovery time. |
|||
* @param[in] bus Initialised bus instance. |
|||
*/ |
|||
static int _read_bit(const OneWireBus * bus) |
|||
{ |
|||
int result = 0; |
|||
if (_is_init(bus)) |
|||
{ |
|||
gpio_set_direction(bus->gpio, GPIO_MODE_OUTPUT); |
|||
gpio_set_level(bus->gpio, 0); // Drive DQ low
|
|||
_us_delay(bus->timing->A); |
|||
gpio_set_level(bus->gpio, 1); // Release the bus
|
|||
_us_delay(bus->timing->E); |
|||
|
|||
gpio_set_direction(bus->gpio, GPIO_MODE_INPUT); |
|||
int level = gpio_get_level(bus->gpio); |
|||
_us_delay(bus->timing->F); // Complete the timeslot and 10us recovery
|
|||
result = level & 0x01; |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
/**
|
|||
* @brief Write 1-Wire data byte. |
|||
* @param[in] bus Initialised bus instance. |
|||
* @param[in] data Value to write. |
|||
*/ |
|||
static void _write_byte(const OneWireBus * bus, uint8_t data) |
|||
{ |
|||
if (_is_init(bus)) |
|||
{ |
|||
ESP_LOGD(TAG, "write 0x%02x", data); |
|||
for (int i = 0; i < 8; ++i) |
|||
{ |
|||
_write_bit(bus, data & 0x01); |
|||
data >>= 1; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/**
|
|||
* @brief Read 1-Wire data byte from bus. |
|||
* @param[in] bus Initialised bus instance. |
|||
* @return Byte value read from bus. |
|||
*/ |
|||
static uint8_t _read_byte(const OneWireBus * bus) |
|||
{ |
|||
uint8_t result = 0; |
|||
if (_is_init(bus)) |
|||
{ |
|||
for (int i = 0; i < 8; ++i) |
|||
{ |
|||
result >>= 1; |
|||
if (_read_bit(bus)) |
|||
{ |
|||
result |= 0x80; |
|||
} |
|||
} |
|||
ESP_LOGD(TAG, "read 0x%02x", result); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
/**
|
|||
* @param Read a block of bytes from 1-Wire bus. |
|||
* @param[in] bus Initialised bus instance. |
|||
* @param[in,out] buffer Pointer to buffer to receive read data. |
|||
* @param[in] len Number of bytes to read, must not exceed length of receive buffer. |
|||
* @return Pointer to receive buffer. |
|||
*/ |
|||
static uint8_t * _read_block(const OneWireBus * bus, uint8_t * buffer, unsigned int len) |
|||
{ |
|||
for (int i = 0; i < len; ++i) |
|||
{ |
|||
*buffer++ = _read_byte(bus); |
|||
} |
|||
return buffer; |
|||
} |
|||
|
|||
/**
|
|||
* @param Write a block of bytes from 1-Wire bus. |
|||
* @param[in] bus Initialised bus instance. |
|||
* @param[in] buffer Pointer to buffer to write data from. |
|||
* @param[in] len Number of bytes to write. |
|||
* @return Pointer to write buffer. |
|||
*/ |
|||
static const uint8_t * _write_block(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) |
|||
{ |
|||
for (int i = 0; i < len; ++i) |
|||
{ |
|||
_write_byte(bus, buffer[i]); |
|||
} |
|||
return buffer; |
|||
} |
|||
|
|||
/**
|
|||
* @brief 1-Wire 8-bit CRC lookup. |
|||
* @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. |
|||
* @param[in] data Byte to feed into CRC. |
|||
* @return Resultant CRC value. |
|||
*/ |
|||
static uint8_t _calc_crc(uint8_t crc, uint8_t data) |
|||
{ |
|||
// https://www.maximintegrated.com/en/app-notes/index.mvp/id/27
|
|||
static const uint8_t table[256] = { |
|||
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, |
|||
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, |
|||
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, |
|||
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, |
|||
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, |
|||
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, |
|||
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, |
|||
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, |
|||
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, |
|||
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, |
|||
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, |
|||
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, |
|||
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, |
|||
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, |
|||
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, |
|||
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 |
|||
}; |
|||
|
|||
return table[crc ^ data]; |
|||
} |
|||
|
|||
static uint8_t _calc_crc_block(uint8_t crc, const uint8_t * buffer, size_t len) |
|||
{ |
|||
uint8_t crc8 = 0; |
|||
do |
|||
{ |
|||
crc8 = _calc_crc(crc8, *buffer++); |
|||
} |
|||
while (--len > 0); |
|||
return crc8; |
|||
} |
|||
|
|||
static bool _search(const OneWireBus * bus, OneWireBus_SearchState * state) |
|||
{ |
|||
// Based on https://www.maximintegrated.com/en/app-notes/index.mvp/id/187
|
|||
|
|||
// initialize for search
|
|||
int id_bit_number = 1; |
|||
int last_zero = 0; |
|||
int rom_byte_number = 0; |
|||
int id_bit = 0; |
|||
int cmp_id_bit = 0; |
|||
uint8_t rom_byte_mask = 1; |
|||
uint8_t search_direction = 0; |
|||
bool search_result = false; |
|||
uint8_t crc8 = 0; |
|||
|
|||
if (_is_init(bus)) |
|||
{ |
|||
// if the last call was not the last one
|
|||
if (!state->last_device_flag) |
|||
{ |
|||
// 1-Wire reset
|
|||
if (!_reset(bus)) |
|||
{ |
|||
// reset the search
|
|||
state->last_discrepancy = 0; |
|||
state->last_device_flag = false; |
|||
state->last_family_discrepancy = 0; |
|||
return false; |
|||
} |
|||
|
|||
// issue the search command
|
|||
_write_byte(bus, OWB_ROM_SEARCH); |
|||
|
|||
// loop to do the search
|
|||
do |
|||
{ |
|||
// read a bit and its complement
|
|||
id_bit = _read_bit(bus); |
|||
cmp_id_bit = _read_bit(bus); |
|||
|
|||
// check for no devices on 1-wire
|
|||
if ((id_bit == 1) && (cmp_id_bit == 1)) |
|||
break; |
|||
else |
|||
{ |
|||
// all devices coupled have 0 or 1
|
|||
if (id_bit != cmp_id_bit) |
|||
search_direction = id_bit; // bit write value for search
|
|||
else |
|||
{ |
|||
// if this discrepancy if before the Last Discrepancy
|
|||
// on a previous next then pick the same as last time
|
|||
if (id_bit_number < state->last_discrepancy) |
|||
search_direction = ((state->rom_code.bytes[rom_byte_number] & rom_byte_mask) > 0); |
|||
else |
|||
// if equal to last pick 1, if not then pick 0
|
|||
search_direction = (id_bit_number == state->last_discrepancy); |
|||
|
|||
// if 0 was picked then record its position in LastZero
|
|||
if (search_direction == 0) |
|||
{ |
|||
last_zero = id_bit_number; |
|||
|
|||
// check for Last discrepancy in family
|
|||
if (last_zero < 9) |
|||
state->last_family_discrepancy = last_zero; |
|||
} |
|||
} |
|||
|
|||
// set or clear the bit in the ROM byte rom_byte_number
|
|||
// with mask rom_byte_mask
|
|||
if (search_direction == 1) |
|||
state->rom_code.bytes[rom_byte_number] |= rom_byte_mask; |
|||
else |
|||
state->rom_code.bytes[rom_byte_number] &= ~rom_byte_mask; |
|||
|
|||
// serial number search direction write bit
|
|||
_write_bit(bus, search_direction); |
|||
|
|||
// increment the byte counter id_bit_number
|
|||
// and shift the mask rom_byte_mask
|
|||
id_bit_number++; |
|||
rom_byte_mask <<= 1; |
|||
|
|||
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
|
|||
if (rom_byte_mask == 0) |
|||
{ |
|||
crc8 = _calc_crc(crc8, state->rom_code.bytes[rom_byte_number]); // accumulate the CRC
|
|||
rom_byte_number++; |
|||
rom_byte_mask = 1; |
|||
} |
|||
} |
|||
} |
|||
while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
|
|||
|
|||
// if the search was successful then
|
|||
if (!((id_bit_number < 65) || (crc8 != 0))) |
|||
{ |
|||
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
|
|||
state->last_discrepancy = last_zero; |
|||
|
|||
// check for last device
|
|||
if (state->last_discrepancy == 0) |
|||
state->last_device_flag = true; |
|||
|
|||
search_result = true; |
|||
} |
|||
} |
|||
|
|||
// if no device found then reset counters so next 'search' will be like a first
|
|||
if (!search_result || !state->rom_code.bytes[0]) |
|||
{ |
|||
state->last_discrepancy = 0; |
|||
state->last_device_flag = false; |
|||
state->last_family_discrepancy = 0; |
|||
search_result = false; |
|||
} |
|||
} |
|||
return search_result; |
|||
} |
|||
|
|||
|
|||
// Public API
|
|||
|
|||
OneWireBus * owb_malloc() |
|||
{ |
|||
OneWireBus * bus = malloc(sizeof(*bus)); |
|||
if (bus != NULL) |
|||
{ |
|||
memset(bus, 0, sizeof(*bus)); |
|||
ESP_LOGD(TAG, "malloc %p", bus); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "malloc failed"); |
|||
} |
|||
return bus; |
|||
} |
|||
|
|||
void owb_free(OneWireBus ** bus) |
|||
{ |
|||
if (bus != NULL && (*bus != NULL)) |
|||
{ |
|||
ESP_LOGD(TAG, "free %p", *bus); |
|||
free(*bus); |
|||
*bus = NULL; |
|||
} |
|||
} |
|||
|
|||
void owb_init(OneWireBus * bus, int gpio) |
|||
{ |
|||
if (bus != NULL) |
|||
{ |
|||
bus->gpio = gpio; |
|||
bus->timing = &_StandardTiming; |
|||
bus->init = true; |
|||
|
|||
// platform specific:
|
|||
gpio_pad_select_gpio(bus->gpio); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "bus is NULL"); |
|||
} |
|||
} |
|||
|
|||
void owb_use_crc(OneWireBus * bus, bool use_crc) |
|||
{ |
|||
if (_is_init(bus)) |
|||
{ |
|||
bus->use_crc = use_crc; |
|||
ESP_LOGD(TAG, "use_crc %d", bus->use_crc); |
|||
} |
|||
} |
|||
|
|||
OneWireBus_ROMCode owb_read_rom(const OneWireBus * bus) |
|||
{ |
|||
OneWireBus_ROMCode rom_code = {0}; |
|||
if (_is_init(bus)) |
|||
{ |
|||
if (_reset(bus)) |
|||
{ |
|||
_write_byte(bus, OWB_ROM_READ); |
|||
_read_block(bus, rom_code.bytes, sizeof(rom_code)); |
|||
|
|||
if (bus->use_crc) |
|||
{ |
|||
if (owb_crc8_bytes(0, rom_code.bytes, sizeof(rom_code)) != 0) |
|||
{ |
|||
ESP_LOGE(TAG, "CRC failed"); |
|||
memset(rom_code.bytes, 0, sizeof(rom_code)); |
|||
} |
|||
} |
|||
char rom_code_s[17]; |
|||
owb_string_from_rom_code(rom_code, rom_code_s, sizeof(rom_code_s)); |
|||
ESP_LOGD(TAG, "rom_code %s", rom_code_s); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "ds18b20 device not responding"); |
|||
} |
|||
} |
|||
return rom_code; |
|||
} |
|||
|
|||
bool owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code) |
|||
{ |
|||
bool result = false; |
|||
if (_is_init(bus)) |
|||
{ |
|||
OneWireBus_SearchState state = { |
|||
.last_discrepancy = 64, |
|||
.last_device_flag = false, |
|||
}; |
|||
|
|||
if (_search(bus, &state)) |
|||
{ |
|||
result = true; |
|||
for (int i = 0; i < sizeof(state.rom_code.bytes) && result; ++i) |
|||
{ |
|||
result = rom_code.bytes[i] == state.rom_code.bytes[i]; |
|||
ESP_LOGD(TAG, "%02x %02x", rom_code.bytes[i], state.rom_code.bytes[i]); |
|||
} |
|||
ESP_LOGD(TAG, "rom code %sfound", result ? "" : "not "); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
bool owb_reset(const OneWireBus * bus) |
|||
{ |
|||
return _reset(bus); |
|||
} |
|||
|
|||
void owb_write_byte(const OneWireBus * bus, uint8_t data) |
|||
{ |
|||
_write_byte(bus, data); |
|||
} |
|||
|
|||
uint8_t owb_read_byte(const OneWireBus * bus) |
|||
{ |
|||
return _read_byte(bus); |
|||
} |
|||
|
|||
uint8_t * owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len) |
|||
{ |
|||
return _read_block(bus, buffer, len); |
|||
} |
|||
|
|||
const uint8_t * owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) |
|||
{ |
|||
return _write_block(bus, buffer, len); |
|||
} |
|||
|
|||
void owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code) |
|||
{ |
|||
_write_block(bus, (uint8_t *)&rom_code, sizeof(rom_code)); |
|||
} |
|||
|
|||
uint8_t owb_crc8_byte(uint8_t crc, uint8_t data) |
|||
{ |
|||
return _calc_crc(crc, data); |
|||
} |
|||
|
|||
uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len) |
|||
{ |
|||
return _calc_crc_block(crc, data, len); |
|||
} |
|||
|
|||
bool owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state) |
|||
{ |
|||
bool result = false; |
|||
if (state != NULL) |
|||
{ |
|||
memset(&state->rom_code, 0, sizeof(state->rom_code)); |
|||
state->last_discrepancy = 0; |
|||
state->last_family_discrepancy = 0; |
|||
state->last_device_flag = false; |
|||
result = _search(bus, state); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "state is NULL"); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
bool owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state) |
|||
{ |
|||
bool result = false; |
|||
if (state != NULL) |
|||
{ |
|||
result = _search(bus, state); |
|||
} |
|||
else |
|||
{ |
|||
ESP_LOGE(TAG, "state is NULL"); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len) |
|||
{ |
|||
for (int i = sizeof(rom_code.bytes) - 1; i >= 0; i--) |
|||
{ |
|||
sprintf(buffer, "%02x", rom_code.bytes[i]); |
|||
buffer += 2; |
|||
} |
|||
return buffer; |
|||
} |
|||
@ -1,238 +0,0 @@ |
|||
/*
|
|||
* MIT License |
|||
* |
|||
* Copyright (c) 2017 David Antliff |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in all |
|||
* copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
* SOFTWARE. |
|||
*/ |
|||
|
|||
/**
|
|||
* @file owb.h |
|||
* @brief Interface definitions for the 1-Wire bus component. |
|||
* |
|||
* This component provides structures and functions that are useful for communicating |
|||
* with devices connected to a Maxim Integrated 1-Wire® bus via a single GPIO. |
|||
* |
|||
* Currently only externally powered devices are supported. Parasitic power is not supported. |
|||
*/ |
|||
|
|||
#ifndef ONE_WIRE_BUS_H |
|||
#define ONE_WIRE_BUS_H |
|||
|
|||
#include <stdint.h> |
|||
#include <stdbool.h> |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
|
|||
// ROM commands
|
|||
#define OWB_ROM_SEARCH 0xF0 |
|||
#define OWB_ROM_READ 0x33 |
|||
#define OWB_ROM_MATCH 0x55 |
|||
#define OWB_ROM_SKIP 0xCC |
|||
#define OWB_ROM_SEARCH_ALARM 0xEC |
|||
|
|||
/**
|
|||
* @brief Structure containing 1-Wire bus information relevant to a single instance. |
|||
*/ |
|||
typedef struct |
|||
{ |
|||
bool init; ///< True if struct has been initialised, otherwise false.
|
|||
int gpio; ///< Value of GPIO connected to 1-Wire bus
|
|||
const struct _OneWireBus_Timing * timing; ///< Pointer to timing information
|
|||
bool use_crc; ///< True if CRC checks are to be used when retrieving information from a device on the bus
|
|||
} OneWireBus; |
|||
|
|||
/**
|
|||
* @brief Represents a 1-Wire ROM Code. This is a sequence of eight bytes, where |
|||
* the first byte is the family number, then the following 6 bytes form the |
|||
* serial number. The final byte is the CRC8 check byte. |
|||
*/ |
|||
typedef union |
|||
{ |
|||
/// Provides access via field names
|
|||
struct fields |
|||
{ |
|||
uint8_t family[1]; ///< family identifier (1 byte, LSB - read/write first)
|
|||
uint8_t serial_number[6]; ///< serial number (6 bytes)
|
|||
uint8_t crc[1]; ///< CRC check byte (1 byte, MSB - read/write last)
|
|||
} fields; ///< Provides access via field names
|
|||
|
|||
uint8_t bytes[8]; ///< Provides raw byte access
|
|||
|
|||
} OneWireBus_ROMCode; |
|||
|
|||
/**
|
|||
* @brief Represents the state of a device search on the 1-Wire bus. |
|||
* |
|||
* Pass a pointer to this structure to owb_search_first() and |
|||
* owb_search_next() to iterate through detected devices on the bus. |
|||
*/ |
|||
typedef struct |
|||
{ |
|||
OneWireBus_ROMCode rom_code; |
|||
int last_discrepancy; |
|||
int last_family_discrepancy; |
|||
int last_device_flag; |
|||
} OneWireBus_SearchState; |
|||
|
|||
/**
|
|||
* @brief Construct a new 1-Wire bus instance. |
|||
* New instance should be initialised before calling other functions. |
|||
* @return Pointer to new bus instance, or NULL if it cannot be created. |
|||
*/ |
|||
OneWireBus * owb_malloc(void); |
|||
|
|||
/**
|
|||
* @brief Delete an existing device info instance. |
|||
* @param[in] bus Pointer to bus instance. |
|||
* @param[in,out] ds18b20_info Pointer to device info instance that will be freed and set to NULL. |
|||
*/ |
|||
void owb_free(OneWireBus ** bus); |
|||
|
|||
/**
|
|||
* @brief Initialise a 1-Wire bus instance with the specified GPIO. |
|||
* @param[in] bus Pointer to bus instance. |
|||
* @param[in] gpio GPIO number to associate with device. |
|||
*/ |
|||
void owb_init(OneWireBus * bus, int gpio); |
|||
|
|||
/**
|
|||
* @brief Enable or disable use of CRC checks on device communications. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in] use_crc True to enable CRC checks, false to disable. |
|||
*/ |
|||
void owb_use_crc(OneWireBus * bus, bool use_crc); |
|||
|
|||
/**
|
|||
* @brief Read ROM code from device - only works when there is a single device on the bus. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @return The value read from the device's ROM. |
|||
*/ |
|||
OneWireBus_ROMCode owb_read_rom(const OneWireBus * bus); |
|||
|
|||
/**
|
|||
* @brief Verify the device specified by ROM code is present. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in] rom_code ROM code to verify. |
|||
* @return true if device is present, false if not present. |
|||
*/ |
|||
bool owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code); |
|||
|
|||
/**
|
|||
* @brief Reset the 1-Wire bus. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @return True if at least one device is present on the bus. |
|||
*/ |
|||
bool owb_reset(const OneWireBus * bus); |
|||
|
|||
/**
|
|||
* @brief Write a single byte to the 1-Wire bus. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in] data Byte value to write to bus. |
|||
*/ |
|||
void owb_write_byte(const OneWireBus * bus, uint8_t data); |
|||
|
|||
/**
|
|||
* @brief Read a single byte from the 1-Wire bus. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @return The byte value read from the bus. |
|||
*/ |
|||
uint8_t owb_read_byte(const OneWireBus * bus); |
|||
|
|||
/**
|
|||
* @brief Read a number of bytes from the 1-Wire bus. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in, out] buffer Pointer to buffer to receive read data. |
|||
* @param[in] len Number of bytes to read, must not exceed length of receive buffer. |
|||
* @return Pointer to receive buffer. |
|||
*/ |
|||
uint8_t * owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, size_t len); |
|||
|
|||
/**
|
|||
* @brief Write a number of bytes to the 1-Wire bus. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in] buffer Pointer to buffer to write data from. |
|||
* @param[in] len Number of bytes to write. |
|||
* @return Pointer to write buffer. |
|||
*/ |
|||
const uint8_t * owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, size_t len); |
|||
|
|||
/**
|
|||
* @brief Write a ROM code to the 1-Wire bus ensuring LSB is sent first. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in] rom_code ROM code to write to bus. |
|||
*/ |
|||
void owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code); |
|||
|
|||
/**
|
|||
* @brief 1-Wire 8-bit CRC lookup. |
|||
* @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. |
|||
* @param[in] data Byte to feed into CRC. |
|||
* @return Resultant CRC value. |
|||
* Should be zero if last byte was the CRC byte and the CRC matches. |
|||
*/ |
|||
uint8_t owb_crc8_byte(uint8_t crc, uint8_t data); |
|||
|
|||
/**
|
|||
* @brief 1-Wire 8-bit CRC lookup with accumulation over a block of bytes. |
|||
* @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. |
|||
* @param[in] data Array of bytes to feed into CRC. |
|||
* @param[in] len Length of data array in bytes. |
|||
* @return Resultant CRC value. |
|||
* Should be zero if last byte was the CRC byte and the CRC matches. |
|||
*/ |
|||
uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len); |
|||
|
|||
/**
|
|||
* @brief Locates the first device on the 1-Wire bus, if present. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in,out] state Pointer to an existing search state structure. |
|||
* @return True if a device is found, false if no devices are found. |
|||
* If a device is found, the ROM Code can be obtained from the state. |
|||
*/ |
|||
bool owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state); |
|||
|
|||
/**
|
|||
* @brief Locates the next device on the 1-Wire bus, if present, starting from |
|||
* the provided state. Further calls will yield additional devices, if present. |
|||
* @param[in] bus Pointer to initialised bus instance. |
|||
* @param[in,out] state Pointer to an existing search state structure. |
|||
* @return True if a device is found, false if no devices are found. |
|||
* If a device is found, the ROM Code can be obtained from the state. |
|||
*/ |
|||
bool owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state); |
|||
|
|||
/**
|
|||
* @brief Create a string representation of a ROM code. |
|||
* @param[in] rom_code The ROM code to convert to string representation. |
|||
* @param[out] buffer The destination for the string representation. It will be null terminated. |
|||
* @param[in] len The length of the buffer in bytes. 64-bit ROM codes require 16 characters |
|||
* to represent as a string, plus a null terminator, for 17 bytes. |
|||
*/ |
|||
char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len); |
|||
|
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif // ONE_WIRE_BUS_H
|
|||
@ -1,280 +0,0 @@ |
|||
# |
|||
# Automatically generated file; DO NOT EDIT. |
|||
# Espressif IoT Development Framework Configuration |
|||
# |
|||
|
|||
# |
|||
# SDK tool configuration |
|||
# |
|||
CONFIG_TOOLPREFIX="xtensa-esp32-elf-" |
|||
CONFIG_PYTHON="python" |
|||
|
|||
# |
|||
# Bootloader config |
|||
# |
|||
# CONFIG_LOG_BOOTLOADER_LEVEL_NONE is not set |
|||
# CONFIG_LOG_BOOTLOADER_LEVEL_ERROR is not set |
|||
# CONFIG_LOG_BOOTLOADER_LEVEL_WARN is not set |
|||
CONFIG_LOG_BOOTLOADER_LEVEL_INFO=y |
|||
# CONFIG_LOG_BOOTLOADER_LEVEL_DEBUG is not set |
|||
# CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE is not set |
|||
CONFIG_LOG_BOOTLOADER_LEVEL=3 |
|||
|
|||
# |
|||
# Security features |
|||
# |
|||
# CONFIG_SECURE_BOOT_ENABLED is not set |
|||
# CONFIG_FLASH_ENCRYPTION_ENABLED is not set |
|||
|
|||
# |
|||
# Serial flasher config |
|||
# |
|||
CONFIG_ESPTOOLPY_PORT="/dev/cu.SLAB_USBtoUART" |
|||
# CONFIG_ESPTOOLPY_BAUD_115200B is not set |
|||
# CONFIG_ESPTOOLPY_BAUD_230400B is not set |
|||
CONFIG_ESPTOOLPY_BAUD_921600B=y |
|||
# CONFIG_ESPTOOLPY_BAUD_2MB is not set |
|||
# CONFIG_ESPTOOLPY_BAUD_OTHER is not set |
|||
CONFIG_ESPTOOLPY_BAUD_OTHER_VAL=115200 |
|||
CONFIG_ESPTOOLPY_BAUD=921600 |
|||
CONFIG_ESPTOOLPY_COMPRESSED=y |
|||
# CONFIG_FLASHMODE_QIO is not set |
|||
# CONFIG_FLASHMODE_QOUT is not set |
|||
CONFIG_FLASHMODE_DIO=y |
|||
# CONFIG_FLASHMODE_DOUT is not set |
|||
CONFIG_ESPTOOLPY_FLASHMODE="dio" |
|||
# CONFIG_ESPTOOLPY_FLASHFREQ_80M is not set |
|||
CONFIG_ESPTOOLPY_FLASHFREQ_40M=y |
|||
# CONFIG_ESPTOOLPY_FLASHFREQ_26M is not set |
|||
# CONFIG_ESPTOOLPY_FLASHFREQ_20M is not set |
|||
CONFIG_ESPTOOLPY_FLASHFREQ="40m" |
|||
# CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set |
|||
CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y |
|||
# CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set |
|||
# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set |
|||
# CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set |
|||
CONFIG_ESPTOOLPY_FLASHSIZE="2MB" |
|||
CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y |
|||
CONFIG_ESPTOOLPY_BEFORE_RESET=y |
|||
# CONFIG_ESPTOOLPY_BEFORE_NORESET is not set |
|||
CONFIG_ESPTOOLPY_BEFORE="default_reset" |
|||
CONFIG_ESPTOOLPY_AFTER_RESET=y |
|||
# CONFIG_ESPTOOLPY_AFTER_NORESET is not set |
|||
CONFIG_ESPTOOLPY_AFTER="hard_reset" |
|||
# CONFIG_MONITOR_BAUD_9600B is not set |
|||
# CONFIG_MONITOR_BAUD_57600B is not set |
|||
CONFIG_MONITOR_BAUD_115200B=y |
|||
# CONFIG_MONITOR_BAUD_230400B is not set |
|||
# CONFIG_MONITOR_BAUD_921600B is not set |
|||
# CONFIG_MONITOR_BAUD_2MB is not set |
|||
# CONFIG_MONITOR_BAUD_OTHER is not set |
|||
CONFIG_MONITOR_BAUD_OTHER_VAL=115200 |
|||
CONFIG_MONITOR_BAUD=115200 |
|||
|
|||
# |
|||
# Partition Table |
|||
# |
|||
CONFIG_PARTITION_TABLE_SINGLE_APP=y |
|||
# CONFIG_PARTITION_TABLE_TWO_OTA is not set |
|||
# CONFIG_PARTITION_TABLE_CUSTOM is not set |
|||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" |
|||
CONFIG_PARTITION_TABLE_CUSTOM_APP_BIN_OFFSET=0x10000 |
|||
CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv" |
|||
CONFIG_APP_OFFSET=0x10000 |
|||
CONFIG_OPTIMIZATION_LEVEL_DEBUG=y |
|||
# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set |
|||
|
|||
# |
|||
# Component config |
|||
# |
|||
# CONFIG_AWS_IOT_SDK is not set |
|||
# CONFIG_BT_ENABLED is not set |
|||
CONFIG_BT_RESERVE_DRAM=0 |
|||
|
|||
# |
|||
# ESP32-specific |
|||
# |
|||
# CONFIG_ESP32_DEFAULT_CPU_FREQ_80 is not set |
|||
CONFIG_ESP32_DEFAULT_CPU_FREQ_160=y |
|||
# CONFIG_ESP32_DEFAULT_CPU_FREQ_240 is not set |
|||
CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=160 |
|||
CONFIG_MEMMAP_SMP=y |
|||
# CONFIG_MEMMAP_TRACEMEM is not set |
|||
# CONFIG_MEMMAP_TRACEMEM_TWOBANKS is not set |
|||
# CONFIG_ESP32_TRAX is not set |
|||
CONFIG_TRACEMEM_RESERVE_DRAM=0x0 |
|||
# CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set |
|||
# CONFIG_ESP32_ENABLE_COREDUMP_TO_UART is not set |
|||
CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE=y |
|||
# CONFIG_ESP32_ENABLE_COREDUMP is not set |
|||
# CONFIG_ESP32_APPTRACE_DEST_TRAX is not set |
|||
# CONFIG_ESP32_APPTRACE_DEST_UART is not set |
|||
CONFIG_ESP32_APPTRACE_DEST_NONE=y |
|||
# CONFIG_ESP32_APPTRACE_ENABLE is not set |
|||
# CONFIG_TWO_UNIVERSAL_MAC_ADDRESS is not set |
|||
CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS=y |
|||
CONFIG_NUMBER_OF_UNIVERSAL_MAC_ADDRESS=4 |
|||
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 |
|||
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=4096 |
|||
CONFIG_MAIN_TASK_STACK_SIZE=4096 |
|||
CONFIG_IPC_TASK_STACK_SIZE=1024 |
|||
CONFIG_NEWLIB_STDOUT_ADDCR=y |
|||
# CONFIG_NEWLIB_NANO_FORMAT is not set |
|||
CONFIG_CONSOLE_UART_DEFAULT=y |
|||
# CONFIG_CONSOLE_UART_CUSTOM is not set |
|||
# CONFIG_CONSOLE_UART_NONE is not set |
|||
CONFIG_CONSOLE_UART_NUM=0 |
|||
CONFIG_CONSOLE_UART_BAUDRATE=115200 |
|||
# CONFIG_ULP_COPROC_ENABLED is not set |
|||
CONFIG_ULP_COPROC_RESERVE_MEM=0 |
|||
# CONFIG_ESP32_PANIC_PRINT_HALT is not set |
|||
CONFIG_ESP32_PANIC_PRINT_REBOOT=y |
|||
# CONFIG_ESP32_PANIC_SILENT_REBOOT is not set |
|||
# CONFIG_ESP32_PANIC_GDBSTUB is not set |
|||
CONFIG_ESP32_DEBUG_OCDAWARE=y |
|||
CONFIG_INT_WDT=y |
|||
CONFIG_INT_WDT_TIMEOUT_MS=300 |
|||
CONFIG_INT_WDT_CHECK_CPU1=y |
|||
CONFIG_TASK_WDT=y |
|||
# CONFIG_TASK_WDT_PANIC is not set |
|||
CONFIG_TASK_WDT_TIMEOUT_S=5 |
|||
CONFIG_TASK_WDT_CHECK_IDLE_TASK=y |
|||
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1=y |
|||
# CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set |
|||
CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y |
|||
# CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set |
|||
# CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set |
|||
CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC=y |
|||
# CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL is not set |
|||
CONFIG_ESP32_RTC_CLK_CAL_CYCLES=1024 |
|||
CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY=2000 |
|||
# CONFIG_ESP32_XTAL_FREQ_40 is not set |
|||
# CONFIG_ESP32_XTAL_FREQ_26 is not set |
|||
CONFIG_ESP32_XTAL_FREQ_AUTO=y |
|||
CONFIG_ESP32_XTAL_FREQ=0 |
|||
CONFIG_WIFI_ENABLED=y |
|||
CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=10 |
|||
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=32 |
|||
# CONFIG_ESP32_WIFI_STATIC_TX_BUFFER is not set |
|||
CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER=y |
|||
CONFIG_ESP32_WIFI_TX_BUFFER_TYPE=1 |
|||
CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=32 |
|||
CONFIG_ESP32_WIFI_AMPDU_ENABLED=y |
|||
CONFIG_ESP32_WIFI_NVS_ENABLED=y |
|||
CONFIG_PHY_ENABLED=y |
|||
|
|||
# |
|||
# PHY |
|||
# |
|||
CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y |
|||
# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set |
|||
CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20 |
|||
CONFIG_ESP32_PHY_MAX_TX_POWER=20 |
|||
# CONFIG_ETHERNET is not set |
|||
|
|||
# |
|||
# FAT Filesystem support |
|||
# |
|||
CONFIG_FATFS_CODEPAGE_ASCII=y |
|||
# CONFIG_FATFS_CODEPAGE_437 is not set |
|||
# CONFIG_FATFS_CODEPAGE_720 is not set |
|||
# CONFIG_FATFS_CODEPAGE_737 is not set |
|||
# CONFIG_FATFS_CODEPAGE_771 is not set |
|||
# CONFIG_FATFS_CODEPAGE_775 is not set |
|||
# CONFIG_FATFS_CODEPAGE_850 is not set |
|||
# CONFIG_FATFS_CODEPAGE_852 is not set |
|||
# CONFIG_FATFS_CODEPAGE_855 is not set |
|||
# CONFIG_FATFS_CODEPAGE_857 is not set |
|||
# CONFIG_FATFS_CODEPAGE_860 is not set |
|||
# CONFIG_FATFS_CODEPAGE_861 is not set |
|||
# CONFIG_FATFS_CODEPAGE_862 is not set |
|||
# CONFIG_FATFS_CODEPAGE_863 is not set |
|||
# CONFIG_FATFS_CODEPAGE_864 is not set |
|||
# CONFIG_FATFS_CODEPAGE_865 is not set |
|||
# CONFIG_FATFS_CODEPAGE_866 is not set |
|||
# CONFIG_FATFS_CODEPAGE_869 is not set |
|||
# CONFIG_FATFS_CODEPAGE_932 is not set |
|||
# CONFIG_FATFS_CODEPAGE_936 is not set |
|||
# CONFIG_FATFS_CODEPAGE_949 is not set |
|||
# CONFIG_FATFS_CODEPAGE_950 is not set |
|||
CONFIG_FATFS_CODEPAGE=1 |
|||
CONFIG_FATFS_MAX_LFN=255 |
|||
|
|||
# |
|||
# FreeRTOS |
|||
# |
|||
# CONFIG_FREERTOS_UNICORE is not set |
|||
CONFIG_FREERTOS_CORETIMER_0=y |
|||
# CONFIG_FREERTOS_CORETIMER_1 is not set |
|||
CONFIG_FREERTOS_HZ=100 |
|||
CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y |
|||
# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_NONE is not set |
|||
# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set |
|||
CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY=y |
|||
# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set |
|||
CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=1 |
|||
CONFIG_FREERTOS_ASSERT_FAIL_ABORT=y |
|||
# CONFIG_FREERTOS_ASSERT_FAIL_PRINT_CONTINUE is not set |
|||
# CONFIG_FREERTOS_ASSERT_DISABLE is not set |
|||
CONFIG_FREERTOS_BREAK_ON_SCHEDULER_START_JTAG=y |
|||
# CONFIG_ENABLE_MEMORY_DEBUG is not set |
|||
CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=1024 |
|||
CONFIG_FREERTOS_ISR_STACKSIZE=1536 |
|||
# CONFIG_FREERTOS_LEGACY_HOOKS is not set |
|||
CONFIG_FREERTOS_MAX_TASK_NAME_LEN=16 |
|||
# CONFIG_SUPPORT_STATIC_ALLOCATION is not set |
|||
CONFIG_TIMER_TASK_PRIORITY=1 |
|||
CONFIG_TIMER_TASK_STACK_DEPTH=2048 |
|||
CONFIG_TIMER_QUEUE_LENGTH=10 |
|||
# CONFIG_FREERTOS_DEBUG_INTERNALS is not set |
|||
|
|||
# |
|||
# Log output |
|||
# |
|||
# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set |
|||
# CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set |
|||
# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set |
|||
# CONFIG_LOG_DEFAULT_LEVEL_INFO is not set |
|||
CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y |
|||
# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set |
|||
CONFIG_LOG_DEFAULT_LEVEL=4 |
|||
CONFIG_LOG_COLORS=y |
|||
|
|||
# |
|||
# LWIP |
|||
# |
|||
# CONFIG_L2_TO_L3_COPY is not set |
|||
CONFIG_LWIP_MAX_SOCKETS=10 |
|||
CONFIG_LWIP_THREAD_LOCAL_STORAGE_INDEX=0 |
|||
# CONFIG_LWIP_SO_REUSE is not set |
|||
# CONFIG_LWIP_SO_RCVBUF is not set |
|||
CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1 |
|||
# CONFIG_LWIP_IP_FRAG is not set |
|||
# CONFIG_LWIP_IP_REASSEMBLY is not set |
|||
CONFIG_TCP_MAXRTX=12 |
|||
CONFIG_TCP_SYNMAXRTX=6 |
|||
CONFIG_LWIP_DHCP_DOES_ARP_CHECK=y |
|||
CONFIG_TCPIP_TASK_STACK_SIZE=2560 |
|||
# CONFIG_PPP_SUPPORT is not set |
|||
|
|||
# |
|||
# mbedTLS |
|||
# |
|||
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384 |
|||
# CONFIG_MBEDTLS_DEBUG is not set |
|||
# CONFIG_MBEDTLS_UNSAFE_ACCELERATION is not set |
|||
CONFIG_MBEDTLS_HAVE_TIME=y |
|||
# CONFIG_MBEDTLS_HAVE_TIME_DATE is not set |
|||
|
|||
# |
|||
# OpenSSL |
|||
# |
|||
# CONFIG_OPENSSL_DEBUG is not set |
|||
CONFIG_OPENSSL_ASSERT_DO_NOTHING=y |
|||
# CONFIG_OPENSSL_ASSERT_EXIT is not set |
|||
|
|||
# |
|||
# SPI Flash driver |
|||
# |
|||
# CONFIG_SPI_FLASH_ENABLE_COUNTERS is not set |
|||
CONFIG_SPI_FLASH_ROM_DRIVER_PATCH=y |
|||
Loading…
Reference in new issue