2 Copyright (c) 2007, Jim Studt (original old version - many contributors since)
4 The latest version of this library may be found at:
5 http://www.pjrc.com/teensy/td_libs_OneWire.html
7 OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since
8 January 2010. At the time, it was in need of many bug fixes, but had
9 been abandoned the original author (Jim Studt). None of the known
10 contributors were interested in maintaining OneWire. Paul typically
11 works on OneWire every 6 to 12 months. Patches usually wait that
12 long. If anyone is interested in more actively maintaining OneWire,
16 Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com
17 Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030
18 Fix DS18B20 example negative temperature
19 Fix DS18B20 example's low res modes, Ken Butcher
20 Improve reset timing, Mark Tillotson
21 Add const qualifiers, Bertrik Sikken
22 Add initial value input to crc16, Bertrik Sikken
23 Add target_search() function, Scott Roberts
26 Arduino 1.0 compatibility, Paul Stoffregen
27 Improve temperature example, Paul Stoffregen
28 DS250x_PROM example, Guillermo Lovato
29 PIC32 (chipKit) compatibility, Jason Dangel, dangel.jason AT gmail.com
30 Improvements from Glenn Trewitt:
32 - check_crc16() does all of calculation/checking work.
33 - Added read_bytes() and write_bytes(), to reduce tedious loops.
34 - Added ds2408 example.
35 Delete very old, out-of-date readme file (info is here)
37 Version 2.0: Modifications by Paul Stoffregen, January 2010:
38 http://www.pjrc.com/teensy/td_libs_OneWire.html
39 Search fix from Robin James
40 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27
41 Use direct optimized I/O in all cases
42 Disable interrupts during timing critical sections
43 (this solves many random communication errors)
44 Disable interrupts during read-modify-write I/O
45 Reduce RAM consumption by eliminating unnecessary
46 variables and trimming many to 8 bits
47 Optimize both crc8 - table version moved to flash
49 Modified to work with larger numbers of devices - avoids loop.
50 Tested in Arduino 11 alpha with 12 sensors.
51 26 Sept 2008 -- Robin James
52 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27
54 Updated to work with arduino-0008 and to include skip() as of
57 Modified to calculate the 8-bit CRC directly, avoiding the need for
58 the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010
59 -- Tom Pollard, Jan 23, 2008
61 Jim Studt's original library was modified by Josh Larios.
63 Tom Pollard, pollard@alum.mit.edu, contributed around May 20, 2008
65 Permission is hereby granted, free of charge, to any person obtaining
66 a copy of this software and associated documentation files (the
67 "Software"), to deal in the Software without restriction, including
68 without limitation the rights to use, copy, modify, merge, publish,
69 distribute, sublicense, and/or sell copies of the Software, and to
70 permit persons to whom the Software is furnished to do so, subject to
71 the following conditions:
73 The above copyright notice and this permission notice shall be
74 included in all copies or substantial portions of the Software.
76 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
77 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
78 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
79 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
80 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
81 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
82 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
84 Much of the code was inspired by Derek Yerger's code, though I don't
85 think much of that remains. In any event that was..
86 (copyleft) 2006 by Derek Yerger - Free to distribute freely.
88 The CRC code was excerpted and inspired by the Dallas Semiconductor
89 sample code bearing this copyright.
90 //---------------------------------------------------------------------------
91 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
93 // Permission is hereby granted, free of charge, to any person obtaining a
94 // copy of this software and associated documentation files (the "Software"),
95 // to deal in the Software without restriction, including without limitation
96 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
97 // and/or sell copies of the Software, and to permit persons to whom the
98 // Software is furnished to do so, subject to the following conditions:
100 // The above copyright notice and this permission notice shall be included
101 // in all copies or substantial portions of the Software.
103 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
104 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
105 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
106 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
107 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
108 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
109 // OTHER DEALINGS IN THE SOFTWARE.
111 // Except as contained in this notice, the name of Dallas Semiconductor
112 // shall not be used except as stated in the Dallas Semiconductor
114 //--------------------------------------------------------------------------
120 OneWire::OneWire(uint8_t pin)
123 bitmask = PIN_TO_BITMASK(pin);
124 baseReg = PIN_TO_BASEREG(pin);
131 // Perform the onewire reset function. We will wait up to 250uS for
132 // the bus to come high, if it doesn't then it is broken or shorted
133 // and we return a 0;
135 // Returns 1 if a device asserted a presence pulse, 0 otherwise.
137 uint8_t OneWire::reset(void)
139 IO_REG_TYPE mask = bitmask;
140 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
142 uint8_t retries = 125;
145 DIRECT_MODE_INPUT(reg, mask);
147 // wait until the wire is high... just in case
149 if (--retries == 0) return 0;
150 delayMicroseconds(2);
151 } while ( !DIRECT_READ(reg, mask));
154 DIRECT_WRITE_LOW(reg, mask);
155 DIRECT_MODE_OUTPUT(reg, mask); // drive output low
157 delayMicroseconds(480);
159 DIRECT_MODE_INPUT(reg, mask); // allow it to float
160 delayMicroseconds(70);
161 r = !DIRECT_READ(reg, mask);
163 delayMicroseconds(410);
168 // Write a bit. Port and bit is used to cut lookup time and provide
169 // more certain timing.
171 void OneWire::write_bit(uint8_t v)
173 IO_REG_TYPE mask=bitmask;
174 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
178 DIRECT_WRITE_LOW(reg, mask);
179 DIRECT_MODE_OUTPUT(reg, mask); // drive output low
180 delayMicroseconds(10);
181 DIRECT_WRITE_HIGH(reg, mask); // drive output high
183 delayMicroseconds(55);
186 DIRECT_WRITE_LOW(reg, mask);
187 DIRECT_MODE_OUTPUT(reg, mask); // drive output low
188 delayMicroseconds(65);
189 DIRECT_WRITE_HIGH(reg, mask); // drive output high
191 delayMicroseconds(5);
196 // Read a bit. Port and bit is used to cut lookup time and provide
197 // more certain timing.
199 uint8_t OneWire::read_bit(void)
201 IO_REG_TYPE mask=bitmask;
202 volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg;
206 DIRECT_MODE_OUTPUT(reg, mask);
207 DIRECT_WRITE_LOW(reg, mask);
208 delayMicroseconds(3);
209 DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise
210 delayMicroseconds(10);
211 r = DIRECT_READ(reg, mask);
213 delayMicroseconds(53);
218 // Write a byte. The writing code uses the active drivers to raise the
219 // pin high, if you need power after the write (e.g. DS18S20 in
220 // parasite power mode) then set 'power' to 1, otherwise the pin will
221 // go tri-state at the end of the write to avoid heating in a short or
224 void OneWire::write(uint8_t v, uint8_t power /* = 0 */) {
227 for (bitMask = 0x01; bitMask; bitMask <<= 1) {
228 OneWire::write_bit( (bitMask & v)?1:0);
232 DIRECT_MODE_INPUT(baseReg, bitmask);
233 DIRECT_WRITE_LOW(baseReg, bitmask);
238 void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) {
239 for (uint16_t i = 0 ; i < count ; i++)
243 DIRECT_MODE_INPUT(baseReg, bitmask);
244 DIRECT_WRITE_LOW(baseReg, bitmask);
252 uint8_t OneWire::read() {
256 for (bitMask = 0x01; bitMask; bitMask <<= 1) {
257 if ( OneWire::read_bit()) r |= bitMask;
262 void OneWire::read_bytes(uint8_t *buf, uint16_t count) {
263 for (uint16_t i = 0 ; i < count ; i++)
270 void OneWire::select(const uint8_t rom[8])
274 write(0x55); // Choose ROM
276 for (i = 0; i < 8; i++) write(rom[i]);
284 write(0xCC); // Skip ROM
287 void OneWire::depower()
290 DIRECT_MODE_INPUT(baseReg, bitmask);
297 // You need to use this function to start a search again from the beginning.
298 // You do not need to do it for the first search, though you could.
300 void OneWire::reset_search()
302 // reset the search state
304 LastDeviceFlag = FALSE;
305 LastFamilyDiscrepancy = 0;
306 for(int i = 7; ; i--) {
312 // Setup the search to find the device type 'family_code' on the next call
313 // to search(*newAddr) if it is present.
315 void OneWire::target_search(uint8_t family_code)
317 // set the search state to find SearchFamily type devices
318 ROM_NO[0] = family_code;
319 for (uint8_t i = 1; i < 8; i++)
321 LastDiscrepancy = 64;
322 LastFamilyDiscrepancy = 0;
323 LastDeviceFlag = FALSE;
327 // Perform a search. If this function returns a '1' then it has
328 // enumerated the next device and you may retrieve the ROM from the
329 // OneWire::address variable. If there are no devices, no further
330 // devices, or something horrible happens in the middle of the
331 // enumeration then a 0 is returned. If a new device is found then
332 // its address is copied to newAddr. Use OneWire::reset_search() to
335 // --- Replaced by the one from the Dallas Semiconductor web site ---
336 //--------------------------------------------------------------------------
337 // Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
339 // Return TRUE : device found, ROM number in ROM_NO buffer
340 // FALSE : device not found, end of search
342 uint8_t OneWire::search(uint8_t *newAddr)
344 uint8_t id_bit_number;
345 uint8_t last_zero, rom_byte_number, search_result;
346 uint8_t id_bit, cmp_id_bit;
348 unsigned char rom_byte_mask, search_direction;
350 // initialize for search
357 // if the last call was not the last one
365 LastDeviceFlag = FALSE;
366 LastFamilyDiscrepancy = 0;
370 // issue the search command
373 // loop to do the search
376 // read a bit and its complement
378 cmp_id_bit = read_bit();
380 // check for no devices on 1-wire
381 if ((id_bit == 1) && (cmp_id_bit == 1))
385 // all devices coupled have 0 or 1
386 if (id_bit != cmp_id_bit)
387 search_direction = id_bit; // bit write value for search
390 // if this discrepancy if before the Last Discrepancy
391 // on a previous next then pick the same as last time
392 if (id_bit_number < LastDiscrepancy)
393 search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
395 // if equal to last pick 1, if not then pick 0
396 search_direction = (id_bit_number == LastDiscrepancy);
398 // if 0 was picked then record its position in LastZero
399 if (search_direction == 0)
401 last_zero = id_bit_number;
403 // check for Last discrepancy in family
405 LastFamilyDiscrepancy = last_zero;
409 // set or clear the bit in the ROM byte rom_byte_number
410 // with mask rom_byte_mask
411 if (search_direction == 1)
412 ROM_NO[rom_byte_number] |= rom_byte_mask;
414 ROM_NO[rom_byte_number] &= ~rom_byte_mask;
416 // serial number search direction write bit
417 write_bit(search_direction);
419 // increment the byte counter id_bit_number
420 // and shift the mask rom_byte_mask
424 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
425 if (rom_byte_mask == 0)
432 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
434 // if the search was successful then
435 if (!(id_bit_number < 65))
437 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
438 LastDiscrepancy = last_zero;
440 // check for last device
441 if (LastDiscrepancy == 0)
442 LastDeviceFlag = TRUE;
444 search_result = TRUE;
448 // if no device found then reset counters so next 'search' will be like a first
449 if (!search_result || !ROM_NO[0])
452 LastDeviceFlag = FALSE;
453 LastFamilyDiscrepancy = 0;
454 search_result = FALSE;
456 for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];
457 return search_result;
463 // The 1-Wire CRC scheme is described in Maxim Application Note 27:
464 // "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
467 #if ONEWIRE_CRC8_TABLE
468 // This table comes from Dallas sample code where it is freely reusable,
469 // though Copyright (C) 2000 Dallas Semiconductor Corporation
470 static const uint8_t PROGMEM dscrc_table[] = {
471 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
472 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
473 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
474 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
475 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
476 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
477 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
478 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
479 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
480 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
481 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
482 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
483 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
484 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
485 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
486 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
489 // Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM
490 // and the registers. (note: this might better be done without to
491 // table, it would probably be smaller and certainly fast enough
492 // compared to all those delayMicrosecond() calls. But I got
493 // confused, so I use this table from the examples.)
495 uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
500 crc = pgm_read_byte(dscrc_table + (crc ^ *addr++));
506 // Compute a Dallas Semiconductor 8 bit CRC directly.
507 // this is much slower, but much smaller, than the lookup table.
509 uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
514 uint8_t inbyte = *addr++;
515 for (uint8_t i = 8; i; i--) {
516 uint8_t mix = (crc ^ inbyte) & 0x01;
518 if (mix) crc ^= 0x8C;
527 bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc)
529 crc = ~crc16(input, len, crc);
530 return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
533 uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc)
535 static const uint8_t oddparity[16] =
536 { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
538 for (uint16_t i = 0 ; i < len ; i++) {
539 // Even though we're just copying a byte from the input,
540 // we'll be doing 16-bit computation with it.
541 uint16_t cdata = input[i];
542 cdata = (cdata ^ crc) & 0xff;
545 if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4])