Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DS2430 Support #55

Merged
merged 10 commits into from
Oct 8, 2022
102 changes: 102 additions & 0 deletions src/DS2430.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "DS2430.h"

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");

clearMemory();
clearScratchpad();
}

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;
if (hub->recv(&cmd,1)) return;
switch (cmd)
{
case 0x0F: // WRITE SCRATCHPAD COMMAND

if (hub->recv(reinterpret_cast<uint8_t *>(&reg_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)
{
if (hub->recv(&scratchpad[reg_ES], 1))
{
if (hub->getError() == Error::AWAIT_TIMESLOT_TIMEOUT_HIGH) reg_ES |= REG_ES_PF_MASK;
break;
}
}
reg_ES--;
scratchpad_size = scratchpad_start_address;
reg_ES &= SCRATCHPAD_MASK;

break;

case 0xAA: // READ SCRATCHPAD COMMAND

if (hub->send(reinterpret_cast<uint8_t *>(&reg_TA), 1)) return;

{ // send Scratchpad content
const uint8_t start = uint8_t(reg_TA) & SCRATCHPAD_MASK;
const uint8_t length = SCRATCHPAD_MASK - start;
if (hub->send(&scratchpad[start],length)) 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 != 0xA5) break;

writeMemory(scratchpad, scratchpad_size, scratchpad_start_address);

break;

case 0xF0: // READ MEMORY COMMAND
if (hub->recv(reinterpret_cast<uint8_t *>(&reg_TA),1)) return;

if (reg_TA >= MEM_SIZE) 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:

hub->raiseSlaveError(cmd);
}
}

void DS2430::clearMemory(void)
{
memset(memory, static_cast<uint8_t>(0x00), sizeof(memory));
}

void DS2430::clearScratchpad(void)
{
memset(scratchpad, static_cast<uint8_t>(0x00), SCRATCHPAD_SIZE);
}

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;
memory[position + i] = source[position + i];
}

return true;
}

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;
memcpy(destination,&memory[position],_length);
return (_length==length);
}
43 changes: 43 additions & 0 deletions src/DS2430.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 1Kb 1-Wire EEPROM
// usercontribution, based on ds2431 (but without some features)

#ifndef ONEWIRE_DS2430_H
#define ONEWIRE_DS2430_H

#include "OneWireItem.h"

class DS2430 : public OneWireItem
{
private:

static constexpr uint8_t MEM_SIZE { 32 };

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

uint8_t memory[MEM_SIZE];

uint8_t scratchpad[SCRATCHPAD_SIZE];

uint8_t scratchpad_start_address;
uint8_t scratchpad_size;

void clearScratchpad(void);

public:

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);

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;
};

#endif
2 changes: 2 additions & 0 deletions src/OneWireHub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ bool OneWireHub::recvAndProcessCmd(void)
noInterrupts();
searchIDTree();
interrupts();
// 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
Expand Down