annotate 1wire.c @ 11:ccc39c9f320b

Enable pretty LED command.
author darius
date Fri, 16 Jul 2004 23:39:58 +0930
parents eb1faf51968e
children 4b141cc7cbd4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
1 /*
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
2 * Various 1 wire routines
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
3 * Search routine is copied from the Dallas owpd library with mods.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
4 *
8
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
5 * $Id$
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
6 *
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
7 * Copyright (c) 2004
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
8 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
9 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
10 * Redistribution and use in source and binary forms, with or without
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
11 * modification, are permitted provided that the following conditions
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
12 * are met:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
13 * 1. Redistributions of source code must retain the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
15 * 2. Redistributions in binary form must reproduce the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
16 * notice, this list of conditions and the following disclaimer in the
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
17 * documentation and/or other materials provided with the distribution.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
18 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
29 * SUCH DAMAGE.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
30 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
31
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
32 #include <stdio.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
33 #include <avr/io.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
34 #include <avr/pgmspace.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
35
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
36 #include "1wire.h"
8
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
37 #include "1wire-delay.h"
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
38
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
39 void uart_putsP(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
40 void uart_puts(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
41 void uart_getc();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
42 int uart_putc(char c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
43
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
44 static uint8_t OW_LastDevice = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
45 static uint8_t OW_LastDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
46 static uint8_t OW_LastFamilyDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
47
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
48 static void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
49 OWdelay(void) {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
50 DELAY_I;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
51 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
52
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
53 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
54 * Generate a 1-Wire reset, return 1 if no presence detect was found,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
55 * return 0 otherwise.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
56 * (NOTE: Does not handle alarm presence from DS2404/DS1994)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
57 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
58 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
59 OWTouchReset(void) {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
60 DELAY_G;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
61 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
62 OWIREDDR |= _BV(OWIREOUTPIN);
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
63 DELAY_H;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
64 OWIREDDR &= ~(_BV(OWIREOUTPIN));
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
65 DELAY_I;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
66
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
67 return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
68 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
69
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
70 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
71 * Send a 1-wire write bit.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
72 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
73 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
74 OWWriteBit(int bit) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
75 OWdelay();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
76 if (bit) {
8
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
77 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
78 OWIREDDR |= _BV(OWIREOUTPIN);
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
79 DELAY_A;
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
80 OWIREDDR &= ~(_BV(OWIREOUTPIN));
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
81 DELAY_B;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
82 } else {
8
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
83 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
84 OWIREDDR |= _BV(OWIREOUTPIN);
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
85 DELAY_C;
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
86 OWIREDDR &= ~(_BV(OWIREOUTPIN));
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
87 DELAY_D;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
88 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
89 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
90
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
91 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
92 * Read a bit from the 1-wire bus and return it.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
93 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
94 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
95 OWReadBit(void) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
96 OWdelay();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
97
8
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
98 OWIREOUTPORT &= ~(_BV(OWIREOUTPIN));
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
99 OWIREDDR |= _BV(OWIREOUTPIN);
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
100 DELAY_A;
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
101 OWIREDDR &= ~(_BV(OWIREOUTPIN));
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
102 DELAY_E;
f9a085a0ba93 Change the 1 wire routines to mostly C with assembly delay routines
darius
parents: 0
diff changeset
103 return(OWIREINPORT & _BV(OWIREINPIN) ? 1 : 0);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
104 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
105
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
106 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
107 * Write a byte to the 1-wire bus
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
108 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
109 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
110 OWWriteByte(uint8_t data) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
111 int i;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
112
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
113 /* Send LSB first */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
114 for (i = 0; i < 8; i++) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
115 OWWriteBit(data & 0x01);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
116 data >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
117 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
118 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
119
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
120 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
121 * Read a byte from the 1-wire bus
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
122 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
123 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
124 OWReadByte(void) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
125 int i, result = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
126
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
127 for (i = 0; i < 8; i++) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
128 result >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
129 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
130 result |= 0x80;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
131 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
132 return(result);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
133 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
134
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
135 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
136 * Write a 1-wire data byte and return the sampled result.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
137 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
138 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
139 OWTouchByte(uint8_t data) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
140 int i, result = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
141
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
142 for (i = 0; i < 8; i++) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
143 result >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
144
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
145 /* If sending a 1 then read a bit, otherwise write a 0 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
146 if (data & 0x01) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
147 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
148 result |= 0x80;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
149 } else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
150 OWWriteBit(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
151
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
152 data >>= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
153 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
154
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
155 return(result);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
156 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
157
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
158 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
159 * Write a block of bytes to the 1-wire bus and return the sampled result in
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
160 * the same buffer
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
161 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
162 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
163 OWBlock(uint8_t *data, int len) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
164 int i;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
165
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
166 for (i = 0; i < len; i++)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
167 data[i] = OWTouchByte(data[i]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
168 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
169
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
170
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
171 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
172 * Send a 1 wire command to a device, or all if no ROM ID provided
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
173 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
174 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
175 OWSendCmd(uint8_t *ROM, uint8_t cmd) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
176 int i;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
177
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
178 OWTouchReset();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
179
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
180 if (ROM == NULL)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
181 OWWriteByte(OW_SKIP_ROM_CMD);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
182 else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
183 OWWriteByte(OW_MATCH_ROM_CMD);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
184 for (i = 0; i < 8; i++)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
185 OWWriteByte(ROM[i]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
186 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
187
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
188 OWWriteByte(cmd);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
189 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
190
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
191 /*-----------------------------------------------------------------------------
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
192 * Search algorithm from App note 187 (and 162)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
193 *
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
194 * OWFirst/OWNext return..
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
195 * 1 when something is found,
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
196 * 0 no more modules
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
197 * -1 if no presense pulse,
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
198 * -2 if bad CRC,
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
199 * -3 if bad wiring.
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
200 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
201 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
202 OWFirst(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
203 /* Reset state */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
204 OW_LastDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
205 OW_LastDevice = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
206 OW_LastFamilyDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
207
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
208 /* Go looking */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
209 return (OWNext(ROM, do_reset, alarm_only));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
210 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
211
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
212 /* Returns 1 when something is found, 0 if nothing left */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
213 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
214 OWNext(uint8_t *ROM, uint8_t do_reset, uint8_t alarm_only) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
215 uint8_t bit_test, search_direction, bit_number;
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
216 uint8_t last_zero, rom_byte_number, rom_byte_mask;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
217 uint8_t lastcrc8, crcaccum;
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
218 int next_result;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
219 char errstr[30];
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
220
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
221 /* Init for search */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
222 bit_number = 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
223 last_zero = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
224 rom_byte_number = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
225 rom_byte_mask = 1;
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
226 next_result = OW_NOMODULES;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
227 lastcrc8 = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
228 crcaccum = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
229
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
230 /* if the last call was not the last one */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
231 if (!OW_LastDevice) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
232 /* check if reset first is requested */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
233 if (do_reset) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
234 /* reset the 1-wire
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
235 * if there are no parts on 1-wire, return 0 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
236 #if OW_DEBUG
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
237 uart_putsP(PSTR("Resetting\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
238 #endif
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
239 if (OWTouchReset()) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
240 /* reset the search */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
241 OW_LastDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
242 OW_LastFamilyDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
243 #if OW_DEBUG
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
244 uart_putsP(PSTR("No devices on bus\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
245 #endif
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
246 return OW_NOPRESENCE;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
247 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
248 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
249
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
250 /* If finding alarming devices issue a different command */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
251 if (alarm_only)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
252 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
253 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
254 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
255
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
256 /* pause before beginning the search */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
257 OWdelay();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
258 OWdelay();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
259 OWdelay();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
260
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
261 /* loop to do the search */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
262 do {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
263 /* read a bit and its compliment */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
264 bit_test = OWReadBit() << 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
265 bit_test |= OWReadBit();
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
266
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
267 #if OW_DEBUG
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
268 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
269 uart_puts(errstr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
270 #endif
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
271
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
272 /* check for no devices on 1-wire */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
273 if (bit_test == 3) {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
274 #if OW_DEBUG
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
275 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
276 uart_puts(errstr);
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
277 #endif
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
278 return(OW_BADWIRE);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
279 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
280 else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
281 /* all devices coupled have 0 or 1 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
282 if (bit_test > 0)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
283 search_direction = !(bit_test & 0x01); /* bit write value for search */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
284 else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
285 /* if this discrepancy is before the Last Discrepancy
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
286 * on a previous OWNext then pick the same as last time */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
287 if (bit_number < OW_LastDiscrepancy)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
288 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
289 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
290 /* if equal to last pick 1, if not then pick 0 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
291 search_direction = (bit_number == OW_LastDiscrepancy);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
292
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
293 /* if 0 was picked then record its position in LastZero */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
294 if (search_direction == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
295 last_zero = bit_number;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
296
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
297 /* check for Last discrepancy in family */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
298 if (last_zero < 9)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
299 OW_LastFamilyDiscrepancy = last_zero;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
300 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
301 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
302
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
303 /* set or clear the bit in the ROM byte rom_byte_number
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
304 * with mask rom_byte_mask */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
305 if (search_direction == 1)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
306 ROM[rom_byte_number] |= rom_byte_mask;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
307 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
308 ROM[rom_byte_number] &= ~rom_byte_mask;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
309
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
310 /* serial number search direction write bit */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
311 OWWriteBit(search_direction);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
312
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
313 /* increment the byte counter bit_number
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
314 * and shift the mask rom_byte_mask */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
315 bit_number++;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
316 rom_byte_mask <<= 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
318 /* if the mask is 0 then go to new ROM byte rom_byte_number
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
319 * and reset mask */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
320 if (rom_byte_mask == 0) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
321 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
322 lastcrc8 = crcaccum;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
323
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
324 rom_byte_number++;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
325 rom_byte_mask = 1;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
326 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
327 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
328 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
329
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
330 /* if the search was successful then */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
331 if (!(bit_number < 65) || lastcrc8) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
332 if (lastcrc8) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
333 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
334 uart_puts(errstr);
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
335 next_result = OW_BADCRC;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
336 } else {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
337 /* search successful so set LastDiscrepancy,LastDevice,next_result */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
338 OW_LastDiscrepancy = last_zero;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
339 OW_LastDevice = (OW_LastDiscrepancy == 0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
340 #if OW_DEBUG
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
341 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
342 uart_puts(errstr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
343 #endif
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
344 next_result = OW_FOUND;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
345 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
346 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
347 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
348
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
349 /* if no device found then reset counters so next 'next' will be
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
350 * like a first */
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
351 if (next_result != OW_FOUND || ROM[0] == 0) {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
352 OW_LastDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
353 OW_LastDevice = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
354 OW_LastFamilyDiscrepancy = 0;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
355 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
356
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
357 if (next_result == OW_FOUND && ROM[0] == 0x00)
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
358 next_result = OW_BADWIRE;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 8
diff changeset
359
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
360 return next_result;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
361
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
362 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
363
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
364 uint8_t PROGMEM dscrc_table[] = {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
365 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
366 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
367 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
368 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
369 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
370 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
371 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
372 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
373 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
374 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
375 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
376 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
377 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
378 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
379 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
380 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
381 };
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
382
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
383 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
384 OWCRC(uint8_t x, uint8_t *crc) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
385 *crc = pgm_read_byte(&dscrc_table[(*crc) ^ x]);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
386 }