Memory read write on DS2438
[owTools.git] / src / owDevice.cpp
index 070ec40..e710102 100755 (executable)
@@ -280,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;