-
Notifications
You must be signed in to change notification settings - Fork 0
/
one_wire.c
297 lines (260 loc) · 8.77 KB
/
one_wire.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "one_wire.h"
// This code came from https://github.com/ppkt, mostly. Some changes
// were made for the Cypress hardware.
one_wire_device one_wire_devices[10];
uint8_t one_wire_device_count = 0;
one_wire_state state;
// global search state
uint8_t last_mismatch;
uint8_t last_device_flag;
uint8_t crc8;
uint8_t ROM_NO[8];
const unsigned char crc_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
};
uint8_t one_wire_reset_pulse() {
// Pull bus down for 500 us (min. 480 us)
Wire1_Write(0); // do the gpio operation to bring the Wire1 pin low
CyDelayUs(RST_TIME); // Cypress delays are fairly accurate, even at the microsecond level
Wire1_Write(1); // bring the Wire1 pin back high
// Wait 70 us, bus should be pulled up by resistor and then
// pulled down by slave (15-60 us after detecting rising edge)
CyDelayUs(70);
uint8_t bit = Wire1_Read(); // read the input of the Wire1 pin
if (bit == 0) {
state = ONE_WIRE_SLAVE_PRESENT;
} else {
state = ONE_WIRE_ERROR;
return 0;
}
// Wait additional 430 us until slave keeps bus down (total 500 us, min. 480 us)
CyDelayUs(430);
return 1;
}
void one_wire_write_1() {
// Pull bus down for 15 us
Wire1_Write(0);
CyDelayUs(WRITE_1_TIME);
Wire1_Write(1);
// Wait until end of timeslot (60 us) + 5 us for recovery
CyDelayUs(50);
}
void one_wire_write_0() {
// Pull bus down for 60 us
Wire1_Write(0);
CyDelayUs(WRITE_0_TIME);
Wire1_Write(1);
// Wait until end of timeslot (60 us) + 5 us for recovery
CyDelayUs(5);
}
void one_wire_write_bit(uint8_t bit) {
if (bit) {
one_wire_write_1();
} else {
one_wire_write_0();
}
}
uint8_t one_wire_read_bit() {
// Pull bus down for 5 us
Wire1_Write(0);
CyDelayUs(5);
Wire1_Write(1);
// Wait 5 us and check bus state
CyDelayUs(5);
uint8_t bit;
bit = Wire1_Read();
// Wait until end of timeslot (60 us) + 5 us for recovery
CyDelayUs(TIME_SLOT);
return bit;
}
void one_wire_write_byte(uint8_t data) {
uint8_t i;
for (i = 0; i < 8; ++i) {
if ((data >> i) & 1) {
one_wire_write_1();
} else {
one_wire_write_0();
}
}
}
uint8_t one_wire_read_byte() {
uint8_t i;
uint8_t data = 0;
uint8_t bit;
for (i = 0; i < 8; ++i) {
bit = one_wire_read_bit();
data |= bit << i;
}
return data;
}
void one_wire_reset_crc() {
crc8 = 0;
}
uint8_t one_wire_get_crc() {
return crc8;
}
uint8_t one_wire_crc(uint8_t data) {
crc8 = crc_table[crc8 ^ data];
return crc8;
}
// I don't know why nothing is done with the bytes read...
// it never seems to get used anyway
void one_wire_read_rom() {
one_wire_reset_pulse();
one_wire_write_byte(READ_ROM_CMD);
one_wire_read_byte();
one_wire_read_byte();
one_wire_read_byte();
one_wire_read_byte();
one_wire_read_byte();
one_wire_read_byte();
one_wire_read_byte();
one_wire_read_byte();
}
int one_wire_search()
{
uint8_t id_bit_number;
int last_zero, rom_byte_number, search_result;
uint8_t id_bit, cmp_id_bit;
uint8_t rom_byte_mask, search_direction;
// initialize for search
id_bit_number = 1;
last_zero = 0;
rom_byte_number = 0;
rom_byte_mask = 1;
search_result = 0;
crc8 = 0;
// if the last call was not the last one
if (!last_device_flag)
{
// 1-Wire reset
if (!one_wire_reset_pulse()) {
// reset the search
last_mismatch = 0;
last_device_flag = 0;
return 0;
}
// issue the search command
one_wire_write_byte(SEARCH_CMD);
// loop to do the search
do
{
// read a bit and its complement
id_bit = one_wire_read_bit();
cmp_id_bit = one_wire_read_bit();
// check for no devices on 1-wire
if ((id_bit == 1) && (cmp_id_bit == 1))
break; // no devices
// 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 < last_mismatch)
search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
else
// if equal to last pick 1, if not then pick 0
search_direction = (id_bit_number == last_mismatch);
// if 0 was picked then record its position in LastZero
if (search_direction == 0)
{
last_zero = id_bit_number;
}
}
// set or clear the bit in the ROM byte rom_byte_number
// with mask rom_byte_mask
if (search_direction == 1)
ROM_NO[rom_byte_number] |= rom_byte_mask;
else
ROM_NO[rom_byte_number] &= ~rom_byte_mask;
// serial number search direction write bit
one_wire_write_bit(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)
{
one_wire_crc(ROM_NO[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 last_mismatch, last_device_flag, search_result
last_mismatch = last_zero;
// check for last device
if (last_mismatch == 0)
last_device_flag = 1;
search_result = 1;
}
}
// if no device found then reset counters so next 'search' will be like a first
if (!search_result || !ROM_NO[0])
{
last_mismatch = 0;
last_device_flag = 0;
search_result = 0;
}
return search_result;
}
uint8_t one_wire_match_rom(one_wire_device device) {
int i;
one_wire_reset_pulse();
one_wire_write_byte(MATCH_ROM_CMD); // Match ROM command
for (i = 0; i < 8; ++i) {
one_wire_write_byte(device.address[i]);
}
return 0;
}
int one_wire_first()
{
// reset the search state
last_mismatch = 0;
last_device_flag = 0;
return one_wire_search();
}
int one_wire_next()
{
// leave the search state alone
return one_wire_search();
}
one_wire_device* one_wire_search_rom(uint8_t *devices) {
int result, i;
one_wire_device_count = 0;
result = one_wire_first();
while (result)
{
one_wire_device device;
for (i = 7; i >= 0; i--) {
}
for (i = 7; i >= 0; i--) {
device.address[i] = ROM_NO[i];
}
one_wire_devices[one_wire_device_count++] = device;
result = one_wire_next();
}
*devices = one_wire_device_count;
return one_wire_devices;
}