replace OneWire

This commit is contained in:
MichaelDvP
2022-05-02 11:19:48 +02:00
parent dc9c21fc65
commit 0c6c7f999f
4 changed files with 484 additions and 565 deletions

View File

@@ -1,4 +1,8 @@
/*
taken from:s
https://github.com/arendst/Tasmota/blob/development/lib/lib_basic/OneWire-Stickbreaker/OneWire.cpp
modified for ems-esp old lib compatibility
Copyright (c) 2007, Jim Studt (original old version - many contributors since)
The latest version of this library may be found at:
@@ -32,6 +36,17 @@ private email about OneWire).
OneWire is now very mature code. No changes other than adding
definitions for newer hardware support are anticipated.
=======
Version 2.3.3 ESP32 Stickbreaker 06MAY2019
Add a #ifdef to isolate ESP32 mods
Version 2.3.1 ESP32 everslick 30APR2018
add IRAM_ATTR attribute to write_bit/read_bit to fix icache miss delay
https://github.com/espressif/arduino-esp32/issues/1335
Version 2.3 ESP32 stickbreaker 28DEC2017
adjust to use portENTER_CRITICAL(&mux) instead of noInterrupts();
adjust to use portEXIT_CRITICAL(&mux) instead of Interrupts();
Version 2.3:
Unknown chip fallback mode, Roger Clark
Teensy-LC compatibility, Paul Stoffregen
@@ -139,12 +154,20 @@ sample code bearing this copyright.
//--------------------------------------------------------------------------
*/
#include <Arduino.h>
#include "OneWire.h"
#include "OneWire_direct_gpio.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#ifdef ESP32
#define t_noInterrupts() \
{ \
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; \
portENTER_CRITICAL(&mux)
#define t_interrupts() \
portEXIT_CRITICAL(&mux); \
}
#else
#define t_noInterrupts noInterrupts
#define t_interrupts interrupts
#endif
void OneWire::begin(uint8_t pin) {
pinMode(pin, INPUT);
@@ -155,6 +178,7 @@ void OneWire::begin(uint8_t pin) {
#endif
}
// Perform the onewire reset function. We will wait up to 250uS for
// the bus to come high, if it doesn't then it is broken or shorted
// and we return a 0;
@@ -162,18 +186,18 @@ void OneWire::begin(uint8_t pin) {
// Returns 1 if a device asserted a presence pulse, 0 otherwise.
//
#ifdef ARDUINO_ARCH_ESP32
uint8_t IRAM_ATTR OneWire::reset(void) {
uint8_t IRAM_ATTR OneWire::reset(void)
#else
uint8_t OneWire::reset(void) {
uint8_t OneWire::reset(void)
#endif
{
IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask;
volatile IO_REG_TYPE * reg IO_REG_BASE_ATTR = baseReg;
uint8_t r;
uint8_t retries = 125;
noInterrupts();
t_noInterrupts();
DIRECT_MODE_INPUT(reg, mask);
interrupts();
t_interrupts();
// wait until the wire is high... just in case
do {
if (--retries == 0)
@@ -181,16 +205,14 @@ uint8_t OneWire::reset(void) {
delayMicroseconds(2);
} while (!DIRECT_READ(reg, mask));
noInterrupts();
t_noInterrupts();
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
interrupts();
delayMicroseconds(480);
noInterrupts();
DIRECT_MODE_INPUT(reg, mask); // allow it to float
delayMicroseconds(70);
r = !DIRECT_READ(reg, mask);
interrupts();
t_interrupts();
delayMicroseconds(410);
return r;
}
@@ -200,28 +222,29 @@ uint8_t OneWire::reset(void) {
// more certain timing.
//
#ifdef ARDUINO_ARCH_ESP32
void IRAM_ATTR OneWire::write_bit(uint8_t v) {
void IRAM_ATTR OneWire::write_bit(uint8_t v)
#else
void OneWire::write_bit(uint8_t v) {
void OneWire::write_bit(uint8_t v)
#endif
{
IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask;
volatile IO_REG_TYPE * reg IO_REG_BASE_ATTR = baseReg;
if (v & 1) {
noInterrupts();
t_noInterrupts();
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
delayMicroseconds(10);
DIRECT_WRITE_HIGH(reg, mask); // drive output high
interrupts();
t_interrupts();
delayMicroseconds(55);
} else {
noInterrupts();
t_noInterrupts();
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
delayMicroseconds(65);
DIRECT_WRITE_HIGH(reg, mask); // drive output high
interrupts();
t_interrupts();
delayMicroseconds(5);
}
}
@@ -231,22 +254,23 @@ void OneWire::write_bit(uint8_t v) {
// more certain timing.
//
#ifdef ARDUINO_ARCH_ESP32
uint8_t IRAM_ATTR OneWire::read_bit(void) {
uint8_t IRAM_ATTR OneWire::read_bit(void)
#else
uint8_t OneWire::read_bit(void) {
uint8_t OneWire::read_bit(void)
#endif
{
IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask;
volatile IO_REG_TYPE * reg IO_REG_BASE_ATTR = baseReg;
uint8_t r;
noInterrupts();
t_noInterrupts();
DIRECT_MODE_OUTPUT(reg, mask);
DIRECT_WRITE_LOW(reg, mask);
delayMicroseconds(3);
DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise
delayMicroseconds(10);
r = DIRECT_READ(reg, mask);
interrupts();
t_interrupts();
delayMicroseconds(53);
return r;
}
@@ -265,10 +289,10 @@ void OneWire::write(uint8_t v, uint8_t power /* = 0 */) {
OneWire::write_bit((bitMask & v) ? 1 : 0);
}
if (!power) {
noInterrupts();
t_noInterrupts();
DIRECT_MODE_INPUT(baseReg, bitmask);
DIRECT_WRITE_LOW(baseReg, bitmask);
interrupts();
t_interrupts();
}
}
@@ -276,10 +300,10 @@ void OneWire::write_bytes(const uint8_t * buf, uint16_t count, bool power /* = 0
for (uint16_t i = 0; i < count; i++)
write(buf[i]);
if (!power) {
noInterrupts();
t_noInterrupts();
DIRECT_MODE_INPUT(baseReg, bitmask);
DIRECT_WRITE_LOW(baseReg, bitmask);
interrupts();
t_interrupts();
}
}
@@ -322,9 +346,9 @@ void OneWire::skip() {
}
void OneWire::depower() {
noInterrupts();
t_noInterrupts();
DIRECT_MODE_INPUT(baseReg, bitmask);
interrupts();
t_interrupts();
}
#if ONEWIRE_SEARCH
@@ -336,7 +360,7 @@ void OneWire::depower() {
void OneWire::reset_search() {
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = false;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
for (int i = 7;; i--) {
ROM_NO[i] = 0;
@@ -355,7 +379,7 @@ void OneWire::target_search(uint8_t family_code) {
ROM_NO[i] = 0;
LastDiscrepancy = 64;
LastFamilyDiscrepancy = 0;
LastDeviceFlag = false;
LastDeviceFlag = FALSE;
}
//
@@ -374,10 +398,9 @@ void OneWire::target_search(uint8_t family_code) {
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : device not found, end of search
//
bool OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) {
uint8_t OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) {
uint8_t id_bit_number;
uint8_t last_zero, rom_byte_number;
bool search_result;
uint8_t last_zero, rom_byte_number, search_result;
uint8_t id_bit, cmp_id_bit;
unsigned char rom_byte_mask, search_direction;
@@ -387,7 +410,7 @@ bool OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) {
last_zero = 0;
rom_byte_number = 0;
rom_byte_mask = 1;
search_result = false;
search_result = 0;
// if the last call was not the last one
if (!LastDeviceFlag) {
@@ -395,11 +418,10 @@ bool OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) {
if (!reset()) {
// reset the search
LastDiscrepancy = 0;
LastDeviceFlag = false;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
return false;
return FALSE;
}
// issue the search command
if (search_mode == true) {
write(0xF0); // NORMAL SEARCH
@@ -414,21 +436,21 @@ bool OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) {
cmp_id_bit = read_bit();
// check for no devices on 1-wire
if ((id_bit == 1) && (cmp_id_bit == 1)) {
if ((id_bit == 1) && (cmp_id_bit == 1))
break;
} else {
else {
// all devices coupled have 0 or 1
if (id_bit != cmp_id_bit) {
if (id_bit != cmp_id_bit)
search_direction = id_bit; // bit write value for search
} else {
else {
// if this discrepancy if before the Last Discrepancy
// on a previous next then pick the same as last time
if (id_bit_number < LastDiscrepancy) {
if (id_bit_number < LastDiscrepancy)
search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
} else {
else
// if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == LastDiscrepancy);
}
// if 0 was picked then record its position in LastZero
if (search_direction == 0) {
last_zero = id_bit_number;
@@ -461,31 +483,29 @@ bool OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) {
}
}
} while (rom_byte_number < 8); // loop until through all ROM bytes 0-7
// if the search was successful then
if (!(id_bit_number < 65)) {
// search successful so set LastDiscrepancy,LastDeviceFlag,search_result
LastDiscrepancy = last_zero;
// check for last device
if (LastDiscrepancy == 0) {
LastDeviceFlag = true;
}
search_result = true;
if (LastDiscrepancy == 0)
LastDeviceFlag = TRUE;
search_result = TRUE;
}
}
// if no device found then reset counters so next 'search' will be like a first
if (!search_result || !ROM_NO[0]) {
LastDiscrepancy = 0;
LastDeviceFlag = false;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
search_result = false;
search_result = FALSE;
} else {
for (int i = 0; i < 8; i++)
newAddr[i] = ROM_NO[i];
}
// depower(); // https://github.com/PaulStoffregen/OneWire/pull/80
return search_result;
}
@@ -497,28 +517,40 @@ bool OneWire::search(uint8_t * newAddr, bool search_mode /* = true */) {
//
#if ONEWIRE_CRC8_TABLE
// Dow-CRC using polynomial X^8 + X^5 + X^4 + X^0
// Tiny 2x16 entry CRC table created by Arjen Lentz
// See http://lentz.com.au/blog/calculating-crc-with-a-tiny-32-entry-lookup-table
static const uint8_t PROGMEM dscrc2x16_table[] = {0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8, 0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74};
// This table comes from Dallas sample code where it is freely reusable,
// though Copyright (C) 2000 Dallas Semiconductor Corporation
static const uint8_t PROGMEM dscrc_table[] = {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};
//
// Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM
// and the registers. (Use tiny 2x16 entry CRC table)
// and the registers. (note: this might better be done without to
// table, it would probably be smaller and certainly fast enough
// compared to all those delayMicrosecond() calls. But I got
// confused, so I use this table from the examples.)
//
uint8_t OneWire::crc8(const uint8_t * addr, uint8_t len) {
uint8_t crc = 0;
while (len--) {
crc = *addr++ ^ crc; // just re-using crc as intermediate
crc = pgm_read_byte(dscrc2x16_table + (crc & 0x0f)) ^ pgm_read_byte(dscrc2x16_table + 16 + ((crc >> 4) & 0x0f));
crc = pgm_read_byte(dscrc_table + (crc ^ *addr++));
}
return crc;
}
#else
//
// Compute a Dallas Semiconductor 8 bit CRC directly.
// this is much slower, but a little smaller, than the lookup table.
// this is much slower, but much smaller, than the lookup table.
//
uint8_t OneWire::crc8(const uint8_t * addr, uint8_t len) {
uint8_t crc = 0;
@@ -574,8 +606,4 @@ uint16_t OneWire::crc16(const uint8_t * input, uint16_t len, uint16_t crc) {
return crc;
}
#endif
#endif
#pragma GCC diagnostic pop