Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / cnt1d.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 //  cnt1D.c - Module to read the DS2423 - counter.
28 //
29 //  Version: 2.00
30 //
31 //
32 #include "ownet.h"
33 #include "cnt1d.h"
34
35 //----------------------------------------------------------------------
36 // Read the counter on a specified page of a DS2423.
37 //
38 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number is provided to
39 //              indicate the symbolic port number.
40 // 'SerialNum'   - Serial Number of DS2423 that contains the counter 
41 //                 to be read
42 // 'CounterPage' - page number that the counter is associated with
43 // 'Count'       - pointer to variable where that count will be returned
44 //
45 // Returns: TRUE(1)  counter has been read and verified
46 //          FALSE(0) could not read the counter, perhaps device is not
47 //                   in contact
48 //
49 int ReadCounter(int portnum, uchar SerialNum[8], int CounterPage, 
50                 unsigned long *Count)
51 {
52    int rt=FALSE;
53    uchar send_block[30];
54    int send_cnt=0, address, i;
55    ushort lastcrc16;
56
57    setcrc16(portnum,0);
58
59    // set the device serial number to the counter device
60    owSerialNum(portnum,SerialNum,FALSE);
61    
62    // access the device 
63    if (owAccess(portnum))
64    {
65       // create a block to send that reads the counter
66       // read memory and counter command
67       send_block[send_cnt++] = 0xA5;
68       docrc16(portnum,0xA5);
69       // address of last data byte before counter
70       address = (CounterPage << 5) + 31;  // (1.02)
71       send_block[send_cnt++] = (uchar)(address & 0xFF);
72       docrc16(portnum,(ushort)(address & 0xFF));
73       send_block[send_cnt++] = (uchar)(address >> 8);
74       docrc16(portnum,(ushort)(address >> 8));
75       // now add the read bytes for data byte,counter,zero bits, crc16
76       for (i = 0; i < 11; i++)
77          send_block[send_cnt++] = 0xFF;
78
79       // now send the block
80       if (owBlock(portnum,FALSE,send_block,send_cnt))
81       {
82          // perform the CRC16 on the last 11 bytes of packet
83          for (i = send_cnt - 11; i < send_cnt; i++)
84             lastcrc16 = docrc16(portnum,send_block[i]);
85
86          // verify CRC16 is correct
87          if (lastcrc16 == 0xB001)
88          {
89             // success
90             rt = TRUE;
91             // extract the counter value
92             *Count = 0;
93             for (i = send_cnt - 7; i >= send_cnt - 10; i--)
94             {
95                *Count <<= 8;
96                *Count |= send_block[i];
97             }  
98          }
99       }
100    }
101    
102    // return the result flag rt
103    return rt;
104