Skip to content

Commit

Permalink
extend the ds2450
Browse files Browse the repository at this point in the history
  • Loading branch information
orgua committed Feb 21, 2016
1 parent 25bc676 commit c04e97d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
2 changes: 0 additions & 2 deletions DS2408.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ void DS2408::updateCRC()

bool DS2408::duty(OneWireHub *hub)
{
uint8_t addrL; // TODO: unused for now
uint8_t addrH; // TODO: unused for now
uint8_t data; // TODO: unused for now

uint8_t done = hub->recv();
Expand Down
77 changes: 62 additions & 15 deletions DS2450.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
DS2450::DS2450(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)
{

memset(&memory, 0, sizeof(memory));
uint8_t mem_size = PAGE_COUNT*PAGE_SIZE;
memset(&memory, 0, mem_size);
if (mem_size > 0x1C) memory[0x1C] = 0x40;
}

bool DS2450::duty(OneWireHub *hub)
Expand All @@ -15,36 +16,38 @@ bool DS2450::duty(OneWireHub *hub)
uint8_t b;
uint16_t crc;

ow_crc16_reset();
ow_crc16_reset(); // TODO: dump this function and replace it with the generic crc16

uint8_t done = hub->recv();
switch (done)
{
case 0xAA: // READ MEMORY
// Cmd
ow_crc16_update(0xAA);

// Adr1
b = hub->recv();
((uint8_t *) &memory_address)[0] = b;
ow_crc16_update(0xAA); // Cmd

b = hub->recv(); // Adr1
reinterpret_cast<uint8_t *>(&memory_address)[0] = b;
ow_crc16_update(b);

// Adr2
b = hub->recv();
((uint8_t *) &memory_address)[1] = b;
b = hub->recv(); // Adr2
reinterpret_cast<uint8_t *>(&memory_address)[1] = b;
ow_crc16_update(b);

memory_address_start = memory_address;
if (memory_address > (PAGE_COUNT-1)*PAGE_SIZE) memory_address = 0; // prevent read out of bounds

for (int i = 0; i < 8; ++i)
for (uint8_t i = 0; i < PAGE_SIZE; ++i)
{
b = memory[memory_address + i];
hub->send(b);
hub->send(b); // TODO: add possibility to break loop if send fails
ow_crc16_update(b);
}

crc = ow_crc16_get();
hub->send(((uint8_t *) &crc)[0]);
hub->send(((uint8_t *) &crc)[1]);
hub->send(reinterpret_cast<uint8_t *>(&crc)[0]);
hub->send(reinterpret_cast<uint8_t *>(&crc)[1]);

// TODO: not fully implemented

if (dbg_sensor)
{
Expand All @@ -54,6 +57,50 @@ bool DS2450::duty(OneWireHub *hub)

break;

case 0x55: // write memory (only page 1&2 allowed)
ow_crc16_update(0x55); // Cmd

b = hub->recv(); // Adr1
reinterpret_cast<uint8_t *>(&memory_address)[0] = b;
ow_crc16_update(b);

b = hub->recv(); // Adr2
reinterpret_cast<uint8_t *>(&memory_address)[1] = b;
ow_crc16_update(b);

memory_address_start = memory_address;
if (memory_address > (PAGE_COUNT-1)*PAGE_SIZE) memory_address = 0; // prevent read out of bounds

for (uint8_t i = 0; i < PAGE_SIZE; ++i)
{
memory[memory_address + i] = hub->recv(); // TODO: add possibility to break loop if recv fails, hub->read_error?
ow_crc16_update(memory[memory_address + i]);
}

crc = ow_crc16_get();
hub->send(reinterpret_cast<uint8_t *>(&crc)[0]);
hub->send(reinterpret_cast<uint8_t *>(&crc)[1]);

// TODO: write back data if wanted, till the end of register

if (dbg_sensor)
{
Serial.print("DS2450 : READ MEMORY : ");
Serial.println(memory_address_start, HEX);
}

case 0x3C: // convert, starts adc
ow_crc16_update(0x3C); // Cmd
b = hub->recv(); // input select mask, not important
ow_crc16_update(b);
b = hub->recv(); // read out control byte
ow_crc16_update(b);
crc = ow_crc16_get();
hub->send(reinterpret_cast<uint8_t *>(&crc)[0]);
hub->send(reinterpret_cast<uint8_t *>(&crc)[1]);
hub->sendBit(0); // still converting....
hub->sendBit(1); // finished conversion

default:
if (dbg_HINT)
{
Expand Down
14 changes: 9 additions & 5 deletions DS2450.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 0x20 4 channel A/D
//
// not ready, supported overdrive not implemented

#ifndef ONEWIRE_DS2450_H
#define ONEWIRE_DS2450_H
Expand All @@ -9,7 +9,14 @@ class DS2450 : public OneWireItem
private:
static constexpr bool dbg_sensor = 0; // give debug messages for this sensor

uint8_t memory[0x1F]; // TODO: make readable 3*8 or similar
static constexpr uint8_t PAGE_COUNT = 4;
static constexpr uint8_t PAGE_SIZE = 8;

uint8_t memory[PAGE_COUNT*PAGE_SIZE];
// Page1 : conversion results
// Page2 : control / status
// Page3 : alarm settings
// Page3 : factory calibration

bool duty(OneWireHub *hub);

Expand All @@ -21,9 +28,6 @@ class DS2450 : public OneWireItem
bool setPotentiometer(const uint16_t p1, const uint16_t p2, const uint16_t p3, const uint16_t p4);
bool setPotentiometer(const uint8_t number, const uint16_t value);

//uint8_t Data[13];
//bool updateCRC();
//DS2450_memory memory;
};

#endif

0 comments on commit c04e97d

Please sign in to comment.