commit 8be66217ddcf6235f43bb5745a30ea8fdae93b3e Author: Youen Toupin Date: Sat Apr 11 12:49:46 2015 +0200 Empty Arduino sketch with OWSlave library downloaded from http://forum.arduino.cc/index.php?topic=65706.msg1756727#msg1756727 diff --git a/OWSlave.cpp b/OWSlave.cpp new file mode 100644 index 0000000..c498aa5 --- /dev/null +++ b/OWSlave.cpp @@ -0,0 +1,382 @@ +/* +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 diff --git a/OWSlave.h b/OWSlave.h new file mode 100644 index 0000000..f054858 --- /dev/null +++ b/OWSlave.h @@ -0,0 +1,64 @@ +#ifndef iButton_h +#define iButton_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include + +// You can exclude CRC checks altogether by defining this to 0 +#ifndef OWSlave_CRC +#define OWSlave_CRC 1 +#endif + +// Select the table-lookup method of computing the 8-bit CRC +// by setting this to 1. The lookup table no longer consumes +// limited RAM, but enlarges total code size by about 250 bytes +#ifndef OWSlave_CRC8_TABLE +#define OWSlave_CRC8_TABLE 0 +#endif + +#define FALSE 0 +#define TRUE 1 + +#define ONEWIRE_NO_ERROR 0 +#define ONEWIRE_READ_TIMESLOT_TIMEOUT 1 +#define ONEWIRE_WRITE_TIMESLOT_TIMEOUT 2 +#define ONEWIRE_WAIT_RESET_TIMEOUT 3 +#define ONEWIRE_VERY_LONG_RESET 4 +#define ONEWIRE_VERY_SHORT_RESET 5 +#define ONEWIRE_PRESENCE_LOW_ON_LINE 6 + +class OWSlave { + private: + bool waitTimeSlot(); + uint8_t pin_bitmask; + volatile uint8_t *baseReg; + char rom[8]; + public: + bool recvAndProcessCmd(); + OWSlave(uint8_t pin); + void setRom(unsigned char rom[8]); + void setRomnc(unsigned char rom[8]); + bool waitForRequest(bool ignore_errors); + bool waitReset(uint16_t timeout_ms); + bool waitReset(); + bool presence(uint8_t delta); + bool presence(); + bool search(); + uint8_t sendData(char buf[], uint8_t data_len); + uint8_t recvData(char buf[], uint8_t data_len); + void send(uint8_t v); + uint8_t recv(void); + void sendBit(uint8_t v); + uint8_t recvBit(void); +#if OWSlave_CRC + static uint8_t crc8(char addr[], uint8_t len); +#endif + uint8_t errno; +}; + +#endif diff --git a/OneWireIO.ino b/OneWireIO.ino new file mode 100644 index 0000000..4b64f3d --- /dev/null +++ b/OneWireIO.ino @@ -0,0 +1,7 @@ +void setup() { + +} + +void loop() { + +}