diff --git a/OneWire.cpp b/DOneWire.cpp similarity index 74% rename from OneWire.cpp rename to DOneWire.cpp index eed2af3..c37a2d2 100644 --- a/OneWire.cpp +++ b/DOneWire.cpp @@ -1,8 +1,9 @@ -#include "OneWire.h" +#include "DOneWire.h" #include +#include -// Constructor with no parameters for compatability with OneWire lib -OneWire::OneWire() +// Constructor with no parameters for compatability with DOneWire lib +DOneWire::DOneWire() { // Address is determined by two pins on the DS2482 AD1/AD0 // Pass 0b00, 0b01, 0b10 or 0b11 @@ -11,7 +12,7 @@ OneWire::OneWire() Wire.begin(); } -OneWire::OneWire(uint8_t address) +DOneWire::DOneWire(uint8_t address) { // Address is determined by two pins on the DS2482 AD1/AD0 // Pass 0b00, 0b01, 0b10 or 0b11 @@ -20,33 +21,55 @@ OneWire::OneWire(uint8_t address) Wire.begin(); } -uint8_t OneWire::getAddress() +DOneWire::DOneWire(uint8_t sdaPin, uint8_t sclPin) + { + // sdaPin and sclPin can override the default SCL and SDA + // pins on some hardware + // Address is determined by two pins on the DS2482 AD1/AD0 + // Pass 0b00, 0b01, 0b10 or 0b11 + mAddress = 0x18; + mError = 0; + Wire.begin(sdaPin, sclPin); + } + + DOneWire::DOneWire(uint8_t sdaPin, uint8_t sclPin, uint8_t address) + { + // sdaPin and sclPin can override the default SCL and SDA + // pins on some hardware + // Address is determined by two pins on the DS2482 AD1/AD0 + // Pass 0b00, 0b01, 0b10 or 0b11 + mAddress = 0x18 | address; + mError = 0; + Wire.begin(sdaPin, sclPin); + } + +uint8_t DOneWire::getAddress() { return mAddress; } -uint8_t OneWire::getError() +uint8_t DOneWire::getError() { return mError; } // Helper functions to make dealing with I2C side easier -void OneWire::begin() +void DOneWire::begin() { Wire.beginTransmission(mAddress); } -uint8_t OneWire::end() +uint8_t DOneWire::end() { return Wire.endTransmission(); } -void OneWire::writeByte(uint8_t data) +void DOneWire::writeByte(uint8_t data) { Wire.write(data); } -uint8_t OneWire::readByte() +uint8_t DOneWire::readByte() { Wire.requestFrom(mAddress,1u); return Wire.read(); @@ -54,14 +77,14 @@ uint8_t OneWire::readByte() // Simply starts and ends an Wire transmission // If no devices are present, this returns false -uint8_t OneWire::checkPresence() +uint8_t DOneWire::checkPresence() { begin(); return !end() ? true : false; } // Performs a global reset of device state machine logic. Terminates any ongoing 1-Wire communication. -void OneWire::deviceReset() +void DOneWire::deviceReset() { begin(); write(DS2482_COMMAND_RESET); @@ -69,7 +92,7 @@ void OneWire::deviceReset() } // Sets the read pointer to the specified register. Overwrites the read pointer position of any 1-Wire communication command in progress. -void OneWire::setReadPointer(uint8_t readPointer) +void DOneWire::setReadPointer(uint8_t readPointer) { begin(); writeByte(DS2482_COMMAND_SRP); @@ -78,38 +101,38 @@ void OneWire::setReadPointer(uint8_t readPointer) } // Read the status register -uint8_t OneWire::readStatus() +uint8_t DOneWire::readStatus() { setReadPointer(DS2482_POINTER_STATUS); return readByte(); } // Read the data register -uint8_t OneWire::readData() +uint8_t DOneWire::readData() { setReadPointer(DS2482_POINTER_DATA); return readByte(); } // Read the config register -uint8_t OneWire::readConfig() +uint8_t DOneWire::readConfig() { setReadPointer(DS2482_POINTER_CONFIG); return readByte(); } -void OneWire::setStrongPullup() +void DOneWire::setStrongPullup() { writeConfig(readConfig() | DS2482_CONFIG_SPU); } -void OneWire::clearStrongPullup() +void DOneWire::clearStrongPullup() { writeConfig(readConfig() & !DS2482_CONFIG_SPU); } // Churn until the busy bit in the status register is clear -uint8_t OneWire::waitOnBusy() +uint8_t DOneWire::waitOnBusy() { uint8_t status; @@ -130,7 +153,7 @@ uint8_t OneWire::waitOnBusy() } // Write to the config register -void OneWire::writeConfig(uint8_t config) +void DOneWire::writeConfig(uint8_t config) { waitOnBusy(); begin(); @@ -147,7 +170,7 @@ void OneWire::writeConfig(uint8_t config) // Generates a 1-Wire reset/presence-detect cycle (Figure 4) at the 1-Wire line. The state // of the 1-Wire line is sampled at tSI and tMSP and the result is reported to the host // processor through the Status Register, bits PPD and SD. -uint8_t OneWire::wireReset() +uint8_t DOneWire::wireReset() { waitOnBusy(); // Datasheet warns that reset with SPU set can exceed max ratings @@ -170,7 +193,7 @@ uint8_t OneWire::wireReset() } // Writes a single data byte to the 1-Wire line. -void OneWire::wireWriteByte(uint8_t data, uint8_t power) +void DOneWire::wireWriteByte(uint8_t data, uint8_t power) { waitOnBusy(); if (power) @@ -182,7 +205,7 @@ void OneWire::wireWriteByte(uint8_t data, uint8_t power) } // Generates eight read-data time slots on the 1-Wire line and stores result in the Read Data Register. -uint8_t OneWire::wireReadByte() +uint8_t DOneWire::wireReadByte() { waitOnBusy(); begin(); @@ -196,7 +219,7 @@ uint8_t OneWire::wireReadByte() // (see Table 2). A V value of 0b generates a write-zero time slot (Figure 5); a V value of 1b generates a // write-one time slot, which also functions as a read-data time slot (Figure 6). In either case, the logic // level at the 1-Wire line is tested at tMSR and SBR is updated. -void OneWire::wireWriteBit(uint8_t data, uint8_t power) +void DOneWire::wireWriteBit(uint8_t data, uint8_t power) { waitOnBusy(); if (power) @@ -208,7 +231,7 @@ void OneWire::wireWriteBit(uint8_t data, uint8_t power) } // As wireWriteBit -uint8_t OneWire::wireReadBit() +uint8_t DOneWire::wireReadBit() { wireWriteBit(1); uint8_t status = waitOnBusy(); @@ -216,12 +239,12 @@ uint8_t OneWire::wireReadBit() } // 1-Wire skip -void OneWire::wireSkip() +void DOneWire::wireSkip() { wireWriteByte(WIRE_COMMAND_SKIP); } -void OneWire::wireSelect(const uint8_t rom[8]) +void DOneWire::wireSelect(const uint8_t rom[8]) { wireWriteByte(WIRE_COMMAND_SELECT); for (int i=0;i<8;i++) @@ -229,7 +252,7 @@ void OneWire::wireSelect(const uint8_t rom[8]) } // 1-Wire reset seatch algorithm -void OneWire::wireResetSearch() +void DOneWire::wireResetSearch() { searchLastDiscrepancy = 0; searchLastDeviceFlag = 0; @@ -241,22 +264,8 @@ void OneWire::wireResetSearch() } -// Set the channel on the DS2482-800 -uint8_t OneWire::setChannel(uint8_t ch){ - uint8_t w[] = {0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87}; - uint8_t r[] = {0xb8, 0xb1, 0xaa, 0xa3, 0x9c, 0x95, 0x8e, 0x87}; - waitOnBusy(); - begin(); - writeByte(0xc3); - writeByte(w[ch]); - end(); - waitOnBusy(); - return readByte() == r[ch]; -} - - // Perform a search of the 1-Wire bus -uint8_t OneWire::wireSearch(uint8_t *address) +uint8_t DOneWire::wireSearch(uint8_t *address) { uint8_t direction; uint8_t last_zero=0; @@ -351,7 +360,7 @@ static const uint8_t PROGMEM dscrc_table[] = { // compared to all those delayMicrosecond() calls. But I got // confused, so I use this table from the examples.) // -uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +uint8_t DOneWire::crc8(const uint8_t *addr, uint8_t len) { uint8_t crc = 0; @@ -365,7 +374,7 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) // Compute a Dallas Semiconductor 8 bit CRC directly. // this is much slower, but much smaller, than the lookup table. // -uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) +uint8_t DOneWire::crc8(const uint8_t *addr, uint8_t len) { uint8_t crc = 0; @@ -383,17 +392,17 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) #endif // **************************************** -// These are here to mirror the functions in the original OneWire +// These are here to mirror the functions in the original DOneWire // **************************************** // This is a lazy way of getting compatibility with DallasTemperature // Not all functions are implemented, only those used in DallasTemeperature -void OneWire::reset_search() +void DOneWire::reset_search() { wireResetSearch(); } -uint8_t OneWire::search(uint8_t *newAddr) +uint8_t DOneWire::search(uint8_t *newAddr) { return wireSearch(newAddr); } @@ -401,44 +410,44 @@ uint8_t OneWire::search(uint8_t *newAddr) // Perform a 1-Wire reset cycle. Returns 1 if a device responds // with a presence pulse. Returns 0 if there is no device or the // bus is shorted or otherwise held low for more than 250uS -uint8_t OneWire::reset(void) +uint8_t DOneWire::reset(void) { return wireReset(); } // Issue a 1-Wire rom select command, you do the reset first. -void OneWire::select(const uint8_t rom[8]) +void DOneWire::select(const uint8_t rom[8]) { wireSelect(rom); } // Issue a 1-Wire rom skip command, to address all on bus. -void OneWire::skip(void) +void DOneWire::skip(void) { wireSkip(); } // Write a byte. // Ignore the power bit -void OneWire::write(uint8_t v, uint8_t power) +void DOneWire::write(uint8_t v, uint8_t power) { wireWriteByte(v, power); } // Read a byte. -uint8_t OneWire::read(void) +uint8_t DOneWire::read(void) { return wireReadByte(); } // Read a bit. -uint8_t OneWire::read_bit(void) +uint8_t DOneWire::read_bit(void) { return wireReadBit(); } // Write a bit. -void OneWire::write_bit(uint8_t v) +void DOneWire::write_bit(uint8_t v) { wireWriteBit(v); } @@ -447,9 +456,55 @@ void OneWire::write_bit(uint8_t v) // End mirrored functions // **************************************** +bool DOneWire::selectChannel(uint8_t channel) +{ + uint8_t ch, ch_read; + switch (channel) + { + case 0: + default: + ch = 0xf0; + ch_read = 0xb8; + break; + case 1: + ch = 0xe1; + ch_read = 0xb1; + break; + case 2: + ch = 0xd2; + ch_read = 0xaa; + break; + case 3: + ch = 0xc3; + ch_read = 0xa3; + break; + case 4: + ch = 0xb4; + ch_read = 0x9c; + break; + case 5: + ch = 0xa5; + ch_read = 0x95; + break; + case 6: + ch = 0x96; + ch_read = 0x8e; + break; + case 7: + ch = 0x87; + ch_read = 0x87; + break; + }; + waitOnBusy(); + begin(); + Wire.write(0xc3); + Wire.write(ch); + end(); + waitOnBusy(); + uint8_t check = readByte(); - - + return check == ch_read; +} \ No newline at end of file diff --git a/OneWire.h b/DOneWire.h similarity index 82% rename from OneWire.h rename to DOneWire.h index 0892f7b..ea7654b 100644 --- a/OneWire.h +++ b/DOneWire.h @@ -1,13 +1,12 @@ -#ifndef __ONEWIRE_H__ -#define __ONEWIRE_H__ +#ifndef __DONEWIRE_H__ +#define __DONEWIRE_H__ -#include #include - +#include // Chose between a table based CRC (flash expensive, fast) // or a computed CRC (smaller, slow) -#define ONEWIRE_CRC8_TABLE 1 +#define DONEWIRE_CRC8_TABLE 1 #define DS2482_COMMAND_RESET 0xF0 // Device reset @@ -43,11 +42,13 @@ #define DS2482_ERROR_SHORT (1<<1) #define DS2482_ERROR_CONFIG (1<<2) -class OneWire +class DOneWire : public OneWire { public: - OneWire(); - OneWire(uint8_t address); + DOneWire(); + DOneWire(uint8_t address); + DOneWire(uint8_t sdaPin, uint8_t sclPin); + DOneWire(uint8_t sdaPin, uint8_t sclPin, uint8_t address); uint8_t getAddress(); uint8_t getError(); @@ -61,7 +62,6 @@ class OneWire uint8_t readConfig(); void writeConfig(uint8_t config); void setStrongPullup(); - uint8_t setChannel(uint8_t ch); void clearStrongPullup(); uint8_t wireReset(); void wireWriteByte(uint8_t data, uint8_t power = 0); @@ -73,7 +73,7 @@ class OneWire void wireResetSearch(); uint8_t wireSearch(uint8_t *address); - // emulation of original OneWire library + // emulation of original DOneWire library void reset_search(); uint8_t search(uint8_t *newAddr); static uint8_t crc8(const uint8_t *addr, uint8_t len); @@ -85,6 +85,10 @@ class OneWire uint8_t read_bit(void); void write_bit(uint8_t v); + // Brought selectChannel from Paeaetech's DS2482 lib + // https://github.com/paeaetech/paeae/tree/master/Libraries/ds2482 + bool selectChannel(uint8_t channel); + private: void begin(); uint8_t end(); @@ -99,4 +103,4 @@ class OneWire uint8_t searchLastDeviceFlag; }; -#endif +#endif \ No newline at end of file