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 // ds2480ut.c - DS2480 utility functions.
31 // History: 1.00 -> 1.01 Default PDSRC changed from 0.83 to 1.37V/us
32 // in DS2480Detect. Changed to use msDelay instead
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.
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
49 //---------------------------------------------------------------------------
50 // Attempt to resyc and detect a DS2480
52 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
53 // OpenCOM to indicate the port number.
55 // Returns: TRUE - DS2480 detected successfully
56 // FALSE - Could not detect DS2480
58 int DS2480Detect(int portnum)
60 uchar sendpacket[10],readbuffer[10];
65 ULevel[portnum] = MODE_NORMAL;
66 UMode[portnum] = MODSEL_COMMAND;
67 UBaud[portnum] = PARMSET_9600;
68 USpeed[portnum] = SPEEDSEL_FLEX;
70 // set the baud rate to 9600
71 SetBaudCOM(portnum,(uchar)UBaud[portnum]);
73 // send a break to reset the DS2480
76 // delay to let line settle
82 // send the timing byte
84 if (WriteCOM(portnum,1,sendpacket) != 1)
87 // set the FLEX configuration parameters
88 // default PDSRC = 1.37Vus
89 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus;
91 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write8us;
92 // default DSO/WORT = 10us
93 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff10us;
95 // construct the command to read the baud rate (to test command block)
96 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
98 // also do 1 bit operation (to test 1-Wire block)
99 sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | UBaud[portnum] | BITPOL_ONE;
105 if (WriteCOM(portnum,sendlen,sendpacket))
107 // read back the response
108 if (ReadCOM(portnum,5,readbuffer) == 5)
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]))
123 //---------------------------------------------------------------------------
124 // Change the DS2480 from the current baud rate to the new baud rate.
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:
130 // PARMSET_19200 0x02
131 // PARMSET_57600 0x04
132 // PARMSET_115200 0x06
134 // Returns: current DS2480 baud rate.
136 int DS2480ChangeBaud(int portnum, uchar newbaud)
139 uchar readbuffer[5],sendpacket[5],sendpacket2[5];
140 int sendlen=0,sendlen2=0;
142 // see if diffenent then current baud rate
143 if (UBaud[portnum] == newbaud)
147 // build the command packet
148 // check if correct mode
149 if (UMode[portnum] != MODSEL_COMMAND)
151 UMode[portnum] = MODSEL_COMMAND;
152 sendpacket[sendlen++] = MODE_COMMAND;
155 sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newbaud;
161 if (!WriteCOM(portnum,sendlen,sendpacket))
165 // make sure buffer is flushed
168 // change our baud rate
169 SetBaudCOM(portnum,newbaud);
170 UBaud[portnum] = newbaud;
172 // wait for things to settle
175 // build a command packet to read back baud rate
176 sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
182 if (WriteCOM(portnum,sendlen2,sendpacket2))
184 // read back the 1 byte response
185 if (ReadCOM(portnum,1,readbuffer) == 1)
187 // verify correct baud
188 if (((readbuffer[0] & 0x0E) == (sendpacket[sendlen-1] & 0x0E)))
195 // if lost communication with DS2480 then reset
197 DS2480Detect(portnum);
199 return UBaud[portnum];