/* OWSlave v1.0 by Alexander Gordeyev It is based on Jim's Studt OneWire library v2.0 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Much of the code was inspired by Derek Yerger's code, though I don't think much of that remains. In any event that was.. (copyleft) 2006 by Derek Yerger - Free to distribute freely. The CRC code was excerpted and inspired by the Dallas Semiconductor sample code bearing this copyright. //--------------------------------------------------------------------------- // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // // Except as contained in this notice, the name of Dallas Semiconductor // shall not be used except as stated in the Dallas Semiconductor // Branding Policy. //-------------------------------------------------------------------------- */ #include "OWSlave.h" #include "pins_arduino.h" extern "C" { // #include "WConstants.h" #include #include #include } #define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) #define DIRECT_MODE_INPUT(base, mask) ((*(base+1)) &= ~(mask)) #define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) |= (mask)) #define DIRECT_WRITE_LOW(base, mask) ((*(base+2)) &= ~(mask)) #define DIRECT_WRITE_HIGH(base, mask) ((*(base+2)) |= (mask)) #define TIMESLOT_WAIT_RETRY_COUNT microsecondsToClockCycles(120) / 10L OWSlave::OWSlave(uint8_t pin) { pin_bitmask = digitalPinToBitMask(pin); baseReg = portInputRegister(digitalPinToPort(pin)); } void OWSlave::setRom(unsigned char rom[8]) { for (int i=0; i<7; i++) this->rom[i] = rom[i]; this->rom[7] = crc8(this->rom, 7); } void OWSlave::setRomnc(unsigned char rom[8]) { for (int i=0; i<8; i++) this->rom[i] = rom[i]; } bool OWSlave::waitForRequest(bool ignore_errors) { errno = ONEWIRE_NO_ERROR; for (;;) { if (!waitReset(0) ) continue; if (!presence() ) continue; if (recvAndProcessCmd() ) return TRUE; else if ((errno == ONEWIRE_NO_ERROR) || ignore_errors) continue; else return FALSE; } } bool OWSlave::recvAndProcessCmd() { char addr[8]; for (;;) { switch (recv() ) { case 0xF0: // SEARCH ROM search(); return FALSE; case 0x33: // READ ROM sendData(rom, 8); if (errno != ONEWIRE_NO_ERROR) return FALSE; break; case 0x55: // MATCH ROM recvData(addr, 8); if (errno != ONEWIRE_NO_ERROR) return FALSE; for (int i=0; i<8; i++) if (rom[i] != addr[i]) return FALSE; return TRUE; case 0xCC: // SKIP ROM return TRUE; default: // Unknown command if (errno == ONEWIRE_NO_ERROR) return FALSE; else return FALSE; } } } bool OWSlave::search() { uint8_t bitmask; uint8_t bit_send, bit_recv; for (int i=0; i<8; i++) { for (bitmask = 0x01; bitmask; bitmask <<= 1) { bit_send = (bitmask & rom[i])?1:0; sendBit(bit_send); sendBit(!bit_send); bit_recv = recvBit(); if (errno != ONEWIRE_NO_ERROR) return FALSE; if (bit_recv != bit_send) return FALSE; } } return TRUE; } bool OWSlave::waitReset(uint16_t timeout_ms) { uint8_t mask = pin_bitmask; volatile uint8_t *reg asm("r30") = baseReg; unsigned long time_stamp; errno = ONEWIRE_NO_ERROR; cli(); DIRECT_MODE_INPUT(reg, mask); sei(); if (timeout_ms != 0) { time_stamp = micros() + timeout_ms*1000; while (DIRECT_READ(reg, mask)) { if (micros() > time_stamp) { errno = ONEWIRE_WAIT_RESET_TIMEOUT; return FALSE; } } } else while (DIRECT_READ(reg, mask)) {}; time_stamp = micros() + 540; while (DIRECT_READ(reg, mask) == 0) { if (micros() > time_stamp) { errno = ONEWIRE_VERY_LONG_RESET; return FALSE; } } if ((time_stamp - micros()) > 70) { errno = ONEWIRE_VERY_SHORT_RESET; return FALSE; } delayMicroseconds(30); return TRUE; } bool OWSlave::waitReset() { return waitReset(1000); } bool OWSlave::presence(uint8_t delta) { uint8_t mask = pin_bitmask; volatile uint8_t *reg asm("r30") = baseReg; errno = ONEWIRE_NO_ERROR; cli(); DIRECT_WRITE_LOW(reg, mask); DIRECT_MODE_OUTPUT(reg, mask); // drive output low sei(); delayMicroseconds(120); cli(); DIRECT_MODE_INPUT(reg, mask); // allow it to float sei(); delayMicroseconds(300 - delta); if ( !DIRECT_READ(reg, mask)) { errno = ONEWIRE_PRESENCE_LOW_ON_LINE; return FALSE; } else return TRUE; } bool OWSlave::presence() { return presence(25); } uint8_t OWSlave::sendData(char buf[], uint8_t len) { uint8_t bytes_sended = 0; for (int i=0; i>= 1; if (mix) crc ^= 0x8C; inbyte >>= 1; } } return crc; } #endif #endif