Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / swt12.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 //  swt12.c - Modifies Channel A and B and returns info byte data for
28 //            the DS2406 and DS2407.
29 //  version 2.00
30
31
32 // Include files
33 #include <stdio.h>
34 #include "ownet.h"
35 #include "swt12.h"
36
37 //----------------------------------------------------------------------
38 //  SUBROUTINE - ReadSwitch12
39 //
40 //      This routine gets the Channel Info Byte and returns it.
41 //
42 // 'portnum'       - number 0 to MAX_PORTNUM-1.  This number was provided to
43 //                   OpenCOM to indicate the port number.
44 //      'ClearActivity' - To reset the button
45 //
46 //      Returns: (-1) If the Channel Info Byte could not be read.
47 //                       (Info Byte) If the Channel Info Byte could be read.
48 //                                                           
49 int ReadSwitch12(int portnum, int ClearActivity)
50 {
51    int rt=-1;                      //this is the return value depending if the byte was read
52    int trans_cnt=0;             //this is the counter for the number of bytes to send
53    uchar transfer[30];  //this is the whole block of byte info
54    
55    // access and verify it is there
56    if (owAccess(portnum)) 
57    {
58       // reset CRC 
59       setcrc16(portnum,0);
60    
61       // channel access command 
62            transfer[trans_cnt++] = 0xF5;
63       docrc16(portnum,0xF5);
64    
65       // control bytes                
66       if (ClearActivity)
67       {
68                    transfer[trans_cnt++] = 0xD5;
69          docrc16(portnum,0xD5); 
70       }
71       else
72       {
73                    transfer[trans_cnt++] = 0x55;
74          docrc16(portnum,0x55);                      
75       }
76
77            transfer[trans_cnt++] = 0xFF;
78       docrc16(portnum,0xFF);
79    
80       // read the info byte
81            transfer[trans_cnt++] = 0xFF;
82
83            // dummy data
84            transfer[trans_cnt++] = 0xFF;
85            transfer[trans_cnt++] = 0xFF;
86            transfer[trans_cnt++] = 0xFF;
87  
88       if (owBlock(portnum,FALSE,transfer,trans_cnt))
89       {
90                    rt = transfer[3];
91                    // read a dummy read byte and CRC16
92                    docrc16(portnum,transfer[trans_cnt-4]);
93                    docrc16(portnum,transfer[trans_cnt-3]);
94                    docrc16(portnum,transfer[trans_cnt-2]);
95                    if(docrc16(portnum,transfer[trans_cnt-1]) != 0xB001)
96             rt = -1;
97       }
98    }
99    else
100       rt = -1;
101
102    return rt;
103 }
104
105 //----------------------------------------------------------------------
106 //      SUBROUTINE - SetSwitch12
107 //
108 //  This routine sets the channel state of the specified DS2406
109 //
110 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number was provided to
111 //                 OpenCOM to indicate the port number.
112 // 'SerialNum'   - Serial Number of DS2406 to set the switch state
113 // 'State'       - Is a type containing what to set A and/or B to.  It 
114 //                                 also contains the other fields that maybe written later 
115 //
116 //  Returns: TRUE(1)  State of DS2406 set and verified  
117 //           FALSE(0) could not set the DS2406, perhaps device is not
118 //                    in contact
119 //
120 int SetSwitch12(int portnum, uchar *SerialNum, SwitchProps *State)
121 {
122    ushort st;  
123    int rt=FALSE;
124    uchar send_block[30];
125    int send_cnt=0;
126
127    setcrc16(portnum,0);
128
129    // set the device serial number to the counter device
130    owSerialNum(portnum,SerialNum,FALSE);
131
132    // access the device 
133    if (owAccess(portnum))
134    {
135       // create a block to send that reads the counter
136       // write status command
137       send_block[send_cnt++] = 0x55;
138       docrc16(portnum,0x55);
139       
140       // address of switch state
141       send_block[send_cnt++] = 0x07;
142       docrc16(portnum,0x07);
143       send_block[send_cnt++] = 0x00;
144       docrc16(portnum,0x00);
145       
146       // write state
147            st = 0x1F;                     
148            if(!State->Chan_B) st |= 0x40;  
149            if(!State->Chan_A) st |= 0x20;  
150            // more ifs can be added here for the other fields.
151            
152       send_block[send_cnt++] = (uchar)st;        
153            docrc16(portnum,st);                                 
154       
155       // read CRC16
156       send_block[send_cnt++] = 0xFF;
157       send_block[send_cnt++] = 0xFF;
158
159       // now send the block
160       if (owBlock(portnum,FALSE,send_block,send_cnt))
161       {
162          // perform the CRC16 on the last 2 bytes of packet
163          docrc16(portnum,send_block[send_cnt-2]);
164
165          // verify crc16 is correct
166          if(docrc16(portnum,send_block[send_cnt-1]) == 0xB001)
167             rt = TRUE;
168       }
169    }
170    
171    // return the result flag rt
172    return rt;
173 }
174
175
176 //----------------------------------------------------------------------
177 //      SUBROUTINE - SwitchStateToString12
178 //
179 //  This routine uses the info byte to return a string with all the data.
180 //
181 // 'infobyte'   - This is the information byte data from the hardware.
182 // 'outstr'     - This will be the output string.  It gets set in the 
183 //                                    the procedure.
184 //
185 int SwitchStateToString12(int infobyte, char *outstr)
186 {
187
188    int cnt = 0;
189
190    if(infobyte & 0x40)
191    {
192                 cnt += sprintf(outstr+cnt, "%s", "Channel A and B\n");
193
194            if(infobyte & 0x80)
195                    cnt += sprintf(outstr+cnt, "%s", "Supply\n");
196            else
197                    cnt += sprintf(outstr+cnt, "%s", "No Supply\n");
198                             
199            if(infobyte & 0x20)
200                    cnt += sprintf(outstr+cnt, "%s", "Activity on PIO-B\n");
201            else
202                    cnt += sprintf(outstr+cnt, "%s", "No activity on PIO-B\n");
203
204            if(infobyte & 0x10)
205                    cnt += sprintf(outstr+cnt, "%s", "Activity on PIO-A\n");
206            else
207                    cnt += sprintf(outstr+cnt, "%s", "No activity on PIO-A\n");
208
209            if(infobyte & 0x08)
210                    cnt += sprintf(outstr+cnt, "%s", "Hi level on PIO B\n");
211            else
212                    cnt += sprintf(outstr+cnt, "%s", "Lo level on PIO B\n");
213
214            if(infobyte & 0x04)
215                    cnt += sprintf(outstr+cnt, "%s", "Hi level on PIO A\n");
216            else
217                    cnt += sprintf(outstr+cnt, "%s", "Lo level on PIO A\n");
218
219            if(infobyte & 0x02)
220                    cnt += sprintf(outstr+cnt, "%s", "Channel B off\n");
221            else
222                    cnt += sprintf(outstr+cnt, "%s", "Channel B on\n");
223
224            if(infobyte & 0x01)
225                    cnt += sprintf(outstr+cnt, "%s", "Channel A off\n");
226            else
227                    cnt += sprintf(outstr+cnt, "%s", "Channel A on\n");
228    }
229         else
230    {
231                 cnt += sprintf(outstr+cnt, "%s", "Channel A\n");
232
233            if(infobyte & 0x80)
234                    cnt += sprintf(outstr+cnt, "%s", "Supply\n");
235            else
236                    cnt += sprintf(outstr+cnt, "%s", "No Supply\n");
237
238            if(infobyte & 0x10)
239                    cnt += sprintf(outstr+cnt, "%s", "Activity on PIO-A\n");
240            else
241                    cnt += sprintf(outstr+cnt, "%s", "No activity on PIO-A\n");
242
243            if(infobyte & 0x04)
244                    cnt += sprintf(outstr+cnt, "%s", "Hi level on PIO A\n");
245            else
246                    cnt += sprintf(outstr+cnt, "%s", "Lo level on PIO A\n");
247
248            if(infobyte & 0x01)
249                    cnt += sprintf(outstr+cnt, "%s", "Channel A off\n");
250            else
251                    cnt += sprintf(outstr+cnt, "%s", "Channel A on\n");
252
253    }
254
255    return cnt;
256 }
257
258