-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw1_search_devices.c
138 lines (110 loc) · 2.95 KB
/
w1_search_devices.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "w1.h"
#include "w1_search_devices.h"
#include <stdlib.h>
#include <string.h>
#include <compiler.h>
SBIT(LED, 0x90, 1);
uint8_t rIdx = 0;
__xdata uint8_t buf[64];
__xdata w1SearchDeviceCallback searchDeviceCallback;
__xdata uint8_t *getBuf() {
return buf;
}
#define BRANCH_FLAG 0x02
static void onW1Reset(uint8_t gotByte);
static void onSearchCmdSent(uint8_t gotByte);
static void onReadBit(uint8_t gotByte);
static void onReadBitInv(uint8_t gotByte);
static void onWriteBit(uint8_t gotByte);
static uint8_t decodeByte(__xdata uint8_t *byteBuf);
void w1SearchDevices(w1SearchDeviceCallback cb) {
searchDeviceCallback = cb;
memset(buf, 0, sizeof(buf));
w1Reset(onW1Reset);
}
void onW1Reset(uint8_t gotByte) {
__xdata w1SearchCtx ctx;
memset(&ctx, 0, sizeof(ctx));
switch(gotByte) {
case 0xf0: ctx.status = W1SEARCH_DONE; break; /* No devices on line */
case 0x00: ctx.status = W1SEARCH_RESET_FAILED; break; /* Something is holding the line */
}
if(ctx.status != 0) {
ctx.done = 1;
searchDeviceCallback(&ctx);
return;
}
w1Write(0xF0, onSearchCmdSent);
}
void onSearchCmdSent(uint8_t gotByte) {
(void)(gotByte);
/* Starting 2-read 1-write 64 bit search sequence described in DS manuals */
rIdx = 0;
w1WriteBit(1, onReadBit);
}
static int b = 0, bInv = 0;
void onReadBit(uint8_t gotByte) {
b = gotByte == 0xFF;
w1WriteBit(1, onReadBitInv);
}
void onReadBitInv(uint8_t gotByte) {
__xdata w1SearchCtx ctx;
bInv = gotByte == 0xFF;
if(b == 1 && bInv == 1) {
/* No devices responded */
ctx.status = W1SEARCH_ERR;
ctx.done = 1;
searchDeviceCallback(&ctx);
return;
} else if(b == 0 && bInv == 0) {
/* Two or more devices are holding the line simultaneously, make a branch here */
/* As you probably mentioned, an existing value in buf[] is getting preserved */
buf[rIdx] |= BRANCH_FLAG;
b = buf[rIdx];
} else {
buf[rIdx] = b;
}
rIdx++;
if(rIdx < 64) {
return w1WriteBit(b & 0x01, onWriteBit);
} else {
__xdata uint8_t romID[8];
uint8_t i;
for(i = 0; i < 8; i++) {
romID[i] = decodeByte(&buf[i * 8]);
}
bool has_more = nextBranch();
ctx.status = W1SEARCH_DEVICE_FOUND;
ctx.done = !has_more;
ctx.romID = romID;
bool need_more = searchDeviceCallback(&ctx);
if(has_more && need_more) {
w1Reset(onW1Reset);
}
}
}
void onWriteBit(uint8_t gotByte) {
(void)(gotByte);
w1WriteBit(1, onReadBit);
}
uint8_t decodeByte(__xdata uint8_t *byteBuf) {
uint8_t byte = 0;
int i;
for(i = 7; i >= 0; i--) {
byte |= byteBuf[i] & 0x01;
if(i > 0) byte <<= 1;
}
return byte;
}
bool nextBranch() {
int i;
for(i = 63; i >= 0; i--) {
if(buf[i] == 2) {
buf[i] = 0x01; // flip bit
return true; // there's one more branch to visit
} else {
buf[i] = 0;
}
}
return false;
}