|
|
|
@ -12,7 +12,6 @@ |
|
|
|
|
|
|
|
#define TAG "owb" |
|
|
|
|
|
|
|
|
|
|
|
struct _OneWireBus_Timing |
|
|
|
{ |
|
|
|
int A, B, C, D, E, F, G, H, I, J; |
|
|
|
@ -32,14 +31,6 @@ static const struct _OneWireBus_Timing _StandardTiming = { |
|
|
|
410 * 4, // J
|
|
|
|
}; |
|
|
|
|
|
|
|
//struct _OneWireBus
|
|
|
|
//{
|
|
|
|
// bool init;
|
|
|
|
// int gpio;
|
|
|
|
// const struct _OneWireBus_Timing * timing;
|
|
|
|
// bool use_crc;
|
|
|
|
//};
|
|
|
|
|
|
|
|
static void _tick_delay(int ticks) |
|
|
|
{ |
|
|
|
// Each tick is 0.25 microseconds.
|
|
|
|
@ -47,7 +38,7 @@ static void _tick_delay(int ticks) |
|
|
|
ets_delay_us(time_us); |
|
|
|
} |
|
|
|
|
|
|
|
static bool _is_init(const OneWireBus * bus) |
|
|
|
bool _is_init(const OneWireBus * bus) |
|
|
|
{ |
|
|
|
bool ok = false; |
|
|
|
if (bus != NULL) |
|
|
|
@ -69,7 +60,6 @@ static bool _is_init(const OneWireBus * bus) |
|
|
|
return ok; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generate a 1-Wire reset. |
|
|
|
* @param[in] bus Initialised bus instance. |
|
|
|
@ -245,6 +235,127 @@ static uint8_t _calc_crc(uint8_t crc, uint8_t data) |
|
|
|
return table[crc ^ data]; |
|
|
|
} |
|
|
|
|
|
|
|
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[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[rom_byte_number] |= rom_byte_mask; |
|
|
|
else |
|
|
|
state->rom_code[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[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[0]) |
|
|
|
{ |
|
|
|
state->last_discrepancy = 0; |
|
|
|
state->last_device_flag = false; |
|
|
|
state->last_family_discrepancy = 0; |
|
|
|
search_result = false; |
|
|
|
} |
|
|
|
} |
|
|
|
return search_result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Public API
|
|
|
|
|
|
|
|
@ -392,3 +503,34 @@ uint8_t owb_crc8(uint8_t crc, uint8_t data) |
|
|
|
return _calc_crc(crc, data); |
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
} |
|
|
|
|