optimized and cleaned

master
Ondřej Hruška 6 years ago
parent f7bcc754e4
commit 711f2600ef
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 34
      main.c
  2. 25
      ow_search.c
  3. 21
      ow_search.h

@ -10,30 +10,44 @@
void main(void) void main(void)
{ {
// The search algorithm stores its internal state in a struct.
// This allows calling it repeatedly if there are more devices
// than could fit in the address buffer.
struct ow_search_state search_state; struct ow_search_state search_state;
ow_search_init(&search_state, 0xFF); // the real onewire search command will go here when used with real hw ow_search_init(&search_state, 0xF0); // SEARCH_ROM
ow_romcode_t addresses[4]; // Buffer for the found addresses - can have up to 65536 rows
// Keep in mind that each address uses 8 bytes
#define CODE_BUFFER_LEN 8
ow_romcode_t addresses[CODE_BUFFER_LEN];
int x=0; // The algorithm stores its return value in the status field,
// we can loop until it becomes OW_SEARCH_DONE or OW_SEARCH_FAILED
while (search_state.status == OW_SEARCH_MORE) { while (search_state.status == OW_SEARCH_MORE) {
uint16_t count = ow_search_run(&search_state, addresses, 4); // Perform a run of the algorithm
uint16_t count = ow_search_run(&search_state, addresses, CODE_BUFFER_LEN);
// Do something with the found addresses
printf("Found %d addresses, status %d\n", count, search_state.status); printf("Found %d addresses, status %d\n", count, search_state.status);
for(int i=0; i<count; i++) { for (int i = 0; i < count; i++) {
for(int j=0; j<8; j++) { printf("> ");
for (int j = 0; j < 8; j++) {
printf("%02x ", addresses[i][j]); printf("%02x ", addresses[i][j]);
} }
printf("\n"); uint64_t numeric = ow_romcode_to_u64(addresses[i]);
uint64_t numeric = *((uint64_t*)(void*)&addresses[i][0]); printf(" (0x%016"PRIx64")\n", numeric);
printf("n = 0x%016"PRIx64"\n", numeric);
} }
printf("\n"); printf("\n");
if(++x > 10) break;
} }
} }
// ------------ SIMULATOR --------------- // ------------ SIMULATOR ---------------
// A simple 1-wire bus simulation following the real behavior of bus devices
// in the search operation.
struct owunit { struct owunit {
ow_romcode_t romcode; ow_romcode_t romcode;
bool selected; bool selected;

@ -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;
} }

@ -1,5 +1,6 @@
// //
// Created by MightyPork on 2018/02/01. // Created by MightyPork on 2018/02/01
// MIT license
// //
#ifndef OW_SEARCH_H #ifndef OW_SEARCH_H
@ -39,13 +40,21 @@ extern bool ow_read_bit(void);
// -------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------
/** Data type holding one romcode */ /**
* Data type holding a romcode
*/
typedef uint8_t ow_romcode_t[8]; typedef uint8_t ow_romcode_t[8];
/** Get a bit from a romcode */ /**
#define ow_code_getbit(code, index) (bool)((code)[(index) / 8] & (1 << ((index) % 8))) * Get a single bit from a romcode
/** Set a bit to 1 in a romcode */ */
#define ow_code_setbit(code, index) ((code)[(index) / 8] |= (1 << ((index) % 8))) #define ow_code_getbit(code, index) (bool)((code)[(index) >> 3] & (1 << ((index) & 7)))
/**
* Convert to unsigned 64-bit integer
* (works only on little-endian systems - eg. OK on x86/x86_64, not on PowerPC)
*/
#define ow_romcode_to_u64(code) (*((uint64_t *) (void *)(code)))
/** /**
* States of the search algorithm * States of the search algorithm

Loading…
Cancel
Save