1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 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 //  ownet.C - Network functions for 1-Wire net devices.
28 //
29 //  Version: 2.01
30 //
31 //  History: 1.00 -> 1.01  Change to owFamilySearchSetup, LastDiscrepancy[portnum]
32 //                         was set to 64 instead of 8 to enable devices with
33 //                         early contention to go in the '0' direction first.
34 //           1.02 -> 1.03  Initialized goodbits in  owVerify
35 //           1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for
36 //                         multiple ports.
37 //           2.00 -> 2.01  Added support for owError library
38 //
39 
40 #include <stdio.h>
41 #include "ownet.h"
42 
43 // external functions defined in system specific link file
44 extern SMALLINT owTouchReset(int);
45 extern SMALLINT owTouchBit(int,SMALLINT);
46 extern SMALLINT owWriteByte(int,SMALLINT);
47 extern SMALLINT owReadByte(int);
48 extern SMALLINT owSpeed(int,SMALLINT);
49 extern SMALLINT owLevel(int,SMALLINT);
50 extern void usDelay(int);
51 
52 // external functions defined in owtran.c
53 extern uchar owBlock(int,SMALLINT,uchar *,SMALLINT);
54 
55 extern void setcrc8(int,uchar);
56 extern uchar docrc8(int,uchar);
57 
58 // exportable functions defined in ownet.c
59 SMALLINT owFirst(int,SMALLINT,SMALLINT);
60 SMALLINT owNext(int,SMALLINT,SMALLINT);
61 void     owSerialNum(int,uchar *,SMALLINT);
62 void     owFamilySearchSetup(int,SMALLINT);
63 void     owSkipFamily(int);
64 SMALLINT owAccess(int);
65 SMALLINT owVerify(int,SMALLINT);
66 SMALLINT owOverdriveAccess(int);
67 SMALLINT bitacc(SMALLINT,SMALLINT,SMALLINT,uchar *);
68 
69 // global variables for this module to hold search state information
70 static SMALLINT LastDiscrepancy[MAX_PORTNUM];
71 static SMALLINT LastFamilyDiscrepancy[MAX_PORTNUM];
72 static SMALLINT LastDevice[MAX_PORTNUM];
73 uchar SerialNum[MAX_PORTNUM][8];
74 
75 //--------------------------------------------------------------------------
76 // The 'owFirst' finds the first device on the 1-Wire Net  This function
77 // contains one parameter 'alarm_only'.  When
78 // 'alarm_only' is TRUE (1) the find alarm command 0xEC is
79 // sent instead of the normal search command 0xF0.
80 // Using the find alarm command 0xEC will limit the search to only
81 // 1-Wire devices that are in an 'alarm' state.
82 //
83 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
84 //                indicate the symbolic port number.
85 // 'do_reset'   - TRUE (1) perform reset before search, FALSE (0) do not
86 //                perform reset before search.
87 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is
88 //                sent instead of the normal search command 0xF0
89 //
90 // Returns:   TRUE (1) : when a 1-Wire device was found and it's
91 //                        Serial Number placed in the global SerialNum[portnum]
92 //            FALSE (0): There are no devices on the 1-Wire Net.
93 //
owFirst(int portnum,SMALLINT do_reset,SMALLINT alarm_only)94 SMALLINT owFirst(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
95 {
96    // reset the search state
97    LastDiscrepancy[portnum] = 0;
98    LastDevice[portnum] = FALSE;
99    LastFamilyDiscrepancy[portnum] = 0;
100 
101    return owNext(portnum,do_reset,alarm_only);
102 }
103 
104 //--------------------------------------------------------------------------
105 // The 'owNext' function does a general search.  This function
106 // continues from the previos search state. The search state
107 // can be reset by using the 'owFirst' function.
108 // This function contains one parameter 'alarm_only'.
109 // When 'alarm_only' is TRUE (1) the find alarm command
110 // 0xEC is sent instead of the normal search command 0xF0.
111 // Using the find alarm command 0xEC will limit the search to only
112 // 1-Wire devices that are in an 'alarm' state.
113 //
114 // 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
115 //                indicate the symbolic port number.
116 // 'do_reset'   - TRUE (1) perform reset before search, FALSE (0) do not
117 //                perform reset before search.
118 // 'alarm_only' - TRUE (1) the find alarm command 0xEC is
119 //                sent instead of the normal search command 0xF0
120 //
121 // Returns:   TRUE (1) : when a 1-Wire device was found and it's
122 //                       Serial Number placed in the global SerialNum[portnum]
123 //            FALSE (0): when no new device was found.  Either the
124 //                       last search was the last device or there
125 //                       are no devices on the 1-Wire Net.
126 //
owNext(int portnum,SMALLINT do_reset,SMALLINT alarm_only)127 SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
128 {
129    uchar bit_test, search_direction, bit_number;
130    uchar last_zero, serial_byte_number, next_result;
131    uchar serial_byte_mask;
132    uchar lastcrc8;
133 
134    // initialize for search
135    bit_number = 1;
136    last_zero = 0;
137    serial_byte_number = 0;
138    serial_byte_mask = 1;
139    next_result = 0;
140    setcrc8(portnum,0);
141 
142    // if the last call was not the last one
143    if (!LastDevice[portnum])
144    {
145       // check if reset first is requested
146       if (do_reset)
147       {
148          // reset the 1-wire
149          // if there are no parts on 1-wire, return FALSE
150          if (!owTouchReset(portnum))
151          {
152             // printf("owTouchReset failed\r\n");
153             // reset the search
154             LastDiscrepancy[portnum] = 0;
155             LastFamilyDiscrepancy[portnum] = 0;
156             OWERROR(OWERROR_NO_DEVICES_ON_NET);
157             return FALSE;
158          }
159       }
160 
161       // If finding alarming devices issue a different command
162       if (alarm_only)
163          owWriteByte(portnum,0xEC);  // issue the alarming search command
164       else
165          owWriteByte(portnum,0xF0);  // issue the search command
166 
167       //pause before beginning the search
168       //usDelay(100);
169 
170       // loop to do the search
171       do
172       {
173          // read a bit and its compliment
174          bit_test = owTouchBit(portnum,1) << 1;
175          bit_test |= owTouchBit(portnum,1);
176 
177          // check for no devices on 1-wire
178          if (bit_test == 3)
179             break;
180          else
181          {
182             // all devices coupled have 0 or 1
183             if (bit_test > 0)
184               search_direction = !(bit_test & 0x01);  // bit write value for search
185             else
186             {
187                // if this discrepancy if before the Last Discrepancy
188                // on a previous next then pick the same as last time
189                if (bit_number < LastDiscrepancy[portnum])
190                   search_direction = ((SerialNum[portnum][serial_byte_number] & serial_byte_mask) > 0);
191                else
192                   // if equal to last pick 1, if not then pick 0
193                   search_direction = (bit_number == LastDiscrepancy[portnum]);
194 
195                // if 0 was picked then record its position in LastZero
196                if (search_direction == 0)
197                   last_zero = bit_number;
198 
199                // check for Last discrepancy in family
200                if (last_zero < 9)
201                   LastFamilyDiscrepancy[portnum] = last_zero;
202             }
203 
204             // set or clear the bit in the SerialNum[portnum] byte serial_byte_number
205             // with mask serial_byte_mask
206             if (search_direction == 1)
207               SerialNum[portnum][serial_byte_number] |= serial_byte_mask;
208             else
209               SerialNum[portnum][serial_byte_number] &= ~serial_byte_mask;
210 
211             // serial number search direction write bit
212             owTouchBit(portnum,search_direction);
213 
214             // increment the byte counter bit_number
215             // and shift the mask serial_byte_mask
216             bit_number++;
217             serial_byte_mask <<= 1;
218 
219             // if the mask is 0 then go to new SerialNum[portnum] byte serial_byte_number
220             // and reset mask
221             if (serial_byte_mask == 0)
222             {
223                 lastcrc8 = docrc8(portnum,SerialNum[portnum][serial_byte_number]);  // accumulate the CRC
224                 serial_byte_number++;
225                 serial_byte_mask = 1;
226             }
227          }
228       }
229       while(serial_byte_number < 8);  // loop until through all SerialNum[portnum] bytes 0-7
230 
231       // if the search was successful then
232       if (!((bit_number < 65) || lastcrc8))
233       {
234          // search successful so set LastDiscrepancy[portnum],LastDevice[portnum],next_result
235          LastDiscrepancy[portnum] = last_zero;
236          LastDevice[portnum] = (LastDiscrepancy[portnum] == 0);
237          next_result = TRUE;
238       }
239    }
240 
241    // if no device found then reset counters so next 'next' will be
242    // like a first
243    if (!next_result || !SerialNum[portnum][0])
244    {
245       LastDiscrepancy[portnum] = 0;
246       LastDevice[portnum] = FALSE;
247       LastFamilyDiscrepancy[portnum] = 0;
248       next_result = FALSE;
249    }
250 
251    return next_result;
252 }
253 
254 //--------------------------------------------------------------------------
255 // The 'owSerialNum' function either reads or sets the SerialNum buffer
256 // that is used in the search functions 'owFirst' and 'owNext'.
257 // This function contains two parameters, 'serialnum_buf' is a pointer
258 // to a buffer provided by the caller.  'serialnum_buf' should point to
259 // an array of 8 unsigned chars.  The second parameter is a flag called
260 // 'do_read' that is TRUE (1) if the operation is to read and FALSE
261 // (0) if the operation is to set the internal SerialNum buffer from
262 // the data in the provided buffer.
263 //
264 // 'portnum'       - number 0 to MAX_PORTNUM-1.  This number is provided to
265 //                   indicate the symbolic port number.
266 // 'serialnum_buf' - buffer to that contains the serial number to set
267 //                   when do_read = FALSE (0) and buffer to get the serial
268 //                   number when do_read = TRUE (1).
269 // 'do_read'       - flag to indicate reading (1) or setting (0) the current
270 //                   serial number.
271 //
owSerialNum(int portnum,uchar * serialnum_buf,SMALLINT do_read)272 void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)
273 {
274    uchar i;
275 
276    // read the internal buffer and place in 'serialnum_buf'
277    if (do_read)
278    {
279       for (i = 0; i < 8; i++)
280          serialnum_buf[i] = SerialNum[portnum][i];
281    }
282    // set the internal buffer from the data in 'serialnum_buf'
283    else
284    {
285       for (i = 0; i < 8; i++)
286          SerialNum[portnum][i] = serialnum_buf[i];
287    }
288 }
289 
290 //--------------------------------------------------------------------------
291 // Setup the search algorithm to find a certain family of devices
292 // the next time a search function is called 'owNext'.
293 //
294 // 'portnum'       - number 0 to MAX_PORTNUM-1.  This number was provided to
295 //                   OpenCOM to indicate the port number.
296 // 'search_family' - family code type to set the search algorithm to find
297 //                   next.
298 //
owFamilySearchSetup(int portnum,SMALLINT search_family)299 void owFamilySearchSetup(int portnum, SMALLINT search_family)
300 {
301    uchar i;
302 
303    // set the search state to find SearchFamily type devices
304    SerialNum[portnum][0] = search_family;
305    for (i = 1; i < 8; i++)
306       SerialNum[portnum][i] = 0;
307    LastDiscrepancy[portnum] = 64;
308    LastDevice[portnum] = FALSE;
309 }
310 
311 //--------------------------------------------------------------------------
312 // Set the current search state to skip the current family code.
313 //
314 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
315 //                 indicate the symbolic port number.
316 //
owSkipFamily(int portnum)317 void owSkipFamily(int portnum)
318 {
319    // set the Last discrepancy to last family discrepancy
320    LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum];
321 
322    // check for end of list
323    if (LastDiscrepancy[portnum] == 0)
324       LastDevice[portnum] = TRUE;
325 }
326 
327 //--------------------------------------------------------------------------
328 // The 'owAccess' function resets the 1-Wire and sends a MATCH Serial
329 // Number command followed by the current SerialNum code. After this
330 // function is complete the 1-Wire device is ready to accept device-specific
331 // commands.
332 //
333 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
334 //                 indicate the symbolic port number.
335 //
336 // Returns:   TRUE (1) : reset indicates present and device is ready
337 //                       for commands.
338 //            FALSE (0): reset does not indicate presence or echos 'writes'
339 //                       are not correct.
340 //
owAccess(int portnum)341 SMALLINT owAccess(int portnum)
342 {
343    uchar sendpacket[9];
344    uchar i;
345 
346    // reset the 1-wire
347    if (owTouchReset(portnum))
348    {
349       // create a buffer to use with block function
350       // match Serial Number command 0x55
351       sendpacket[0] = 0x55;
352       // Serial Number
353       for (i = 1; i < 9; i++)
354          sendpacket[i] = SerialNum[portnum][i-1];
355 
356       // send/recieve the transfer buffer
357       if (owBlock(portnum,FALSE,sendpacket,9))
358       {
359          // verify that the echo of the writes was correct
360          for (i = 1; i < 9; i++)
361             if (sendpacket[i] != SerialNum[portnum][i-1])
362                return FALSE;
363          if (sendpacket[0] != 0x55)
364          {
365             OWERROR(OWERROR_WRITE_VERIFY_FAILED);
366             return FALSE;
367          }
368          else
369             return TRUE;
370       }
371       else
372          OWERROR(OWERROR_BLOCK_FAILED);
373    }
374    else
375       OWERROR(OWERROR_NO_DEVICES_ON_NET);
376 
377    // reset or match echo failed
378    return FALSE;
379 }
380 
381 //----------------------------------------------------------------------
382 // The function 'owVerify' verifies that the current device
383 // is in contact with the 1-Wire Net.
384 // Using the find alarm command 0xEC will verify that the device
385 // is in contact with the 1-Wire Net and is in an 'alarm' state.
386 //
387 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
388 //                 indicate the symbolic port number.
389 // 'alarm_only'  - TRUE (1) the find alarm command 0xEC
390 //                 is sent instead of the normal search
391 //                 command 0xF0.
392 //
393 // Returns:   TRUE (1) : when the 1-Wire device was verified
394 //                       to be on the 1-Wire Net
395 //                       with alarm_only == FALSE
396 //                       or verified to be on the 1-Wire Net
397 //                       AND in an alarm state when
398 //                       alarm_only == TRUE.
399 //            FALSE (0): the 1-Wire device was not on the
400 //                       1-Wire Net or if alarm_only
401 //                       == TRUE, the device may be on the
402 //                       1-Wire Net but in a non-alarm state.
403 //
owVerify(int portnum,SMALLINT alarm_only)404 SMALLINT owVerify(int portnum, SMALLINT alarm_only)
405 {
406    uchar i,sendlen=0,goodbits=0,cnt=0,s,tst;
407    uchar sendpacket[50];
408 
409    // construct the search
410    if (alarm_only)
411       sendpacket[sendlen++] = 0xEC; // issue the alarming search command
412    else
413       sendpacket[sendlen++] = 0xF0; // issue the search command
414    // set all bits at first
415    for (i = 1; i <= 24; i++)
416       sendpacket[sendlen++] = 0xFF;
417    // now set or clear apropriate bits for search
418    for (i = 0; i < 64; i++)
419       bitacc(WRITE_FUNCTION,bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]),(int)((i+1)*3-1),&sendpacket[1]);
420 
421    // send/recieve the transfer buffer
422    if (owBlock(portnum,TRUE,sendpacket,sendlen))
423    {
424       // check results to see if it was a success
425       for (i = 0; i < 192; i += 3)
426       {
427          tst = (bitacc(READ_FUNCTION,0,i,&sendpacket[1]) << 1) |
428                 bitacc(READ_FUNCTION,0,(int)(i+1),&sendpacket[1]);
429 
430          s = bitacc(READ_FUNCTION,0,cnt++,&SerialNum[portnum][0]);
431 
432          if (tst == 0x03)  // no device on line
433          {
434               goodbits = 0;    // number of good bits set to zero
435               break;     // quit
436          }
437 
438          if (((s == 0x01) && (tst == 0x02)) ||
439              ((s == 0x00) && (tst == 0x01))    )  // correct bit
440             goodbits++;  // count as a good bit
441       }
442 
443       // check too see if there were enough good bits to be successful
444       if (goodbits >= 8)
445          return TRUE;
446    }
447    else
448       OWERROR(OWERROR_BLOCK_FAILED);
449 
450    // block fail or device not present
451    return FALSE;
452 }
453 
454 //----------------------------------------------------------------------
455 // Perform a overdrive MATCH command to select the 1-Wire device with
456 // the address in the ID data register.
457 //
458 // 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
459 //                 indicate the symbolic port number.
460 //
461 // Returns:  TRUE: If the device is present on the 1-Wire Net and
462 //                 can do overdrive then the device is selected.
463 //           FALSE: Device is not present or not capable of overdrive.
464 //
465 //  *Note: This function could be converted to send DS2480
466 //         commands in one packet.
467 //
owOverdriveAccess(int portnum)468 SMALLINT owOverdriveAccess(int portnum)
469 {
470    uchar sendpacket[8];
471    uchar i, bad_echo = FALSE;
472 
473    // make sure normal level
474    owLevel(portnum,MODE_NORMAL);
475 
476    // force to normal communication speed
477    owSpeed(portnum,MODE_NORMAL);
478 
479    // call the 1-Wire Net reset function
480    if (owTouchReset(portnum))
481    {
482       // send the match command 0x69
483       if (owWriteByte(portnum,0x69))
484       {
485          // switch to overdrive communication speed
486          owSpeed(portnum,MODE_OVERDRIVE);
487 
488          // create a buffer to use with block function
489          // Serial Number
490          for (i = 0; i < 8; i++)
491             sendpacket[i] = SerialNum[portnum][i];
492 
493          // send/recieve the transfer buffer
494          if (owBlock(portnum,FALSE,sendpacket,8))
495          {
496             // verify that the echo of the writes was correct
497             for (i = 0; i < 8; i++)
498                if (sendpacket[i] != SerialNum[portnum][i])
499                   bad_echo = TRUE;
500             // if echo ok then success
501             if (!bad_echo)
502                return TRUE;
503             else
504                OWERROR(OWERROR_WRITE_VERIFY_FAILED);
505          }
506          else
507             OWERROR(OWERROR_BLOCK_FAILED);
508       }
509       else
510          OWERROR(OWERROR_WRITE_BYTE_FAILED);
511    }
512    else
513       OWERROR(OWERROR_NO_DEVICES_ON_NET);
514 
515    // failure, force back to normal communication speed
516    owSpeed(portnum,MODE_NORMAL);
517 
518    return FALSE;
519 }
520 
521 //--------------------------------------------------------------------------
522 // Bit utility to read and write a bit in the buffer 'buf'.
523 //
524 // 'op'    - operation (1) to set and (0) to read
525 // 'state' - set (1) or clear (0) if operation is write (1)
526 // 'loc'   - bit number location to read or write
527 // 'buf'   - pointer to array of bytes that contains the bit
528 //           to read or write
529 //
530 // Returns: 1   if operation is set (1)
531 //          0/1 state of bit number 'loc' if operation is reading
532 //
bitacc(SMALLINT op,SMALLINT state,SMALLINT loc,uchar * buf)533 SMALLINT bitacc(SMALLINT op, SMALLINT state, SMALLINT loc, uchar *buf)
534 {
535    SMALLINT nbyt,nbit;
536 
537    nbyt = (loc / 8);
538    nbit = loc - (nbyt * 8);
539 
540    if (op == WRITE_FUNCTION)
541    {
542       if (state)
543          buf[nbyt] |= (0x01 << nbit);
544       else
545          buf[nbyt] &= ~(0x01 << nbit);
546 
547       return 1;
548    }
549    else
550       return ((buf[nbyt] >> nbit) & 0x01);
551 }
552