New Devices
[owTools.git] / src / owDevice.cpp
1 <<<<<<< HEAD
2 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de\r
3 // All rights reserved.\r
4 //\r
5 // Redistribution and use in source and binary forms, with or without\r
6 // modification, are permitted provided that the following conditions are\r
7 // met:\r
8 //\r
9 //  * Redistributions of source code must retain the above copyright\r
10 //    notice, this list of conditions and the following disclaimer.\r
11 //  * Redistributions in binary form must reproduce the above copyright\r
12 //    notice, this list of conditions and the following disclaimer in the\r
13 //    documentation and/or other materials provided with the\r
14 //    distribution.\r
15 //  * All advertising materials mentioning features or use of this\r
16 //    software must display the following acknowledgement: This product\r
17 //    includes software developed by tm3d.de and its contributors.\r
18 //  * Neither the name of tm3d.de nor the names of its contributors may\r
19 //    be used to endorse or promote products derived from this software\r
20 //    without specific prior written permission.\r
21 //\r
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
33 \r
34 \r
35 #include "owInterface.h"\r
36 #ifdef LINUX\r
37 #include <unistd.h>\r
38 #endif\r
39 #ifdef WIN\r
40 =======
41 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
42 // All rights reserved.
43 //
44 // Redistribution and use in source and binary forms, with or without
45 // modification, are permitted provided that the following conditions are
46 // met:
47 //
48 //  * Redistributions of source code must retain the above copyright
49 //    notice, this list of conditions and the following disclaimer.
50 //  * Redistributions in binary form must reproduce the above copyright
51 //    notice, this list of conditions and the following disclaimer in the
52 //    documentation and/or other materials provided with the
53 //    distribution.
54 //  * All advertising materials mentioning features or use of this
55 //    software must display the following acknowledgement: This product
56 //    includes software developed by tm3d.de and its contributors.
57 //  * Neither the name of tm3d.de nor the names of its contributors may
58 //    be used to endorse or promote products derived from this software
59 //    without specific prior written permission.
60 //
61 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
62 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
63 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
64 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
65 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
66 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
67 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
68 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
69 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
71 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72
73
74 #include "owInterface.h"
75 #ifdef LINUX
76 #include <unistd.h>
77 #endif
78 #ifdef WIN
79 >>>>>>> origin/master
80 void usleep(int waitTime) {\r
81         __int64 time1 = 0, time2 = 0, freq = 0;\r
82 \r
83         QueryPerformanceCounter((LARGE_INTEGER *)&time1);\r
84         QueryPerformanceFrequency((LARGE_INTEGER *)&freq);\r
85 \r
86         do {\r
87                 QueryPerformanceCounter((LARGE_INTEGER *)&time2);\r
88         } while ((time2 - time1) < waitTime);\r
89 <<<<<<< HEAD
90 }\r
91 #endif\r
92 owDevice::owDevice(owInterface *owi_,snum_t num) {\r
93                 snum=num;\r
94                 owi=owi_;\r
95                 config=new owDeviceConfig;\r
96                 configstate=OWDCS_NONE;\r
97                 raw.insert(raw.begin(),4,0);\r
98                 values.insert(values.begin(),4,0);\r
99                 lastfound=0;\r
100         }\r
101         \r
102 void owDevice::setDefaultConfig() {\r
103                 config->setInvalid();\r
104         }\r
105         \r
106 std::vector<std::string> owDevice::getFamilyInfo() {\r
107                 return owDeviceConfig::getFamilyInfo(snum.byte[0]);\r
108         }\r
109 int owDevice::readConfig() {\r
110         std::vector<uint8_t> cl;\r
111         int r=owi->maxrepeat+1;\r
112         setDefaultConfig();\r
113         int conf16=0;\r
114         do {\r
115                 owi->log->clear();\r
116                 cl.clear();\r
117                 cl.push_back(0x85);\r
118                 Communicate(&cl, 1, 26);\r
119                 //for (uint8_t v:cl) printf("%02X ",v); \r
120                 if ((cl[1]==0xFF)||(cl[2]==0xFF)) {\r
121                         owi->log->set(OWLOG_INFO,"Get Config not work, maybe not a emulation from www.tm3d.de");\r
122                         return 0;\r
123                 }\r
124                 int oldInfo=1;\r
125                 //all Code pos +1 \r
126                 for(int i=19;i<27;i++) if (cl[i]!=0xFF) {oldInfo=0;break;} \r
127                 if (oldInfo) {\r
128                         owi->log->set(OWLOG_INFO,"Old 16 Byte Config");\r
129                         if (cl[18]==0xFF) { \r
130                                 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+17))) {\r
131                                         owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");\r
132                                 }\r
133                         } else {\r
134                                 if (!owi->testCRC16(cl)) {\r
135                                         owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");\r
136                                 }\r
137                         }\r
138                         if (owi->log->last()<OWLOG_WARNING) {\r
139                                 cl[9]=2; //Attiny84A\r
140                                 for(int i=0;i<7;i++) {cl[18+i]=cl[10+i];cl[10+i]=0;} //Move OWID2 Set Sensor to 0\r
141                                 conf16=1;\r
142 \r
143                         }\r
144                 } else { //New CRC\r
145                         if (cl[26]==0xFF) { \r
146                                 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25))) {\r
147                                         owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");\r
148                                 }\r
149                         } else {\r
150                                 if (!owi->testCRC16(cl)) {\r
151                                         owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");\r
152                                 }\r
153                         }\r
154                 }               \r
155                 r--;\r
156         }\r
157         while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));\r
158         if (r==0) {\r
159                 owi->log->set(OWLOG_ERROR,"To much Errors while read config");\r
160                 return -1;\r
161         }\r
162         config->setConfig(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25));\r
163         if (config->valid) {\r
164                 if (conf16) configstate=OWDCS_16; else configstate=OWDCS_24;\r
165         } else configstate=OWDCS_NONE;\r
166         return 1;\r
167 }\r
168 \r
169 \r
170 int owDevice::Communicate(std::vector<uint8_t> *data, int scount, int rcount) {\r
171         owi->Wait_Free();\r
172         owi->MatchRom(snum);\r
173         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}\r
174         owi->Communicate(data, scount, rcount);  \r
175         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}\r
176         owi->Free();\r
177         return 1;\r
178 }\r
179 \r
180 int owDevice::CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount) {\r
181         owi->Wait_Free();\r
182         owi->Reset();\r
183         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}\r
184         owi->Communicate(data, scount, rcount);\r
185         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}\r
186         owi->Free();\r
187         return 1;\r
188 }\r
189 \r
190 \r
191 void owDevice::changeID(snum_t nid) {\r
192         int i;\r
193         if (nid.byte[0] != snum.byte[0]) {\r
194                 owi->log->set(OWLOG_ERROR,"Family ID should not be changed, no correct ID");\r
195                 return;\r
196         }\r
197         std::vector<uint8_t> id;\r
198         for(i=0;i<7;i++) id.push_back(nid.byte[i]);\r
199         id.push_back(owi->calcCRC8(id));\r
200         std::vector<uint8_t> cl;\r
201         int r=owi->maxrepeat+1;\r
202         do {\r
203                 owi->log->clear();\r
204                 cl.clear();\r
205                 cl.push_back(0x75);\r
206                 cl.insert(cl.begin()+1,id.begin(),id.end());\r
207                 Communicate(&cl, 9,0);\r
208                 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}\r
209                 cl.clear();\r
210                 cl.push_back(0xA7);\r
211                 Communicate(&cl, 1, 8);\r
212                 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}\r
213                 for (i = 0; i < 8; i++) {\r
214                         if (cl[i + 1] != id[i]) {\r
215                                 owi->log->set(OWLOG_WARNING,"changeID Comunication ERROR");\r
216                                 break;\r
217                         }\r
218                 }\r
219                 r--;\r
220         }\r
221         while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));\r
222         if (r==0) {\r
223                 owi->log->set(OWLOG_ERROR,"Can not change ID");\r
224                 return;\r
225         }       \r
226         cl.clear();\r
227         cl.push_back(0x79);\r
228         cl.push_back(snum.byte[1]);\r
229         cl.push_back(snum.byte[5]);\r
230         cl.push_back(snum.byte[6]);\r
231         Communicate(&cl, 4, 0);\r
232         usleep(50000);  \r
233 }\r
234 \r
235 void owDevice::runFlasher() {\r
236         std::vector<uint8_t> cl;\r
237         cl.push_back(0x88);\r
238         this->Communicate(&cl, 1, 0);\r
239         this->Communicate(&cl, 1, 0);\r
240         this->Communicate(&cl, 1, 0);\r
241 }\r
242 \r
243 \r
244 \r
245 \r
246 \r
247 \r
248 \r
249 \r
250 \r
251 void owDeviceDS18B20::setDefaultConfig() {\r
252         config->setConfig({1,1, 0,0, 0,0, 0,0, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0});\r
253         configstate=OWDCS_DEFAULT;\r
254 };\r
255 \r
256 int owDeviceDS18B20::readScratchpad(std::vector<uint8_t> *sp) {\r
257         std::vector<uint8_t> cl;\r
258         int r=owi->maxrepeat+1;\r
259         do {\r
260                 cl.clear();\r
261                 cl.push_back(0xBE);\r
262                 Communicate(&cl, 1, 9);\r
263                 if (owi->log->last()>=OWLOG_ERROR) return -1;\r
264                 sp->clear();\r
265                 sp->insert(sp->begin(),cl.begin()+1,cl.end());\r
266                 if (owi->calcCRC8(*sp)==0) return 1;\r
267                 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS18B20 Scrachpad");\r
268                 r--;\r
269         } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));\r
270                         \r
271         return 0;\r
272 }\r
273 \r
274 int owDeviceDS18B20::convertAll() {\r
275         std::vector<uint8_t> sp;\r
276         sp.push_back(0x44);\r
277         Communicate(&sp, 1, 0); ///########################################################\r
278         usleep(750000);\r
279 \r
280         if (readScratchpad(&sp)) {\r
281                 int16_t tsht;\r
282                 tsht = sp[0] | ((int)sp[1] << 8);\r
283                 if (sp[1] & 0x080)\r
284                         tsht |= 0xFFFFF0000;\r
285                 raw[0]=tsht;\r
286                 values[0]=config->calculateValue(0, raw);\r
287                         return 1;\r
288         }               \r
289         return 0;\r
290 }\r
291 \r
292 \r
293 void owDeviceDS2438::setDefaultConfig() {\r
294         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});\r
295         configstate=OWDCS_DEFAULT;\r
296 }\r
297 \r
298 \r
299 int owDeviceDS2438::readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall) {\r
300         std::vector<uint8_t> cl;\r
301         int r=owi->maxrepeat+1;\r
302         do {\r
303                 if (recall) {\r
304                         cl.clear();\r
305                         cl.push_back(0xB8); //recall\r
306                         cl.push_back(page);\r
307                         Communicate(&cl, 2, 0);\r
308                         if (owi->log->last()>=OWLOG_ERROR) return -1;\r
309                 }\r
310                 cl.clear();\r
311                 cl.push_back(0xBE);\r
312                 cl.push_back(page);\r
313                 Communicate(&cl, 2, 9);\r
314                 if (owi->log->last()>=OWLOG_ERROR) return -1;\r
315                 sp->clear();\r
316                 sp->insert(sp->begin(),cl.begin()+2,cl.end());\r
317                 if (owi->calcCRC8(*sp)==0) return 1;\r
318                 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");\r
319                 r--;\r
320         } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));\r
321                         \r
322         return 0;\r
323 }\r
324 \r
325 int owDeviceDS2438::readMemory(int page,int start, int count,std::vector<uint8_t> *data) {\r
326         if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;\r
327         std::vector<uint8_t> cl;\r
328         cl.clear();\r
329         cl.push_back(0xB8); //recall\r
330         cl.push_back(page);\r
331         Communicate(&cl, 2, 0);\r
332         if (owi->log->last()>=OWLOG_ERROR) return -1;\r
333         cl.clear();\r
334         cl.push_back(0xBE);\r
335         cl.push_back(page);\r
336         Communicate(&cl, 2, 9);\r
337         if (owi->log->last()>=OWLOG_ERROR) return -1;\r
338         cl.erase(cl.begin());\r
339         cl.erase(cl.begin());\r
340         if (owi->calcCRC8(cl)!=0) {\r
341                 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");\r
342         }\r
343         data->clear();\r
344         data->insert(data->begin(),cl.begin()+start,cl.begin()+start+count);\r
345         return count;\r
346 }\r
347 int owDeviceDS2438::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {\r
348         if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;\r
349         std::vector<uint8_t> sp;\r
350         if ((start>0)||(count<8)) {\r
351                 readMemory(page,0,8,&sp);\r
352         }\r
353         std::vector<uint8_t> cl;\r
354         cl.push_back(0x4E); \r
355         cl.push_back(page);\r
356         int j=0;\r
357         for(int i=0;i<8;i++) {\r
358                 if ((i<start)||(i>=start+count)) cl.push_back(sp[i]); else {cl.push_back((*data)[j]);j++;}\r
359         }\r
360         Communicate(&cl, 10,0);\r
361         if (owi->log->last()>=OWLOG_ERROR) return -1;\r
362         std::vector<uint8_t> cl1;\r
363         cl1.push_back(0xBE);\r
364         cl1.push_back(page);\r
365         Communicate(&cl1, 2, 9);\r
366         if (owi->log->last()>=OWLOG_ERROR) return -1;\r
367         cl1.erase(cl1.begin());\r
368         cl1.erase(cl1.begin());\r
369         if (owi->calcCRC8(cl1)!=0) {\r
370                 owi->log->set(OWLOG_WARNING,"CRC ERROR rereading DS2438 Scrachpad");\r
371         }\r
372         for(int i=0;i<8;i++) {\r
373                 if (cl1[i]!=cl[i+2]) {\r
374                         owi->log->set(OWLOG_ERROR,"Reread not equal, nothing copied");\r
375                         return 0; \r
376                 }\r
377         }\r
378         cl.clear();\r
379         cl.push_back(0x48);\r
380         cl.push_back(page);\r
381         Communicate(&cl, 2, 0);\r
382         if (owi->log->last()>=OWLOG_ERROR) return -1;\r
383         return 1;\r
384 }\r
385 \r
386 \r
387 \r
388 int owDeviceDS2438::setConfigByte(uint8_t cb) {\r
389         std::vector<uint8_t> sp;\r
390         int k;\r
391         for(k=0;k<owi->maxrepeat;k++) {\r
392                 owi->log->clear();\r
393                 sp.clear();\r
394                 sp.push_back(0x4E);\r
395                 sp.push_back(0x00);\r
396                 sp.push_back(cb);\r
397                 Communicate(&sp,3,0);\r
398                 if (owi->log->last()>=OWLOG_ERROR) return -1;\r
399                 sp.clear();\r
400                 readScratchpad(&sp,0,0);\r
401                 if (owi->log->last()>=OWLOG_ERROR) return -2;\r
402                 if (cb==sp[0]) return 1;\r
403                 owi->log->set(OWLOG_WARNING,"ERROR set config byte of DS2438");\r
404         }\r
405         owi->log->set(OWLOG_WARNING,"Config of DS2438 byte not set");\r
406         return 0;\r
407 }\r
408 \r
409 inline int16_t ow_fconvert(uint8_t b1, uint16_t b2) {\r
410   int tsht;\r
411   tsht=b1  |((int)b2<<8);\r
412   if (b2 & 0x080)\r
413         tsht |= 0xFFFFF0000;\r
414   return tsht;\r
415 }\r
416 \r
417 int owDeviceDS2438::convertAll() {\r
418         for(int k=0;k<owi->maxrepeat;k++)  {\r
419                 std::vector<uint8_t> sp;\r
420                 sp.push_back(0x44);\r
421                 Communicate(&sp, 1, 0);///########################################################\r
422                 if (owi->log->last()>=OWLOG_ERROR) continue;\r
423                 usleep(100000);\r
424                 if (setConfigByte(0x08)<=0) continue;\r
425                 for(int k=0;k<owi->maxrepeat;k++) {\r
426                         sp.clear();\r
427                         sp.push_back(0xB4);\r
428                         Communicate(&sp, 1, 0);\r
429                 }\r
430                 if (owi->log->last()>=OWLOG_ERROR) continue;\r
431                 usleep(100000);\r
432 \r
433 \r
434                 readScratchpad(&sp,0,1);\r
435                 int temp=ow_fconvert(sp[1],sp[2]);\r
436                 int VDD=ow_fconvert(sp[3],sp[4]);\r
437                 if (setConfigByte(0x00)<=0) continue;\r
438 \r
439                 for(int k=0;k<owi->maxrepeat;k++) {\r
440                         sp.clear();\r
441                         sp.push_back(0xB4);\r
442                         Communicate(&sp, 1, 0);\r
443                 }\r
444                 if (owi->log->last()>=OWLOG_ERROR) continue;\r
445 \r
446                 usleep(100000);\r
447 \r
448 \r
449 \r
450                 readScratchpad(&sp,0,1);\r
451                 if (owi->log->last()>=OWLOG_ERROR) continue;\r
452                 int I=ow_fconvert(sp[5],sp[6]);\r
453                 int VAD=ow_fconvert(sp[3],sp[4]);\r
454                 raw[0]=temp;\r
455                 raw[1]=VDD;\r
456                 raw[2]=VAD;\r
457                 raw[3]=I;\r
458                 for(int i=0;i<4;i++) values[i]=config->calculateValue(i, raw);\r
459                 break;\r
460         }\r
461         if (owi->log->last()>=OWLOG_ERROR) return 0;\r
462         return 1;\r
463 }\r
464 \r
465 \r
466 void owDeviceDS2450::setDefaultConfig() {\r
467         config->setConfig({6,9, 6,9, 6,9, 6,9, 0,5,5,5,5,0,0,0,0,0,0,0,0,0,0,0});\r
468         configstate=OWDCS_DEFAULT;\r
469 }\r
470 \r
471 int owDeviceDS2450::convertAll() {\r
472         convert(0x0F,0);\r
473         if (owi->log->last()>=OWLOG_WARNING) {\r
474                 return 0;\r
475         }\r
476         std::vector<uint8_t> sp;\r
477         readMemory(0,&sp);\r
478         if (owi->log->last()>=OWLOG_WARNING) {\r
479                 return 0;\r
480         }\r
481 \r
482         for(int i=0;i<4;i++) {\r
483                 raw[i]=(sp[2 * i] | sp[2 * i + 1] << 8);\r
484         }\r
485         for(int i=0;i<4;i++) {\r
486                 values[i]=config->calculateValue(i, raw);\r
487         }\r
488         return 1;\r
489 \r
490 }\r
491 \r
492 void owDeviceDS2450::readMemory(uint8_t addr,std::vector<uint8_t> *sp) {\r
493         std::vector<uint8_t> cl;\r
494         cl.push_back(0xAA);\r
495         cl.push_back(addr);\r
496         cl.push_back(0x00);\r
497         Communicate(&cl,3,10-addr);\r
498         if (owi->log->last()>=OWLOG_WARNING) {\r
499                 return;\r
500         }\r
501         if (!owi->testCRC16(cl)) {\r
502                 for (uint8_t v :cl) printf("%02X ",v);\r
503                 printf("\n");\r
504                 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Memory of DS2450");\r
505                 return;\r
506         }\r
507         sp->clear();\r
508         sp->insert(sp->begin(),cl.begin()+3,cl.end()-2);\r
509 }\r
510 \r
511 void owDeviceDS2450::writeMemory(uint8_t addr,std::vector<uint8_t> *sp) {\r
512         std::vector<uint8_t> cl;\r
513         cl.push_back(0x55);\r
514         cl.push_back(addr);\r
515         cl.push_back(0x00);\r
516         owi->MatchRom(snum);\r
517         if (owi->log->last()>=OWLOG_WARNING) {\r
518                 return;\r
519         }\r
520         for(uint8_t b: (*sp)) {\r
521                 cl.push_back(b);\r
522                 owi->Communicate(&cl,cl.size(),2);\r
523                 if (owi->log->last()>=OWLOG_WARNING) {\r
524                         return;\r
525                 }\r
526                 if (!owi->testCRC16(cl)) {\r
527                         owi->log->set(OWLOG_ERROR,"CRC ERROR Writing Memory of DS2450");\r
528                         return;\r
529                 }\r
530                 cl.clear();\r
531                 owi->Communicate(&cl, 0, 1);\r
532                 if (owi->log->last()>=OWLOG_WARNING) {\r
533                         return;\r
534                 }\r
535                 if (cl[0] != b) {\r
536                         owi->log->set(OWLOG_ERROR,"ERROR Writing Memory of DS2450");\r
537                         return;\r
538                 }\r
539                 cl.clear();\r
540         }\r
541 }\r
542         \r
543 void owDeviceDS2450::convert(uint8_t mask, uint8_t preset) {\r
544         std::vector<uint8_t> cl;\r
545         cl.push_back(0x3C);\r
546         cl.push_back(mask);\r
547         cl.push_back(preset);\r
548         Communicate(&cl, 3, 2);\r
549         if (!owi->testCRC16(cl)) {\r
550                 for (uint8_t v :cl) printf("%02X ",v);\r
551                 printf("\n");\r
552                 owi->log->set(OWLOG_ERROR,"CRC ERROR Convert Command of DS2450");\r
553                 return;\r
554         }\r
555         usleep(10000);\r
556 }\r
557 \r
558 \r
559 \r
560 \r
561 \r
562 \r
563 \r
564 \r
565 \r
566 \r
567 \r
568 void owDeviceDS2423::setDefaultConfig() {\r
569         config->setConfig({9,13, 9,13, 9,13, 9,13, 0,19,19,19,19,0,0,0,0,0,0,0,0,0,0,0});\r
570         configstate=OWDCS_DEFAULT;\r
571 }\r
572 \r
573 int owDeviceDS2423::convertAll() {\r
574         for(uint8_t i=12;i<16;i++) {\r
575                 raw[i-12]=readCounter(i);\r
576         }\r
577         for(int i=0;i<4;i++) {\r
578                 values[i]=config->calculateValue(i, raw);\r
579         }\r
580         return 1;\r
581 }\r
582 uint32_t owDeviceDS2423::readCounter(uint8_t page) {\r
583         std::vector<uint8_t> cl;\r
584         cl.push_back(0xA5);\r
585         uint16_t addr=(page<<5)+31;\r
586         cl.push_back(addr&0xFF);\r
587         cl.push_back(addr>>8);\r
588         Communicate(&cl,3,11);\r
589         //for (uint8_t v :cl) printf("%02X ",v);\r
590         //printf("\n");\r
591         if (!owi->testCRC16(cl)) {\r
592                 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Counter of DS2423");\r
593                 return 0;\r
594         }\r
595         uint32_t ret=0;\r
596          \r
597         for(size_t i=cl.size()-7;i>cl.size()-11;i--) {\r
598                 ret<<=8;\r
599                 ret|=cl[i];\r
600                 \r
601         }\r
602         return ret;\r
603 }\r
604 \r
605 \r
606 int owDeviceDS2423::readMemory(int page,int start, int count,std::vector<uint8_t> *data){\r
607         std::vector<uint8_t> cl;\r
608         cl.push_back(0xF0);\r
609         uint16_t adr=page<<5;\r
610         cl.push_back(adr&0xFF);\r
611         cl.push_back((adr>>8)&0xFF);\r
612         Communicate(&cl, 3, 32);\r
613         //No CRC \r
614         data->clear();\r
615         data->insert(data->begin(),cl.begin()+3+start,cl.begin()+start+3+count);\r
616         return count;   \r
617 \r
618 }\r
619 int owDeviceDS2423::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {\r
620         std::vector<uint8_t> cl;\r
621         cl.push_back(0x0F);\r
622         uint16_t adr=(page<<5)+start;\r
623         cl.push_back(adr&0xFF);\r
624         cl.push_back(adr>>8);\r
625         if (count > (int)(*data).size()) count = (*data).size();\r
626         for(int i=0;i<count;i++) {\r
627                 cl.push_back((*data)[i]);\r
628         }\r
629         Communicate(&cl, 3+count, 0);\r
630         for (uint8_t v :cl) printf("%02X ",v);printf("\n");\r
631         cl.clear();\r
632         cl.push_back(0xAA);\r
633         Communicate(&cl, 1, 36);\r
634         //for (uint8_t v :cl) printf("%02X ",v);;printf("\n");\r
635         cl[0]=0x5A;\r
636         Communicate(&cl, 4, 1);\r
637         usleep(100000);\r
638         if (cl[4]!=0xAA) {\r
639                 for (uint8_t v :cl) printf("%02X ",v);;printf("\n");\r
640                 owi->log->set(OWLOG_ERROR,"DS2423 Copy from Scratchpad to Memmory Error");\r
641                 //return 0;\r
642         }\r
643         return count;\r
644 }\r
645 \r
646 \r
647 \r
648 \r
649 \r
650 \r
651 =======
652 }
653 #endif
654 owDevice::owDevice(owInterface *owi_,snum_t num) {
655                 snum=num;
656                 owi=owi_;
657                 config=new owDeviceConfig;
658                 configstate=OWDCS_NONE;
659                 raw.insert(raw.begin(),4,0);
660                 values.insert(values.begin(),4,0);
661                 lastfound=0;
662         }
663         
664 void owDevice::setDefaultConfig() {
665                 config->setInvalid();
666         }
667         
668 std::vector<std::string> owDevice::getFamilyInfo() {
669                 return owDeviceConfig::getFamilyInfo(snum.byte[0]);
670         }
671 int owDevice::readConfig() {
672         std::vector<uint8_t> cl;
673         int r=owi->maxrepeat+1;
674         setDefaultConfig();
675         int conf16=0;
676         do {
677                 owi->log->clear();
678                 cl.clear();
679                 cl.push_back(0x85);
680                 Communicate(&cl, 1, 26);
681                 //for (uint8_t v:cl) printf("%02X ",v); 
682                 if ((cl[1]==0xFF)||(cl[2]==0xFF)) {
683                         owi->log->set(OWLOG_INFO,"Get Config not work, maybe not a emulation from www.tm3d.de");
684                         return 0;
685                 }
686                 int oldInfo=1;
687                 //all Code pos +1 
688                 for(int i=19;i<27;i++) if (cl[i]!=0xFF) {oldInfo=0;break;} 
689                 if (oldInfo) {
690                         owi->log->set(OWLOG_INFO,"Old 16 Byte Config");
691                         if (cl[18]==0xFF) { 
692                                 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+17))) {
693                                         owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
694                                 }
695                         } else {
696                                 if (!owi->testCRC16(cl)) {
697                                         owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
698                                 }
699                         }
700                         if (owi->log->last()<OWLOG_WARNING) {
701                                 cl[9]=2; //Attiny84A
702                                 for(int i=0;i<7;i++) {cl[18+i]=cl[10+i];cl[10+i]=0;} //Move OWID2 Set Sensor to 0
703                                 conf16=1;
704
705                         }
706                 } else { //New CRC
707                         if (cl[26]==0xFF) { 
708                                 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25))) {
709                                         owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
710                                 }
711                         } else {
712                                 if (!owi->testCRC16(cl)) {
713                                         owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
714                                 }
715                         }
716                 }               
717                 r--;
718         }
719         while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
720         if (r==0) {
721                 owi->log->set(OWLOG_ERROR,"To much Errors while read config");
722                 return -1;
723         }
724         config->setConfig(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25));
725         if (config->valid) {
726                 if (conf16) configstate=OWDCS_16; else configstate=OWDCS_24;
727         } else configstate=OWDCS_NONE;
728         return 1;
729 }
730
731
732 int owDevice::Communicate(std::vector<uint8_t> *data, int scount, int rcount) {
733         owi->Wait_Free();
734         owi->MatchRom(snum);
735         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
736         owi->Communicate(data, scount, rcount);  
737         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
738         owi->Free();
739         return 1;
740 }
741
742 int owDevice::CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount) {
743         owi->Wait_Free();
744         owi->Reset();
745         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
746         owi->Communicate(data, scount, rcount);
747         if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
748         owi->Free();
749         return 1;
750 }
751
752
753 void owDevice::changeID(snum_t nid) {
754         int i;
755         if (nid.byte[0] != snum.byte[0]) {
756                 owi->log->set(OWLOG_ERROR,"Family ID should not be changed, no correct ID");
757                 return;
758         }
759         std::vector<uint8_t> id;
760         for(i=0;i<7;i++) id.push_back(nid.byte[i]);
761         id.push_back(owi->calcCRC8(id));
762         std::vector<uint8_t> cl;
763         int r=owi->maxrepeat+1;
764         do {
765                 owi->log->clear();
766                 cl.clear();
767                 cl.push_back(0x75);
768                 cl.insert(cl.begin()+1,id.begin(),id.end());
769                 Communicate(&cl, 9,0);
770                 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
771                 cl.clear();
772                 cl.push_back(0xA7);
773                 Communicate(&cl, 1, 8);
774                 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
775                 for (i = 0; i < 8; i++) {
776                         if (cl[i + 1] != id[i]) {
777                                 owi->log->set(OWLOG_WARNING,"changeID Comunication ERROR");
778                                 break;
779                         }
780                 }
781                 r--;
782         }
783         while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
784         if (r==0) {
785                 owi->log->set(OWLOG_ERROR,"Can not change ID");
786                 return;
787         }       
788         cl.clear();
789         cl.push_back(0x79);
790         cl.push_back(snum.byte[1]);
791         cl.push_back(snum.byte[5]);
792         cl.push_back(snum.byte[6]);
793         Communicate(&cl, 4, 0);
794         usleep(50000);  
795 }
796
797 void owDevice::runFlasher() {
798         std::vector<uint8_t> cl;
799         cl.push_back(0x88);
800         this->Communicate(&cl, 1, 0);
801         this->Communicate(&cl, 1, 0);
802         this->Communicate(&cl, 1, 0);
803 }
804
805
806
807
808
809
810
811
812
813 void owDeviceDS18B20::setDefaultConfig() {
814         config->setConfig({1,1, 0,0, 0,0, 0,0, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
815         configstate=OWDCS_DEFAULT;
816 };
817
818 int owDeviceDS18B20::readScratchpad(std::vector<uint8_t> *sp) {
819         std::vector<uint8_t> cl;
820         int r=owi->maxrepeat+1;
821         do {
822                 cl.clear();
823                 cl.push_back(0xBE);
824                 Communicate(&cl, 1, 9);
825                 if (owi->log->last()>=OWLOG_ERROR) return -1;
826                 sp->clear();
827                 sp->insert(sp->begin(),cl.begin()+1,cl.end());
828                 if (owi->calcCRC8(*sp)==0) return 1;
829                 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS18B20 Scrachpad");
830                 r--;
831         } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
832                         
833         return 0;
834 }
835
836 int owDeviceDS18B20::convertAll() {
837         std::vector<uint8_t> sp;
838         //sp.push_back(0x44);
839         //communicate(&sp, 1, 0); ///########################################################
840         usleep(750000);
841
842         if (readScratchpad(&sp)) {
843                 int16_t tsht;
844                 tsht = sp[0] | ((int)sp[1] << 8);
845                 if (sp[1] & 0x080)
846                         tsht |= 0xFFFFF0000;
847                 raw[0]=tsht;
848                 values[0]=config->calculateValue(0, raw);
849                         return 1;
850         }               
851         return 0;
852 }
853
854
855 void owDeviceDS2438::setDefaultConfig() {
856         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});
857         configstate=OWDCS_DEFAULT;
858 }
859
860
861 int owDeviceDS2438::readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall) {
862         std::vector<uint8_t> cl;
863         int r=owi->maxrepeat+1;
864         do {
865                 if (recall) {
866                         cl.clear();
867                         cl.push_back(0xB8); //recall
868                         cl.push_back(page);
869                         Communicate(&cl, 2, 0);
870                         if (owi->log->last()>=OWLOG_ERROR) return -1;
871                 }
872                 cl.clear();
873                 cl.push_back(0xBE);
874                 cl.push_back(page);
875                 Communicate(&cl, 2, 9);
876                 if (owi->log->last()>=OWLOG_ERROR) return -1;
877                 sp->clear();
878                 sp->insert(sp->begin(),cl.begin()+2,cl.end());
879                 if (owi->calcCRC8(*sp)==0) return 1;
880                 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
881                 r--;
882         } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
883                         
884         return 0;
885 }
886
887 int owDeviceDS2438::readMemory(int page,int start, int count,std::vector<uint8_t> *data) {
888         if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
889         std::vector<uint8_t> cl;
890         cl.clear();
891         cl.push_back(0xB8); //recall
892         cl.push_back(page);
893         Communicate(&cl, 2, 0);
894         if (owi->log->last()>=OWLOG_ERROR) return -1;
895         cl.clear();
896         cl.push_back(0xBE);
897         cl.push_back(page);
898         Communicate(&cl, 2, 9);
899         if (owi->log->last()>=OWLOG_ERROR) return -1;
900         cl.erase(cl.begin());
901         cl.erase(cl.begin());
902         if (owi->calcCRC8(cl)!=0) {
903                 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
904         }
905         data->clear();
906         data->insert(data->begin(),cl.begin()+start,cl.begin()+start+count);
907         return count;
908 }
909 int owDeviceDS2438::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {
910         if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
911         std::vector<uint8_t> sp;
912         if ((start>0)||(count<8)) {
913                 readMemory(page,0,8,&sp);
914         }
915         std::vector<uint8_t> cl;
916         cl.push_back(0x4E); 
917         cl.push_back(page);
918         int j=0;
919         for(int i=0;i<8;i++) {
920                 if ((i<start)||(i>=start+count)) cl.push_back(sp[i]); else {cl.push_back((*data)[j]);j++;}
921         }
922         Communicate(&cl, 10,0);
923         if (owi->log->last()>=OWLOG_ERROR) return -1;
924         std::vector<uint8_t> cl1;
925         cl1.push_back(0xBE);
926         cl1.push_back(page);
927         Communicate(&cl1, 2, 9);
928         if (owi->log->last()>=OWLOG_ERROR) return -1;
929         cl1.erase(cl1.begin());
930         cl1.erase(cl1.begin());
931         if (owi->calcCRC8(cl1)!=0) {
932                 owi->log->set(OWLOG_WARNING,"CRC ERROR rereading DS2438 Scrachpad");
933         }
934         for(int i=0;i<8;i++) {
935                 if (cl1[i]!=cl[i+2]) {
936                         owi->log->set(OWLOG_ERROR,"Reread not equal, nothing copied");
937                         return 0; 
938                 }
939         }
940         cl.clear();
941         cl.push_back(0x48);
942         cl.push_back(page);
943         Communicate(&cl, 2, 0);
944         if (owi->log->last()>=OWLOG_ERROR) return -1;
945         return 1;
946 }
947
948
949
950 int owDeviceDS2438::setConfigByte(uint8_t cb) {
951         std::vector<uint8_t> sp;
952         int k;
953         for(k=0;k<owi->maxrepeat;k++) {
954                 owi->log->clear();
955                 sp.clear();
956                 sp.push_back(0x4E);
957                 sp.push_back(0x00);
958                 sp.push_back(cb);
959                 Communicate(&sp,3,0);
960                 if (owi->log->last()>=OWLOG_ERROR) return -1;
961                 sp.clear();
962                 readScratchpad(&sp,0,0);
963                 if (owi->log->last()>=OWLOG_ERROR) return -2;
964                 if (cb==sp[0]) return 1;
965                 owi->log->set(OWLOG_WARNING,"ERROR set config byte of DS2438");
966         }
967         owi->log->set(OWLOG_WARNING,"Config of DS2438 byte not set");
968         return 0;
969 }
970
971 inline int16_t ow_fconvert(uint8_t b1, uint16_t b2) {
972   int tsht;
973   tsht=b1  |((int)b2<<8);
974   if (b2 & 0x080)
975         tsht |= 0xFFFFF0000;
976   return tsht;
977 }
978
979 int owDeviceDS2438::convertAll() {
980         for(int k=0;k<owi->maxrepeat;k++)  {
981                 std::vector<uint8_t> sp;
982                 //sp.push_back(0x44);
983                 //Communicate(&sp, 1, 0);///########################################################
984                 if (owi->log->last()>=OWLOG_ERROR) continue;
985                 usleep(100000);
986                 if (setConfigByte(0x08)<=0) continue;
987                 for(int k=0;k<owi->maxrepeat;k++) {
988                         sp.clear();
989                         sp.push_back(0xB4);
990                         Communicate(&sp, 1, 0);
991                 }
992                 if (owi->log->last()>=OWLOG_ERROR) continue;
993                 usleep(100000);
994
995
996                 readScratchpad(&sp,0,1);
997                 int temp=ow_fconvert(sp[1],sp[2]);
998                 int VDD=ow_fconvert(sp[3],sp[4]);
999                 if (setConfigByte(0x00)<=0) continue;
1000
1001                 for(int k=0;k<owi->maxrepeat;k++) {
1002                         sp.clear();
1003                         sp.push_back(0xB4);
1004                         Communicate(&sp, 1, 0);
1005                 }
1006                 if (owi->log->last()>=OWLOG_ERROR) continue;
1007
1008                 usleep(100000);
1009
1010
1011
1012                 readScratchpad(&sp,0,1);
1013                 if (owi->log->last()>=OWLOG_ERROR) continue;
1014                 int I=ow_fconvert(sp[5],sp[6]);
1015                 int VAD=ow_fconvert(sp[3],sp[4]);
1016                 raw[0]=temp;
1017                 raw[1]=VDD;
1018                 raw[2]=VAD;
1019                 raw[3]=I;
1020                 for(int i=0;i<4;i++) values[i]=config->calculateValue(i, raw);
1021                 break;
1022         }
1023         if (owi->log->last()>=OWLOG_ERROR) return 0;
1024         return 1;
1025 }
1026
1027
1028 void owDeviceDS2450::setDefaultConfig() {
1029         config->setConfig({6,9, 6,9, 6,9, 6,9, 0,5,5,5,5,0,0,0,0,0,0,0,0,0,0,0});
1030         configstate=OWDCS_DEFAULT;
1031 }
1032
1033 int owDeviceDS2450::convertAll() {
1034         convert(0x0F,0);
1035         if (owi->log->last()>=OWLOG_WARNING) {
1036                 return 0;
1037         }
1038         std::vector<uint8_t> sp;
1039         readMemory(0,&sp);
1040         if (owi->log->last()>=OWLOG_WARNING) {
1041                 return 0;
1042         }
1043
1044         for(int i=0;i<4;i++) {
1045                 raw[i]=(sp[2 * i] | sp[2 * i + 1] << 8);
1046         }
1047         for(int i=0;i<4;i++) {
1048                 values[i]=config->calculateValue(i, raw);
1049         }
1050         return 1;
1051
1052 }
1053
1054 void owDeviceDS2450::readMemory(uint8_t addr,std::vector<uint8_t> *sp) {
1055         std::vector<uint8_t> cl;
1056         cl.push_back(0xAA);
1057         cl.push_back(addr);
1058         cl.push_back(0x00);
1059         Communicate(&cl,3,10-addr);
1060         if (owi->log->last()>=OWLOG_WARNING) {
1061                 return;
1062         }
1063         if (!owi->testCRC16(cl)) {
1064                 for (uint8_t v :cl) printf("%02X ",v);
1065                 printf("\n");
1066                 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Memory of DS2450");
1067                 return;
1068         }
1069         sp->clear();
1070         sp->insert(sp->begin(),cl.begin()+3,cl.end()-2);
1071 }
1072
1073 void owDeviceDS2450::writeMemory(uint8_t addr,std::vector<uint8_t> *sp) {
1074         std::vector<uint8_t> cl;
1075         cl.push_back(0x55);
1076         cl.push_back(addr);
1077         cl.push_back(0x00);
1078         owi->MatchRom(snum);
1079         if (owi->log->last()>=OWLOG_WARNING) {
1080                 return;
1081         }
1082         for(uint8_t b: (*sp)) {
1083                 cl.push_back(b);
1084                 owi->Communicate(&cl,cl.size(),2);
1085                 if (owi->log->last()>=OWLOG_WARNING) {
1086                         return;
1087                 }
1088                 if (!owi->testCRC16(cl)) {
1089                         owi->log->set(OWLOG_ERROR,"CRC ERROR Writing Memory of DS2450");
1090                         return;
1091                 }
1092                 cl.clear();
1093                 owi->Communicate(&cl, 0, 1);
1094                 if (owi->log->last()>=OWLOG_WARNING) {
1095                         return;
1096                 }
1097                 if (cl[0] != b) {
1098                         owi->log->set(OWLOG_ERROR,"ERROR Writing Memory of DS2450");
1099                         return;
1100                 }
1101                 cl.clear();
1102         }
1103 }
1104         
1105 void owDeviceDS2450::convert(uint8_t mask, uint8_t preset) {
1106         std::vector<uint8_t> cl;
1107         cl.push_back(0x3C);
1108         cl.push_back(mask);
1109         cl.push_back(preset);
1110         Communicate(&cl, 3, 2);
1111         if (!owi->testCRC16(cl)) {
1112                 for (uint8_t v :cl) printf("%02X ",v);
1113                 printf("\n");
1114                 owi->log->set(OWLOG_ERROR,"CRC ERROR Convert Command of DS2450");
1115                 return;
1116         }
1117         usleep(10000);
1118 }
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130 void owDeviceDS2423::setDefaultConfig() {
1131         config->setConfig({9,13, 9,13, 9,13, 9,13, 0,19,19,19,19,0,0,0,0,0,0,0,0,0,0,0});
1132         configstate=OWDCS_DEFAULT;
1133 }
1134
1135 int owDeviceDS2423::convertAll() {
1136         for(uint8_t i=12;i<16;i++) {
1137                 raw[i-12]=readCounter(i);
1138         }
1139         for(int i=0;i<4;i++) {
1140                 values[i]=config->calculateValue(i, raw);
1141         }
1142         return 1;
1143 }
1144 uint32_t owDeviceDS2423::readCounter(uint8_t page) {
1145         std::vector<uint8_t> cl;
1146         cl.push_back(0xA5);
1147         uint16_t addr=(page<<5)+31;
1148         cl.push_back(addr&0xFF);
1149         cl.push_back(addr>>8);
1150         Communicate(&cl,3,11);
1151         //for (uint8_t v :cl) printf("%02X ",v);
1152         //printf("\n");
1153         if (!owi->testCRC16(cl)) {
1154                 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Counter of DS2423");
1155                 return 0;
1156         }
1157         uint32_t ret=0;
1158          
1159         for(size_t i=cl.size()-8;i>cl.size()-11;i--) {
1160                 ret<<=8;
1161                 ret|=cl[i];
1162                 
1163         }
1164                 
1165         return ret;
1166 }
1167
1168
1169 int owDeviceDS2423::readMemory(int page,int start, int count,std::vector<uint8_t> *data){
1170         std::vector<uint8_t> cl;
1171         cl.push_back(0xF0);
1172         uint16_t adr=page<<5;
1173         cl.push_back(adr&0xFF);
1174         cl.push_back((adr>>8)&0xFF);
1175         Communicate(&cl, 3, 32);
1176         //No CRC 
1177         data->clear();
1178         data->insert(data->begin(),cl.begin()+3+start,cl.begin()+start+3+count);
1179         return count;   
1180
1181 }
1182 int owDeviceDS2423::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {
1183         std::vector<uint8_t> cl;
1184         cl.push_back(0x0F);
1185         uint16_t adr=(page<<5)+start;
1186         cl.push_back(adr&0xFF);
1187         cl.push_back(adr>>8);
1188         for(int i=0;i<count;i++) {
1189                 cl.push_back((*data)[i]);
1190         }
1191         Communicate(&cl, 3+count, 0);
1192         for (uint8_t v :cl) printf("%02X ",v);printf("\n");
1193         cl.clear();
1194         cl.push_back(0xAA);
1195         Communicate(&cl, 1, 36);
1196         //for (uint8_t v :cl) printf("%02X ",v);;printf("\n");
1197         cl[0]=0x5A;
1198         Communicate(&cl, 4, 1);
1199         usleep(100000);
1200         if (cl[4]!=0xAA) {
1201                 for (uint8_t v :cl) printf("%02X ",v);;printf("\n");
1202                 owi->log->set(OWLOG_ERROR,"DS2423 Copy from Scratchpad to Memmory Error");
1203                 //return 0;
1204         }
1205         return count;
1206 }
1207
1208
1209
1210
1211
1212
1213 >>>>>>> origin/master