Config Change.... 0xCC for temp measurement DS2438/DS18B20
[owTools.git] / src / owDevice.cpp
index 840a0b2..60a8e99 100755 (executable)
 
 
 #include "owInterface.h"
+#ifdef LINUX
 #include <unistd.h>
-
+#endif
+#ifdef WIN
+void usleep(int waitTime) {\r
+       __int64 time1 = 0, time2 = 0, freq = 0;\r
+\r
+       QueryPerformanceCounter((LARGE_INTEGER *)&time1);\r
+       QueryPerformanceFrequency((LARGE_INTEGER *)&freq);\r
+\r
+       do {\r
+               QueryPerformanceCounter((LARGE_INTEGER *)&time2);\r
+       } while ((time2 - time1) < waitTime);\r
+}
+#endif
 owDevice::owDevice(owInterface *owi_,snum_t num) {
                snum=num;
                owi=owi_;
@@ -218,8 +231,8 @@ int owDeviceDS18B20::readScratchpad(std::vector<uint8_t> *sp) {
 
 int owDeviceDS18B20::convertAll() {
        std::vector<uint8_t> sp;
-       sp.push_back(0x44);
-       Communicate(&sp, 1, 0);
+       //sp.push_back(0x44);
+       //communicate(&sp, 1, 0); ///########################################################
        usleep(750000);
 
        if (readScratchpad(&sp)) {
@@ -236,7 +249,7 @@ int owDeviceDS18B20::convertAll() {
 
 
 void owDeviceDS2438::setDefaultConfig() {
-       config->setConfig({1,6, 6,8, 4,7, 7,17, 0,2,3,12,4,0,0,0,0,0,0,0,0,0,0,0});
+       config->setConfig({1,6, 6,8, 4,7, 6,17, 0,2,3,12,4,0,0,0,0,0,0,0,0,0,0,0});
        configstate=OWDCS_DEFAULT;
 }
 
@@ -267,6 +280,69 @@ int owDeviceDS2438::readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int r
        return 0;
 }
 
+int owDeviceDS2438::readMemory(int page,int start, int count,std::vector<uint8_t> *data) {
+       if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
+       std::vector<uint8_t> cl;
+       cl.clear();
+       cl.push_back(0xB8); //recall
+       cl.push_back(page);
+       Communicate(&cl, 2, 0);
+       if (owi->log->last()>=OWLOG_ERROR) return -1;
+       cl.clear();
+       cl.push_back(0xBE);
+       cl.push_back(page);
+       Communicate(&cl, 2, 9);
+       if (owi->log->last()>=OWLOG_ERROR) return -1;
+       cl.erase(cl.begin());
+       cl.erase(cl.begin());
+       if (owi->calcCRC8(cl)!=0) {
+               owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
+       }
+       data->clear();
+       data->insert(data->begin(),cl.begin()+start,cl.begin()+start+count);
+       return count;
+}
+int owDeviceDS2438::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {
+       if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
+       std::vector<uint8_t> sp;
+       if ((start>0)||(count<8)) {
+               readMemory(page,0,8,&sp);
+       }
+       std::vector<uint8_t> cl;
+       cl.push_back(0x4E); 
+       cl.push_back(page);
+       int j=0;
+       for(int i=0;i<8;i++) {
+               if ((i<start)||(i>=start+count)) cl.push_back(sp[i]); else {cl.push_back((*data)[j]);j++;}
+       }
+       Communicate(&cl, 10,0);
+       if (owi->log->last()>=OWLOG_ERROR) return -1;
+       std::vector<uint8_t> cl1;
+       cl1.push_back(0xBE);
+       cl1.push_back(page);
+       Communicate(&cl1, 2, 9);
+       if (owi->log->last()>=OWLOG_ERROR) return -1;
+       cl1.erase(cl1.begin());
+       cl1.erase(cl1.begin());
+       if (owi->calcCRC8(cl1)!=0) {
+               owi->log->set(OWLOG_WARNING,"CRC ERROR rereading DS2438 Scrachpad");
+       }
+       for(int i=0;i<8;i++) {
+               if (cl1[i]!=cl[i+2]) {
+                       owi->log->set(OWLOG_ERROR,"Reread not equal, nothing copied");
+                       return 0; 
+               }
+       }
+       cl.clear();
+       cl.push_back(0x48);
+       cl.push_back(page);
+       Communicate(&cl, 2, 0);
+       if (owi->log->last()>=OWLOG_ERROR) return -1;
+       return 1;
+}
+
+
+
 int owDeviceDS2438::setConfigByte(uint8_t cb) {
        std::vector<uint8_t> sp;
        int k;
@@ -299,8 +375,8 @@ inline int16_t ow_fconvert(uint8_t b1, uint16_t b2) {
 int owDeviceDS2438::convertAll() {
        for(int k=0;k<owi->maxrepeat;k++)  {
                std::vector<uint8_t> sp;
-               sp.push_back(0x44);
-               Communicate(&sp, 1, 0);
+               //sp.push_back(0x44);
+               //Communicate(&sp, 1, 0);///########################################################
                if (owi->log->last()>=OWLOG_ERROR) continue;
                usleep(100000);
                if (setConfigByte(0x08)<=0) continue;
@@ -381,6 +457,8 @@ void owDeviceDS2450::readMemory(uint8_t addr,std::vector<uint8_t> *sp) {
                return;
        }
        if (!owi->testCRC16(cl)) {
+               for (uint8_t v :cl) printf("%02X ",v);
+               printf("\n");
                owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Memory of DS2450");
                return;
        }
@@ -427,6 +505,8 @@ void owDeviceDS2450::convert(uint8_t mask, uint8_t preset) {
        cl.push_back(preset);
        Communicate(&cl, 3, 2);
        if (!owi->testCRC16(cl)) {
+               for (uint8_t v :cl) printf("%02X ",v);
+               printf("\n");
                owi->log->set(OWLOG_ERROR,"CRC ERROR Convert Command of DS2450");
                return;
        }