7555714a78f28be05e79b656fa99c99c58252889
[owSlave2.git] / tools_cmd / rwOW / tmexlnk.c
1 //---------------------------------------------------------------------------
2 // Copyright (C) 2001 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 //  TMEXLnk.C - Link module to call on TMEX low-level functions to
28 //              excersize the general 1-Wire Net functions.
29 //              (Requires TMEX 3.11 or newer)
30 //
31 //  Version: 3.00
32 //
33 //  History: 1.00 -> 1.01  Return values in owLevel corrected.
34 //                         Added function msDelay.
35 //           1.02 -> 1.03  Add msGettick, always return owLevel success
36 //                         to hide adapters (DS9097E) that do not have
37 //                         power delivery capabilities.
38 //           1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for
39 //                         multiple ports.
40 //           2.10 -> 3.00  Added owReadBitPower and owWriteBytePower
41
42 #include "ownet.h"
43 #include <windows.h>
44
45 // external TMEX variables
46 extern long SessionHandle[MAX_PORTNUM];
47 extern uchar StateBuffer[MAX_PORTNUM][5120];
48 extern short far pascal TMTouchByte(long, short);
49 extern short far pascal TMTouchReset(long);
50 extern short far pascal TMTouchBit(long, short);
51 extern short far pascal TMProgramPulse(long);
52 extern short far pascal TMOneWireCom(long, short, short);
53 extern short far pascal TMOneWireLevel(long, short, short, short);
54
55 // globals
56 SMALLINT FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE = FALSE; // for compatibility purposes
57
58 //--------------------------------------------------------------------------
59 // Reset all of the devices on the 1-Wire Net and return the result.
60 //
61 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
62 //                 indicate the symbolic port number.
63 //
64 // Returns: TRUE(1):  presense pulse(s) detected, device(s) reset
65 //          FALSE(0): no presense pulses detected
66 //
67 SMALLINT owTouchReset(int portnum)
68 {
69    int result;
70
71    // Assume valid Session
72    result = TMTouchReset(SessionHandle[portnum]);
73
74    // success if the normal or alarm presence
75    if ((result == 1) || (result == 2))
76       return TRUE;
77    else
78       return FALSE;
79 }
80
81
82 //--------------------------------------------------------------------------
83 // Send 1 bit of communication to the 1-Wire Net and return the
84 // result 1 bit read from the 1-Wire Net.  The parameter 'sendbit'
85 // least significant bit is used and the least significant bit
86 // of the result is the return bit.
87 //
88 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
89 //                 indicate the symbolic port number.
90 // 'sendbit'     - the least significant bit is the bit to send
91 //
92 // Returns: 0:   0 bit read from sendbit
93 //          1:   1 bit read from sendbit
94 //
95 SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
96 {
97    // Assume valid Session
98    return TMTouchBit(SessionHandle[portnum],(short)sendbit);
99 }
100
101 //--------------------------------------------------------------------------
102 // Send 8 bits of communication to the 1-Wire Net and verify that the
103 // 8 bits read from the 1-Wire Net is the same (write operation).
104 // The parameter 'sendbyte' least significant 8 bits are used.
105 //
106 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
107 //                indicate the symbolic port number.
108 // 'sendbyte'   - 8 bits to send (least significant byte)
109 //
110 // Returns:  TRUE: bytes written and echo was the same
111 //           FALSE: echo was not the same
112 //
113 SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
114 {
115    // Assume valid Session
116    return TMTouchByte(SessionHandle[portnum],(short)sendbyte);
117 }
118
119 //--------------------------------------------------------------------------
120 // Send 8 bits of communication to the MicroLAN and verify that the
121 // 8 bits read from the MicroLAN is the same (write operation).
122 // The parameter 'sendbyte' least significant 8 bits are used.
123 //
124 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
125 //                indicate the symbolic port number.
126 // 'sendbyte'   - 8 bits to send (least significant byte)
127 //
128 // Returns:  TRUE: bytes written and echo was the same
129 //           FALSE: echo was not the same
130 //
131 SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
132 {
133    return (owTouchByte(portnum,sendbyte) == sendbyte) ? TRUE : FALSE;
134 }
135
136 //--------------------------------------------------------------------------
137 // Send 8 bits of read communication to the 1-Wire Net and and return the
138 // result 8 bits read from the 1-Wire Net.
139 //
140 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
141 //                 indicate the symbolic port number.
142 //
143 // Returns:  TRUE:  8 bytes read from 1-Wire Net
144 //           FALSE: the 8 bytes were not read
145 //
146 SMALLINT owReadByte(int portnum)
147 {
148    return owTouchByte(portnum,0xFF);
149 }
150
151 //--------------------------------------------------------------------------
152 // Set the 1-Wire Net communucation speed.
153 //
154 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
155 //                indicate the symbolic port number.
156 // 'new_speed'  - new speed defined as
157 //                MODE_NORMAL     0x00
158 //                MODE_OVERDRIVE  0x01
159 //
160 // Returns:  current 1-Wire Net speed
161 //
162 SMALLINT owSpeed(int portnum, SMALLINT new_speed)
163 {
164    return TMOneWireCom(SessionHandle[portnum],0,(short)new_speed);
165 }
166
167 //--------------------------------------------------------------------------
168 // Set the 1-Wire Net line level.  The values for new_level are
169 // as follows:
170 //
171 // 'portnum'   - number 0 to MAX_PORTNUM-1.  This number is provided to
172 //               indicate the symbolic port number.
173 // 'new_level' - new level defined as
174 //                MODE_NORMAL     0x00
175 //                MODE_STRONG5    0x02
176 //                MODE_PROGRAM    0x04
177 //                MODE_BREAK      0x08 (not supported)
178 //
179 // Returns:  current 1-Wire Net level
180 //
181 SMALLINT owLevel(int portnum, SMALLINT new_level)
182 {
183    int rslt;
184    int docheck = FALSE;
185
186    // check for DS2480 bug
187    if (((SessionHandle[portnum] & 0x0F0) == 0x050) &&
188        (TMOneWireLevel(SessionHandle[portnum],1,0,0) == 1))
189       docheck = TRUE;
190
191    switch (new_level)
192    {
193       case MODE_NORMAL:
194          rslt = TMOneWireLevel(SessionHandle[portnum],0,0,0);
195          // test code for DS2480 bug
196          if (docheck)
197             TMTouchBit(SessionHandle[portnum],1);
198          break;
199       case MODE_STRONG5:
200          rslt = TMOneWireLevel(SessionHandle[portnum],0,1,0);
201          break;
202       case MODE_PROGRAM:
203          rslt = TMOneWireLevel(SessionHandle[portnum],0,3,0);
204          break;
205       case MODE_BREAK:
206          rslt = TMOneWireLevel(SessionHandle[portnum],0,2,0);
207          break;
208       default:
209          rslt = 0;
210    }
211
212    // Assume TMEX can do it so always return NewLevel
213    return new_level;
214 }
215
216 //--------------------------------------------------------------------------
217 // This procedure creates a fixed 480 microseconds 12 volt pulse
218 // on the 1-Wire Net for programming EPROM iButtons.
219 //
220 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
221 //                 indicate the symbolic port number.
222 //
223 // Returns:  TRUE  successful
224 //           FALSE program voltage not available
225 //
226 SMALLINT owProgramPulse(int portnum)
227 {
228    return TMProgramPulse(SessionHandle[portnum]);
229 }
230
231 //--------------------------------------------------------------------------
232 //  Description:
233 //     Delay for at least 'len' ms
234 //
235 void msDelay(int len)
236 {
237    Sleep(len);
238 }
239
240 //--------------------------------------------------------------------------
241 // Get the current millisecond tick count.  Does not have to represent
242 // an actual time, it just needs to be an incrementing timer.
243 //
244 long msGettick(void)
245 {
246    return GetTickCount();
247 }
248
249 //--------------------------------------------------------------------------
250 // Send 8 bits of communication to the 1-Wire Net and verify that the
251 // 8 bits read from the 1-Wire Net is the same (write operation).  
252 // The parameter 'sendbyte' least significant 8 bits are used.  After the
253 // 8 bits are sent change the level of the 1-Wire net.
254 //
255 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
256 //              OpenCOM to indicate the port number.
257 // 'sendbyte' - 8 bits to send (least significant byte)
258 //
259 // Returns:  TRUE: bytes written and echo was the same
260 //           FALSE: echo was not the same 
261 //
262 SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte)
263 {
264    // prime for power delivery after byte
265    TMOneWireLevel(SessionHandle[portnum],0,1,2);
266
267    // send the byte and start strong pullup
268    if(TMTouchByte(SessionHandle[portnum],(short)sendbyte) != sendbyte)
269    {
270       TMOneWireLevel(SessionHandle[portnum],0,0,0);
271       return FALSE;
272    }
273
274    return TRUE;
275 }
276
277 //--------------------------------------------------------------------------
278 // Read 8 bits of communication to the 1-Wire Net and verify that the
279 // 8 bits read from the 1-Wire Net is the same (write operation).  
280 // The parameter 'sendbyte' least significant 8 bits are used.  After the
281 // 8 bits are read change the level of the 1-Wire net.
282 //
283 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
284 //              OpenCOM to indicate the port number.
285 // 'sendbyte' - 8 bits to send (least significant byte)
286 //
287 // Returns:  TRUE: bytes written and echo was the same
288 //           FALSE: echo was not the same 
289 //
290 SMALLINT owReadBytePower(int portnum)
291 {
292    SMALLINT sendbyte = 0xFF;
293
294    // prime for power delivery after byte
295    TMOneWireLevel(SessionHandle[portnum],0,1,2);
296
297    // send the byte and start strong pullup
298    return TMTouchByte(SessionHandle[portnum],(short)sendbyte);
299 }
300
301 //--------------------------------------------------------------------------
302 // Send 1 bit of communication to the 1-Wire Net and verify that the
303 // response matches the 'applyPowerResponse' bit and apply power delivery
304 // to the 1-Wire net.  Note that some implementations may apply the power
305 // first and then turn it off if the response is incorrect.
306 //
307 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
308 //              OpenCOM to indicate the port number.
309 // 'applyPowerResponse' - 1 bit response to check, if correct then start
310 //                        power delivery 
311 //
312 // Returns:  TRUE: bit written and response correct, strong pullup now on
313 //           FALSE: response incorrect
314 //
315 SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse)
316 {
317    // prime for power delivery after bit
318    TMOneWireLevel(SessionHandle[portnum],0,1,1);
319
320    // send the byte and start strong pullup
321    if(TMTouchBit(SessionHandle[portnum],0x01) != applyPowerResponse)
322    {
323       TMOneWireLevel(SessionHandle[portnum],0,0,0);
324       return FALSE;
325    }
326
327    return TRUE;
328 }
329
330 //--------------------------------------------------------------------------
331 // This procedure indicates wether the adapter can deliver power.
332 //
333 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
334 //              OpenCOM to indicate the port number.
335 //
336 // Returns:  TRUE  because all userial adapters have over drive. 
337 //
338 SMALLINT owHasPowerDelivery(int portnum)
339 {
340    return TRUE;
341 }
342
343 //--------------------------------------------------------------------------
344 // This procedure indicates wether the adapter can deliver power.
345 //
346 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
347 //              OpenCOM to indicate the port number.
348 //
349 // Returns:  TRUE  because all userial adapters have over drive. 
350 //
351 SMALLINT owHasOverDrive(int portnum)
352 {
353    return TRUE;
354 }
355
356 //--------------------------------------------------------------------------
357 // This procedure creates a fixed 480 microseconds 12 volt pulse 
358 // on the 1-Wire Net for programming EPROM iButtons.
359 //
360 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
361 //              OpenCOM to indicate the port number.
362 //
363 // Returns:  TRUE  program volatage available
364 //           FALSE program voltage not available  
365 SMALLINT owHasProgramPulse(int portnum)
366 {
367    return TMProgramPulse(SessionHandle[portnum]);
368 }