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