comparison 1wire.c @ 21:e82d15fa9a1a

Re-format the code.
author darius
date Mon, 12 Dec 2005 23:01:00 +1030
parents 026dc24d85e0
children bd792ebf813d
comparison
equal deleted inserted replaced
20:59c7fcf10ea0 21:e82d15fa9a1a
31 */ 31 */
32 32
33 #include <stdio.h> 33 #include <stdio.h>
34 #include <avr/io.h> 34 #include <avr/io.h>
35 #include <avr/pgmspace.h> 35 #include <avr/pgmspace.h>
36 36 #include <util/delay.h>
37 #include "1wire.h" 37 #include "1wire.h"
38 #include "1wire-delay.h" 38 #include "1wire-delay.h"
39 39
40 #if OW_DEBUG 40 #if OW_DEBUG
41 void uart_putsP(const char *addr); 41 void uart_putsP(const char *addr);
42 void uart_puts(const char *addr); 42 void uart_puts(const char *addr);
43 void uart_getc(); 43 void uart_getc();
44 int uart_putc(char c); 44 char uart_putc(char c);
45 #endif 45 #endif
46 46
47 static uint8_t OW_LastDevice = 0; 47 static uint8_t OW_LastDevice = 0;
48 static uint8_t OW_LastDiscrepancy = 0; 48 static uint8_t OW_LastDiscrepancy = 0;
49 static uint8_t OW_LastFamilyDiscrepancy = 0; 49 static uint8_t OW_LastFamilyDiscrepancy = 0;
54 } 54 }
55 55
56 /*----------------------------------------------------------------------------- 56 /*-----------------------------------------------------------------------------
57 * Generate a 1-Wire reset, return 0 if presence pulse was found, 57 * Generate a 1-Wire reset, return 0 if presence pulse was found,
58 * return 1 otherwise. 58 * return 1 otherwise.
59 *
59 * (NOTE: Does not handle alarm presence from DS2404/DS1994) 60 * (NOTE: Does not handle alarm presence from DS2404/DS1994)
60 */ 61 */
61 uint8_t 62 uint8_t
62 OWTouchReset(void) { 63 OWTouchReset(void) {
63 DELAY_G; 64 DELAY_G;
219 uint8_t last_zero, rom_byte_number, rom_byte_mask; 220 uint8_t last_zero, rom_byte_number, rom_byte_mask;
220 uint8_t lastcrc8, crcaccum; 221 uint8_t lastcrc8, crcaccum;
221 int8_t next_result; 222 int8_t next_result;
222 223
223 /* Init for search */ 224 /* Init for search */
224 bit_number = 1; 225 bit_number = 1;
225 last_zero = 0; 226 last_zero = 0;
226 rom_byte_number = 0; 227 rom_byte_number = 0;
227 rom_byte_mask = 1; 228 rom_byte_mask = 1;
228 next_result = OW_NOMODULES; 229 next_result = OW_NOMODULES;
229 lastcrc8 = 0; 230 lastcrc8 = 0;
230 crcaccum = 0; 231 crcaccum = 0;
231 232
232 /* if the last call was not the last one */ 233 /* if the last call was not the last one */
233 if (!OW_LastDevice) { 234 if (!OW_LastDevice) {
234 /* check if reset first is requested */ 235 /* check if reset first is requested */
235 if (do_reset) { 236 if (do_reset) {
236 /* reset the 1-wire 237 /* reset the 1-wire
237 * if there are no parts on 1-wire, return 0 */ 238 * if there are no parts on 1-wire, return 0 */
238 #if OW_DEBUG 239 #if OW_DEBUG
239 uart_putsP(PSTR("Resetting\n\r")); 240 uart_putsP(PSTR("Resetting\n\r"));
240 #endif 241 #endif
241 if (OWTouchReset()) { 242 if (OWTouchReset()) {
242 /* reset the search */ 243 /* reset the search */
243 OW_LastDiscrepancy = 0; 244 OW_LastDiscrepancy = 0;
244 OW_LastFamilyDiscrepancy = 0; 245 OW_LastFamilyDiscrepancy = 0;
245 #if OW_DEBUG 246 #if OW_DEBUG
246 uart_putsP(PSTR("No devices on bus\n\r")); 247 uart_putsP(PSTR("No devices on bus\n\r"));
247 #endif 248 #endif
248 return OW_NOPRESENCE; 249 return OW_NOPRESENCE;
249 } 250 }
250 } 251 }
251 252
252 /* If finding alarming devices issue a different command */ 253 /* If finding alarming devices issue a different command */
253 if (alarm_only) 254 if (alarm_only)
254 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */ 255 OWWriteByte(OW_SEARCH_ALRM_CMD); /* issue the alarming search command */
255 else 256 else
256 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */ 257 OWWriteByte(OW_SEARCH_ROM_CMD); /* issue the search command */
257 258
258 /* pause before beginning the search */ 259 /* pause before beginning the search */
259 OWdelay(); 260 OWdelay();
260 OWdelay(); 261 OWdelay();
261 OWdelay(); 262 OWdelay();
262 263
263 /* loop to do the search */ 264 /* loop to do the search */
264 do { 265 do {
265 /* read a bit and its compliment */ 266 /* read a bit and its compliment */
266 bit_test = OWReadBit() << 1; 267 bit_test = OWReadBit() << 1;
267 bit_test |= OWReadBit(); 268 bit_test |= OWReadBit();
268 269
269 #if OW_DEBUG 270 #if OW_DEBUG
270 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); 271 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
271 uart_puts(errstr); 272 uart_puts(errstr);
272 #endif 273 #endif
273 274
274 /* check for no devices on 1-wire */ 275 /* check for no devices on 1-wire */
275 if (bit_test == 3) { 276 if (bit_test == 3) {
276 #if OW_DEBUG 277 #if OW_DEBUG
277 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test); 278 sprintf_P(errstr, PSTR("bit_test = %d\n\r"), bit_test);
278 uart_puts(errstr); 279 uart_puts(errstr);
279 #endif 280 #endif
280 return(OW_BADWIRE); 281 return(OW_BADWIRE);
281 } 282 }
282 else { 283 else {
283 /* all devices coupled have 0 or 1 */ 284 /* all devices coupled have 0 or 1 */
284 if (bit_test > 0) 285 if (bit_test > 0)
285 search_direction = !(bit_test & 0x01); /* bit write value for search */ 286 search_direction = !(bit_test & 0x01); /* bit write value for search */
286 else { 287 else {
287 /* if this discrepancy is before the Last Discrepancy 288 /* if this discrepancy is before the Last Discrepancy
288 * on a previous OWNext then pick the same as last time */ 289 * on a previous OWNext then pick the same as last time */
289 if (bit_number < OW_LastDiscrepancy) 290 if (bit_number < OW_LastDiscrepancy)
290 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0); 291 search_direction = ((ROM[rom_byte_number] & rom_byte_mask) > 0);
291 else 292 else
292 /* if equal to last pick 1, if not then pick 0 */ 293 /* if equal to last pick 1, if not then pick 0 */
293 search_direction = (bit_number == OW_LastDiscrepancy); 294 search_direction = (bit_number == OW_LastDiscrepancy);
294 295
295 /* if 0 was picked then record its position in LastZero */ 296 /* if 0 was picked then record its position in LastZero */
296 if (search_direction == 0) { 297 if (search_direction == 0) {
297 last_zero = bit_number; 298 last_zero = bit_number;
298 299
299 /* check for Last discrepancy in family */ 300 /* check for Last discrepancy in family */
300 if (last_zero < 9) 301 if (last_zero < 9)
301 OW_LastFamilyDiscrepancy = last_zero; 302 OW_LastFamilyDiscrepancy = last_zero;
302 } 303 }
303 } 304 }
304 305
305 /* set or clear the bit in the ROM byte rom_byte_number 306 /* set or clear the bit in the ROM byte rom_byte_number
306 * with mask rom_byte_mask */ 307 * with mask rom_byte_mask */
307 if (search_direction == 1) 308 if (search_direction == 1)
308 ROM[rom_byte_number] |= rom_byte_mask; 309 ROM[rom_byte_number] |= rom_byte_mask;
309 else 310 else
310 ROM[rom_byte_number] &= ~rom_byte_mask; 311 ROM[rom_byte_number] &= ~rom_byte_mask;
311 312
312 /* serial number search direction write bit */ 313 /* serial number search direction write bit */
313 OWWriteBit(search_direction); 314 OWWriteBit(search_direction);
314 315
315 /* increment the byte counter bit_number 316 /* increment the byte counter bit_number
316 * and shift the mask rom_byte_mask */ 317 * and shift the mask rom_byte_mask */
317 bit_number++; 318 bit_number++;
318 rom_byte_mask <<= 1; 319 rom_byte_mask <<= 1;
319 320
320 /* if the mask is 0 then go to new ROM byte rom_byte_number 321 /* if the mask is 0 then go to new ROM byte rom_byte_number
321 * and reset mask */ 322 * and reset mask */
322 if (rom_byte_mask == 0) { 323 if (rom_byte_mask == 0) {
323 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */ 324 OWCRC(ROM[rom_byte_number], &crcaccum); /* accumulate the CRC */
324 lastcrc8 = crcaccum; 325 lastcrc8 = crcaccum;
325 326
326 rom_byte_number++; 327 rom_byte_number++;
327 rom_byte_mask = 1; 328 rom_byte_mask = 1;
328 } 329 }
329 } 330 }
330 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */ 331 } while (rom_byte_number < 8); /* loop until through all ROM bytes 0-7 */
331 332
332 /* if the search was successful then */ 333 /* if the search was successful then */
333 if (!(bit_number < 65) || lastcrc8) { 334 if (!(bit_number < 65) || lastcrc8) {
334 if (lastcrc8) { 335 if (lastcrc8) {
335 #if OW_DEBUG 336 #if OW_DEBUG
336 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8); 337 sprintf_P(errstr, PSTR("Bad CRC (%d)\n\r"), lastcrc8);
337 uart_puts(errstr); 338 uart_puts(errstr);
338 #endif 339 #endif
339 next_result = OW_BADCRC; 340 next_result = OW_BADCRC;
340 } else { 341 } else {
341 /* search successful so set LastDiscrepancy,LastDevice,next_result */ 342 /* search successful so set LastDiscrepancy,LastDevice,next_result */
342 OW_LastDiscrepancy = last_zero; 343 OW_LastDiscrepancy = last_zero;
343 OW_LastDevice = (OW_LastDiscrepancy == 0); 344 OW_LastDevice = (OW_LastDiscrepancy == 0);
344 #if OW_DEBUG 345 #if OW_DEBUG
345 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice); 346 sprintf_P(errstr, PSTR("Last device = %d\n\r"), OW_LastDevice);
346 uart_puts(errstr); 347 uart_puts(errstr);
347 #endif 348 #endif
348 next_result = OW_FOUND; 349 next_result = OW_FOUND;
349 } 350 }
350 } 351 }
351 } 352 }
352 353
353 /* if no device found then reset counters so next 'next' will be 354 /* if no device found then reset counters so next 'next' will be
354 * like a first */ 355 * like a first */
355 if (next_result != OW_FOUND || ROM[0] == 0) { 356 if (next_result != OW_FOUND || ROM[0] == 0) {
356 OW_LastDiscrepancy = 0; 357 OW_LastDiscrepancy = 0;
357 OW_LastDevice = 0; 358 OW_LastDevice = 0;
358 OW_LastFamilyDiscrepancy = 0; 359 OW_LastFamilyDiscrepancy = 0;
359 } 360 }
360 361
361 if (next_result == OW_FOUND && ROM[0] == 0x00) 362 if (next_result == OW_FOUND && ROM[0] == 0x00)
362 next_result = OW_BADWIRE; 363 next_result = OW_BADWIRE;
363 364
364 return next_result; 365 return next_result;
365 366
366 } 367 }
367 368
368 uint8_t PROGMEM dscrc_table[] = { 369 uint8_t PROGMEM dscrc_table[] = {
369 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 370 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,