1
//---------------------------------------------------------------------------
2
// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
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:
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
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.
22
// Except as contained in this notice, the name of Dallas Semiconductor
23
// shall not be used except as stated in the Dallas Semiconductor
25
//---------------------------------------------------------------------------
27
// cnt1D.c - Module to read the DS2423 - counter.
34
// external One Wire functions from nework layer
35
extern SMALLINT owAccess(int);
36
extern void owSerialNum(int,uchar *,SMALLINT);
37
extern SMALLINT owVerify(int,SMALLINT);
39
// external One Wire functions from transaction layer
40
extern SMALLINT owBlock(int,SMALLINT,uchar *,SMALLINT);
42
// external functions defined in crcutil.c
43
extern void setcrc16(int,ushort);
44
extern ushort docrc16(int,ushort);
46
// exportable functions defined in cnt1d.c
47
SMALLINT ReadCounter(int,int,ulong *);
49
//----------------------------------------------------------------------
50
// Read the counter on a specified page of a DS2423.
52
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
53
// indicate the symbolic port number.
54
// 'SerialNum' - Serial Number of DS2423 that contains the counter
56
// 'CounterPage' - page number that the counter is associated with
57
// 'Count' - pointer to variable where that count will be returned
59
// Returns: TRUE(1) counter has been read and verified
60
// FALSE(0) could not read the counter, perhaps device is not
63
SMALLINT ReadCounter(int portnum, int CounterPage, unsigned long *Count)
73
// set the device serial number to the counter device
74
/* 2/12/2003 [bcl] DigiTemp does this before calling the routine */
75
/* owSerialNum(portnum,SerialNum,FALSE); */
78
if (owAccess(portnum))
80
// create a block to send that reads the counter
81
// read memory and counter command
82
send_block[send_cnt++] = 0xA5;
83
docrc16(portnum,0xA5);
84
// address of last data byte before counter
85
address = (CounterPage << 5) + 31; // (1.02)
86
send_block[send_cnt++] = (uchar)(address & 0xFF);
87
docrc16(portnum,(ushort)(address & 0xFF));
88
send_block[send_cnt++] = (uchar)(address >> 8);
89
docrc16(portnum,(ushort)(address >> 8));
90
// now add the read bytes for data byte,counter,zero bits, crc16
91
for (i = 0; i < 11; i++)
92
send_block[send_cnt++] = 0xFF;
95
if (owBlock(portnum,FALSE,send_block,send_cnt))
97
// perform the CRC16 on the last 11 bytes of packet
98
for (i = send_cnt - 11; i < send_cnt; i++)
99
lastcrc16 = docrc16(portnum,send_block[i]);
101
// verify CRC16 is correct
102
if (lastcrc16 == 0xB001)
106
// extract the counter value
108
for (i = send_cnt - 7; i >= send_cnt - 10; i--)
111
*Count |= send_block[i];
117
// return the result flag rt