to github
[svn42.git] / rf433ctl / OneWire / OneWire.cpp
diff --git a/rf433ctl/OneWire/OneWire.cpp b/rf433ctl/OneWire/OneWire.cpp
deleted file mode 100644 (file)
index e717620..0000000
+++ /dev/null
@@ -1,502 +0,0 @@
-/*\r
-Copyright (c) 2007, Jim Studt\r
-\r
-\r
-Version 2.0: Modifications by Paul Stoffregen, January 2010:\r
-http://www.pjrc.com/teensy/td_libs_OneWire.html\r
-  Search fix from Robin James\r
-    http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27\r
-  Use direct optimized I/O in all cases\r
-  Disable interrupts during timing critical sections\r
-    (this solves many random communication errors)\r
-  Disable interrupts during read-modify-write I/O\r
-  Reduce RAM consumption by eliminating unnecessary\r
-    variables and trimming many to 8 bits\r
-  Optimize both crc8 - table version moved to flash\r
-\r
-Modified to work with larger numbers of devices - avoids loop.\r
-Tested in Arduino 11 alpha with 12 sensors.\r
-26 Sept 2008 -- Robin James\r
-http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295/27#27\r
-\r
-Updated to work with arduino-0008 and to include skip() as of\r
-2007/07/06. --RJL20\r
-\r
-Modified to calculate the 8-bit CRC directly, avoiding the need for\r
-the 256-byte lookup table to be loaded in RAM.  Tested in arduino-0010\r
--- Tom Pollard, Jan 23, 2008\r
-\r
-Permission is hereby granted, free of charge, to any person obtaining\r
-a copy of this software and associated documentation files (the\r
-"Software"), to deal in the Software without restriction, including\r
-without limitation the rights to use, copy, modify, merge, publish,\r
-distribute, sublicense, and/or sell copies of the Software, and to\r
-permit persons to whom the Software is furnished to do so, subject to\r
-the following conditions:\r
-\r
-The above copyright notice and this permission notice shall be\r
-included in all copies or substantial portions of the Software.\r
-\r
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
-\r
-Much of the code was inspired by Derek Yerger's code, though I don't\r
-think much of that remains.  In any event that was..\r
-    (copyleft) 2006 by Derek Yerger - Free to distribute freely.\r
-\r
-The CRC code was excerpted and inspired by the Dallas Semiconductor\r
-sample code bearing this copyright.\r
-//---------------------------------------------------------------------------\r
-// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.\r
-//\r
-// Permission is hereby granted, free of charge, to any person obtaining a\r
-// copy of this software and associated documentation files (the "Software"),\r
-// to deal in the Software without restriction, including without limitation\r
-// the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
-// and/or sell copies of the Software, and to permit persons to whom the\r
-// Software is furnished to do so, subject to the following conditions:\r
-//\r
-// The above copyright notice and this permission notice shall be included\r
-// in all copies or substantial portions of the Software.\r
-//\r
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
-// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
-// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES\r
-// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\r
-// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\r
-// OTHER DEALINGS IN THE SOFTWARE.\r
-//\r
-// Except as contained in this notice, the name of Dallas Semiconductor\r
-// shall not be used except as stated in the Dallas Semiconductor\r
-// Branding Policy.\r
-//--------------------------------------------------------------------------\r
-*/\r
-\r
-#include "OneWire.h"\r
-#include "pins_arduino.h"\r
-#include "Arduino.h"\r
-\r
-extern "C" {\r
-#include <avr/io.h>\r
-#include <avr/interrupt.h>\r
-#include <avr/pgmspace.h>\r
-}\r
-\r
-OneWire::OneWire(uint8_t pin)\r
-{\r
-       bitmask =  digitalPinToBitMask(pin);\r
-       baseReg = portInputRegister(digitalPinToPort(pin));\r
-#if ONEWIRE_SEARCH\r
-       reset_search();\r
-#endif\r
-}\r
-\r
-\r
-#define DIRECT_READ(base, mask)                (((*(base)) & (mask)) ? 1 : 0)\r
-#define DIRECT_MODE_INPUT(base, mask)  ((*(base+1)) &= ~(mask))\r
-#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) |= (mask))\r
-#define DIRECT_WRITE_LOW(base, mask)   ((*(base+2)) &= ~(mask))\r
-#define DIRECT_WRITE_HIGH(base, mask)  ((*(base+2)) |= (mask))\r
-\r
-\r
-// Perform the onewire reset function.  We will wait up to 250uS for\r
-// the bus to come high, if it doesn't then it is broken or shorted\r
-// and we return a 0;\r
-//\r
-// Returns 1 if a device asserted a presence pulse, 0 otherwise.\r
-//\r
-uint8_t OneWire::reset(void)\r
-{\r
-       uint8_t mask=bitmask;\r
-       volatile uint8_t *reg asm("r30") = baseReg;\r
-       uint8_t r;\r
-       uint8_t retries = 125;\r
-\r
-       cli();\r
-       DIRECT_MODE_INPUT(reg, mask);\r
-       sei();\r
-       // wait until the wire is high... just in case\r
-       do {\r
-               if (--retries == 0) return 0;\r
-               delayMicroseconds(2);\r
-       } while ( !DIRECT_READ(reg, mask));\r
-\r
-       cli();\r
-       DIRECT_WRITE_LOW(reg, mask);\r
-       DIRECT_MODE_OUTPUT(reg, mask);  // drive output low\r
-       sei();\r
-       delayMicroseconds(500);\r
-       cli();\r
-       DIRECT_MODE_INPUT(reg, mask);   // allow it to float\r
-       delayMicroseconds(80);\r
-       r = !DIRECT_READ(reg, mask);\r
-       sei();\r
-       delayMicroseconds(420);\r
-       return r;\r
-}\r
-\r
-//\r
-// Write a bit. Port and bit is used to cut lookup time and provide\r
-// more certain timing.\r
-//\r
-void OneWire::write_bit(uint8_t v)\r
-{\r
-       uint8_t mask=bitmask;\r
-       volatile uint8_t *reg asm("r30") = baseReg;\r
-\r
-       if (v & 1) {\r
-               cli();\r
-               DIRECT_WRITE_LOW(reg, mask);\r
-               DIRECT_MODE_OUTPUT(reg, mask);  // drive output low\r
-               delayMicroseconds(10);\r
-               DIRECT_WRITE_HIGH(reg, mask);   // drive output high\r
-               sei();\r
-               delayMicroseconds(55);\r
-       } else {\r
-               cli();\r
-               DIRECT_WRITE_LOW(reg, mask);\r
-               DIRECT_MODE_OUTPUT(reg, mask);  // drive output low\r
-               delayMicroseconds(65);\r
-               DIRECT_WRITE_HIGH(reg, mask);   // drive output high\r
-               sei();\r
-               delayMicroseconds(5);\r
-       }\r
-}\r
-\r
-//\r
-// Read a bit. Port and bit is used to cut lookup time and provide\r
-// more certain timing.\r
-//\r
-uint8_t OneWire::read_bit(void)\r
-{\r
-       uint8_t mask=bitmask;\r
-       volatile uint8_t *reg asm("r30") = baseReg;\r
-       uint8_t r;\r
-\r
-       cli();\r
-       DIRECT_MODE_OUTPUT(reg, mask);\r
-       DIRECT_WRITE_LOW(reg, mask);\r
-       delayMicroseconds(3);\r
-       DIRECT_MODE_INPUT(reg, mask);   // let pin float, pull up will raise\r
-       delayMicroseconds(9);\r
-       r = DIRECT_READ(reg, mask);\r
-       sei();\r
-       delayMicroseconds(53);\r
-       return r;\r
-}\r
-\r
-//\r
-// Write a byte. The writing code uses the active drivers to raise the\r
-// pin high, if you need power after the write (e.g. DS18S20 in\r
-// parasite power mode) then set 'power' to 1, otherwise the pin will\r
-// go tri-state at the end of the write to avoid heating in a short or\r
-// other mishap.\r
-//\r
-void OneWire::write(uint8_t v, uint8_t power /* = 0 */) {\r
-    uint8_t bitMask;\r
-\r
-    for (bitMask = 0x01; bitMask; bitMask <<= 1) {\r
-       OneWire::write_bit( (bitMask & v)?1:0);\r
-    }\r
-    if ( !power) {\r
-       cli();\r
-       DIRECT_MODE_INPUT(baseReg, bitmask);\r
-       DIRECT_WRITE_LOW(baseReg, bitmask);\r
-       sei();\r
-    }\r
-}\r
-\r
-//\r
-// Read a byte\r
-//\r
-uint8_t OneWire::read() {\r
-    uint8_t bitMask;\r
-    uint8_t r = 0;\r
-\r
-    for (bitMask = 0x01; bitMask; bitMask <<= 1) {\r
-       if ( OneWire::read_bit()) r |= bitMask;\r
-    }\r
-    return r;\r
-}\r
-\r
-//\r
-// Do a ROM select\r
-//\r
-void OneWire::select( uint8_t rom[8])\r
-{\r
-    int i;\r
-\r
-    write(0x55);           // Choose ROM\r
-\r
-    for( i = 0; i < 8; i++) write(rom[i]);\r
-}\r
-\r
-//\r
-// Do a ROM skip\r
-//\r
-void OneWire::skip()\r
-{\r
-    write(0xCC);           // Skip ROM\r
-}\r
-\r
-void OneWire::depower()\r
-{\r
-       cli();\r
-       DIRECT_MODE_INPUT(baseReg, bitmask);\r
-       sei();\r
-}\r
-\r
-#if ONEWIRE_SEARCH\r
-\r
-//\r
-// You need to use this function to start a search again from the beginning.\r
-// You do not need to do it for the first search, though you could.\r
-//\r
-void OneWire::reset_search()\r
-  {\r
-  // reset the search state\r
-  LastDiscrepancy = 0;\r
-  LastDeviceFlag = FALSE;\r
-  LastFamilyDiscrepancy = 0;\r
-  for(int i = 7; ; i--)\r
-    {\r
-    ROM_NO[i] = 0;\r
-    if ( i == 0) break;\r
-    }\r
-  }\r
-\r
-//\r
-// Perform a search. If this function returns a '1' then it has\r
-// enumerated the next device and you may retrieve the ROM from the\r
-// OneWire::address variable. If there are no devices, no further\r
-// devices, or something horrible happens in the middle of the\r
-// enumeration then a 0 is returned.  If a new device is found then\r
-// its address is copied to newAddr.  Use OneWire::reset_search() to\r
-// start over.\r
-//\r
-// --- Replaced by the one from the Dallas Semiconductor web site ---\r
-//--------------------------------------------------------------------------\r
-// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing\r
-// search state.\r
-// Return TRUE  : device found, ROM number in ROM_NO buffer\r
-//        FALSE : device not found, end of search\r
-//\r
-uint8_t OneWire::search(uint8_t *newAddr)\r
-{\r
-   uint8_t id_bit_number;\r
-   uint8_t last_zero, rom_byte_number, search_result;\r
-   uint8_t id_bit, cmp_id_bit;\r
-\r
-   unsigned char rom_byte_mask, search_direction;\r
-\r
-   // initialize for search\r
-   id_bit_number = 1;\r
-   last_zero = 0;\r
-   rom_byte_number = 0;\r
-   rom_byte_mask = 1;\r
-   search_result = 0;\r
-\r
-   // if the last call was not the last one\r
-   if (!LastDeviceFlag)\r
-   {\r
-      // 1-Wire reset\r
-      if (!reset())\r
-      {\r
-         // reset the search\r
-         LastDiscrepancy = 0;\r
-         LastDeviceFlag = FALSE;\r
-         LastFamilyDiscrepancy = 0;\r
-         return FALSE;\r
-      }\r
-\r
-      // issue the search command\r
-      write(0xF0);\r
-\r
-      // loop to do the search\r
-      do\r
-      {\r
-         // read a bit and its complement\r
-         id_bit = read_bit();\r
-         cmp_id_bit = read_bit();\r
-\r
-         // check for no devices on 1-wire\r
-         if ((id_bit == 1) && (cmp_id_bit == 1))\r
-            break;\r
-         else\r
-         {\r
-            // all devices coupled have 0 or 1\r
-            if (id_bit != cmp_id_bit)\r
-               search_direction = id_bit;  // bit write value for search\r
-            else\r
-            {\r
-               // if this discrepancy if before the Last Discrepancy\r
-               // on a previous next then pick the same as last time\r
-               if (id_bit_number < LastDiscrepancy)\r
-                  search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);\r
-               else\r
-                  // if equal to last pick 1, if not then pick 0\r
-                  search_direction = (id_bit_number == LastDiscrepancy);\r
-\r
-               // if 0 was picked then record its position in LastZero\r
-               if (search_direction == 0)\r
-               {\r
-                  last_zero = id_bit_number;\r
-\r
-                  // check for Last discrepancy in family\r
-                  if (last_zero < 9)\r
-                     LastFamilyDiscrepancy = last_zero;\r
-               }\r
-            }\r
-\r
-            // set or clear the bit in the ROM byte rom_byte_number\r
-            // with mask rom_byte_mask\r
-            if (search_direction == 1)\r
-              ROM_NO[rom_byte_number] |= rom_byte_mask;\r
-            else\r
-              ROM_NO[rom_byte_number] &= ~rom_byte_mask;\r
-\r
-            // serial number search direction write bit\r
-            write_bit(search_direction);\r
-\r
-            // increment the byte counter id_bit_number\r
-            // and shift the mask rom_byte_mask\r
-            id_bit_number++;\r
-            rom_byte_mask <<= 1;\r
-\r
-            // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask\r
-            if (rom_byte_mask == 0)\r
-            {\r
-                rom_byte_number++;\r
-                rom_byte_mask = 1;\r
-            }\r
-         }\r
-      }\r
-      while(rom_byte_number < 8);  // loop until through all ROM bytes 0-7\r
-\r
-      // if the search was successful then\r
-      if (!(id_bit_number < 65))\r
-      {\r
-         // search successful so set LastDiscrepancy,LastDeviceFlag,search_result\r
-         LastDiscrepancy = last_zero;\r
-\r
-         // check for last device\r
-         if (LastDiscrepancy == 0)\r
-            LastDeviceFlag = TRUE;\r
-\r
-         search_result = TRUE;\r
-      }\r
-   }\r
-\r
-   // if no device found then reset counters so next 'search' will be like a first\r
-   if (!search_result || !ROM_NO[0])\r
-   {\r
-      LastDiscrepancy = 0;\r
-      LastDeviceFlag = FALSE;\r
-      LastFamilyDiscrepancy = 0;\r
-      search_result = FALSE;\r
-   }\r
-   for (int i = 0; i < 8; i++) newAddr[i] = ROM_NO[i];\r
-   return search_result;\r
-  }\r
-\r
-#endif\r
-\r
-#if ONEWIRE_CRC\r
-// The 1-Wire CRC scheme is described in Maxim Application Note 27:\r
-// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"\r
-//\r
-\r
-#if ONEWIRE_CRC8_TABLE\r
-// This table comes from Dallas sample code where it is freely reusable,\r
-// though Copyright (C) 2000 Dallas Semiconductor Corporation\r
-static const uint8_t PROGMEM dscrc_table[] = {\r
-      0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,\r
-    157,195, 33,127,252,162, 64, 30, 95,  1,227,189, 62, 96,130,220,\r
-     35,125,159,193, 66, 28,254,160,225,191, 93,  3,128,222, 60, 98,\r
-    190,224,  2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,\r
-     70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89,  7,\r
-    219,133,103, 57,186,228,  6, 88, 25, 71,165,251,120, 38,196,154,\r
-    101, 59,217,135,  4, 90,184,230,167,249, 27, 69,198,152,122, 36,\r
-    248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91,  5,231,185,\r
-    140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,\r
-     17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,\r
-    175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,\r
-     50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,\r
-    202,148,118, 40,171,245, 23, 73,  8, 86,180,234,105, 55,213,139,\r
-     87,  9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,\r
-    233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,\r
-    116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};\r
-\r
-//\r
-// Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM\r
-// and the registers.  (note: this might better be done without to\r
-// table, it would probably be smaller and certainly fast enough\r
-// compared to all those delayMicrosecond() calls.  But I got\r
-// confused, so I use this table from the examples.)\r
-//\r
-uint8_t OneWire::crc8( uint8_t *addr, uint8_t len)\r
-{\r
-       uint8_t crc = 0;\r
-\r
-       while (len--) {\r
-               crc = pgm_read_byte(dscrc_table + (crc ^ *addr++));\r
-       }\r
-       return crc;\r
-}\r
-#else\r
-//\r
-// Compute a Dallas Semiconductor 8 bit CRC directly.\r
-//\r
-uint8_t OneWire::crc8( uint8_t *addr, uint8_t len)\r
-{\r
-       uint8_t crc = 0;\r
-       \r
-       while (len--) {\r
-               uint8_t inbyte = *addr++;\r
-               for (uint8_t i = 8; i; i--) {\r
-                       uint8_t mix = (crc ^ inbyte) & 0x01;\r
-                       crc >>= 1;\r
-                       if (mix) crc ^= 0x8C;\r
-                       inbyte >>= 1;\r
-               }\r
-       }\r
-       return crc;\r
-}\r
-#endif\r
-\r
-#if ONEWIRE_CRC16\r
-static short oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };\r
-\r
-//\r
-// Compute a Dallas Semiconductor 16 bit CRC. I have never seen one of\r
-// these, but here it is.\r
-//\r
-unsigned short OneWire::crc16(unsigned short *data, unsigned short len)\r
-{\r
-    unsigned short i;\r
-    unsigned short crc = 0;\r
-\r
-    for ( i = 0; i < len; i++) {\r
-       unsigned short cdata = data[len];\r
-\r
-       cdata = (cdata ^ (crc & 0xff)) & 0xff;\r
-       crc >>= 8;\r
-\r
-       if (oddparity[cdata & 0xf] ^ oddparity[cdata >> 4]) crc ^= 0xc001;\r
-\r
-       cdata <<= 6;\r
-       crc ^= cdata;\r
-       cdata <<= 1;\r
-       crc ^= cdata;\r
-    }\r
-    return crc;\r
-}\r
-#endif\r
-\r
-#endif\r