|
|
@ -1,5 +1,6 @@ |
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MightyPork on 2018/02/01.
|
|
|
|
// Created by MightyPork on 2018/02/01.
|
|
|
|
|
|
|
|
// MIT license
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
@ -12,14 +13,13 @@ |
|
|
|
void ow_search_init(struct ow_search_state *state, uint8_t command) |
|
|
|
void ow_search_init(struct ow_search_state *state, uint8_t command) |
|
|
|
{ |
|
|
|
{ |
|
|
|
state->prev_last_fork = 64; |
|
|
|
state->prev_last_fork = 64; |
|
|
|
memset(&state->prev_code[0], 0, 8); |
|
|
|
memset(state->prev_code, 0, 8); |
|
|
|
state->status = OW_SEARCH_MORE; |
|
|
|
state->status = OW_SEARCH_MORE; |
|
|
|
state->command = command; |
|
|
|
state->command = command; |
|
|
|
state->first = true; |
|
|
|
state->first = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint16_t |
|
|
|
uint16_t ow_search_run(struct ow_search_state *state, ow_romcode_t *codes, uint16_t capacity) |
|
|
|
ow_search_run(struct ow_search_state *state, ow_romcode_t *codes, uint16_t capacity) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (state->status != OW_SEARCH_MORE) return 0; |
|
|
|
if (state->status != OW_SEARCH_MORE) return 0; |
|
|
|
|
|
|
|
|
|
|
@ -38,6 +38,8 @@ ow_search_run(struct ow_search_state *state, ow_romcode_t *codes, uint16_t capac |
|
|
|
// Send the search command (SEARCH_ROM, SEARCH_ALARM)
|
|
|
|
// Send the search command (SEARCH_ROM, SEARCH_ALARM)
|
|
|
|
ow_write_u8(state->command); |
|
|
|
ow_write_u8(state->command); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t *code_byte = &code[0]; |
|
|
|
|
|
|
|
|
|
|
|
bool p, n; |
|
|
|
bool p, n; |
|
|
|
while (index != 64) { |
|
|
|
while (index != 64) { |
|
|
|
// Read a bit and its complement
|
|
|
|
// Read a bit and its complement
|
|
|
@ -47,8 +49,7 @@ ow_search_run(struct ow_search_state *state, ow_romcode_t *codes, uint16_t capac |
|
|
|
if (!p && !n) { |
|
|
|
if (!p && !n) { |
|
|
|
// A fork: there are devices on the bus with different bit value
|
|
|
|
// A fork: there are devices on the bus with different bit value
|
|
|
|
// (the bus is open-drain, in both cases one device pulls it low)
|
|
|
|
// (the bus is open-drain, in both cases one device pulls it low)
|
|
|
|
if ((found_devices > 0 || !state->first) && |
|
|
|
if ((found_devices > 0 || !state->first) && index < state->prev_last_fork) { |
|
|
|
index < state->prev_last_fork) { |
|
|
|
|
|
|
|
// earlier than the last fork, take the same turn as before
|
|
|
|
// earlier than the last fork, take the same turn as before
|
|
|
|
p = ow_code_getbit(state->prev_code, index); |
|
|
|
p = ow_code_getbit(state->prev_code, index); |
|
|
|
if (!p) last_fork = index; // remember for future runs, 1 not explored yet
|
|
|
|
if (!p) last_fork = index; // remember for future runs, 1 not explored yet
|
|
|
@ -67,15 +68,18 @@ ow_search_run(struct ow_search_state *state, ow_romcode_t *codes, uint16_t capac |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// All devices have a matching bit here, or it was resolved in a fork
|
|
|
|
// All devices have a matching bit here, or it was resolved in a fork
|
|
|
|
if (p) ow_code_setbit(code, index); |
|
|
|
if (p) *code_byte |= (1 << (index & 7)); |
|
|
|
ow_write_bit(p); |
|
|
|
ow_write_bit(p); |
|
|
|
|
|
|
|
|
|
|
|
index++; |
|
|
|
index++; |
|
|
|
|
|
|
|
if((index & 7) == 0) { |
|
|
|
|
|
|
|
code_byte++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Record a found address
|
|
|
|
// Record a found address
|
|
|
|
for (int i = 0; i < 8; i++) { |
|
|
|
memcpy(state->prev_code, code, 8); |
|
|
|
state->prev_code[i] = codes[found_devices][i] = code[i]; |
|
|
|
memcpy(codes[found_devices], code, 8); |
|
|
|
} |
|
|
|
|
|
|
|
found_devices++; |
|
|
|
found_devices++; |
|
|
|
|
|
|
|
|
|
|
|
// Stop condition
|
|
|
|
// Stop condition
|
|
|
@ -86,7 +90,8 @@ ow_search_run(struct ow_search_state *state, ow_romcode_t *codes, uint16_t capac |
|
|
|
|
|
|
|
|
|
|
|
state->prev_last_fork = last_fork; |
|
|
|
state->prev_last_fork = last_fork; |
|
|
|
} |
|
|
|
} |
|
|
|
done: |
|
|
|
|
|
|
|
|
|
|
|
done: |
|
|
|
state->first = false; |
|
|
|
state->first = false; |
|
|
|
return found_devices; |
|
|
|
return found_devices; |
|
|
|
} |
|
|
|
} |
|
|
|