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