From 98135ac8ab481233d200af4e3a10e02c0e1c27ed Mon Sep 17 00:00:00 2001 From: Siarhei Hanchuk Date: Fri, 30 Mar 2018 09:24:20 +0300 Subject: [PATCH 1/9] Copy DS2431 to DS2430 --- src/DS2430.cpp | 277 +++++++++++++++++++++++++++++++++++++++++++++++++ src/DS2430.h | 60 +++++++++++ 2 files changed, 337 insertions(+) create mode 100644 src/DS2430.cpp create mode 100644 src/DS2430.h diff --git a/src/DS2430.cpp b/src/DS2430.cpp new file mode 100644 index 0000000..8dbe02b --- /dev/null +++ b/src/DS2430.cpp @@ -0,0 +1,277 @@ +#include "DS2431.h" + +DS2431::DS2431(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, uint8_t ID6, uint8_t ID7) : OneWireItem(ID1, ID2, ID3, ID4, ID5, ID6, ID7) +{ + static_assert(sizeof(scratchpad) < 256, "Implementation does not cover the whole address-space"); + static_assert(sizeof(memory) < 256, "Implementation does not cover the whole address-space"); + + clearMemory(); + clearScratchpad(); + + page_protection = 0; + page_eprom_mode = 0; + + updatePageStatus(); +} + +void DS2431::duty(OneWireHub * const hub) +{ + constexpr uint8_t ALTERNATING_10 { 0xAA }; + static uint16_t reg_TA { 0 }; // contains TA1, TA2 + static uint8_t reg_ES { 0 }; // E/S register + uint16_t crc { 0 }; + + uint8_t page_offset { 0 }; + uint8_t cmd, data; + if (hub->recv(&cmd,1,crc)) return; + + switch (cmd) + { + case 0x0F: // WRITE SCRATCHPAD COMMAND + + if (hub->recv(reinterpret_cast(®_TA),2,crc)) return; + reg_ES = uint8_t(reg_TA) & SCRATCHPAD_MASK; + page_offset = reg_ES; + + // receive up to 8 bytes of data + for (; reg_ES < SCRATCHPAD_SIZE; ++reg_ES) + { + if (hub->recv(&scratchpad[reg_ES], 1, crc)) + { + if (hub->getError() == Error::AWAIT_TIMESLOT_TIMEOUT_HIGH) reg_ES |= REG_ES_PF_MASK; + break; + } + } + reg_ES--; + reg_ES &= SCRATCHPAD_MASK; + + if (hub->getError() == Error::NO_ERROR) // try to send crc if wanted + { + crc = ~crc; // normally crc16 is sent ~inverted + hub->send(reinterpret_cast(&crc), 2); + } + + if (reg_TA < (4*PAGE_SIZE)) // check if page is protected or in eprom-mode + { + const uint8_t position = uint8_t(reg_TA) & ~SCRATCHPAD_MASK; + if (getPageProtection(reinterpret_cast(®_TA)[0])) // protected: load memory-segment to scratchpad + { + for (uint8_t i = 0; i < SCRATCHPAD_SIZE; ++i) scratchpad[i] = memory[position + i]; + } + else if (getPageEpromMode(reinterpret_cast(®_TA)[0])) // eprom: logical AND of memory and data + { + for (uint8_t i = page_offset; i <= reg_ES; ++i) scratchpad[i] &= memory[position + i]; + } + } + break; + + case 0xAA: // READ SCRATCHPAD COMMAND + + if (hub->send(reinterpret_cast(®_TA),2,crc)) return; + if (hub->send(®_ES,1,crc)) return; + + { // send Scratchpad content + const uint8_t start = uint8_t(reg_TA) & SCRATCHPAD_MASK; + const uint8_t length = (reg_ES & SCRATCHPAD_MASK)+ uint8_t(1) - start; // difference to ds2433 + if (hub->send(&scratchpad[start],length,crc)) return; + } + + crc = ~crc; + if (hub->send(reinterpret_cast(&crc),2)) return; + break; // send 1s when read is complete, is passive, so do nothing + + case 0x55: // COPY SCRATCHPAD COMMAND + + if (hub->recv(&data)) return; + if (data != reinterpret_cast(®_TA)[0]) break; + if (hub->recv(&data)) return; + if (data != reinterpret_cast(®_TA)[1]) break; + if (hub->recv(&data)) return; + if (data != reg_ES) return; // Auth code must match + + if (getPageProtection(uint8_t(reg_TA))) break; // stop if page is protected (WriteMemory also checks this) + if ((reg_ES & REG_ES_PF_MASK) != 0) break; // stop if error occured earlier + + reg_ES |= REG_ES_AA_MASK; // compare was successful + + reg_TA &= ~uint16_t(SCRATCHPAD_MASK); + + // Write Scratchpad to memory, writing takes about 10ms + writeMemory(scratchpad, SCRATCHPAD_SIZE, reinterpret_cast(®_TA)[0]); // checks if copy protected + + noInterrupts(); + + do + { + hub->clearError(); + + hub->sendBit(true); // send passive 1s + + } + while (hub->getError() == Error::AWAIT_TIMESLOT_TIMEOUT_HIGH); // wait for timeslots + + interrupts(); + + while (!hub->send(&ALTERNATING_10)); // alternating 1 & 0 after copy is complete + break; + + case 0xF0: // READ MEMORY COMMAND + + if (hub->recv(reinterpret_cast(®_TA),2)) return; + if (reg_TA >= MEM_SIZE) return; + if (hub->send(&memory[reg_TA],MEM_SIZE - uint8_t(reg_TA),crc)) return; + break; // send 1s when read is complete, is passive, so do nothing here + + default: + + hub->raiseSlaveError(cmd); + } +} + +void DS2431::clearMemory(void) +{ + memset(memory, static_cast(0x00), sizeof(memory)); +} + +void DS2431::clearScratchpad(void) +{ + memset(scratchpad, static_cast(0x00), SCRATCHPAD_SIZE); +} + +bool DS2431::writeMemory(const uint8_t* const source, const uint8_t length, const uint8_t position) +{ + for (uint8_t i = 0; i < length; ++i) { + if ((position + i) >= sizeof(memory)) break; + if (getPageProtection(position + i)) continue; + memory[position + i] = source[i]; + } + + if ((position+length) > 127) updatePageStatus(); + + return true; +} + +bool DS2431::readMemory(uint8_t* const destination, const uint16_t length, const uint16_t position) const +{ + if (position >= MEM_SIZE) return false; + const uint16_t _length = (position + length >= MEM_SIZE) ? (MEM_SIZE - position) : length; + memcpy(destination,&memory[position],_length); + return (_length==length); +} + +void DS2431::setPageProtection(const uint8_t position) +{ + if (position < 1*PAGE_SIZE) memory[0x80] = WP_MODE; + else if (position < 2*PAGE_SIZE) memory[0x81] = WP_MODE; + else if (position < 3*PAGE_SIZE) memory[0x82] = WP_MODE; + else if (position < 4*PAGE_SIZE) memory[0x83] = WP_MODE; + else if (position < 0x85) memory[0x84] = WP_MODE; + else if (position == 0x85) memory[0x85] = WP_MODE; + else if (position < 0x88) memory[0x85] = EP_MODE; + + updatePageStatus(); +} + +bool DS2431::getPageProtection(const uint8_t position) const +{ + // should be an accurate model of the control bytes + if (position < 1*PAGE_SIZE) + { + if ((page_protection & 1) != 0) return true; + } + else if (position < 2*PAGE_SIZE) + { + if ((page_protection & 2) != 0) return true; + } + else if (position < 3*PAGE_SIZE) + { + if ((page_protection & 4) != 0) return true; + } + else if (position < 4*PAGE_SIZE) + { + if ((page_protection & 8) != 0) return true; + } + else if (position == 0x80) + { + if (((page_protection & (1 + 16)) != 0) || ((page_eprom_mode & 1)) != 0) return true; + } + else if (position == 0x81) + { + if (((page_protection & (2 + 16)) != 0) || ((page_eprom_mode & 2)) != 0) return true; + } + else if (position == 0x82) + { + if (((page_protection & (4 + 16)) != 0) || ((page_eprom_mode & 4)) != 0) return true; + } + else if (position == 0x83) + { + if (((page_protection & (8 + 16)) != 0) || ((page_eprom_mode & 8)) != 0) return true; + } + else if (position == 0x85) + { + if ((page_protection & (32+16)) != 0) return true; + } + else if ((position == 0x86) || (position == 0x87)) + { + if ((page_protection & (64+16)) != 0) return true; + } + else if (position > 127) // filter the rest + { + if ((page_protection & 16) != 0) return true; + } + return false; +} + +void DS2431::setPageEpromMode(const uint8_t position) +{ + if (position < 1*PAGE_SIZE) memory[0x80] = EP_MODE; + else if (position < 2*PAGE_SIZE) memory[0x81] = EP_MODE; + else if (position < 3*PAGE_SIZE) memory[0x82] = EP_MODE; + else if (position < 4*PAGE_SIZE) memory[0x83] = EP_MODE; + updatePageStatus(); +} + +bool DS2431::getPageEpromMode(const uint8_t position) const +{ + if (position < 1*PAGE_SIZE) + { + if ((page_eprom_mode & 1) != 0) return true; + } + else if (position < 2*PAGE_SIZE) + { + if ((page_eprom_mode & 2) != 0) return true; + } + else if (position < 3*PAGE_SIZE) + { + if ((page_eprom_mode & 4) != 0) return true; + } + else if (position < 4*PAGE_SIZE) + { + if ((page_eprom_mode & 8) != 0) return true; + } + return false; +} + + +bool DS2431::updatePageStatus(void) +{ + page_eprom_mode = 0; + page_protection = 0; + + if (memory[0x80] == WP_MODE) page_protection |= 1; + if (memory[0x81] == WP_MODE) page_protection |= 2; + if (memory[0x82] == WP_MODE) page_protection |= 4; + if (memory[0x83] == WP_MODE) page_protection |= 8; + + if (memory[0x84] == WP_MODE) page_protection |= 16; + if (memory[0x84] == EP_MODE) page_protection |= 16; + + if (memory[0x85] == WP_MODE) page_protection |= 32; // only byte x85 + if (memory[0x85] == EP_MODE) page_protection |= 64+32; // also byte x86 x87 + + if (memory[0x80] == EP_MODE) page_eprom_mode |= 1; + if (memory[0x81] == EP_MODE) page_eprom_mode |= 2; + if (memory[0x82] == EP_MODE) page_eprom_mode |= 4; + if (memory[0x83] == EP_MODE) page_eprom_mode |= 8; + return true; +} diff --git a/src/DS2430.h b/src/DS2430.h new file mode 100644 index 0000000..229d215 --- /dev/null +++ b/src/DS2430.h @@ -0,0 +1,60 @@ +// 1Kb 1-Wire EEPROM +// works, +// note: datasheet is fuzzy, but device is similar to ds2433 +// native bus-features: Overdrive capable + +#ifndef ONEWIRE_DS2431_H +#define ONEWIRE_DS2431_H + +#include "OneWireItem.h" + +class DS2431 : public OneWireItem +{ +private: + + static constexpr uint8_t MEM_SIZE { 144 }; + + static constexpr uint8_t PAGE_SIZE { 32 }; + static constexpr uint8_t PAGE_COUNT { MEM_SIZE / PAGE_SIZE }; + static constexpr uint8_t PAGE_MASK { 0b00011111 }; + + static constexpr uint8_t SCRATCHPAD_SIZE { 8 }; + static constexpr uint8_t SCRATCHPAD_MASK { 0b00000111 }; + + static constexpr uint8_t REG_ES_PF_MASK { 0b00100000 }; // partial byte flag + static constexpr uint8_t REG_ES_ZERO_MASK { 0b01011000 }; // reads always zero + static constexpr uint8_t REG_ES_AA_MASK { 0b10000000 }; // authorization accepted (data copied to target memory) + + static constexpr uint8_t WP_MODE { 0x55 }; // write protect mode + static constexpr uint8_t EP_MODE { 0xAA }; // eprom mode + + uint8_t memory[MEM_SIZE]; + + uint8_t scratchpad[SCRATCHPAD_SIZE]; + uint8_t page_protection; + uint8_t page_eprom_mode; + + bool updatePageStatus(void); + void clearScratchpad(void); + +public: + + static constexpr uint8_t family_code { 0x2D }; + + DS2431(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, uint8_t ID6, uint8_t ID7); + + void duty(OneWireHub * hub) final; + + void clearMemory(void); + + bool writeMemory(const uint8_t* source, uint8_t length, uint8_t position = 0); + bool readMemory(uint8_t* destination, uint16_t length, uint16_t position = 0) const; + + void setPageProtection(uint8_t position); + bool getPageProtection(uint8_t position) const; + + void setPageEpromMode(uint8_t position); + bool getPageEpromMode(uint8_t position) const; +}; + +#endif From 935611591bbc40c50d660fe1e22f7c49a1d037fc Mon Sep 17 00:00:00 2001 From: Siarhei Hanchuk Date: Thu, 5 Apr 2018 18:55:38 +0300 Subject: [PATCH 2/9] Rename Class for DS2430 --- src/DS2430.cpp | 24 ++++++++++++------------ src/DS2430.h | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/DS2430.cpp b/src/DS2430.cpp index 8dbe02b..330d134 100644 --- a/src/DS2430.cpp +++ b/src/DS2430.cpp @@ -1,6 +1,6 @@ -#include "DS2431.h" +#include "DS2430.h" -DS2431::DS2431(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, uint8_t ID6, uint8_t ID7) : OneWireItem(ID1, ID2, ID3, ID4, ID5, ID6, ID7) +DS2430::DS2430(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, uint8_t ID6, uint8_t ID7) : OneWireItem(ID1, ID2, ID3, ID4, ID5, ID6, ID7) { static_assert(sizeof(scratchpad) < 256, "Implementation does not cover the whole address-space"); static_assert(sizeof(memory) < 256, "Implementation does not cover the whole address-space"); @@ -14,7 +14,7 @@ DS2431::DS2431(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, updatePageStatus(); } -void DS2431::duty(OneWireHub * const hub) +void DS2430::duty(OneWireHub * const hub) { constexpr uint8_t ALTERNATING_10 { 0xAA }; static uint16_t reg_TA { 0 }; // contains TA1, TA2 @@ -128,17 +128,17 @@ void DS2431::duty(OneWireHub * const hub) } } -void DS2431::clearMemory(void) +void DS2430::clearMemory(void) { memset(memory, static_cast(0x00), sizeof(memory)); } -void DS2431::clearScratchpad(void) +void DS2430::clearScratchpad(void) { memset(scratchpad, static_cast(0x00), SCRATCHPAD_SIZE); } -bool DS2431::writeMemory(const uint8_t* const source, const uint8_t length, const uint8_t position) +bool DS2430::writeMemory(const uint8_t* const source, const uint8_t length, const uint8_t position) { for (uint8_t i = 0; i < length; ++i) { if ((position + i) >= sizeof(memory)) break; @@ -151,7 +151,7 @@ bool DS2431::writeMemory(const uint8_t* const source, const uint8_t length, cons return true; } -bool DS2431::readMemory(uint8_t* const destination, const uint16_t length, const uint16_t position) const +bool DS2430::readMemory(uint8_t* const destination, const uint16_t length, const uint16_t position) const { if (position >= MEM_SIZE) return false; const uint16_t _length = (position + length >= MEM_SIZE) ? (MEM_SIZE - position) : length; @@ -159,7 +159,7 @@ bool DS2431::readMemory(uint8_t* const destination, const uint16_t length, const return (_length==length); } -void DS2431::setPageProtection(const uint8_t position) +void DS2430::setPageProtection(const uint8_t position) { if (position < 1*PAGE_SIZE) memory[0x80] = WP_MODE; else if (position < 2*PAGE_SIZE) memory[0x81] = WP_MODE; @@ -172,7 +172,7 @@ void DS2431::setPageProtection(const uint8_t position) updatePageStatus(); } -bool DS2431::getPageProtection(const uint8_t position) const +bool DS2430::getPageProtection(const uint8_t position) const { // should be an accurate model of the control bytes if (position < 1*PAGE_SIZE) @@ -222,7 +222,7 @@ bool DS2431::getPageProtection(const uint8_t position) const return false; } -void DS2431::setPageEpromMode(const uint8_t position) +void DS2430::setPageEpromMode(const uint8_t position) { if (position < 1*PAGE_SIZE) memory[0x80] = EP_MODE; else if (position < 2*PAGE_SIZE) memory[0x81] = EP_MODE; @@ -231,7 +231,7 @@ void DS2431::setPageEpromMode(const uint8_t position) updatePageStatus(); } -bool DS2431::getPageEpromMode(const uint8_t position) const +bool DS2430::getPageEpromMode(const uint8_t position) const { if (position < 1*PAGE_SIZE) { @@ -253,7 +253,7 @@ bool DS2431::getPageEpromMode(const uint8_t position) const } -bool DS2431::updatePageStatus(void) +bool DS2430::updatePageStatus(void) { page_eprom_mode = 0; page_protection = 0; diff --git a/src/DS2430.h b/src/DS2430.h index 229d215..b008ef8 100644 --- a/src/DS2430.h +++ b/src/DS2430.h @@ -3,12 +3,12 @@ // note: datasheet is fuzzy, but device is similar to ds2433 // native bus-features: Overdrive capable -#ifndef ONEWIRE_DS2431_H -#define ONEWIRE_DS2431_H +#ifndef ONEWIRE_DS2430_H +#define ONEWIRE_DS2430_H #include "OneWireItem.h" -class DS2431 : public OneWireItem +class DS2430 : public OneWireItem { private: @@ -41,7 +41,7 @@ class DS2431 : public OneWireItem static constexpr uint8_t family_code { 0x2D }; - DS2431(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, uint8_t ID6, uint8_t ID7); + DS2430(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, uint8_t ID6, uint8_t ID7); void duty(OneWireHub * hub) final; From 5c225e51f3f70f22ad38b2a189891a9062146af9 Mon Sep 17 00:00:00 2001 From: Siarhei Hanchuk Date: Thu, 5 Apr 2018 19:33:20 +0300 Subject: [PATCH 3/9] Completely rewrite DS2430 module by specs --- src/DS2430.cpp | 65 +++++++++++++------------------------------------- src/DS2430.h | 8 +++---- 2 files changed, 20 insertions(+), 53 deletions(-) diff --git a/src/DS2430.cpp b/src/DS2430.cpp index 330d134..f384bf9 100644 --- a/src/DS2430.cpp +++ b/src/DS2430.cpp @@ -16,27 +16,22 @@ DS2430::DS2430(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, void DS2430::duty(OneWireHub * const hub) { - constexpr uint8_t ALTERNATING_10 { 0xAA }; - static uint16_t reg_TA { 0 }; // contains TA1, TA2 + static uint8_t reg_TA { 0 }; // contains TA1, TA2 static uint8_t reg_ES { 0 }; // E/S register - uint16_t crc { 0 }; - - uint8_t page_offset { 0 }; - uint8_t cmd, data; - if (hub->recv(&cmd,1,crc)) return; + uint8_t cmd, data; + if (hub->recv(&cmd,1)) return; switch (cmd) { case 0x0F: // WRITE SCRATCHPAD COMMAND - if (hub->recv(reinterpret_cast(®_TA),2,crc)) return; + if (hub->recv(reinterpret_cast(®_TA),1)) return; reg_ES = uint8_t(reg_TA) & SCRATCHPAD_MASK; - page_offset = reg_ES; - // receive up to 8 bytes of data + // receive up to 32 bytes of data for (; reg_ES < SCRATCHPAD_SIZE; ++reg_ES) { - if (hub->recv(&scratchpad[reg_ES], 1, crc)) + if (hub->recv(&scratchpad[reg_ES], 1)) { if (hub->getError() == Error::AWAIT_TIMESLOT_TIMEOUT_HIGH) reg_ES |= REG_ES_PF_MASK; break; @@ -45,56 +40,26 @@ void DS2430::duty(OneWireHub * const hub) reg_ES--; reg_ES &= SCRATCHPAD_MASK; - if (hub->getError() == Error::NO_ERROR) // try to send crc if wanted - { - crc = ~crc; // normally crc16 is sent ~inverted - hub->send(reinterpret_cast(&crc), 2); - } - - if (reg_TA < (4*PAGE_SIZE)) // check if page is protected or in eprom-mode - { - const uint8_t position = uint8_t(reg_TA) & ~SCRATCHPAD_MASK; - if (getPageProtection(reinterpret_cast(®_TA)[0])) // protected: load memory-segment to scratchpad - { - for (uint8_t i = 0; i < SCRATCHPAD_SIZE; ++i) scratchpad[i] = memory[position + i]; - } - else if (getPageEpromMode(reinterpret_cast(®_TA)[0])) // eprom: logical AND of memory and data - { - for (uint8_t i = page_offset; i <= reg_ES; ++i) scratchpad[i] &= memory[position + i]; - } - } break; case 0xAA: // READ SCRATCHPAD COMMAND - - if (hub->send(reinterpret_cast(®_TA),2,crc)) return; - if (hub->send(®_ES,1,crc)) return; + if (hub->send(reinterpret_cast(®_TA), 1)) return; { // send Scratchpad content const uint8_t start = uint8_t(reg_TA) & SCRATCHPAD_MASK; - const uint8_t length = (reg_ES & SCRATCHPAD_MASK)+ uint8_t(1) - start; // difference to ds2433 - if (hub->send(&scratchpad[start],length,crc)) return; + const uint8_t length = SCRATCHPAD_MASK - start; + if (hub->send(&scratchpad[start],length)) return; } - crc = ~crc; - if (hub->send(reinterpret_cast(&crc),2)) return; break; // send 1s when read is complete, is passive, so do nothing case 0x55: // COPY SCRATCHPAD COMMAND - - if (hub->recv(&data)) return; - if (data != reinterpret_cast(®_TA)[0]) break; - if (hub->recv(&data)) return; - if (data != reinterpret_cast(®_TA)[1]) break; if (hub->recv(&data)) return; - if (data != reg_ES) return; // Auth code must match - - if (getPageProtection(uint8_t(reg_TA))) break; // stop if page is protected (WriteMemory also checks this) - if ((reg_ES & REG_ES_PF_MASK) != 0) break; // stop if error occured earlier + if (data != 0xA5) break; reg_ES |= REG_ES_AA_MASK; // compare was successful - reg_TA &= ~uint16_t(SCRATCHPAD_MASK); + reg_TA &= ~uint8_t(SCRATCHPAD_MASK); // Write Scratchpad to memory, writing takes about 10ms writeMemory(scratchpad, SCRATCHPAD_SIZE, reinterpret_cast(®_TA)[0]); // checks if copy protected @@ -112,14 +77,15 @@ void DS2430::duty(OneWireHub * const hub) interrupts(); - while (!hub->send(&ALTERNATING_10)); // alternating 1 & 0 after copy is complete break; case 0xF0: // READ MEMORY COMMAND - if (hub->recv(reinterpret_cast(®_TA),2)) return; + if (hub->recv(&data)) return; + if (hub->recv(reinterpret_cast(®_TA),1)) return; + if (reg_TA >= MEM_SIZE) return; - if (hub->send(&memory[reg_TA],MEM_SIZE - uint8_t(reg_TA),crc)) return; + if (hub->send(&memory[reg_TA],MEM_SIZE - uint8_t(reg_TA))) return; break; // send 1s when read is complete, is passive, so do nothing here default: @@ -141,6 +107,7 @@ void DS2430::clearScratchpad(void) bool DS2430::writeMemory(const uint8_t* const source, const uint8_t length, const uint8_t position) { for (uint8_t i = 0; i < length; ++i) { + if ((position + i) >= sizeof(memory)) break; if (getPageProtection(position + i)) continue; memory[position + i] = source[i]; diff --git a/src/DS2430.h b/src/DS2430.h index b008ef8..eb2afc6 100644 --- a/src/DS2430.h +++ b/src/DS2430.h @@ -12,14 +12,14 @@ class DS2430 : public OneWireItem { private: - static constexpr uint8_t MEM_SIZE { 144 }; + static constexpr uint8_t MEM_SIZE { 32 }; static constexpr uint8_t PAGE_SIZE { 32 }; static constexpr uint8_t PAGE_COUNT { MEM_SIZE / PAGE_SIZE }; static constexpr uint8_t PAGE_MASK { 0b00011111 }; - static constexpr uint8_t SCRATCHPAD_SIZE { 8 }; - static constexpr uint8_t SCRATCHPAD_MASK { 0b00000111 }; + static constexpr uint8_t SCRATCHPAD_SIZE { 32 }; + static constexpr uint8_t SCRATCHPAD_MASK { 0b00011111 }; static constexpr uint8_t REG_ES_PF_MASK { 0b00100000 }; // partial byte flag static constexpr uint8_t REG_ES_ZERO_MASK { 0b01011000 }; // reads always zero @@ -39,7 +39,7 @@ class DS2430 : public OneWireItem public: - static constexpr uint8_t family_code { 0x2D }; + static constexpr uint8_t family_code { 0x14 }; DS2430(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, uint8_t ID6, uint8_t ID7); From e94f1dd5be34183fe336d7a0c0ea471c81110b8f Mon Sep 17 00:00:00 2001 From: Siarhei Hanchuk Date: Fri, 6 Apr 2018 11:56:19 +0300 Subject: [PATCH 4/9] CleanUp Unused DS2430 code --- src/DS2430.cpp | 145 +++++++++---------------------------------------- 1 file changed, 26 insertions(+), 119 deletions(-) diff --git a/src/DS2430.cpp b/src/DS2430.cpp index f384bf9..c67cfc0 100644 --- a/src/DS2430.cpp +++ b/src/DS2430.cpp @@ -11,7 +11,7 @@ DS2430::DS2430(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, page_protection = 0; page_eprom_mode = 0; - updatePageStatus(); + // updatePageStatus(); } void DS2430::duty(OneWireHub * const hub) @@ -19,7 +19,7 @@ void DS2430::duty(OneWireHub * const hub) static uint8_t reg_TA { 0 }; // contains TA1, TA2 static uint8_t reg_ES { 0 }; // E/S register - uint8_t cmd, data; + uint8_t cmd, data; if (hub->recv(&cmd,1)) return; switch (cmd) { @@ -43,6 +43,7 @@ void DS2430::duty(OneWireHub * const hub) break; case 0xAA: // READ SCRATCHPAD COMMAND + if (hub->send(reinterpret_cast(®_TA), 1)) return; { // send Scratchpad content @@ -54,13 +55,16 @@ void DS2430::duty(OneWireHub * const hub) break; // send 1s when read is complete, is passive, so do nothing case 0x55: // COPY SCRATCHPAD COMMAND + if (hub->recv(&data)) return; if (data != 0xA5) break; + // if ((reg_ES & REG_ES_PF_MASK) != 0) break; // stop if error occured earlier reg_ES |= REG_ES_AA_MASK; // compare was successful reg_TA &= ~uint8_t(SCRATCHPAD_MASK); + // Write Scratchpad to memory, writing takes about 10ms writeMemory(scratchpad, SCRATCHPAD_SIZE, reinterpret_cast(®_TA)[0]); // checks if copy protected @@ -80,8 +84,6 @@ void DS2430::duty(OneWireHub * const hub) break; case 0xF0: // READ MEMORY COMMAND - - if (hub->recv(&data)) return; if (hub->recv(reinterpret_cast(®_TA),1)) return; if (reg_TA >= MEM_SIZE) return; @@ -107,13 +109,12 @@ void DS2430::clearScratchpad(void) bool DS2430::writeMemory(const uint8_t* const source, const uint8_t length, const uint8_t position) { for (uint8_t i = 0; i < length; ++i) { - if ((position + i) >= sizeof(memory)) break; - if (getPageProtection(position + i)) continue; + // if (getPageProtection(position + i)) continue; memory[position + i] = source[i]; } - if ((position+length) > 127) updatePageStatus(); + // if ((position+length) > 127) updatePageStatus(); return true; } @@ -126,119 +127,25 @@ bool DS2430::readMemory(uint8_t* const destination, const uint16_t length, const return (_length==length); } -void DS2430::setPageProtection(const uint8_t position) -{ - if (position < 1*PAGE_SIZE) memory[0x80] = WP_MODE; - else if (position < 2*PAGE_SIZE) memory[0x81] = WP_MODE; - else if (position < 3*PAGE_SIZE) memory[0x82] = WP_MODE; - else if (position < 4*PAGE_SIZE) memory[0x83] = WP_MODE; - else if (position < 0x85) memory[0x84] = WP_MODE; - else if (position == 0x85) memory[0x85] = WP_MODE; - else if (position < 0x88) memory[0x85] = EP_MODE; - - updatePageStatus(); -} - -bool DS2430::getPageProtection(const uint8_t position) const -{ - // should be an accurate model of the control bytes - if (position < 1*PAGE_SIZE) - { - if ((page_protection & 1) != 0) return true; - } - else if (position < 2*PAGE_SIZE) - { - if ((page_protection & 2) != 0) return true; - } - else if (position < 3*PAGE_SIZE) - { - if ((page_protection & 4) != 0) return true; - } - else if (position < 4*PAGE_SIZE) - { - if ((page_protection & 8) != 0) return true; - } - else if (position == 0x80) - { - if (((page_protection & (1 + 16)) != 0) || ((page_eprom_mode & 1)) != 0) return true; - } - else if (position == 0x81) - { - if (((page_protection & (2 + 16)) != 0) || ((page_eprom_mode & 2)) != 0) return true; - } - else if (position == 0x82) - { - if (((page_protection & (4 + 16)) != 0) || ((page_eprom_mode & 4)) != 0) return true; - } - else if (position == 0x83) - { - if (((page_protection & (8 + 16)) != 0) || ((page_eprom_mode & 8)) != 0) return true; - } - else if (position == 0x85) - { - if ((page_protection & (32+16)) != 0) return true; - } - else if ((position == 0x86) || (position == 0x87)) - { - if ((page_protection & (64+16)) != 0) return true; - } - else if (position > 127) // filter the rest - { - if ((page_protection & 16) != 0) return true; - } - return false; -} - -void DS2430::setPageEpromMode(const uint8_t position) -{ - if (position < 1*PAGE_SIZE) memory[0x80] = EP_MODE; - else if (position < 2*PAGE_SIZE) memory[0x81] = EP_MODE; - else if (position < 3*PAGE_SIZE) memory[0x82] = EP_MODE; - else if (position < 4*PAGE_SIZE) memory[0x83] = EP_MODE; - updatePageStatus(); -} +// bool DS2430::updatePageStatus(void) +// { +// page_eprom_mode = 0; +// page_protection = 0; -bool DS2430::getPageEpromMode(const uint8_t position) const -{ - if (position < 1*PAGE_SIZE) - { - if ((page_eprom_mode & 1) != 0) return true; - } - else if (position < 2*PAGE_SIZE) - { - if ((page_eprom_mode & 2) != 0) return true; - } - else if (position < 3*PAGE_SIZE) - { - if ((page_eprom_mode & 4) != 0) return true; - } - else if (position < 4*PAGE_SIZE) - { - if ((page_eprom_mode & 8) != 0) return true; - } - return false; -} +// if (memory[0x80] == WP_MODE) page_protection |= 1; +// if (memory[0x81] == WP_MODE) page_protection |= 2; +// if (memory[0x82] == WP_MODE) page_protection |= 4; +// if (memory[0x83] == WP_MODE) page_protection |= 8; +// if (memory[0x84] == WP_MODE) page_protection |= 16; +// if (memory[0x84] == EP_MODE) page_protection |= 16; -bool DS2430::updatePageStatus(void) -{ - page_eprom_mode = 0; - page_protection = 0; - - if (memory[0x80] == WP_MODE) page_protection |= 1; - if (memory[0x81] == WP_MODE) page_protection |= 2; - if (memory[0x82] == WP_MODE) page_protection |= 4; - if (memory[0x83] == WP_MODE) page_protection |= 8; - - if (memory[0x84] == WP_MODE) page_protection |= 16; - if (memory[0x84] == EP_MODE) page_protection |= 16; +// if (memory[0x85] == WP_MODE) page_protection |= 32; // only byte x85 +// if (memory[0x85] == EP_MODE) page_protection |= 64+32; // also byte x86 x87 - if (memory[0x85] == WP_MODE) page_protection |= 32; // only byte x85 - if (memory[0x85] == EP_MODE) page_protection |= 64+32; // also byte x86 x87 - - if (memory[0x80] == EP_MODE) page_eprom_mode |= 1; - if (memory[0x81] == EP_MODE) page_eprom_mode |= 2; - if (memory[0x82] == EP_MODE) page_eprom_mode |= 4; - if (memory[0x83] == EP_MODE) page_eprom_mode |= 8; - return true; -} +// if (memory[0x80] == EP_MODE) page_eprom_mode |= 1; +// if (memory[0x81] == EP_MODE) page_eprom_mode |= 2; +// if (memory[0x82] == EP_MODE) page_eprom_mode |= 4; +// if (memory[0x83] == EP_MODE) page_eprom_mode |= 8; +// return true; +// } From 8a3a98d82ac851f2d3d10b7ba2b98997355a44f9 Mon Sep 17 00:00:00 2001 From: Siarhei Hanchuk Date: Sat, 7 Apr 2018 21:05:43 +0300 Subject: [PATCH 5/9] Fixed ignoring commands after search for DS2430 --- src/OneWireHub.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OneWireHub.cpp b/src/OneWireHub.cpp index bc3422b..d3b7626 100644 --- a/src/OneWireHub.cpp +++ b/src/OneWireHub.cpp @@ -420,6 +420,7 @@ bool OneWireHub::recvAndProcessCmd(void) slave_selected = nullptr; searchIDTree(); + slave_selected->duty(this); return false; // always trigger a re-init after searchIDTree case 0x69: // overdrive MATCH ROM From 083cadf3e5feeb7dfec45e5c87dd863f372ee427 Mon Sep 17 00:00:00 2001 From: Siarhei Hanchuk Date: Sat, 7 Apr 2018 21:06:04 +0300 Subject: [PATCH 6/9] Refactoring DS2430 and fixed copy memory operation --- src/DS2430.cpp | 57 ++++---------------------------------------------- src/DS2430.h | 16 +++----------- 2 files changed, 7 insertions(+), 66 deletions(-) diff --git a/src/DS2430.cpp b/src/DS2430.cpp index c67cfc0..60ecad7 100644 --- a/src/DS2430.cpp +++ b/src/DS2430.cpp @@ -7,11 +7,6 @@ DS2430::DS2430(uint8_t ID1, uint8_t ID2, uint8_t ID3, uint8_t ID4, uint8_t ID5, clearMemory(); clearScratchpad(); - - page_protection = 0; - page_eprom_mode = 0; - - // updatePageStatus(); } void DS2430::duty(OneWireHub * const hub) @@ -27,6 +22,7 @@ void DS2430::duty(OneWireHub * const hub) if (hub->recv(reinterpret_cast(®_TA),1)) return; reg_ES = uint8_t(reg_TA) & SCRATCHPAD_MASK; + scratchpad_start_address = reg_ES; // receive up to 32 bytes of data for (; reg_ES < SCRATCHPAD_SIZE; ++reg_ES) @@ -38,6 +34,7 @@ void DS2430::duty(OneWireHub * const hub) } } reg_ES--; + scratchpad_size = scratchpad_start_address; reg_ES &= SCRATCHPAD_MASK; break; @@ -58,28 +55,8 @@ void DS2430::duty(OneWireHub * const hub) if (hub->recv(&data)) return; if (data != 0xA5) break; - // if ((reg_ES & REG_ES_PF_MASK) != 0) break; // stop if error occured earlier - - reg_ES |= REG_ES_AA_MASK; // compare was successful - - reg_TA &= ~uint8_t(SCRATCHPAD_MASK); - - - // Write Scratchpad to memory, writing takes about 10ms - writeMemory(scratchpad, SCRATCHPAD_SIZE, reinterpret_cast(®_TA)[0]); // checks if copy protected - - noInterrupts(); - - do - { - hub->clearError(); - hub->sendBit(true); // send passive 1s - - } - while (hub->getError() == Error::AWAIT_TIMESLOT_TIMEOUT_HIGH); // wait for timeslots - - interrupts(); + writeMemory(scratchpad, scratchpad_size, scratchpad_start_address); break; @@ -110,12 +87,9 @@ bool DS2430::writeMemory(const uint8_t* const source, const uint8_t length, cons { for (uint8_t i = 0; i < length; ++i) { if ((position + i) >= sizeof(memory)) break; - // if (getPageProtection(position + i)) continue; - memory[position + i] = source[i]; + memory[position + i] = source[position + i]; } - // if ((position+length) > 127) updatePageStatus(); - return true; } @@ -126,26 +100,3 @@ bool DS2430::readMemory(uint8_t* const destination, const uint16_t length, const memcpy(destination,&memory[position],_length); return (_length==length); } - -// bool DS2430::updatePageStatus(void) -// { -// page_eprom_mode = 0; -// page_protection = 0; - -// if (memory[0x80] == WP_MODE) page_protection |= 1; -// if (memory[0x81] == WP_MODE) page_protection |= 2; -// if (memory[0x82] == WP_MODE) page_protection |= 4; -// if (memory[0x83] == WP_MODE) page_protection |= 8; - -// if (memory[0x84] == WP_MODE) page_protection |= 16; -// if (memory[0x84] == EP_MODE) page_protection |= 16; - -// if (memory[0x85] == WP_MODE) page_protection |= 32; // only byte x85 -// if (memory[0x85] == EP_MODE) page_protection |= 64+32; // also byte x86 x87 - -// if (memory[0x80] == EP_MODE) page_eprom_mode |= 1; -// if (memory[0x81] == EP_MODE) page_eprom_mode |= 2; -// if (memory[0x82] == EP_MODE) page_eprom_mode |= 4; -// if (memory[0x83] == EP_MODE) page_eprom_mode |= 8; -// return true; -// } diff --git a/src/DS2430.h b/src/DS2430.h index eb2afc6..aff77d1 100644 --- a/src/DS2430.h +++ b/src/DS2430.h @@ -22,19 +22,15 @@ class DS2430 : public OneWireItem static constexpr uint8_t SCRATCHPAD_MASK { 0b00011111 }; static constexpr uint8_t REG_ES_PF_MASK { 0b00100000 }; // partial byte flag - static constexpr uint8_t REG_ES_ZERO_MASK { 0b01011000 }; // reads always zero static constexpr uint8_t REG_ES_AA_MASK { 0b10000000 }; // authorization accepted (data copied to target memory) - static constexpr uint8_t WP_MODE { 0x55 }; // write protect mode - static constexpr uint8_t EP_MODE { 0xAA }; // eprom mode - uint8_t memory[MEM_SIZE]; uint8_t scratchpad[SCRATCHPAD_SIZE]; - uint8_t page_protection; - uint8_t page_eprom_mode; - bool updatePageStatus(void); + uint8_t scratchpad_start_address; + uint8_t scratchpad_size; + void clearScratchpad(void); public: @@ -49,12 +45,6 @@ class DS2430 : public OneWireItem bool writeMemory(const uint8_t* source, uint8_t length, uint8_t position = 0); bool readMemory(uint8_t* destination, uint16_t length, uint16_t position = 0) const; - - void setPageProtection(uint8_t position); - bool getPageProtection(uint8_t position) const; - - void setPageEpromMode(uint8_t position); - bool getPageEpromMode(uint8_t position) const; }; #endif From 5e6839797920b20c2533633850dd4270e9acfbd0 Mon Sep 17 00:00:00 2001 From: Siarhei Hanchuk Date: Sat, 11 Aug 2018 14:06:23 +0300 Subject: [PATCH 7/9] Remove unused code from DS2430 header --- src/DS2430.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/DS2430.h b/src/DS2430.h index aff77d1..552567b 100644 --- a/src/DS2430.h +++ b/src/DS2430.h @@ -14,15 +14,10 @@ class DS2430 : public OneWireItem static constexpr uint8_t MEM_SIZE { 32 }; - static constexpr uint8_t PAGE_SIZE { 32 }; - static constexpr uint8_t PAGE_COUNT { MEM_SIZE / PAGE_SIZE }; - static constexpr uint8_t PAGE_MASK { 0b00011111 }; - static constexpr uint8_t SCRATCHPAD_SIZE { 32 }; static constexpr uint8_t SCRATCHPAD_MASK { 0b00011111 }; static constexpr uint8_t REG_ES_PF_MASK { 0b00100000 }; // partial byte flag - static constexpr uint8_t REG_ES_AA_MASK { 0b10000000 }; // authorization accepted (data copied to target memory) uint8_t memory[MEM_SIZE]; From c37d050aafb90e7c9fc2f69f34ea1f4484547bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingmar=20In=CA=92o=20Splitt?= Date: Sat, 8 Oct 2022 22:55:45 +0200 Subject: [PATCH 8/9] Update DS2430.h --- src/DS2430.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/DS2430.h b/src/DS2430.h index 552567b..51b09e1 100644 --- a/src/DS2430.h +++ b/src/DS2430.h @@ -1,7 +1,5 @@ // 1Kb 1-Wire EEPROM -// works, -// note: datasheet is fuzzy, but device is similar to ds2433 -// native bus-features: Overdrive capable +// usercontribution, based on ds2431 (but without some features) #ifndef ONEWIRE_DS2430_H #define ONEWIRE_DS2430_H From a231b53759a008316afd072047dad6148d53a3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingmar=20In=CA=92o=20Splitt?= Date: Sat, 8 Oct 2022 23:26:47 +0200 Subject: [PATCH 9/9] Update OneWireHub.cpp prepare TODO for future version --- src/OneWireHub.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OneWireHub.cpp b/src/OneWireHub.cpp index d3b7626..7875db1 100644 --- a/src/OneWireHub.cpp +++ b/src/OneWireHub.cpp @@ -420,7 +420,8 @@ bool OneWireHub::recvAndProcessCmd(void) slave_selected = nullptr; searchIDTree(); - slave_selected->duty(this); + // slave_selected->duty(this); + // TODO: some ICs like DS2430 allow going for duty() right after search return false; // always trigger a re-init after searchIDTree case 0x69: // overdrive MATCH ROM