* Removed svn:executable property from non-executable files
[fw/sdcc] / device / examples / ds390 / ow390 / ds2480ut.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 //  ds2480ut.c - DS2480 utility functions.
28 //
29 //  Version: 2.00
30 //
31 //  History: 1.00 -> 1.01  Default PDSRC changed from 0.83 to 1.37V/us
32 //                         in DS2480Detect. Changed to use msDelay instead
33 //                         of Delay.
34 //           1.01 -> 1.02  Changed global declarations from 'uchar' to 'int'.
35 //                         Changed DSO/WORT from 7 to 10us in DS2480Detect.
36 //           1.02 -> 1.03  Removed caps in #includes for Linux capatibility
37 //           1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for 
38 //                         multiple ports.  Changed W1LT to 8us.
39
40 #include "ownet.h"
41 #include "ds2480.h"
42
43 // global DS2480 state
44 int ULevel[MAX_PORTNUM]; // current DS2480 1-Wire Net level
45 int UBaud[MAX_PORTNUM];  // current DS2480 baud rate
46 int UMode[MAX_PORTNUM];  // current DS2480 command or data mode state
47 int USpeed[MAX_PORTNUM]; // current DS2480 1-Wire Net communication speed
48
49 //---------------------------------------------------------------------------
50 // Attempt to resyc and detect a DS2480
51 //
52 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
53 //                OpenCOM to indicate the port number.
54 //
55 // Returns:  TRUE  - DS2480 detected successfully
56 //           FALSE - Could not detect DS2480
57 //
58 int DS2480Detect(int portnum)
59 {
60    uchar sendpacket[10],readbuffer[10];
61    short sendlen=0;
62    short rt=FALSE;
63
64    // reset modes
65    ULevel[portnum] = MODE_NORMAL;
66    UMode[portnum] = MODSEL_COMMAND;
67    UBaud[portnum] = PARMSET_9600;
68    USpeed[portnum] = SPEEDSEL_FLEX;
69
70    // set the baud rate to 9600
71    SetBaudCOM(portnum,(uchar)UBaud[portnum]);
72
73    // send a break to reset the DS2480
74    BreakCOM(portnum);
75
76    // delay to let line settle 
77    msDelay(2);
78
79    // flush the buffers
80    FlushCOM(portnum);
81
82    // send the timing byte 
83    sendpacket[0] = 0xC1;
84    if (WriteCOM(portnum,1,sendpacket) != 1) 
85       return FALSE;
86
87    // set the FLEX configuration parameters
88    // default PDSRC = 1.37Vus
89    sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus;
90    // default W1LT = 8us
91    sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write8us;
92    // default DSO/WORT = 10us
93    sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff10us;
94
95    // construct the command to read the baud rate (to test command block) 
96    sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
97
98    // also do 1 bit operation (to test 1-Wire block)
99    sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | UBaud[portnum] | BITPOL_ONE;
100
101    // flush the buffers
102    FlushCOM(portnum);
103    
104    // send the packet 
105    if (WriteCOM(portnum,sendlen,sendpacket)) 
106    {
107       // read back the response 
108       if (ReadCOM(portnum,5,readbuffer) == 5)
109       {    
110          // look at the baud rate and bit operation
111          // to see if the response makes sense
112          if (((readbuffer[3] & 0xF1) == 0x00) && 
113              ((readbuffer[3] & 0x0E) == UBaud[portnum]) && 
114              ((readbuffer[4] & 0xF0) == 0x90) &&                
115              ((readbuffer[4] & 0x0C) == UBaud[portnum]))  
116             return TRUE;
117       }
118    }
119
120    return FALSE;
121 }
122
123 //---------------------------------------------------------------------------
124 // Change the DS2480 from the current baud rate to the new baud rate.
125 //
126 // 'portnum' - number 0 to MAX_PORTNUM-1.  This number was provided to
127 //             OpenCOM to indicate the port number.
128 // 'newbaud' - the new baud rate to change to, defined as:
129 //               PARMSET_9600     0x00
130 //               PARMSET_19200    0x02
131 //               PARMSET_57600    0x04
132 //               PARMSET_115200   0x06
133 //
134 // Returns:  current DS2480 baud rate.
135 //
136 int DS2480ChangeBaud(int portnum, uchar newbaud)
137 {
138    int rt=FALSE;
139    uchar readbuffer[5],sendpacket[5],sendpacket2[5];
140    int sendlen=0,sendlen2=0;
141
142    // see if diffenent then current baud rate
143    if (UBaud[portnum] == newbaud)
144       return TRUE;
145    else
146    {
147       // build the command packet
148       // check if correct mode 
149       if (UMode[portnum] != MODSEL_COMMAND)
150       {
151          UMode[portnum] = MODSEL_COMMAND;
152          sendpacket[sendlen++] = MODE_COMMAND;
153       }
154       // build the command 
155       sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newbaud;
156
157       // flush the buffers
158       FlushCOM(portnum);
159       
160       // send the packet 
161       if (!WriteCOM(portnum,sendlen,sendpacket)) 
162          rt = FALSE;
163       else
164       {
165          // make sure buffer is flushed
166          msDelay(5);
167
168          // change our baud rate
169          SetBaudCOM(portnum,newbaud);
170          UBaud[portnum] = newbaud;
171
172          // wait for things to settle
173          msDelay(5);
174
175          // build a command packet to read back baud rate
176          sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
177
178          // flush the buffers
179          FlushCOM(portnum);
180
181          // send the packet 
182          if (WriteCOM(portnum,sendlen2,sendpacket2)) 
183          {
184             // read back the 1 byte response 
185             if (ReadCOM(portnum,1,readbuffer) == 1)
186             {
187                // verify correct baud 
188                if (((readbuffer[0] & 0x0E) == (sendpacket[sendlen-1] & 0x0E)))
189                   rt = TRUE;
190             }
191          }
192       }
193    }
194
195    // if lost communication with DS2480 then reset 
196    if (rt != TRUE)
197       DS2480Detect(portnum);
198
199    return UBaud[portnum];
200 }