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