User | Revision | Line number | New contents of line |
svlach |
0:70c3bea805ee
|
1
|
//---------------------------------------------------------------------------
|
svlach |
0:70c3bea805ee
|
2
|
// Copyright (C) 2000v Dallas Semiconductor Corporation, All Rights Reserved.
|
svlach |
0:70c3bea805ee
|
3
|
//
|
svlach |
0:70c3bea805ee
|
4
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
svlach |
0:70c3bea805ee
|
5
|
// copy of this software and associated documentation files (the "Software"),
|
svlach |
0:70c3bea805ee
|
6
|
// to deal in the Software without restriction, including without limitation
|
svlach |
0:70c3bea805ee
|
7
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
svlach |
0:70c3bea805ee
|
8
|
// and/or sell copies of the Software, and to permit persons to whom the
|
svlach |
0:70c3bea805ee
|
9
|
// Software is furnished to do so, subject to the following conditions:
|
svlach |
0:70c3bea805ee
|
10
|
//
|
svlach |
0:70c3bea805ee
|
11
|
// The above copyright notice and this permission notice shall be included
|
svlach |
0:70c3bea805ee
|
12
|
// in all copies or substantial portions of the Software.
|
svlach |
0:70c3bea805ee
|
13
|
//
|
svlach |
0:70c3bea805ee
|
14
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
svlach |
0:70c3bea805ee
|
15
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
svlach |
0:70c3bea805ee
|
16
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
svlach |
0:70c3bea805ee
|
17
|
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
|
svlach |
0:70c3bea805ee
|
18
|
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
svlach |
0:70c3bea805ee
|
19
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
svlach |
0:70c3bea805ee
|
20
|
// OTHER DEALINGS IN THE SOFTWARE.
|
svlach |
0:70c3bea805ee
|
21
|
//
|
svlach |
0:70c3bea805ee
|
22
|
// Except as contained in this notice, the name of Dallas Semiconductor
|
svlach |
0:70c3bea805ee
|
23
|
// shall not be used except as stated in the Dallas Semiconductor
|
svlach |
0:70c3bea805ee
|
24
|
// Branding Policy.
|
svlach |
0:70c3bea805ee
|
25
|
//---------------------------------------------------------------------------
|
svlach |
0:70c3bea805ee
|
26
|
//
|
svlach |
0:70c3bea805ee
|
27
|
// ds2480ut.c - DS2480B utility functions.
|
svlach |
0:70c3bea805ee
|
28
|
//
|
svlach |
0:70c3bea805ee
|
29
|
// Version: 2.01
|
svlach |
0:70c3bea805ee
|
30
|
//
|
svlach |
0:70c3bea805ee
|
31
|
// History: 1.00 -> 1.01 Default PDSRC changed from 0.83 to 1.37V/us
|
svlach |
0:70c3bea805ee
|
32
|
// in DS2480Detect. Changed to use msDelay instead
|
svlach |
0:70c3bea805ee
|
33
|
// of Delay.
|
svlach |
0:70c3bea805ee
|
34
|
// 1.01 -> 1.02 Changed global declarations from 'uchar' to 'int'.
|
svlach |
0:70c3bea805ee
|
35
|
// Changed DSO/WORT from 7 to 10us in DS2480Detect.
|
svlach |
0:70c3bea805ee
|
36
|
// 1.02 -> 1.03 Removed caps in #includes for Linux capatibility
|
svlach |
0:70c3bea805ee
|
37
|
// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
|
svlach |
0:70c3bea805ee
|
38
|
// multiple ports. Changed W1LT to 8us.
|
svlach |
0:70c3bea805ee
|
39
|
// 2.00 -> 2.01 Added error handling. Added circular-include check.
|
svlach |
0:70c3bea805ee
|
40
|
// 2.01 -> 2.10 Added raw memory error handling and SMALLINT
|
svlach |
0:70c3bea805ee
|
41
|
// 2.10 -> 3.00 Added memory bank functionality
|
svlach |
0:70c3bea805ee
|
42
|
// Added file I/O operations
|
svlach |
0:70c3bea805ee
|
43
|
//
|
svlach |
0:70c3bea805ee
|
44
|
|
svlach |
1:dcf2f8359398
|
45
|
#include "ownet.h"
|
svlach |
0:70c3bea805ee
|
46
|
#include "./Headers/ds2480.h"
|
svlach |
0:70c3bea805ee
|
47
|
|
svlach |
0:70c3bea805ee
|
48
|
// global DS2480B state
|
svlach |
0:70c3bea805ee
|
49
|
SMALLINT ULevel[MAX_PORTNUM]; // current DS2480B 1-Wire Net level
|
svlach |
0:70c3bea805ee
|
50
|
SMALLINT UBaud[MAX_PORTNUM]; // current DS2480B baud rate
|
svlach |
0:70c3bea805ee
|
51
|
SMALLINT UMode[MAX_PORTNUM]; // current DS2480B command or data mode state
|
svlach |
0:70c3bea805ee
|
52
|
SMALLINT USpeed[MAX_PORTNUM]; // current DS2480B 1-Wire Net communication speed
|
svlach |
0:70c3bea805ee
|
53
|
SMALLINT UVersion[MAX_PORTNUM]; // current DS2480B version
|
svlach |
0:70c3bea805ee
|
54
|
|
svlach |
0:70c3bea805ee
|
55
|
//---------------------------------------------------------------------------
|
svlach |
0:70c3bea805ee
|
56
|
// Attempt to resyc and detect a DS2480B
|
svlach |
0:70c3bea805ee
|
57
|
//
|
svlach |
0:70c3bea805ee
|
58
|
// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
|
svlach |
0:70c3bea805ee
|
59
|
// OpenCOM to indicate the port number.
|
svlach |
0:70c3bea805ee
|
60
|
//
|
svlach |
0:70c3bea805ee
|
61
|
// Returns: TRUE - DS2480B detected successfully
|
svlach |
0:70c3bea805ee
|
62
|
// FALSE - Could not detect DS2480B
|
svlach |
0:70c3bea805ee
|
63
|
//
|
svlach |
0:70c3bea805ee
|
64
|
SMALLINT DS2480Detect(int portnum)
|
svlach |
0:70c3bea805ee
|
65
|
{
|
svlach |
0:70c3bea805ee
|
66
|
uchar sendpacket[10],readbuffer[10];
|
svlach |
0:70c3bea805ee
|
67
|
uchar sendlen=0;
|
svlach |
0:70c3bea805ee
|
68
|
|
svlach |
0:70c3bea805ee
|
69
|
// reset modes
|
svlach |
0:70c3bea805ee
|
70
|
UMode[portnum] = MODSEL_COMMAND;
|
svlach |
0:70c3bea805ee
|
71
|
UBaud[portnum] = PARMSET_INV_9600;
|
svlach |
0:70c3bea805ee
|
72
|
USpeed[portnum] = SPEEDSEL_FLEX;
|
svlach |
0:70c3bea805ee
|
73
|
|
svlach |
0:70c3bea805ee
|
74
|
// set the baud rate to 9600
|
svlach |
0:70c3bea805ee
|
75
|
SetBaudCOM(portnum,(uchar)UBaud[portnum]);
|
svlach |
0:70c3bea805ee
|
76
|
|
svlach |
0:70c3bea805ee
|
77
|
// send a break to reset the DS2480
|
svlach |
0:70c3bea805ee
|
78
|
BreakCOM(portnum);
|
svlach |
0:70c3bea805ee
|
79
|
|
svlach |
0:70c3bea805ee
|
80
|
// delay to let line settle
|
svlach |
0:70c3bea805ee
|
81
|
msDelay(2);
|
svlach |
0:70c3bea805ee
|
82
|
|
svlach |
0:70c3bea805ee
|
83
|
// flush the buffers
|
svlach |
0:70c3bea805ee
|
84
|
FlushCOM(portnum);
|
svlach |
0:70c3bea805ee
|
85
|
|
svlach |
0:70c3bea805ee
|
86
|
// send the reset byte
|
svlach |
0:70c3bea805ee
|
87
|
sendpacket[0] = 0xC1;
|
svlach |
0:70c3bea805ee
|
88
|
if (WriteCOM(portnum,1,sendpacket) != 1)
|
svlach |
0:70c3bea805ee
|
89
|
{
|
svlach |
0:70c3bea805ee
|
90
|
OWERROR(OWERROR_WRITECOM_FAILED);
|
svlach |
0:70c3bea805ee
|
91
|
return FALSE;
|
svlach |
0:70c3bea805ee
|
92
|
}
|
svlach |
0:70c3bea805ee
|
93
|
|
svlach |
0:70c3bea805ee
|
94
|
// delay to let line settle
|
svlach |
0:70c3bea805ee
|
95
|
msDelay(4);
|
svlach |
0:70c3bea805ee
|
96
|
|
svlach |
0:70c3bea805ee
|
97
|
// send the Baud Rate byte, which also inverts RX signal
|
svlach |
0:70c3bea805ee
|
98
|
sendpacket[0] = 0x79;
|
svlach |
0:70c3bea805ee
|
99
|
if (WriteCOM(portnum,1,sendpacket) != 1)
|
svlach |
0:70c3bea805ee
|
100
|
{
|
svlach |
0:70c3bea805ee
|
101
|
OWERROR(OWERROR_WRITECOM_FAILED);
|
svlach |
0:70c3bea805ee
|
102
|
return FALSE;
|
svlach |
0:70c3bea805ee
|
103
|
}
|
svlach |
0:70c3bea805ee
|
104
|
|
svlach |
0:70c3bea805ee
|
105
|
// delay to let line settle
|
svlach |
0:70c3bea805ee
|
106
|
msDelay(4);
|
svlach |
0:70c3bea805ee
|
107
|
|
svlach |
0:70c3bea805ee
|
108
|
// set the FLEX configuration parameters
|
svlach |
0:70c3bea805ee
|
109
|
// default PDSRC = 1.37Vus
|
svlach |
0:70c3bea805ee
|
110
|
sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus;
|
svlach |
0:70c3bea805ee
|
111
|
// default W1LT = 10us
|
svlach |
0:70c3bea805ee
|
112
|
sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write10us;
|
svlach |
0:70c3bea805ee
|
113
|
// default DSO/WORT = 8us
|
svlach |
0:70c3bea805ee
|
114
|
sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff8us;
|
svlach |
0:70c3bea805ee
|
115
|
|
svlach |
0:70c3bea805ee
|
116
|
// construct the command to read the baud rate (to test command block)
|
svlach |
0:70c3bea805ee
|
117
|
sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
|
svlach |
0:70c3bea805ee
|
118
|
|
svlach |
0:70c3bea805ee
|
119
|
// also do 1 bit operation (to test 1-Wire block)
|
svlach |
0:70c3bea805ee
|
120
|
sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | UBaud[portnum] | BITPOL_ONE;
|
svlach |
0:70c3bea805ee
|
121
|
|
svlach |
0:70c3bea805ee
|
122
|
|
svlach |
0:70c3bea805ee
|
123
|
// flush the buffers
|
svlach |
0:70c3bea805ee
|
124
|
FlushCOM(portnum);
|
svlach |
0:70c3bea805ee
|
125
|
|
svlach |
0:70c3bea805ee
|
126
|
// send the packet
|
svlach |
0:70c3bea805ee
|
127
|
if (WriteCOM(portnum,sendlen,sendpacket))
|
svlach |
0:70c3bea805ee
|
128
|
{
|
svlach |
0:70c3bea805ee
|
129
|
// read back the response
|
svlach |
0:70c3bea805ee
|
130
|
if (ReadCOM(portnum,5,readbuffer) == 5)
|
svlach |
0:70c3bea805ee
|
131
|
{
|
svlach |
0:70c3bea805ee
|
132
|
// look at the baud rate and bit operation
|
svlach |
0:70c3bea805ee
|
133
|
// to see if the response makes sense
|
svlach |
0:70c3bea805ee
|
134
|
if (((readbuffer[3] & 0xF1) == 0x00) &&
|
svlach |
0:70c3bea805ee
|
135
|
((readbuffer[3] & 0x0E) == UBaud[portnum]) &&
|
svlach |
0:70c3bea805ee
|
136
|
((readbuffer[4] & 0xF0) == 0x90) &&
|
svlach |
0:70c3bea805ee
|
137
|
((readbuffer[4] & 0x0C) == UBaud[portnum]))
|
svlach |
0:70c3bea805ee
|
138
|
return TRUE;
|
svlach |
0:70c3bea805ee
|
139
|
else
|
svlach |
0:70c3bea805ee
|
140
|
OWERROR(OWERROR_DS2480_BAD_RESPONSE);
|
svlach |
0:70c3bea805ee
|
141
|
}
|
svlach |
0:70c3bea805ee
|
142
|
else
|
svlach |
0:70c3bea805ee
|
143
|
OWERROR(OWERROR_READCOM_FAILED);
|
svlach |
0:70c3bea805ee
|
144
|
}
|
svlach |
0:70c3bea805ee
|
145
|
else
|
svlach |
0:70c3bea805ee
|
146
|
OWERROR(OWERROR_WRITECOM_FAILED);
|
svlach |
0:70c3bea805ee
|
147
|
|
svlach |
0:70c3bea805ee
|
148
|
return FALSE;
|
svlach |
0:70c3bea805ee
|
149
|
}
|
svlach |
0:70c3bea805ee
|
150
|
|
svlach |
0:70c3bea805ee
|
151
|
//---------------------------------------------------------------------------
|
svlach |
0:70c3bea805ee
|
152
|
// Change the DS2480B from the current baud rate to the new baud rate.
|
svlach |
0:70c3bea805ee
|
153
|
//
|
svlach |
0:70c3bea805ee
|
154
|
// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
|
svlach |
0:70c3bea805ee
|
155
|
// OpenCOM to indicate the port number.
|
svlach |
0:70c3bea805ee
|
156
|
// 'newbaud' - the new baud rate to change to, defined as:
|
svlach |
0:70c3bea805ee
|
157
|
// PARMSET_9600 0x00
|
svlach |
0:70c3bea805ee
|
158
|
// PARMSET_19200 0x02
|
svlach |
0:70c3bea805ee
|
159
|
// PARMSET_57600 0x04
|
svlach |
0:70c3bea805ee
|
160
|
// PARMSET_115200 0x06
|
svlach |
0:70c3bea805ee
|
161
|
//
|
svlach |
0:70c3bea805ee
|
162
|
// Returns: current DS2480B baud rate.
|
svlach |
0:70c3bea805ee
|
163
|
//
|
svlach |
0:70c3bea805ee
|
164
|
SMALLINT DS2480ChangeBaud(int portnum, uchar newbaud)
|
svlach |
0:70c3bea805ee
|
165
|
{
|
svlach |
0:70c3bea805ee
|
166
|
uchar rt=FALSE;
|
svlach |
0:70c3bea805ee
|
167
|
uchar readbuffer[5],sendpacket[5],sendpacket2[5];
|
svlach |
0:70c3bea805ee
|
168
|
uchar sendlen=0,sendlen2=0;
|
svlach |
0:70c3bea805ee
|
169
|
|
svlach |
0:70c3bea805ee
|
170
|
// see if diffenent then current baud rate
|
svlach |
0:70c3bea805ee
|
171
|
if (UBaud[portnum] == newbaud)
|
svlach |
0:70c3bea805ee
|
172
|
return UBaud[portnum];
|
svlach |
0:70c3bea805ee
|
173
|
else
|
svlach |
0:70c3bea805ee
|
174
|
{
|
svlach |
0:70c3bea805ee
|
175
|
// build the command packet
|
svlach |
0:70c3bea805ee
|
176
|
// check if correct mode
|
svlach |
0:70c3bea805ee
|
177
|
if (UMode[portnum] != MODSEL_COMMAND)
|
svlach |
0:70c3bea805ee
|
178
|
{
|
svlach |
0:70c3bea805ee
|
179
|
UMode[portnum] = MODSEL_COMMAND;
|
svlach |
0:70c3bea805ee
|
180
|
sendpacket[sendlen++] = MODE_COMMAND;
|
svlach |
0:70c3bea805ee
|
181
|
}
|
svlach |
0:70c3bea805ee
|
182
|
// build the command
|
svlach |
0:70c3bea805ee
|
183
|
sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newbaud;
|
svlach |
0:70c3bea805ee
|
184
|
|
svlach |
0:70c3bea805ee
|
185
|
// flush the buffers
|
svlach |
0:70c3bea805ee
|
186
|
FlushCOM(portnum);
|
svlach |
0:70c3bea805ee
|
187
|
|
svlach |
0:70c3bea805ee
|
188
|
// send the packet
|
svlach |
0:70c3bea805ee
|
189
|
if (!WriteCOM(portnum,sendlen,sendpacket))
|
svlach |
0:70c3bea805ee
|
190
|
{
|
svlach |
0:70c3bea805ee
|
191
|
OWERROR(OWERROR_WRITECOM_FAILED);
|
svlach |
0:70c3bea805ee
|
192
|
rt = FALSE;
|
svlach |
0:70c3bea805ee
|
193
|
}
|
svlach |
0:70c3bea805ee
|
194
|
else
|
svlach |
0:70c3bea805ee
|
195
|
{
|
svlach |
0:70c3bea805ee
|
196
|
// make sure buffer is flushed
|
svlach |
0:70c3bea805ee
|
197
|
msDelay(5);
|
svlach |
0:70c3bea805ee
|
198
|
|
svlach |
0:70c3bea805ee
|
199
|
// change our baud rate
|
svlach |
0:70c3bea805ee
|
200
|
SetBaudCOM(portnum,newbaud);
|
svlach |
0:70c3bea805ee
|
201
|
UBaud[portnum] = newbaud;
|
svlach |
0:70c3bea805ee
|
202
|
|
svlach |
0:70c3bea805ee
|
203
|
// wait for things to settle
|
svlach |
0:70c3bea805ee
|
204
|
msDelay(5);
|
svlach |
0:70c3bea805ee
|
205
|
|
svlach |
0:70c3bea805ee
|
206
|
// build a command packet to read back baud rate
|
svlach |
0:70c3bea805ee
|
207
|
sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3);
|
svlach |
0:70c3bea805ee
|
208
|
|
svlach |
0:70c3bea805ee
|
209
|
// flush the buffers
|
svlach |
0:70c3bea805ee
|
210
|
FlushCOM(portnum);
|
svlach |
0:70c3bea805ee
|
211
|
|
svlach |
0:70c3bea805ee
|
212
|
// send the packet
|
svlach |
0:70c3bea805ee
|
213
|
if (WriteCOM(portnum,sendlen2,sendpacket2))
|
svlach |
0:70c3bea805ee
|
214
|
{
|
svlach |
0:70c3bea805ee
|
215
|
// read back the 1 byte response
|
svlach |
0:70c3bea805ee
|
216
|
if (ReadCOM(portnum,1,readbuffer) == 1)
|
svlach |
0:70c3bea805ee
|
217
|
{
|
svlach |
0:70c3bea805ee
|
218
|
// verify correct baud
|
svlach |
0:70c3bea805ee
|
219
|
if (((readbuffer[0] & 0x0E) == (sendpacket[sendlen-1] & 0x0E)))
|
svlach |
0:70c3bea805ee
|
220
|
rt = TRUE;
|
svlach |
0:70c3bea805ee
|
221
|
else
|
svlach |
0:70c3bea805ee
|
222
|
OWERROR(OWERROR_DS2480_WRONG_BAUD);
|
svlach |
0:70c3bea805ee
|
223
|
}
|
svlach |
0:70c3bea805ee
|
224
|
else
|
svlach |
0:70c3bea805ee
|
225
|
OWERROR(OWERROR_READCOM_FAILED);
|
svlach |
0:70c3bea805ee
|
226
|
}
|
svlach |
0:70c3bea805ee
|
227
|
else
|
svlach |
0:70c3bea805ee
|
228
|
OWERROR(OWERROR_WRITECOM_FAILED);
|
svlach |
0:70c3bea805ee
|
229
|
}
|
svlach |
0:70c3bea805ee
|
230
|
}
|
svlach |
0:70c3bea805ee
|
231
|
|
svlach |
0:70c3bea805ee
|
232
|
// if lost communication with DS2480 then reset
|
svlach |
0:70c3bea805ee
|
233
|
if (rt != TRUE)
|
svlach |
0:70c3bea805ee
|
234
|
DS2480Detect(portnum);
|
svlach |
0:70c3bea805ee
|
235
|
|
svlach |
0:70c3bea805ee
|
236
|
return UBaud[portnum];
|
svlach |
0:70c3bea805ee
|
237
|
}
|