Memory read write on DS2438
authorTobias <tm@tm3d.de>
Fri, 12 May 2017 17:37:18 +0000 (19:37 +0200)
committerTobias <tm@tm3d.de>
Fri, 12 May 2017 17:37:18 +0000 (19:37 +0200)
owTools [new symlink]
src/main.cpp
src/owDevice.cpp
src/owInterface.h

diff --git a/owTools b/owTools
new file mode 120000 (symlink)
index 0000000..5d0e9c5
--- /dev/null
+++ b/owTools
@@ -0,0 +1 @@
+./src/owTools
\ No newline at end of file
index 8bc4452..5595980 100755 (executable)
 #define TC "\e(0\x77\e(B" // 203 Top Cross
 #define VL "\e(0\x78\e(B" // 186 Vertical Line
 #define SP " "                   // space string
+#define BLUE_B "\033[1;34m"
+#define COLOR_E "\033[0m"
+#define C2_B "\033[0;36m"
+#define C3_B "\033[3;34m"
+#define C4_B "\033[1;33m"
+#define C5_B "\033[4;33m"
 #endif
 #ifdef WIN
 #define RB " "// 188 Right Bottom corner
 #define TC  " "// 203 Top Cross
 #define VL  " "// 186 Vertical Line
 #define SP " "                   // space string
+#define BLUE_B ""
+#define C2_B ""
+#define C3_B ""
+#define C4_B ""
+#define COLOR_E ""
+#define C5_B ""
+
+
 #endif
 
 void printhelp() {
@@ -113,6 +127,7 @@ printf("   -f hexfile  Flash new\n");
 printf("   -n change id   \n");
 printf("      -g get from server\n");
 printf("   -w [1|2] show Warnings (1) or all Infos (2)\n");
+printf("   -m memory functions\n");
 
 
 
@@ -215,7 +230,7 @@ void continuous(std::vector<owDevice*> *devices,int intervall,int headline,int s
                                                if (dev->configstate!=OWDCS_NONE) {
                                                        for (size_t i=0;i<4;i++) {
                                                                if (dev->config->getPropertyID((uint8_t)i)!=0)
-                                                                       printf("\033[1;34m%02X.%02X%02X\033[0m\t",dev->getNum().byte[7],dev->getNum().byte[1],dev->getNum().byte[0]);
+                                                                       printf(BLUE_B "%02X.%02X%02X" COLOR_E "\t",dev->getNum().byte[7],dev->getNum().byte[1],dev->getNum().byte[0]);
                                                        }
                                                }
                                        }
@@ -224,7 +239,7 @@ void continuous(std::vector<owDevice*> *devices,int intervall,int headline,int s
                                                if (dev->configstate!=OWDCS_NONE) {
                                                        for (size_t i=0;i<4;i++) {
                                                                if (dev->config->getPropertyID((uint8_t)i)!=0)
-                                                                       printf("\033[0;36m%s\033[0m\t",dev->config->getQuantity((uint8_t)i).substr(0,7).c_str());
+                                                                       printf(C2_B "%s" COLOR_E "\t",dev->config->getQuantity((uint8_t)i).substr(0,7).c_str());
                                                        }
                                                }
                                        }
@@ -235,7 +250,7 @@ void continuous(std::vector<owDevice*> *devices,int intervall,int headline,int s
                                                                if (dev->config->getPropertyID((uint8_t)i)!=0) {
                                                                        size_t l=utf8_length(dev->config->getUnit((uint8_t)i));
                                                                        std::string ls="           ";
-                                                                       printf("\033[3;34m[%s]%s\033[0m\t",dev->config->getUnit((uint8_t)i).c_str(),ls.substr(0,5-l).c_str());
+                                                                       printf(C3_B "[%s]%s" COLOR_E "\t",dev->config->getUnit((uint8_t)i).c_str(),ls.substr(0,5-l).c_str());
                                                                }
                                                        }
                                                }
@@ -319,7 +334,7 @@ void device_menu(owDevice* d) {
        for(int i=0;i<70;i++) printf(HL);printf("\n");
        printf("Selected Device: ");
        snum_t snum=d->getNum();
-       printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
+       printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num);
        printf("  \n");
        printf("Chip Info: ");
        std::vector<std::string> info=d->getFamilyInfo();
@@ -335,7 +350,7 @@ void device_menu(owDevice* d) {
        printf("\n");
        d->convertAll();
        for (size_t i=0;i<d->values.size();i++) {
-               printf("\033[1;33m%0.4f %s\033[0m  ",d->values[i],d->config->getUnit((uint8_t)i).c_str());
+               printf(C4_B "%0.4f %s" COLOR_E,d->values[i],d->config->getUnit((uint8_t)i).c_str());
        }
        printf("\n");
        for(int i=0;i<70;i++) printf(HL);printf("\n");
@@ -365,7 +380,7 @@ int selectDevice() {
                i++;
                snum_t snum=dev->getNum();
                printf("%i) ",i);
-               printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
+               printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num);
 
                printf("  ");
                for (int j=0;j<4;j++) printf("%s ",dev->config->getQuantity(j).c_str());
@@ -379,7 +394,7 @@ int selectDevice() {
                dnr=atoi(inp.c_str());
                if (dnr<=0) return 0;
                if (dnr<=i) break; 
-               printf("\033[4;33mSelect a number between 0 and %i\033[0m\n",i);
+               printf(C5_B "Select a number between 0 and %i" COLOR_E "\n",i);
        }
        return dnr;
 
@@ -507,7 +522,6 @@ int main(int argc, char *argv[]) {
                        }
        else
                if (adapter.find("COM") != std::string::npos) {
-                       printf("USB \n");
                        owi = new owTMEXWIN();
                        int port = atoi(adapter.substr(adapter.find("COM") + 3).c_str());
                        printf("Open Adapter on COM%i\n", port);
@@ -576,7 +590,7 @@ int main(int argc, char *argv[]) {
                owi->Find();
                for (owDevice* dev :owi->devices) {
                        snum_t snum=dev->getNum();
-                       printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
+                       printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num);
 
                        printf("\n");
                }
@@ -595,7 +609,65 @@ int main(int argc, char *argv[]) {
                cl.push_back(0x1F);
                dev->Communicate(&cl,4,0);
        } else 
+       //------------------------------------------------------------------------------------------
+       // --------------------- Memory functions
+       //------------------------------------------------------------------------------------------
        
+       if ((s=getArg("m"))!="") {
+               int pnr=0;
+               std::vector<uint8_t> mem;
+               if(s.find("read")!=std::string::npos) {
+                       size_t cla=s.find_first_of("(",0);
+                       size_t page=s.find_first_of("p=",cla);
+                       size_t epage=s.find_first_of(";",page);
+                       if (epage==std::string::npos) epage=s.find_first_of(")",page);
+                       std::string pn=s.substr(page+2,epage-page-2);
+                       printf("%s Read Memory Page %s of Device:\n",s.c_str(),pn.c_str());
+                       pnr=atoi(pn.c_str());
+
+               } else 
+               if(s.find("write")!=std::string::npos) {
+                       size_t cla=s.find("(",0);
+                       size_t page=s.find("p=",cla);
+                       size_t epage=s.find(";",page);
+                       if (epage==std::string::npos) epage=s.find_first_of(")",page);
+                       std::string pn=s.substr(page+2,epage-page-2);
+                       size_t data=s.find("d=",cla);
+                       size_t edata=s.find(";",data);
+                       if (edata==std::string::npos) edata=s.find(")",data);
+                       std::string pd=s.substr(data+2,edata-data-2);
+                       std::istringstream pdss(pd);
+                       //std::cout << pd <<std::endl;
+                       while (!pdss.eof()) {
+                               int v;
+                               pdss>>std::hex>>v;
+                               mem.push_back(v);
+                       }
+                       printf("Write ");
+                       for(uint8_t v :mem) printf("%02X ",v);
+                       printf("to Memory Page %s of Device:\n",pn.c_str());
+                       pnr=atoi(pn.c_str());
+
+               } else exit(1);
+               int sel=selectDevice();
+               if (sel==0) exit(0);
+               sel-=1;
+               //snum_t snum=owi->devices[sel]->getNum();
+               owDevice* dev=owi->devices[sel];
+               if(s.find("read")!=std::string::npos) {
+                       int r=dev->readMemory(pnr,0,8,&mem);
+                       for(int i=0;i<r;i++) printf("%02X ",mem[i]);
+                       printf("\n");
+                       exit(0);
+               } else 
+               if(s.find("write")!=std::string::npos) {
+                       int r=dev->writeMemory(pnr,0,8,&mem);
+                       r=dev->readMemory(pnr,0,8,&mem);
+                       for(int i=0;i<r;i++) printf("%02X ",mem[i]);
+                       printf("\n");
+                       exit(0);
+               }
+       }
        if ((s=getArg("n"))!="") {
                printf("Change id of selected Device\n");
                int sel=selectDevice();
@@ -652,7 +724,7 @@ int main(int argc, char *argv[]) {
 
 
                printf("New ID: ");
-               printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
+               printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num);
 
                printf("\n");
                
@@ -662,7 +734,7 @@ int main(int argc, char *argv[]) {
                owi->Find();
                for (owDevice* dev :owi->devices) {
                        snum_t snum=dev->getNum();
-                       printf("\033[1;34m%016llX\033[0m",(unsigned long long)snum.num);
+                       printf(BLUE_B "%016llX" COLOR_E,(unsigned long long)snum.num);
 
                        printf("\n");
                }
@@ -673,7 +745,7 @@ int main(int argc, char *argv[]) {
                for (owDevice* dev :owi->devices) {
                        printf("_______________________________________________________________________\n");
                        snum_t snum=dev->getNum();
-                       printf("\033[1;34m%016llX\033[0m ",(unsigned long long)snum.num);
+                       printf(BLUE_B "%016llX " COLOR_E,(unsigned long long)snum.num);
                        switch (dev->configstate) {
                                case OWDCS_16:printf("old Configcode from www.tm3d.de");break;
                                case OWDCS_24:printf("Configcode from www.tm3d.de");break;
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;
index 0370d03..415c5dd 100755 (executable)
@@ -84,6 +84,29 @@ class owDeviceConfig;
 #define OWLOG_WARNING 2
 #define OWLOG_ERROR 3
 
+#ifdef LINUX
+
+#define BLUE_B "\033[1;34m"
+#define COLOR_E "\033[0m"
+#define C2_B "\033[0;36m"
+#define C3_B "\033[3;34m"
+#define C4_B "\033[1;33m"
+#define C5_B "\033[4;33m"
+#define CLE_B "\033[1;31m"
+#define CLW_B "\033[1;33m"
+#endif
+#ifdef WIN
+#define BLUE_B ""
+#define C2_B ""
+#define C3_B ""
+#define C4_B ""
+#define COLOR_E ""
+#define C5_B ""
+#define CLE_B ""
+#define CLW_B ""
+
+#endif
+
 class owLog {
        int lcount;
        std::string logtext;
@@ -111,8 +134,8 @@ public:
                level=llevel;
                lcount++;
                if (level>=loglevel) {
-                       if (loglevel==OWLOG_ERROR) printf("\033[1;31m%s\033[0m\n",s);
-                       else if (loglevel==OWLOG_WARNING) printf("\033[1;33m%s\033[0m\n",s);
+                       if (loglevel==OWLOG_ERROR) printf(CLE_B "%s" COLOR_E "\n",s);
+                       else if (loglevel==OWLOG_WARNING) printf(CLW_B "%s" COLOR_E "\n",s);
                        else printf("%s\n",s);
                }
                return done;
@@ -262,6 +285,8 @@ public:
        void changeID(snum_t nid);
        void runFlasher();
        virtual int convertAll() {return 1;}
+       virtual int readMemory(int page,int start, int count,std::vector<uint8_t> *data) {return 0;};
+       virtual int writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {return 0;};
        virtual ~owDevice() {}
 };
 
@@ -281,7 +306,8 @@ public:
 
        int setConfigByte(uint8_t cb);
        int readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall);
-
+       virtual int readMemory(int page,int start, int count,std::vector<uint8_t> *data);
+       virtual int writeMemory(int page,int start, int count,std::vector<uint8_t> *data);
        virtual int convertAll();
 
 } ;