1 //---------------------------------------------------------------------------
2 // Copyright (C) 1999 Dallas Semiconductor Corporation, All Rights Reserved.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 // Except as contained in this notice, the name of Dallas Semiconductor
23 // shall not be used except as stated in the Dallas Semiconductor
25 //---------------------------------------------------------------------------
27 // tmexnet.C - Wrapper class to hook 1-Wire Public Domain API to TMEX API
28 // for transport functions.
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);
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.
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
51 // 'tran_len' - length in bytes to transfer
52 // Supported devices: all
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).
60 // The maximum tran_len is 64
62 SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len)
66 // check for a block too big
69 OWERROR(OWERROR_BLOCK_TOO_BIG);
73 // check if need to do a owTouchReset first
75 rslt = TMBlockIO(SessionHandle[portnum], tran_buf, (short)tran_len);
77 rslt = TMBlockStream(SessionHandle[portnum], tran_buf, (short)tran_len);
79 return (rslt == tran_len);
82 //--------------------------------------------------------------------------
83 // Write a byte to an EPROM 1-Wire device.
85 // Supported devices: crc_type=0(CRC8)
88 // DS1985, DS1986, DS2407
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.
101 // Returns: >=0 success, this is the resulting byte from the program
103 // -1 error, device not connected or program pulse voltage
106 SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd,
107 SMALLINT crc_type, SMALLINT do_access)
112 // optionally access the device
115 if (!owAccess(portnum))
117 OWERROR(OWERROR_ACCESS_FAILED);
121 // send the write command
122 if (!owWriteByte(portnum,write_cmd))
124 OWERROR(OWERROR_WRITE_BYTE_FAILED);
129 if (!owWriteByte(portnum,addr & 0xFF) || !owWriteByte(portnum,addr >> 8))
131 OWERROR(OWERROR_WRITE_BYTE_FAILED);
136 // send the data to write
137 if (!owWriteByte(portnum,write_byte))
139 OWERROR(OWERROR_WRITE_BYTE_FAILED);
150 docrc8(portnum,(uchar)write_cmd);
151 docrc8(portnum,(uchar)(addr & 0xFF));
152 docrc8(portnum,(uchar)(addr >> 8));
155 setcrc8(portnum,(uchar)(addr & 0xFF));
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
163 OWERROR(OWERROR_CRC_FAILED);
173 docrc16(portnum,(ushort)write_cmd);
174 docrc16(portnum,(ushort)(addr & 0xFF));
175 docrc16(portnum,(ushort)(addr >> 8));
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)
188 // send the program pulse
189 if (!owProgramPulse(portnum))
191 OWERROR(OWERROR_PROGRAM_PULSE_FAILED);
195 // read back and return the resulting byte
196 return owReadByte(portnum);