1 #define DEBUG_OW_NETU 0
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
22 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 // OTHER DEALINGS IN THE SOFTWARE.
26 // Except as contained in this notice, the name of Dallas Semiconductor
27 // shall not be used except as stated in the Dallas Semiconductor
29 //---------------------------------------------------------------------------
31 // owNetU.C - Network functions for 1-Wire Net devices
32 // using the DS2480/DS2480B (U) serial interface chip.
36 // 1.02 -> 1.03 Removed caps in #includes for Linux capatibility
37 // 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
45 uchar SerialNum[MAX_PORTNUM][8];
47 // local variables for this module to hold search state information
48 static int LastDiscrepancy[MAX_PORTNUM];
49 static int LastFamilyDiscrepancy[MAX_PORTNUM];
50 static int LastDevice[MAX_PORTNUM];
52 //--------------------------------------------------------------------------
53 // The 'owFirst' finds the first device on the 1-Wire Net This function
54 // contains one parameter 'alarm_only'. When
55 // 'alarm_only' is TRUE (1) the find alarm command 0xEC is
56 // sent instead of the normal search command 0xF0.
57 // Using the find alarm command 0xEC will limit the search to only
58 // 1-Wire devices that are in an 'alarm' state.
60 // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
61 // indicate the symbolic port number.
62 // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not
63 // perform reset before search.
64 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is
65 // sent instead of the normal search command 0xF0
67 // Returns: TRUE (1) : when a 1-Wire device was found and it's
68 // Serial Number placed in the global SerialNum
69 // FALSE (0): There are no devices on the 1-Wire Net.
71 int owFirst(int portnum, int do_reset, int alarm_only)
73 // reset the search state
74 LastDiscrepancy[portnum] = 0;
75 LastDevice[portnum] = FALSE;
76 LastFamilyDiscrepancy[portnum] = 0;
78 return owNext(portnum, do_reset, alarm_only);
81 //--------------------------------------------------------------------------
82 // The 'owNext' function does a general search. This function
83 // continues from the previos search state. The search state
84 // can be reset by using the 'owFirst' function.
85 // This function contains one parameter 'alarm_only'.
86 // When 'alarm_only' is TRUE (1) the find alarm command
87 // 0xEC is sent instead of the normal search command 0xF0.
88 // Using the find alarm command 0xEC will limit the search to only
89 // 1-Wire devices that are in an 'alarm' state.
91 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
92 // OpenCOM to indicate the port number.
93 // 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not
94 // perform reset before search.
95 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is
96 // sent instead of the normal search command 0xF0
98 // Returns: TRUE (1) : when a 1-Wire device was found and it's
99 // Serial Number placed in the global SerialNum
100 // FALSE (0): when no new device was found. Either the
101 // last search was the last device or there
102 // are no devices on the 1-Wire Net.
104 int owNext(int portnum, int do_reset, int alarm_only)
106 int i,tmp_last_desc,pos;
107 uchar tmp_serial_num[8];
108 uchar readbuffer[20],sendpacket[40];
112 // if the last call was the last one
113 if (LastDevice[portnum])
116 LastDiscrepancy[portnum] = 0;
117 LastDevice[portnum] = FALSE;
118 LastFamilyDiscrepancy[portnum] = 0;
120 printf ("owNext: no (more) devices\n");
125 // check if reset first is requested
129 // if there are no parts on 1-wire, return FALSE
130 if (!owTouchReset(portnum))
133 LastDiscrepancy[portnum] = 0;
134 LastFamilyDiscrepancy[portnum] = 0;
139 // build the command stream
140 // call a function that may add the change mode command to the buff
141 // check if correct mode
142 if (UMode[portnum] != MODSEL_DATA)
144 UMode[portnum] = MODSEL_DATA;
145 sendpacket[sendlen++] = MODE_DATA;
150 sendpacket[sendlen++] = 0xEC; // issue the alarming search command
152 sendpacket[sendlen++] = 0xF0; // issue the search command
154 // change back to command mode
155 UMode[portnum] = MODSEL_COMMAND;
156 sendpacket[sendlen++] = MODE_COMMAND;
159 sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHON | USpeed[portnum]);
161 // change back to data mode
162 UMode[portnum] = MODSEL_DATA;
163 sendpacket[sendlen++] = MODE_DATA;
165 // set the temp Last Descrep to none
166 tmp_last_desc = 0xFF;
168 // add the 16 bytes of the search
170 for (i = 0; i < 16; i++)
171 sendpacket[sendlen++] = 0;
173 // only modify bits if not the first search
174 if (LastDiscrepancy[portnum] != 0xFF)
176 // set the bits in the added buffer
177 for (i = 0; i < 64; i++)
179 // before last discrepancy
180 if (i < (LastDiscrepancy[portnum] - 1))
181 bitacc(WRITE_FUNCTION,
182 bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]),
185 // at last discrepancy
186 else if (i == (LastDiscrepancy[portnum] - 1))
187 bitacc(WRITE_FUNCTION,1,
190 // after last discrepancy so leave zeros
194 // change back to command mode
195 UMode[portnum] = MODSEL_COMMAND;
196 sendpacket[sendlen++] = MODE_COMMAND;
199 sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHOFF | USpeed[portnum]);
205 if (WriteCOM(portnum,sendlen,sendpacket))
207 // read back the 1 byte response
208 if (ReadCOM(portnum,17,readbuffer) == 17)
210 // interpret the bit stream
211 for (i = 0; i < 64; i++)
213 // get the SerialNum bit
214 bitacc(WRITE_FUNCTION,
215 bitacc(READ_FUNCTION,0,(short)(i * 2 + 1),&readbuffer[1]),
218 // check LastDiscrepancy
219 if ((bitacc(READ_FUNCTION,0,(short)(i * 2),&readbuffer[1]) == 1) &&
220 (bitacc(READ_FUNCTION,0,(short)(i * 2 + 1),&readbuffer[1]) == 0))
222 tmp_last_desc = i + 1;
223 // check LastFamilyDiscrepancy
225 LastFamilyDiscrepancy[portnum] = i + 1;
231 for (i = 0; i < 8; i++)
232 lastcrc8 = docrc8(portnum,tmp_serial_num[i]);
235 if ((lastcrc8 != 0) || (LastDiscrepancy[portnum] == 63) || (tmp_serial_num[0] == 0))
237 // error during search
239 LastDiscrepancy[portnum] = 0;
240 LastDevice[portnum] = FALSE;
241 LastFamilyDiscrepancy[portnum] = 0;
243 printf ("owNext: check results failed\n");
251 if ((tmp_last_desc == LastDiscrepancy[portnum]) || (tmp_last_desc == 0xFF))
252 LastDevice[portnum] = TRUE;
254 // copy the SerialNum to the buffer
255 for (i = 0; i < 8; i++)
256 SerialNum[portnum][i] = tmp_serial_num[i];
259 LastDiscrepancy[portnum] = tmp_last_desc;
264 printf ("owNext: ReadCOM failed\n");
269 printf ("owNext: WriteCOM failed\n");
273 // an error occured so re-sync with DS2480
274 DS2480Detect(portnum);
277 LastDiscrepancy[portnum] = 0;
278 LastDevice[portnum] = FALSE;
279 LastFamilyDiscrepancy[portnum] = 0;
284 //--------------------------------------------------------------------------
285 // The 'owSerialNum' function either reads or sets the SerialNum buffer
286 // that is used in the search functions 'owFirst' and 'owNext'.
287 // This function contains two parameters, 'serialnum_buf' is a pointer
288 // to a buffer provided by the caller. 'serialnum_buf' should point to
289 // an array of 8 unsigned chars. The second parameter is a flag called
290 // 'do_read' that is TRUE (1) if the operation is to read and FALSE
291 // (0) if the operation is to set the internal SerialNum buffer from
292 // the data in the provided buffer.
294 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
295 // OpenCOM to indicate the port number.
296 // 'serialnum_buf' - buffer to that contains the serial number to set
297 // when do_read = FALSE (0) and buffer to get the serial
298 // number when do_read = TRUE (1).
299 // 'do_read' - flag to indicate reading (1) or setting (0) the current
302 void owSerialNum(int portnum, uchar *serialnum_buf, int do_read)
306 // read the internal buffer and place in 'serialnum_buf'
309 for (i = 0; i < 8; i++)
310 serialnum_buf[i] = SerialNum[portnum][i];
312 // set the internal buffer from the data in 'serialnum_buf'
315 for (i = 0; i < 8; i++)
316 SerialNum[portnum][i] = serialnum_buf[i];
320 //--------------------------------------------------------------------------
321 // Setup the search algorithm to find a certain family of devices
322 // the next time a search function is called 'owNext'.
324 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
325 // OpenCOM to indicate the port number.
326 // 'search_family' - family code type to set the search algorithm to find
329 void owFamilySearchSetup(int portnum, int search_family)
333 // set the search state to find search_family type devices
334 SerialNum[portnum][0] = (uchar)search_family;
335 for (i = 1; i < 8; i++)
336 SerialNum[portnum][i] = 0;
337 LastDiscrepancy[portnum] = 64;
338 LastDevice[portnum] = FALSE;
341 //--------------------------------------------------------------------------
342 // Set the current search state to skip the current family code.
344 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
345 // OpenCOM to indicate the port number.
347 void owSkipFamily(int portnum)
349 // set the Last discrepancy to last family discrepancy
350 LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum];
352 // check for end of list
353 if (LastDiscrepancy[portnum] == 0)
354 LastDevice[portnum] = TRUE;
357 //--------------------------------------------------------------------------
358 // The 'owAccess' function resets the 1-Wire and sends a MATCH Serial
359 // Number command followed by the current SerialNum code. After this
360 // function is complete the 1-Wire device is ready to accept device-specific
363 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
364 // OpenCOM to indicate the port number.
366 // Returns: TRUE (1) : reset indicates present and device is ready
368 // FALSE (0): reset does not indicate presence or echos 'writes'
371 int owAccess(int portnum)
377 if (owTouchReset(portnum))
379 // create a buffer to use with block function
380 // match Serial Number command 0x55
381 sendpacket[0] = 0x55;
383 for (i = 1; i < 9; i++)
384 sendpacket[i] = SerialNum[portnum][i-1];
386 // send/recieve the transfer buffer
387 if (owBlock(portnum,FALSE,sendpacket,9))
389 // verify that the echo of the writes was correct
390 for (i = 1; i < 9; i++)
391 if (sendpacket[i] != SerialNum[portnum][i-1])
393 if (sendpacket[0] != 0x55)
400 // reset or match echo failed
404 //----------------------------------------------------------------------
405 // The function 'owVerify' verifies that the current device
406 // is in contact with the 1-Wire Net.
407 // Using the find alarm command 0xEC will verify that the device
408 // is in contact with the 1-Wire Net and is in an 'alarm' state.
410 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
411 // OpenCOM to indicate the port number.
412 // 'alarm_only' - TRUE (1) the find alarm command 0xEC
413 // is sent instead of the normal search
416 // Returns: TRUE (1) : when the 1-Wire device was verified
417 // to be on the 1-Wire Net
418 // with alarm_only == FALSE
419 // or verified to be on the 1-Wire Net
420 // AND in an alarm state when
421 // alarm_only == TRUE.
422 // FALSE (0): the 1-Wire device was not on the
423 // 1-Wire Net or if alarm_only
424 // == TRUE, the device may be on the
425 // 1-Wire Net but in a non-alarm state.
427 int owVerify(int portnum, int alarm_only)
429 int i,sendlen=0,goodbits=0,cnt=0,s,tst;
430 uchar sendpacket[50];
432 // construct the search rom
434 sendpacket[sendlen++] = 0xEC; // issue the alarming search command
436 sendpacket[sendlen++] = 0xF0; // issue the search command
437 // set all bits at first
438 for (i = 1; i <= 24; i++)
439 sendpacket[sendlen++] = 0xFF;
440 // now set or clear apropriate bits for search
441 for (i = 0; i < 64; i++)
442 bitacc(WRITE_FUNCTION,bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]),(int)((i+1)*3-1),&sendpacket[1]);
444 // send/recieve the transfer buffer
445 if (owBlock(portnum,TRUE,sendpacket,sendlen))
447 // check results to see if it was a success
448 for (i = 0; i < 192; i += 3)
450 tst = (bitacc(READ_FUNCTION,0,i,&sendpacket[1]) << 1) |
451 bitacc(READ_FUNCTION,0,(int)(i+1),&sendpacket[1]);
453 s = bitacc(READ_FUNCTION,0,cnt++,&SerialNum[portnum][0]);
455 if (tst == 0x03) // no device on line
457 goodbits = 0; // number of good bits set to zero
461 if (((s == 0x01) && (tst == 0x02)) ||
462 ((s == 0x00) && (tst == 0x01)) ) // correct bit
463 goodbits++; // count as a good bit
466 // check too see if there were enough good bits to be successful
471 // block fail or device not present
475 //----------------------------------------------------------------------
476 // Perform a overdrive MATCH command to select the 1-Wire device with
477 // the address in the ID data register.
479 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
480 // OpenCOM to indicate the port number.
482 // Returns: TRUE: If the device is present on the 1-Wire Net and
483 // can do overdrive then the device is selected.
484 // FALSE: Device is not present or not capable of overdrive.
486 // *Note: This function could be converted to send DS2480
487 // commands in one packet.
489 int owOverdriveAccess(int portnum)
492 int i, bad_echo = FALSE;
495 printf ("owOverdriveAccess\n");
497 // make sure normal level
498 owLevel(portnum,MODE_NORMAL);
500 // force to normal communication speed
501 owSpeed(portnum,MODE_NORMAL);
503 // call the 1-Wire Net reset function
504 if (owTouchReset(portnum))
506 // send the match command 0x69
507 if (owWriteByte(portnum,0x69))
509 // switch to overdrive communication speed
510 owSpeed(portnum,MODE_OVERDRIVE);
512 // create a buffer to use with block function
514 for (i = 0; i < 8; i++)
515 sendpacket[i] = SerialNum[portnum][i];
517 // send/recieve the transfer buffer
518 if (owBlock(portnum,FALSE,sendpacket,8))
520 // verify that the echo of the writes was correct
521 for (i = 0; i < 8; i++)
522 if (sendpacket[i] != SerialNum[portnum][i])
524 // if echo ok then success
527 printf ("owOverdriveAccess success\n");
535 // failure, force back to normal communication speed
536 owSpeed(portnum,MODE_NORMAL);
541 //--------------------------------------------------------------------------
542 // Bit utility to read and write a bit in the buffer 'buf'.
544 // 'op' - operation (1) to set and (0) to read
545 // 'state' - set (1) or clear (0) if operation is write (1)
546 // 'loc' - bit number location to read or write
547 // 'buf' - pointer to array of bytes that contains the bit
550 // Returns: 1 if operation is set (1)
551 // 0/1 state of bit number 'loc' if operation is reading
553 int bitacc(int op, int state, int loc, uchar *buf)
558 nbit = loc - (nbyt * 8);
560 if (op == WRITE_FUNCTION)
563 buf[nbyt] |= (0x01 << nbit);
565 buf[nbyt] &= ~(0x01 << nbit);
570 return ((buf[nbyt] >> nbit) & 0x01);