Works now with Windows Visual Studio C++ too
[owTools.git] / windows / owTools / tmextran.cpp
1 //---------------------------------------------------------------------------\r
2 // Copyright (C) 1999 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 //  tmexnet.C - Wrapper class to hook 1-Wire Public Domain API to TMEX API\r
28 //              for transport functions.\r
29 //\r
30 //  Version: 3.00\r
31 //\r
32 \r
33 #include "ownet.h"\r
34 #include <windows.h>\r
35 \r
36 // external TMEX variables\r
37 extern long SessionHandle[MAX_PORTNUM];\r
38 extern "C" short far pascal TMBlockIO(long, uchar *, short);\r
39 extern "C" short far pascal TMBlockStream(long, uchar *, short);\r
40 \r
41 //--------------------------------------------------------------------------\r
42 // The 'owBlock' transfers a block of data to and from the\r
43 // 1-Wire Net with an optional reset at the begining of communication.\r
44 // The result is returned in the same buffer.\r
45 //\r
46 // 'do_reset' - cause a owTouchReset to occure at the begining of\r
47 //              communication TRUE(1) or not FALSE(0)\r
48 // 'tran_buf' - pointer to a block of unsigned\r
49 //              chars of length 'TranferLength' that will be sent\r
50 //              to the 1-Wire Net\r
51 // 'tran_len' - length in bytes to transfer\r
52 // Supported devices: all\r
53 //\r
54 // Returns:   TRUE (1) : The optional reset returned a valid\r
55 //                       presence (do_reset == TRUE) or there\r
56 //                       was no reset required.\r
57 //            FALSE (0): The reset did not return a valid prsence\r
58 //                       (do_reset == TRUE).\r
59 //\r
60 //  The maximum tran_len is 64\r
61 //\r
62 SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len)\r
63 {\r
64    short rslt;\r
65 \r
66    // check for a block too big\r
67    if (tran_len > 192)\r
68    {\r
69       OWERROR(OWERROR_BLOCK_TOO_BIG);\r
70       return FALSE;\r
71    }\r
72 \r
73    // check if need to do a owTouchReset first\r
74    if (do_reset)\r
75       rslt = TMBlockIO(SessionHandle[portnum], tran_buf, (short)tran_len);\r
76    else\r
77       rslt = TMBlockStream(SessionHandle[portnum], tran_buf, (short)tran_len);\r
78 \r
79    return (rslt == tran_len);\r
80 }\r
81 \r
82 //--------------------------------------------------------------------------\r
83 // Write a byte to an EPROM 1-Wire device.\r
84 //\r
85 // Supported devices: crc_type=0(CRC8)\r
86 //                        DS1982\r
87 //                    crc_type=1(CRC16)\r
88 //                        DS1985, DS1986, DS2407\r
89 //\r
90 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to\r
91 //                indicate the symbolic port number.\r
92 // 'write_byte' - byte to program\r
93 // 'addr'       - address of byte to program\r
94 // 'write_cmd'  - command used to write (0x0F reg mem, 0x55 status)\r
95 // 'crc_type'   - CRC used (0 CRC8, 1 CRC16)\r
96 // 'do_access'  - Flag to access device for each byte\r
97 //                (0 skip access, 1 do the access)\r
98 //                WARNING, only use do_access=0 if programing the NEXT\r
99 //                byte immediatly after the previous byte.\r
100 //\r
101 // Returns: >=0   success, this is the resulting byte from the program\r
102 //                effort\r
103 //          -1    error, device not connected or program pulse voltage\r
104 //                not available\r
105 //\r
106 SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd,\r
107                        SMALLINT crc_type, SMALLINT do_access)\r
108 {\r
109    ushort lastcrc16;\r
110    uchar lastcrc8;\r
111 \r
112    // optionally access the device\r
113    if (do_access)\r
114    {\r
115       if (!owAccess(portnum))\r
116       {\r
117          OWERROR(OWERROR_ACCESS_FAILED);\r
118          return -1;\r
119       }\r
120 \r
121       // send the write command\r
122       if (!owWriteByte(portnum,write_cmd))\r
123       {\r
124          OWERROR(OWERROR_WRITE_BYTE_FAILED);\r
125          return -1;\r
126       }\r
127 \r
128       // send the address\r
129       if (!owWriteByte(portnum,addr & 0xFF) || !owWriteByte(portnum,addr >> 8))\r
130       {\r
131          OWERROR(OWERROR_WRITE_BYTE_FAILED);\r
132          return -1;\r
133       }\r
134    }\r
135 \r
136    // send the data to write\r
137    if (!owWriteByte(portnum,write_byte))\r
138    {\r
139       OWERROR(OWERROR_WRITE_BYTE_FAILED);\r
140       return -1;\r
141    }\r
142 \r
143    // read the CRC\r
144    if (crc_type == 0)\r
145    {\r
146       // calculate CRC8\r
147       if (do_access)\r
148       {\r
149          setcrc8(portnum,0);\r
150          docrc8(portnum,(uchar)write_cmd);\r
151          docrc8(portnum,(uchar)(addr & 0xFF));\r
152          docrc8(portnum,(uchar)(addr >> 8));\r
153       }\r
154       else\r
155          setcrc8(portnum,(uchar)(addr & 0xFF));\r
156 \r
157       docrc8(portnum,(uchar)write_byte);\r
158       // read and calculate the read crc\r
159       lastcrc8 = docrc8(portnum,(uchar)owReadByte(portnum));\r
160       // crc should now be 0x00\r
161       if (lastcrc8 != 0)\r
162       {\r
163          OWERROR(OWERROR_CRC_FAILED);\r
164          return -1;\r
165       }\r
166    }\r
167    else\r
168    {\r
169       // CRC16\r
170       if (do_access)\r
171       {\r
172          setcrc16(portnum,0);\r
173          docrc16(portnum,(ushort)write_cmd);\r
174          docrc16(portnum,(ushort)(addr & 0xFF));\r
175          docrc16(portnum,(ushort)(addr >> 8));\r
176       }\r
177       else\r
178          setcrc16(portnum,(ushort)addr);\r
179       docrc16(portnum,(ushort)write_byte);\r
180       // read and calculate the read crc\r
181       docrc16(portnum,(ushort)owReadByte(portnum));\r
182       lastcrc16 = docrc16(portnum,(ushort)owReadByte(portnum));\r
183       // crc should now be 0xB001\r
184       if (lastcrc16 != 0xB001)\r
185          return -1;\r
186    }\r
187 \r
188    // send the program pulse\r
189    if (!owProgramPulse(portnum))\r
190    {\r
191       OWERROR(OWERROR_PROGRAM_PULSE_FAILED);\r
192       return -1;\r
193    }\r
194 \r
195    // read back and return the resulting byte\r
196    return owReadByte(portnum);\r
197 }\r
198 \r
199 \r