Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / ad26.c
1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
3 // 
4 // Permission is hereby granted, free of charge, to any person obtaining a 
5 // copy of this software and associated documentation files (the "Software"), 
6 // to deal in the Software without restriction, including without limitation 
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8 // and/or sell copies of the Software, and to permit persons to whom the 
9 // Software is furnished to do so, subject to the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be included 
12 // in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
16 // MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES 
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
20 // OTHER DEALINGS IN THE SOFTWARE.
21 // 
22 // Except as contained in this notice, the name of Dallas Semiconductor 
23 // shall not be used except as stated in the Dallas Semiconductor 
24 // Branding Policy. 
25 //--------------------------------------------------------------------------
26 //
27 //  ad26.c - Reads the voltage on the 1-Wire
28 //  version 1.00
29 //
30
31 // Include Files
32 #include <stdio.h>
33 #include "ownet.h"
34 #include "ad26.h"
35
36 int Volt_AD(int portnum, int vdd, uchar *SNum)
37 {
38    uchar send_block[50];
39    uchar test;
40    int send_cnt=0;
41    int i;
42    ushort lastcrc8;
43    int busybyte; 
44
45    owSerialNum(portnum,SNum,FALSE);
46    // Recall the Status/Configuration page
47    // Recall command
48    send_block[send_cnt++] = 0xB8;
49
50    // Page to Recall
51    send_block[send_cnt++] = 0x00;
52
53    if(!owBlock(portnum,FALSE,send_block,send_cnt))
54       return FALSE;
55
56    send_cnt = 0;
57
58    if(owAccess(portnum))
59    {
60       // Read the Status/Configuration byte
61       // Read scratchpad command
62       send_block[send_cnt++] = 0xBE;
63
64       // Page for the Status/Configuration byte
65       send_block[send_cnt++] = 0x00;
66
67       for(i=0;i<9;i++)
68          send_block[send_cnt++] = 0xFF;
69
70       if(owBlock(portnum,FALSE,send_block,send_cnt))
71       {
72         setcrc8(portnum,0);
73
74          for(i=2;i<send_cnt;i++)
75            lastcrc8 = docrc8(portnum,send_block[i]);
76
77          if(lastcrc8 != 0x00)
78             return FALSE;
79       }//Block
80       else
81          return FALSE;
82
83       test = send_block[2] & 0x08;
84       if(((test == 0x08) && vdd) || ((test == 0x00) && !(vdd)))
85          return TRUE;
86    }//Access
87
88    if(owAccess(portnum))
89    {
90       send_cnt = 0;
91       // Write the Status/Configuration byte
92       // Write scratchpad command
93       send_block[send_cnt++] = 0x4E;
94
95       // Write page
96       send_block[send_cnt++] = 0x00;
97
98       if(vdd)
99          send_block[send_cnt++] = send_block[2] | 0x08;
100       else
101          send_block[send_cnt++] = send_block[2] & 0xF7;
102
103       for(i=0;i<7;i++)
104          send_block[send_cnt++] = send_block[i+4];
105
106       if(owBlock(portnum,FALSE,send_block,send_cnt))
107       {
108          send_cnt = 0;
109
110          if(owAccess(portnum))
111          {
112             // Copy the Status/Configuration byte
113             // Copy scratchpad command
114             send_block[send_cnt++] = 0x48;
115
116             // Copy page
117             send_block[send_cnt++] = 0x00;
118
119             if(owBlock(portnum,FALSE,send_block,send_cnt))
120             {
121                busybyte = owReadByte(portnum);
122          
123                while(busybyte == 0)
124                   busybyte = owReadByte(portnum);
125
126                return TRUE;
127             }//Block
128          }//Access
129       }//Block
130
131    }//Access
132
133    return FALSE;
134 }
135       
136
137 float Volt_Reading(int portnum, int vdd, uchar *SNum)
138 {
139    uchar send_block[50];
140    int send_cnt=0;
141    int i;
142    int busybyte; 
143    uchar lastcrc8;
144    ushort volts;
145    float ret=-1.0;
146
147    if(Volt_AD(portnum,vdd,SNum))
148    {
149
150       if(owAccess(portnum))
151       {
152          if(!owWriteByte(portnum,0xB4))
153          {
154            //output_status(LV_ALWAYS,(char *)"DIDN'T WRITE CORRECTLY\n");
155            printf ("DIDN'T WRITE CORRECTLY\n");
156            return ret;
157          }
158
159          busybyte = owReadByte(portnum);
160          
161          while(busybyte == 0)
162             busybyte = owReadByte(portnum);
163       }
164
165       if(owAccess(portnum))
166       {
167          // Recall the Status/Configuration page
168          // Recall command
169          send_block[send_cnt++] = 0xB8;
170
171          // Page to Recall
172          send_block[send_cnt++] = 0x00;
173
174          if(!owBlock(portnum,FALSE,send_block,send_cnt))
175             return ret;
176       }
177
178
179       send_cnt = 0;
180
181       if(owAccess(portnum))
182       {
183          // Read the Status/Configuration byte
184          // Read scratchpad command
185          send_block[send_cnt++] = 0xBE;
186
187          // Page for the Status/Configuration byte
188          send_block[send_cnt++] = 0x00;
189
190          for(i=0;i<9;i++)
191             send_block[send_cnt++] = 0xFF;
192
193          if(owBlock(portnum,FALSE,send_block,send_cnt))
194          {
195             setcrc8(portnum,0);
196
197             for(i=2;i<send_cnt;i++)
198                lastcrc8 = docrc8(portnum,send_block[i]);
199
200             if(lastcrc8 != 0x00)
201                return ret;
202
203          }
204          else
205             return ret;    
206       
207          volts = ((int)send_block[6] << 8) | send_block[5];
208          ret = (float) volts/100;
209       }//Access
210    }
211
212    return ret;
213
214 }
215
216 double Get_Temperature(int portnum,uchar *SNum)
217 {
218    double ret=-1.0;
219    uchar send_block[50];
220    int send_cnt=0;
221    int i;
222    uchar lastcrc8;
223
224    owSerialNum(portnum,SNum,FALSE);
225
226    if(owAccess(portnum))
227       // Convert Temperature command
228       owWriteByte(portnum,0x44);
229
230    msDelay(10);
231
232
233    if(owAccess(portnum))
234    {
235       // Read the Status/Configuration byte
236       // Read scratchpad command
237       send_block[send_cnt++] = 0xBE;
238
239       // Page for the Status/Configuration byte
240       send_block[send_cnt++] = 0x00;
241
242       for(i=0;i<9;i++)
243          send_block[send_cnt++] = 0xFF;
244
245       if(owBlock(portnum,FALSE,send_block,send_cnt))
246       {
247          setcrc8(portnum,0);
248
249          for(i=2;i<send_cnt;i++) {
250             lastcrc8 = docrc8(portnum,send_block[i]);
251          } printf ("\n");
252
253          if(lastcrc8 != 0x00)
254             return ret;
255
256       }
257       else
258          return ret;    
259       
260       ret = ((((unsigned int)send_block[4] << 8) | send_block[3]) >> 3) * 0.03125;
261    }//Access
262
263    return ret;
264 }
265