-
Notifications
You must be signed in to change notification settings - Fork 0
/
dallas_one_wire.c
368 lines (290 loc) · 8.86 KB
/
dallas_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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* An AVR library for communication on a Dallas 1-Wire bus.
*
* Features (?)
* ------------
*
* * It uses a single GPIO pin (no UARTs).
* * It does not use dynamic memory allocation. The only drawback is that you
* have to know the number of devices on the bus in advance.
* * It is polled, not interrupt-driven. There are several sections of code
* that must run for a specific amount of time and have to disable
* interrupts globally.
* * Only the MATCH_ROM, SEARCH_ROM, and SKIP_ROM commands have been
* implemented. At this point other commands would be trivial to add.
*
* Directions
* ----------
*
* 1. Modify F_CPU, DALLAS_PORT, DALLAS_DDR, DALLAS_PORT_IN, DALLAS_PIN, and
* DALLAS_NUM_DEVICES to match your application.
* 2. In your code, first run dallas_search_identifiers() to populate the
* the list of identifiers with the devices on your bus.
* 3. ???
* 4. Profit!
*
* Cautions/Caveats
* ----------------
*
* The 1-Wire bus is *very* timing-dependent. If you are having issues and it's
* not an electrical/connectivity one it is most likely a timing issue. The
* worst function in this regard is dallas_read(). The delays chosen there are a
* compromise between having to wait for the bus to return to 5V after being
* pulled to ground (determined by the RC time constant) while also needing to
* read the bus before the 15 usec time slot expires.
*
* Verify the timing with a logic analyzer or oscilloscope. Also check the
* datasheet for your specific device to make sure that it is ok with the timing
* values chosen in the code and modify, if necessary.
*
* I am not a 1-Wire expert by any means so this code is provided as-is.
*
* Enjoy!
* ------
*
* April 30, 2010
*/
/******************************************************************************
* Copyright © 2010, Mike Roddewig ([email protected]).
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License v3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <avr/io.h>
#include <stdint.h>
#include "dallas_one_wire.h"
#include <util/atomic.h>
#include <util/delay.h>
//////////////////////
// Global variables //
//////////////////////
DALLAS_IDENTIFIER_LIST_t identifier_list;
/////////////////////////////////////
// Identifier routine return codes //
/////////////////////////////////////
#define DALLAS_IDENTIFIER_NO_ERROR 0x00
#define DALLAS_IDENTIFIER_DONE 0x01
#define DALLAS_IDENTIFIER_SEARCH_ERROR 0x02
/////////////////////////////////
// Private function prototypes //
/////////////////////////////////
static uint8_t dallas_discover_identifier(DALLAS_IDENTIFIER_t *, DALLAS_IDENTIFIER_t *);
///////////////
// Functions //
///////////////
void dallas_write(uint8_t bit) {
if (bit == 0x00) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// Configure the pin as an output.
DALLAS_DDR |= _BV(DALLAS_PIN);
// Pull the bus low.
DALLAS_PORT &= ~_BV(DALLAS_PIN);
// Wait the required time.
_delay_us(90);
// Release the bus.
DALLAS_PORT |= _BV(DALLAS_PIN);
// Let the rest of the time slot expire.
_delay_us(30);
}
}
else {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// Configure the pin as an output.
DALLAS_DDR |= _BV(DALLAS_PIN);
// Pull the bus low.
DALLAS_PORT &= ~_BV(DALLAS_PIN);
// Wait the required time.
_delay_us(10);
// Release the bus.
DALLAS_PORT |= _BV(DALLAS_PIN);
// Let the rest of the time slot expire.
_delay_us(50);
}
}
}
uint8_t dallas_read(void) {
uint8_t reply;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// Configure the pin as an output.
DALLAS_DDR |= _BV(DALLAS_PIN);
// Pull the bus low.
DALLAS_PORT &= ~_BV(DALLAS_PIN);
// Wait the required time.
_delay_us(2);
// Configure as input.
DALLAS_PORT |= _BV(DALLAS_PIN);
DALLAS_DDR &= ~_BV(DALLAS_PIN);
// Wait for a bit.
_delay_us(11);
if ((DALLAS_PORT_IN & _BV(DALLAS_PIN)) == 0x00) {
reply = 0x00;
}
else {
reply = 0x01;
}
// Let the rest of the time slot expire.
_delay_us(47);
}
return reply;
}
// Resets the bus and returns 0x01 if a slave indicates present, 0x00 otherwise.
uint8_t dallas_reset(void) {
uint8_t reply;
// Reset the slave_reply variable.
reply = 0x00;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// Configure the pin as an output.
DALLAS_DDR |= _BV(DALLAS_PIN);
// Pull the bus low.
DALLAS_PORT &= ~_BV(DALLAS_PIN);
// Wait the required time.
_delay_us(500); // 500 uS
// Switch to an input, enable pull-up, and wait.
DALLAS_PORT |= _BV(DALLAS_PIN);
DALLAS_DDR &= ~_BV(DALLAS_PIN);
_delay_us(70);
if ((DALLAS_PORT_IN & _BV(DALLAS_PIN)) == 0x00) {
reply = 0x01;
}
_delay_us(420);
}
return reply;
}
void dallas_write_byte(uint8_t byte) {
uint8_t position;
for (position = 0x00; position < 0x08; position++) {
dallas_write(byte & 0x01);
byte = (byte >> 1);
}
}
uint8_t dallas_read_byte(void) {
uint8_t byte;
uint8_t position;
byte = 0x00;
for (position = 0x00; position < 0x08; position++) {
byte += (dallas_read() << position);
}
return byte;
}
// Uses the uC to power the bus.
void dallas_drive_bus(void) {
// Configure the pin as an output.
DALLAS_DDR |= _BV(DALLAS_PIN);
// Set the bus high.
DALLAS_PORT |= _BV(DALLAS_PIN);
}
void dallas_match_rom(DALLAS_IDENTIFIER_t * identifier) {
uint8_t identifier_bit;
uint8_t current_byte;
uint8_t current_bit;
dallas_reset();
dallas_write_byte(MATCH_ROM_COMMAND);
for (identifier_bit = 0x00; identifier_bit < DALLAS_NUM_IDENTIFIER_BITS; identifier_bit++) {
current_byte = identifier_bit / 8;
current_bit = identifier_bit - (current_byte * 8);
dallas_write(identifier->identifier[current_byte] & _BV(current_bit));
}
}
void dallas_skip_rom(void) {
dallas_reset();
dallas_write_byte(SKIP_ROM_COMMAND);
}
uint8_t dallas_search_identifiers(void) {
uint8_t current_device;
uint8_t return_code;
for (current_device = 0x00; current_device < DALLAS_NUM_DEVICES; current_device++) {
if (current_device == 0x00) {
return_code = dallas_discover_identifier(&identifier_list.identifiers[current_device], 0x00);
}
else {
return_code = dallas_discover_identifier(&identifier_list.identifiers[current_device], &identifier_list.identifiers[current_device-1]);
}
if (return_code == DALLAS_IDENTIFIER_DONE) {
identifier_list.num_devices = current_device + 0x01;
return 0x00;
}
else if (return_code == DALLAS_IDENTIFIER_SEARCH_ERROR) {
return 0x01;
}
}
return 0x02;
}
DALLAS_IDENTIFIER_LIST_t * get_identifier_list(void) {
return &identifier_list;
}
static uint8_t dallas_discover_identifier(DALLAS_IDENTIFIER_t * current_identifier, DALLAS_IDENTIFIER_t * last_identifier) {
uint8_t identifier_bit;
uint8_t received_two_bits;
uint8_t current_bit;
uint8_t current_byte;
uint8_t identifier_diverged;
identifier_diverged = 0x00;
identifier_bit = 0x00;
dallas_reset();
dallas_write_byte(SEARCH_ROM_COMMAND);
for (identifier_bit = 0; identifier_bit < DALLAS_NUM_IDENTIFIER_BITS; identifier_bit++) {
received_two_bits = (dallas_read() << 1);
received_two_bits += dallas_read();
current_byte = identifier_bit / 8;
current_bit = identifier_bit - (current_byte * 8);
if (received_two_bits == 0x02) {
// All devices have a 1 at this position.
current_identifier->identifier[current_byte] += (1 << current_bit);
dallas_write(0x01);
}
else if (received_two_bits == 0x01) {
// All devices have a 0 at this position.
dallas_write(0x00);
}
else if (received_two_bits == 0x00) {
if ((identifier_diverged == 0x00) && (last_identifier != 0x00)) {
identifier_diverged = 0x01;
if ((last_identifier->identifier[current_byte] & _BV(current_bit)) == 0x00) {
// Then we choose 1.
current_identifier->identifier[current_byte] += (1 << current_bit);
dallas_write(0x01);
}
else {
// Otherwise 0.
dallas_write(0x00);
}
}
else {
// We'll go with 0.
dallas_write(0x00);
}
}
else {
// Error!
return DALLAS_IDENTIFIER_SEARCH_ERROR;
}
}
if (identifier_diverged == 0x00) {
return DALLAS_IDENTIFIER_DONE;
}
else {
return DALLAS_IDENTIFIER_NO_ERROR;
}
}
void dallas_write_buffer(uint8_t * buffer, uint8_t buffer_length) {
uint8_t i;
for (i = 0x00; i < buffer_length; i++) {
dallas_write_byte(buffer[i]);
}
}
void dallas_read_buffer(uint8_t * buffer, uint8_t buffer_length) {
uint8_t i;
for (i = 0x00; i < buffer_length; i++) {
buffer[i] = dallas_read_byte();
}
}