1 //---------------------------------------------------------------------------
\r
2 // Copyright (C) 1999 Dallas Semiconductor Corporation, All Rights Reserved.
\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
11 // The above copyright notice and this permission notice shall be included
\r
12 // in all copies or substantial portions of the Software.
\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
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
25 //---------------------------------------------------------------------------
\r
27 // tmexnet.C - Wrapper class to hook 1-Wire Public Domain API to TMEX API
\r
28 // for transport functions.
\r
34 #include <windows.h>
\r
36 // external TMEX variables
\r
37 extern long SessionHandle[MAX_PORTNUM];
\r
38 extern short far pascal TMBlockIO(long, uchar *, short);
\r
39 extern short far pascal TMBlockStream(long, uchar *, short);
\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
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
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
60 // The maximum tran_len is 64
\r
62 SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len)
\r
66 // check for a block too big
\r
69 OWERROR(OWERROR_BLOCK_TOO_BIG);
\r
73 // check if need to do a owTouchReset first
\r
75 rslt = TMBlockIO(SessionHandle[portnum], tran_buf, (short)tran_len);
\r
77 rslt = TMBlockStream(SessionHandle[portnum], tran_buf, (short)tran_len);
\r
79 return (rslt == tran_len);
\r
82 //--------------------------------------------------------------------------
\r
83 // Write a byte to an EPROM 1-Wire device.
\r
85 // Supported devices: crc_type=0(CRC8)
\r
87 // crc_type=1(CRC16)
\r
88 // DS1985, DS1986, DS2407
\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
101 // Returns: >=0 success, this is the resulting byte from the program
\r
103 // -1 error, device not connected or program pulse voltage
\r
106 SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd,
\r
107 SMALLINT crc_type, SMALLINT do_access)
\r
112 // optionally access the device
\r
115 if (!owAccess(portnum))
\r
117 OWERROR(OWERROR_ACCESS_FAILED);
\r
121 // send the write command
\r
122 if (!owWriteByte(portnum,write_cmd))
\r
124 OWERROR(OWERROR_WRITE_BYTE_FAILED);
\r
128 // send the address
\r
129 if (!owWriteByte(portnum,addr & 0xFF) || !owWriteByte(portnum,addr >> 8))
\r
131 OWERROR(OWERROR_WRITE_BYTE_FAILED);
\r
136 // send the data to write
\r
137 if (!owWriteByte(portnum,write_byte))
\r
139 OWERROR(OWERROR_WRITE_BYTE_FAILED);
\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
155 setcrc8(portnum,(uchar)(addr & 0xFF));
\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
163 OWERROR(OWERROR_CRC_FAILED);
\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
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
188 // send the program pulse
\r
189 if (!owProgramPulse(portnum))
\r
191 OWERROR(OWERROR_PROGRAM_PULSE_FAILED);
\r
195 // read back and return the resulting byte
\r
196 return owReadByte(portnum);
\r