1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 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 // owNetU.C - Network functions for 1-Wire Net devices
28 // using the DS2480/DS2480B (U) serial interface chip.
32 // 1.02 -> 1.03 Removed caps in #includes for Linux capatibility
33 // 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
41 uchar SerialNum[MAX_PORTNUM][8];
43 // local variables for this module to hold search state information
44 static int LastDiscrepancy[MAX_PORTNUM];
45 static int LastFamilyDiscrepancy[MAX_PORTNUM];
46 static int LastDevice[MAX_PORTNUM];
48 //--------------------------------------------------------------------------
49 // The 'owFirst' finds the first device on the 1-Wire Net This function
50 // contains one parameter 'alarm_only'. When
51 // 'alarm_only' is TRUE (1) the find alarm command 0xEC is
52 // sent instead of the normal search command 0xF0.
53 // Using the find alarm command 0xEC will limit the search to only
54 // 1-Wire devices that are in an 'alarm' state.
56 // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
57 // indicate the symbolic port number.
58 // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not
59 // perform reset before search.
60 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is
61 // sent instead of the normal search command 0xF0
63 // Returns: TRUE (1) : when a 1-Wire device was found and it's
64 // Serial Number placed in the global SerialNum
65 // FALSE (0): There are no devices on the 1-Wire Net.
67 int owFirst(int portnum, int do_reset, int alarm_only)
69 // reset the search state
70 LastDiscrepancy[portnum] = 0;
71 LastDevice[portnum] = FALSE;
72 LastFamilyDiscrepancy[portnum] = 0;
74 return owNext(portnum, do_reset, alarm_only);
77 //--------------------------------------------------------------------------
78 // The 'owNext' function does a general search. This function
79 // continues from the previos search state. The search state
80 // can be reset by using the 'owFirst' function.
81 // This function contains one parameter 'alarm_only'.
82 // When 'alarm_only' is TRUE (1) the find alarm command
83 // 0xEC is sent instead of the normal search command 0xF0.
84 // Using the find alarm command 0xEC will limit the search to only
85 // 1-Wire devices that are in an 'alarm' state.
87 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
88 // OpenCOM to indicate the port number.
89 // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not
90 // perform reset before search.
91 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is
92 // sent instead of the normal search command 0xF0
94 // Returns: TRUE (1) : when a 1-Wire device was found and it's
95 // Serial Number placed in the global SerialNum
96 // FALSE (0): when no new device was found. Either the
97 // last search was the last device or there
98 // are no devices on the 1-Wire Net.
100 int owNext(int portnum, int do_reset, int alarm_only)
102 int i,tmp_last_desc,pos;
103 uchar tmp_serial_num[8];
104 uchar readbuffer[20],sendpacket[40];
108 // if the last call was the last one
109 if (LastDevice[portnum])
112 LastDiscrepancy[portnum] = 0;
113 LastDevice[portnum] = FALSE;
114 LastFamilyDiscrepancy[portnum] = 0;
118 // check if reset first is requested
122 // if there are no parts on 1-wire, return FALSE
123 if (!owTouchReset(portnum))
126 LastDiscrepancy[portnum] = 0;
127 LastFamilyDiscrepancy[portnum] = 0;
132 // build the command stream
133 // call a function that may add the change mode command to the buff
134 // check if correct mode
135 if (UMode[portnum] != MODSEL_DATA)
137 UMode[portnum] = MODSEL_DATA;
138 sendpacket[sendlen++] = MODE_DATA;
143 sendpacket[sendlen++] = 0xEC; // issue the alarming search command
145 sendpacket[sendlen++] = 0xF0; // issue the search command
147 // change back to command mode
148 UMode[portnum] = MODSEL_COMMAND;
149 sendpacket[sendlen++] = MODE_COMMAND;
152 sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHON | USpeed[portnum]);
154 // change back to data mode
155 UMode[portnum] = MODSEL_DATA;
156 sendpacket[sendlen++] = MODE_DATA;
158 // set the temp Last Descrep to none
159 tmp_last_desc = 0xFF;
161 // add the 16 bytes of the search
163 for (i = 0; i < 16; i++)
164 sendpacket[sendlen++] = 0;
166 // only modify bits if not the first search
167 if (LastDiscrepancy[portnum] != 0xFF)
169 // set the bits in the added buffer
170 for (i = 0; i < 64; i++)
172 // before last discrepancy
173 if (i < (LastDiscrepancy[portnum] - 1))
174 bitacc(WRITE_FUNCTION,
175 bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]),
178 // at last discrepancy
179 else if (i == (LastDiscrepancy[portnum] - 1))
180 bitacc(WRITE_FUNCTION,1,
183 // after last discrepancy so leave zeros
187 // change back to command mode
188 UMode[portnum] = MODSEL_COMMAND;
189 sendpacket[sendlen++] = MODE_COMMAND;
192 sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHOFF | USpeed[portnum]);
198 if (WriteCOM(portnum,sendlen,sendpacket))
200 // read back the 1 byte response
201 if (ReadCOM(portnum,17,readbuffer) == 17)
203 // interpret the bit stream
204 for (i = 0; i < 64; i++)
206 // get the SerialNum bit
207 bitacc(WRITE_FUNCTION,
208 bitacc(READ_FUNCTION,0,(short)(i * 2 + 1),&readbuffer[1]),
211 // check LastDiscrepancy
212 if ((bitacc(READ_FUNCTION,0,(short)(i * 2),&readbuffer[1]) == 1) &&
213 (bitacc(READ_FUNCTION,0,(short)(i * 2 + 1),&readbuffer[1]) == 0))
215 tmp_last_desc = i + 1;
216 // check LastFamilyDiscrepancy
218 LastFamilyDiscrepancy[portnum] = i + 1;
224 for (i = 0; i < 8; i++)
225 lastcrc8 = docrc8(portnum,tmp_serial_num[i]);
228 if ((lastcrc8 != 0) || (LastDiscrepancy[portnum] == 63) || (tmp_serial_num[0] == 0))
230 // error during search
232 LastDiscrepancy[portnum] = 0;
233 LastDevice[portnum] = FALSE;
234 LastFamilyDiscrepancy[portnum] = 0;
241 if ((tmp_last_desc == LastDiscrepancy[portnum]) || (tmp_last_desc == 0xFF))
242 LastDevice[portnum] = TRUE;
244 // copy the SerialNum to the buffer
245 for (i = 0; i < 8; i++)
246 SerialNum[portnum][i] = tmp_serial_num[i];
249 LastDiscrepancy[portnum] = tmp_last_desc;
255 // an error occured so re-sync with DS2480
256 DS2480Detect(portnum);
259 LastDiscrepancy[portnum] = 0;
260 LastDevice[portnum] = FALSE;
261 LastFamilyDiscrepancy[portnum] = 0;
266 //--------------------------------------------------------------------------
267 // The 'owSerialNum' function either reads or sets the SerialNum buffer
268 // that is used in the search functions 'owFirst' and 'owNext'.
269 // This function contains two parameters, 'serialnum_buf' is a pointer
270 // to a buffer provided by the caller. 'serialnum_buf' should point to
271 // an array of 8 unsigned chars. The second parameter is a flag called
272 // 'do_read' that is TRUE (1) if the operation is to read and FALSE
273 // (0) if the operation is to set the internal SerialNum buffer from
274 // the data in the provided buffer.
276 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
277 // OpenCOM to indicate the port number.
278 // 'serialnum_buf' - buffer to that contains the serial number to set
279 // when do_read = FALSE (0) and buffer to get the serial
280 // number when do_read = TRUE (1).
281 // 'do_read' - flag to indicate reading (1) or setting (0) the current
284 void owSerialNum(int portnum, uchar *serialnum_buf, int do_read)
288 // read the internal buffer and place in 'serialnum_buf'
291 for (i = 0; i < 8; i++)
292 serialnum_buf[i] = SerialNum[portnum][i];
294 // set the internal buffer from the data in 'serialnum_buf'
297 for (i = 0; i < 8; i++)
298 SerialNum[portnum][i] = serialnum_buf[i];
302 //--------------------------------------------------------------------------
303 // Setup the search algorithm to find a certain family of devices
304 // the next time a search function is called 'owNext'.
306 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
307 // OpenCOM to indicate the port number.
308 // 'search_family' - family code type to set the search algorithm to find
311 void owFamilySearchSetup(int portnum, int search_family)
315 // set the search state to find search_family type devices
316 SerialNum[portnum][0] = (uchar)search_family;
317 for (i = 1; i < 8; i++)
318 SerialNum[portnum][i] = 0;
319 LastDiscrepancy[portnum] = 64;
320 LastDevice[portnum] = FALSE;
323 //--------------------------------------------------------------------------
324 // Set the current search state to skip the current family code.
326 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
327 // OpenCOM to indicate the port number.
329 void owSkipFamily(int portnum)
331 // set the Last discrepancy to last family discrepancy
332 LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum];
334 // check for end of list
335 if (LastDiscrepancy[portnum] == 0)
336 LastDevice[portnum] = TRUE;
339 //--------------------------------------------------------------------------
340 // The 'owAccess' function resets the 1-Wire and sends a MATCH Serial
341 // Number command followed by the current SerialNum code. After this
342 // function is complete the 1-Wire device is ready to accept device-specific
345 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
346 // OpenCOM to indicate the port number.
348 // Returns: TRUE (1) : reset indicates present and device is ready
350 // FALSE (0): reset does not indicate presence or echos 'writes'
353 int owAccess(int portnum)
359 if (owTouchReset(portnum))
361 // create a buffer to use with block function
362 // match Serial Number command 0x55
363 sendpacket[0] = 0x55;
365 for (i = 1; i < 9; i++)
366 sendpacket[i] = SerialNum[portnum][i-1];
368 // send/recieve the transfer buffer
369 if (owBlock(portnum,FALSE,sendpacket,9))
371 // verify that the echo of the writes was correct
372 for (i = 1; i < 9; i++)
373 if (sendpacket[i] != SerialNum[portnum][i-1])
375 if (sendpacket[0] != 0x55)
382 // reset or match echo failed
386 //----------------------------------------------------------------------
387 // The function 'owVerify' verifies that the current device
388 // is in contact with the 1-Wire Net.
389 // Using the find alarm command 0xEC will verify that the device
390 // is in contact with the 1-Wire Net and is in an 'alarm' state.
392 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
393 // OpenCOM to indicate the port number.
394 // 'alarm_only' - TRUE (1) the find alarm command 0xEC
395 // is sent instead of the normal search
398 // Returns: TRUE (1) : when the 1-Wire device was verified
399 // to be on the 1-Wire Net
400 // with alarm_only == FALSE
401 // or verified to be on the 1-Wire Net
402 // AND in an alarm state when
403 // alarm_only == TRUE.
404 // FALSE (0): the 1-Wire device was not on the
405 // 1-Wire Net or if alarm_only
406 // == TRUE, the device may be on the
407 // 1-Wire Net but in a non-alarm state.
409 int owVerify(int portnum, int alarm_only)
411 int i,sendlen=0,goodbits=0,cnt=0,s,tst;
412 uchar sendpacket[50];
414 // construct the search rom
416 sendpacket[sendlen++] = 0xEC; // issue the alarming search command
418 sendpacket[sendlen++] = 0xF0; // issue the search command
419 // set all bits at first
420 for (i = 1; i <= 24; i++)
421 sendpacket[sendlen++] = 0xFF;
422 // now set or clear apropriate bits for search
423 for (i = 0; i < 64; i++)
424 bitacc(WRITE_FUNCTION,bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]),(int)((i+1)*3-1),&sendpacket[1]);
426 // send/recieve the transfer buffer
427 if (owBlock(portnum,TRUE,sendpacket,sendlen))
429 // check results to see if it was a success
430 for (i = 0; i < 192; i += 3)
432 tst = (bitacc(READ_FUNCTION,0,i,&sendpacket[1]) << 1) |
433 bitacc(READ_FUNCTION,0,(int)(i+1),&sendpacket[1]);
435 s = bitacc(READ_FUNCTION,0,cnt++,&SerialNum[portnum][0]);
437 if (tst == 0x03) // no device on line
439 goodbits = 0; // number of good bits set to zero
443 if (((s == 0x01) && (tst == 0x02)) ||
444 ((s == 0x00) && (tst == 0x01)) ) // correct bit
445 goodbits++; // count as a good bit
448 // check too see if there were enough good bits to be successful
453 // block fail or device not present
457 //----------------------------------------------------------------------
458 // Perform a overdrive MATCH command to select the 1-Wire device with
459 // the address in the ID data register.
461 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
462 // OpenCOM to indicate the port number.
464 // Returns: TRUE: If the device is present on the 1-Wire Net and
465 // can do overdrive then the device is selected.
466 // FALSE: Device is not present or not capable of overdrive.
468 // *Note: This function could be converted to send DS2480
469 // commands in one packet.
471 int owOverdriveAccess(int portnum)
474 int i, bad_echo = FALSE;
476 // make sure normal level
477 owLevel(portnum,MODE_NORMAL);
479 // force to normal communication speed
480 owSpeed(portnum,MODE_NORMAL);
482 // call the 1-Wire Net reset function
483 if (owTouchReset(portnum))
485 // send the match command 0x69
486 if (owWriteByte(portnum,0x69))
488 // switch to overdrive communication speed
489 owSpeed(portnum,MODE_OVERDRIVE);
491 // create a buffer to use with block function
493 for (i = 0; i < 8; i++)
494 sendpacket[i] = SerialNum[portnum][i];
496 // send/recieve the transfer buffer
497 if (owBlock(portnum,FALSE,sendpacket,8))
499 // verify that the echo of the writes was correct
500 for (i = 0; i < 8; i++)
501 if (sendpacket[i] != SerialNum[portnum][i])
503 // if echo ok then success
510 // failure, force back to normal communication speed
511 owSpeed(portnum,MODE_NORMAL);
516 //--------------------------------------------------------------------------
517 // Bit utility to read and write a bit in the buffer 'buf'.
519 // 'op' - operation (1) to set and (0) to read
520 // 'state' - set (1) or clear (0) if operation is write (1)
521 // 'loc' - bit number location to read or write
522 // 'buf' - pointer to array of bytes that contains the bit
525 // Returns: 1 if operation is set (1)
526 // 0/1 state of bit number 'loc' if operation is reading
528 int bitacc(int op, int state, int loc, uchar *buf)
533 nbit = loc - (nbyt * 8);
535 if (op == WRITE_FUNCTION)
538 buf[nbyt] |= (0x01 << nbit);
540 buf[nbyt] &= ~(0x01 << nbit);
545 return ((buf[nbyt] >> nbit) & 0x01);