~ubuntu-branches/debian/jessie/digitemp/jessie

« back to all changes in this revision

Viewing changes to userial/cnt1d.c

  • Committer: Bazaar Package Importer
  • Author(s): Jesus Roncero
  • Date: 2004-09-01 01:34:37 UTC
  • Revision ID: james.westby@ubuntu.com-20040901013437-eicsrrd40dr371u0
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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);
 
38
 
 
39
// external One Wire functions from transaction layer
 
40
extern SMALLINT owBlock(int,SMALLINT,uchar *,SMALLINT);
 
41
 
 
42
// external functions defined in crcutil.c
 
43
extern void setcrc16(int,ushort);
 
44
extern ushort docrc16(int,ushort);
 
45
 
 
46
// exportable functions defined in cnt1d.c
 
47
SMALLINT ReadCounter(int,int,ulong *);
 
48
 
 
49
//----------------------------------------------------------------------
 
50
// Read the counter on a specified page of a DS2423.
 
51
//
 
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
 
55
//                 to be read
 
56
// 'CounterPage' - page number that the counter is associated with
 
57
// 'Count'       - pointer to variable where that count will be returned
 
58
//
 
59
// Returns: TRUE(1)  counter has been read and verified
 
60
//          FALSE(0) could not read the counter, perhaps device is not
 
61
//                   in contact
 
62
//
 
63
SMALLINT ReadCounter(int portnum, int CounterPage,  unsigned long *Count)
 
64
{
 
65
   uchar rt=FALSE;
 
66
   uchar send_block[30];
 
67
   uchar send_cnt=0, i;
 
68
   int address;
 
69
   ushort lastcrc16;
 
70
 
 
71
   setcrc16(portnum,0);
 
72
 
 
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); */
 
76
 
 
77
   // access the device
 
78
   if (owAccess(portnum))
 
79
   {
 
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;
 
93
 
 
94
      // now send the block
 
95
      if (owBlock(portnum,FALSE,send_block,send_cnt))
 
96
      {
 
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]);
 
100
 
 
101
         // verify CRC16 is correct
 
102
         if (lastcrc16 == 0xB001)
 
103
         {
 
104
            // success
 
105
            rt = TRUE;
 
106
            // extract the counter value
 
107
            *Count = 0;
 
108
            for (i = send_cnt - 7; i >= send_cnt - 10; i--)
 
109
            {
 
110
               *Count <<= 8;
 
111
               *Count |= send_block[i];
 
112
            }
 
113
         }
 
114
      }
 
115
   }
 
116
 
 
117
   // return the result flag rt
 
118
   return rt;
 
119
}