added tstow.c
[fw/sdcc] / device / examples / ds390 / ow390 / tstow.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 //  tstow.C - Test application to test 1-Wire Net functions. No EPROM writes.
28 //
29 //  Version: 2.00
30 //
31 //  History: 1.00 -> 1.01  Change to use msDelay instead of Sleep. 
32 //
33 //           1.01 -> 1.02  Changed to generic OpenCOM/CloseCOM for easier 
34 //                           use with other platforms.  
35 //           1.02 -> 1.03  Removed caps in #includes for Linux capatibility
36 //                         Changed to use Acquire/Release 1-Wire Net functions
37 //           1.03 -> 2.00  Reorganization of Public Domain Kit 
38 //
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include "ownet.h"
43
44 // local funcitons
45 void PrintSerialNum(int portnum);
46
47 // tini hack
48 int argc=2;
49 char *argv[]={__FILE__, "exow"};
50
51 //----------------------------------------------------------------------
52 //  Main Test
53 //
54 int main() //short argc, char **argv)
55 {
56    int PortNum=1,rslt,i,j,testcnt=0,length;
57    uchar TempSerialNum[8];
58    uchar tran_buffer[2000], filename[10];
59    char return_msg[128];
60    int portnum=0;
61
62    // check for required port name
63    if (argc != 2)
64    {
65       printf("1-Wire Net name required on command line!\n"
66              " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
67              "(Linux DS2480),\"1\" (Win32 TMEX)\n");
68       exit(1);
69    }
70
71    // attempt to acquire the 1-Wire Net
72    if (!owAcquire(portnum, argv[1], return_msg))
73    {  
74       printf("%s",return_msg);
75       exit(1);
76    }
77
78    // success
79    printf("%s",return_msg);
80
81    //----------------------------------------
82    // Introduction
83    printf("\n/---------------------------------------------\n");
84    printf("  The following is a test excersize of the\n"
85           "  1-Wire Net public domain library Version 2.00.\n\n"
86           "  This test was run using with 2 DS1920's (DS1820),\n"
87           "  1 DS1971 (DS2430), and 1 DS1996.\n\n");
88
89    //----------------------------------------
90    // First the devices on the 1-Wire Net
91    printf("\n/---------------------------------------------\n");
92    printf("TEST%d: Searching for devices on 1-Wire Net\n",testcnt++);
93
94    // find the first device (all devices not just alarming)
95    rslt = owFirst(portnum,TRUE, FALSE);
96    while (rslt)
97    {
98       // print the Serial Number of the device just found
99       PrintSerialNum(portnum);
100
101       // find the next device
102       rslt = owNext(portnum,TRUE, FALSE);
103    }
104
105    //----------------------------------------
106    // now search for the part with a 0x0C family code (DS1996)
107    printf("\n/---------------------------------------------\n");
108    printf("TEST%d: Set to find first device with 0x0C family code\n",testcnt++);
109
110    owFamilySearchSetup(portnum,0x0C);
111
112    // find the next device
113    printf("search result %d\n",owNext(portnum,TRUE, FALSE));
114
115    // print the Serial Number of the device just found
116    PrintSerialNum(portnum);
117    
118    //----------------------------------------
119    // Access a device and read ram
120    printf("\n/---------------------------------------------\n");
121    printf("TEST%d: Access the current device and read ram\n",testcnt++);
122
123    printf("owAccess %d\n",owAccess(portnum));
124
125    printf("Read Ram 0xF0: %02X\n",owTouchByte(portnum,0xF0));
126    printf("Address0 0x00: %02X\n",owTouchByte(portnum,0x00));
127    printf("Address1 0x00: %02X\n",owTouchByte(portnum,0x00));
128
129    printf("Page 0: ");
130    for (i = 0; i < 32; i++)
131       printf("%02X ",owTouchByte(portnum,0xFF));
132    printf("\n");
133
134    //----------------------------------------
135    // Read ram with owBlock
136    printf("\n/---------------------------------------------\n");
137    printf("TEST%d: Read ram with owBlock\n",testcnt++);
138    for (i = 0; i < 32; i++)
139       tran_buffer[i] = 0xFF;
140
141    printf("owBlock %d\n",owBlock(portnum,FALSE,tran_buffer,32));
142    printf("Page 1: ");
143    for (i = 0; i < 32; i++)
144       printf("%02X ",tran_buffer[i]);
145    printf("\n");
146
147    //----------------------------------------
148    // Write a packet in each page of DS1996
149    printf("\n/---------------------------------------------\n");
150    printf("TEST%d: Place the DS1996 into overdrive\n",testcnt++);
151    printf("owOverdriveAccess %d\n",owOverdriveAccess(portnum));
152
153    //----------------------------------------
154    // Write 4 packets with owWritePacketStd 
155    printf("\n/---------------------------------------------\n");
156    printf("TEST%d: Write 4 packets with owWritePacketStd\n",testcnt++);
157      
158    for (j = 0; j < 4; j++)
159    {
160       for (i = 0; i < 29; i++)
161          tran_buffer[i] = (uchar)i + j;
162
163       printf("Write page %d: %d\n",j,owWritePacketStd(portnum,j,tran_buffer,29,FALSE,FALSE));
164
165       for (i = 0; i < 29; i++)
166          tran_buffer[i] = 0;
167    
168       length = owReadPacketStd(portnum,TRUE,j,tran_buffer);
169
170       printf("Read page %d: %d\n",j,length);
171
172       for (i = 0; i < length; i++)
173          printf("%02X",tran_buffer[i]);
174       printf("\n");
175    }
176
177    //----------------------------------------
178    // Write a file to DS1996
179    printf("\n/---------------------------------------------\n");
180    printf("TEST%d: Format and write a file (in overdrive)\n",testcnt++);
181    sprintf(filename,"DEMO");
182    // set the data to write
183    for (i = 0; i < 2000; i++)
184       tran_buffer[i] = i % 255;
185    printf("Format and write file DEMO.000 %d\n",
186            owFormatWriteFile(portnum,filename,2000,tran_buffer));
187    // clear the buffer
188    for (i = 0; i < 2000; i++)
189       tran_buffer[i] = 0;
190    printf("Read file DEMO.000 %d\n",owReadFile(portnum,filename,tran_buffer));
191    // print the data result
192    for (i = 0; i < 2000; i++)
193    {
194       if ((i % 0x20) == 0)
195          printf("\n%03X    ",i);
196       printf("%02X",tran_buffer[i]);
197    }
198    printf("\n");
199   
200    //----------------------------------------
201    // Turn off overdrive
202    printf("\n/---------------------------------------------\n");
203    printf("TEST%d: Turn off overdrive\n",testcnt++);
204    printf("Set 1-Wire Net speed to normal %d\n",owSpeed(portnum,MODE_NORMAL));
205
206    //----------------------------------------
207    // Verify a device
208    printf("\n/---------------------------------------------\n");
209    printf("TEST%d: Verify the current device\n",testcnt++);
210
211    printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
212    printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));
213
214    //----------------------------------------
215    // Skip the first family code found
216    printf("\n/---------------------------------------------\n");
217    printf("TEST%d: Skip the first family code found\n",testcnt++);
218    
219    // find the next device
220    printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE));
221
222    // print the Serial Number of the device just found
223    PrintSerialNum(portnum);
224
225    // skip the first family type found
226    owSkipFamily(portnum);
227    printf("owSkipFamily called\n");
228
229    // find the next device
230    printf("search result of owNext %d\n",owNext(portnum,TRUE, FALSE));
231    
232    // print the Serial Number of the device just found
233    PrintSerialNum(portnum);
234
235    //----------------------------------------
236    // Find first family code (DS1920) and read temperature
237    printf("\n/---------------------------------------------\n");
238    printf("TEST%d: Find first family code (DS1920) and read temperature\n",testcnt++);
239
240    // find the next device
241    printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE));
242
243    // print the Serial Number of the device just found
244    PrintSerialNum(portnum);
245
246    // send the convert temperature command
247    printf("Convert temperature command %02X\n",owTouchByte(portnum,0x44));
248
249    // set the 1-Wire Net to strong pull-up
250    printf("Set power delivery %d\n",owLevel(portnum,MODE_STRONG5));
251
252    // sleep for 1 second
253    msDelay(1000);
254
255    // turn off the 1-Wire Net strong pull-up
256    printf("Disable power delivery %d\n",owLevel(portnum,MODE_NORMAL));
257
258    // read the DS1920 temperature value
259    printf("Access the DS1920 %d\n",owAccess(portnum));
260    tran_buffer[0] = 0xBE;
261    tran_buffer[1] = 0xFF;
262    tran_buffer[2] = 0xFF;
263    printf("Block to read temperature %d\n",owBlock(portnum,FALSE,tran_buffer,3));
264    // interpret the result
265    printf("result: DS1920 temperature read: %d C\n", (tran_buffer[1] |
266            ((int)tran_buffer[2] << 8)) / 2);
267   
268    //----------------------------------------
269    //  Verify the current device, could also be alarming
270    printf("\n/---------------------------------------------\n");
271    printf("TEST%d: Verify the current device, could also be alarming\n",testcnt++);
272
273    printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
274    printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));
275
276    //----------------------------------------
277    // Test setting the Serial Number with owSerialNum
278    printf("\n/---------------------------------------------\n");
279    printf("TEST%d: Test setting the Serial Number with owSerialNum\n",testcnt++);
280
281    // set the Serial Num to 0 to 7
282    for (i = 0; i < 8; i++)
283       TempSerialNum[i] = (uchar)i;
284    owSerialNum(portnum,TempSerialNum,FALSE);
285
286    // read back the Serial Number 
287    PrintSerialNum(portnum);
288
289    //----------------------------------------
290    //  Verify the current device (should fail, no such device)
291    printf("\n/---------------------------------------------\n");
292    printf("TEST%d: Verify the current device (should fail, no such device)\n",testcnt++);
293
294    printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
295    printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));
296
297    // release the 1-Wire Net
298    owRelease(portnum,return_msg);
299    printf("%s",return_msg);
300    exit(0);
301
302    return 0;
303 }
304
305 //----------------------------------------------------------------------
306 //  Read and print the Serial Number.
307 //
308 void PrintSerialNum(int portnum)
309 {
310    uchar TempSerialNumber[8];
311    int i;
312
313    owSerialNum(portnum,TempSerialNumber,TRUE);
314    for (i = 7; i >= 0; i--)
315       printf("%02X",TempSerialNumber[i]);
316    printf("\n");
317 }