File: swt1f.c

package info (click to toggle)
digitemp 3.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 852 kB
  • sloc: ansic: 7,254; perl: 644; makefile: 118; python: 67; sh: 39; sql: 11
file content (335 lines) | stat: -rw-r--r-- 11,410 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//---------------------------------------------------------------------------
// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Except as contained in this notice, the name of Dallas Semiconductor
// shall not be used except as stated in the Dallas Semiconductor
// Branding Policy.
//--------------------------------------------------------------------------
//
//  swt1f.c - Does commands on the DS2409 device
//
//  version 2.00-digitemp
//    Fixed problem of not finding more than 1 device on main branch by
//    changing owBranchNext to use Smart Main instead of Main On command
//
//  version 2.00
//  - future version will have branch of a branch searches
//

// Include files
#include <stdio.h>
#include "ownet.h"

// external One Wire functions from nework layer
extern SMALLINT owAccess(int);
extern SMALLINT owFirst(int,SMALLINT,SMALLINT);
extern SMALLINT owNext(int,SMALLINT,SMALLINT);
extern void     owSerialNum(int,uchar *,SMALLINT);

// external One Wire functions from transaction layer
extern SMALLINT owBlock(int,SMALLINT,uchar *,SMALLINT);

// Local subroutines
int SetSwitch1F(int,uchar *,int,int,uchar *,int);
int SwitchStateToString1F(int, char *);
int FindBranchDevice(int,uchar *,uchar BranchSN[][8],int,int);
int owBranchFirst(int,uchar *,int,int);
int owBranchNext(int,uchar *,int,int);

//----------------------------------------------------------------------
//	SUBROUTINE - SetSwitch1F
//
//  This routine sets the main and auxilary on and off for DS2409.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
// 'SerialNum'   - Serial Number of DS2409 to set the switch state
// 'Swtch'       - '0' Sets Main and Auxilary off
//                 '1' Sets Main on
//                 '2' Sets Auxilary on
//                 '3' Read Status Info Byte
//                 '4' Smart On Main
// 'NumExtra'    - The number of extra bytes for a command.
// 'InfoByte'    - Returns the info byte and other information depending
//                 on the command.  The InfoByte size changes depending on
//                 the NumExtra which is buffer length * (NumExtra + 1)
// 'rst'         - True then reset the search for devices
//                 False don't reset search
//
//  Returns: TRUE(1)  State of DS2409 set and verified
//           FALSE(0) could not set the DS2409, perhaps device is not
//                    in contact
//
int SetSwitch1F(int portnum, uchar *SerialNum, int Swtch, int NumExtra,
                    uchar *InfoByte, int rst)
{
   int send_cnt,i,cmd;
   uchar send_block[50];

   if(owAccess(portnum))
   {
      send_cnt = 0;
      // add the match command
      send_block[send_cnt++] = 0x55;

      for (i = 0; i < 8; i++)
         send_block[send_cnt++] = SerialNum[i];

      // the command
      switch(Swtch)
      {
         case 0: // All lines off
            send_block[send_cnt++] = 0x66;
            cmd = 0x66;
            break;

         case 1: // Direct on Main
            send_block[send_cnt++] = 0xA5;
            cmd = 0xA5;
            break;

         case 2: // Smart on Auxilary
            send_block[send_cnt++] = 0x33;
            cmd = 0x33;
            break;

         case 3: // Status Read/Write
            send_block[send_cnt++] = 0x5A;
            cmd = 0x5A;

            // bytes 0-2: don't care
            // bytes 3-4: write control 0 to change status
            // byte 5: 0 = auto-control, 1 = manual mode
            // byte 6: 0 = main, 1 = auxiliary
            // byte 7: value to be written to control output, manual mode only
            // 0x00 default value
            *InfoByte = 0x00;
            send_block[send_cnt++] = *InfoByte;
            break;

         case 4: // Smart on Main
            send_block[send_cnt++] = 0xCC;
            cmd = 0xCC;
            break;

         default:
            return FALSE;
      }

      // extra bytes and confirmation
      for(i=0; i<=NumExtra; i++)
         send_block[send_cnt++] = 0xFF;

      // send the command string
      if(owBlock(portnum,rst,send_block,send_cnt))
      {
         // returned information for the info byte and command
         for(i=0; i<=NumExtra; i++)
            *(InfoByte+(NumExtra-i)) = send_block[send_cnt - (i+2)];

         // Set because for the read/write command the confirmation
         // byte is the same as the status byte
         if (Swtch == 3)
            cmd = send_block[send_cnt - 2];

         if (send_block[send_cnt - 1] == cmd)
            return TRUE;
      }
   }

   return FALSE;
}

//----------------------------------------------------------------------
// SUBROUTINE = FindBranchDevices
//
// This routine will find all the branches on a certain DS2409 device and
// will return the serial numbers down that branch
//
// 'portnum'        - number 0 to MAX_PORTNUM-1.  This number is provided to
//                    indicate the symbolic port number.
// 'Branch[8]'      - The serial number of the branch to look down
// 'BranchSN[][8]'  - The list of serial numbers on the branch
// 'MAXDEVICES'     - The Max devices on the branch
// 'MainBr'         - True to search down the main branch, False for Aux.
//
// Returns: Returns the number of devices found on the branch
//
int FindBranchDevice(int portnum, uchar Branch[8], uchar BranchSN[][8], int MAXDEVICES,
                     int MainBr)
{
   int NumDevices = 0;
   short result;

   result = owBranchFirst(portnum, &Branch[0], FALSE, MainBr);

   while(result)
   {
      owSerialNum(portnum,BranchSN[NumDevices], TRUE);
      NumDevices++;

      result = owBranchNext(portnum, &Branch[0], FALSE, MainBr);
   }

   return NumDevices;
}

//----------------------------------------------------------------------
// SUBROUTINE - owBranchFirst
//
// This routine is used like owFirst but for devices on a branch.
//
// 'portnum' - number 0 to MAX_PORTNUM-1.  This number is provided to
//             indicate the symbolic port number.
// 'BrSN'    - This is the DS2409 device branch
// 'AlarmD'  - True or False, to do Alarm search or false for regular search
// 'FirMain' - True then search main branch, False search Aux. branch
//
// Returns:   TRUE (1) : when a 1-Wire device was found and it's
//                        Serial Number placed in the global SerialNum
//            FALSE (0): There are no devices on the 1-Wire Net.
//
int owBranchFirst(int portnum, uchar BrSN[8], int AlarmD, int FirMain)
{
   int smart_main = 4;
   int smart_aux = 2;
   int numextra = 2;
   uchar extra[3];


   if(FirMain)
   {
      if(SetSwitch1F(portnum, &BrSN[0], smart_main, numextra, extra, TRUE))
         if(extra[2] != 0xFF)
            return owFirst(portnum,FALSE, AlarmD);
   }
   else
   {
      if(SetSwitch1F(portnum, &BrSN[0], smart_aux, numextra, extra, TRUE))
         if(extra[2] != 0xFF)
            return owFirst(portnum,FALSE, AlarmD);
   }

   return FALSE;
}

//----------------------------------------------------------------------
// SUBROUTINE - owBranchNext
//
// This routine is used like owFirst but for devices on a branch.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
//                indicate the symbolic port number.
// 'BrSN'       - This is the DS2409 device branch
// 'AlarmD'     - True or False, to do Alarm search or false for regular search
// 'NextMain'   - True then search main branch, False search Aux. branch
//
// Returns:   TRUE (1) : when a 1-Wire device was found and it's
//                        Serial Number placed in the global SerialNum
//            FALSE (0): There are no devices on the 1-Wire Net.
//
int owBranchNext(int portnum, uchar BrSN[8], int AlarmD, int NextMain)
{
   int smart_main = 4;		/* Was 1, Main On [bcl - 01132002 */
   int smart_aux = 2;
   int numextra = 2;
   uchar extra[3];

   if(NextMain)
   {
      if(SetSwitch1F(portnum, &BrSN[0], smart_main, numextra, extra, TRUE))
         return owNext(portnum,FALSE, AlarmD);
   }
   else
   {
      if(SetSwitch1F(portnum, &BrSN[0], smart_aux, numextra, extra, TRUE))
         return owNext(portnum,FALSE, AlarmD);
   }

   return FALSE;
}

//----------------------------------------------------------------------
//	SUBROUTINE - SwitchStateToString
//
//  This routine uses the info byte to return a string with all the data.
//
// 'infobyte'   - This is the information byte data from the hardware.
// 'outstr'     - This will be the output string.  It gets set in the
//				      the procedure.
//
// Returns      - Returns the number of characters in the string
//
int SwitchStateToString1F(int infobyte, char *outstr)
{
   int cnt = 0;

   if(infobyte & 0x80)
   {
      cnt += sprintf(outstr+cnt, "%s", "Manual mode\n");
      if(infobyte & 0x40)
         cnt += sprintf(outstr+cnt, "%s", "Output transistor on\n");
      else
         cnt += sprintf(outstr+cnt, "%s", "Output transistor off\n");
   }
   else
   {
      cnt += sprintf(outstr+cnt, "%s", "Auto-control mode\n");
      if(infobyte & 0x40)
         cnt += sprintf(outstr+cnt, "%s", "Output association with Auxillary\n");
      else
         cnt += sprintf(outstr+cnt, "%s", "Output association with Main\n");
   }

   if(infobyte & 0x20)
      cnt += sprintf(outstr+cnt, "%s",
         "Negative edge sensed since inactive on Main\n");
   else
      cnt += sprintf(outstr+cnt, "%s", "No event on Main\n");

   if(infobyte & 0x10)
      cnt += sprintf(outstr+cnt, "%s",
         "Negative edge sensed since inactive on Aux.\n");
   else
      cnt += sprintf(outstr+cnt, "%s", "No event on Aux.\n");

   if(infobyte & 0x08)
      cnt += sprintf(outstr+cnt, "%s", "Voltage High on Aux. output\n");
   else
      cnt += sprintf(outstr+cnt, "%s", "Voltage Low on Aux. output\n");

   if(infobyte & 0x04)
      cnt += sprintf(outstr+cnt, "%s", "Inactive status of Aux. output\n");
   else
      cnt += sprintf(outstr+cnt, "%s", "Active status of Aux. output\n");

   if(infobyte & 0x02)
      cnt += sprintf(outstr+cnt, "%s", "Voltage High on Main output\n");
   else
      cnt += sprintf(outstr+cnt, "%s", "Voltage Low on Main output\n");

   if(infobyte & 0x01)
      cnt += sprintf(outstr+cnt, "%s", "Inactive status on Main output\n");
   else
      cnt += sprintf(outstr+cnt, "%s", "Active status on Main output\n");

   return cnt;
}