Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / ownetu.c
1 #define DEBUG_OW_NETU 0
2 #if DEBUG_OW_NETU
3 #include <stdio.h>
4 #endif
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
7 // 
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:
14 // 
15 // The above copyright notice and this permission notice shall be included 
16 // in all copies or substantial portions of the Software.
17 // 
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.
25 // 
26 // Except as contained in this notice, the name of Dallas Semiconductor 
27 // shall not be used except as stated in the Dallas Semiconductor 
28 // Branding Policy. 
29 //---------------------------------------------------------------------------
30 //
31 //  owNetU.C - Network functions for 1-Wire Net devices
32 //             using the DS2480/DS2480B (U) serial interface chip. 
33 //
34 //  Version: 2.00
35 //
36 //           1.02 -> 1.03  Removed caps in #includes for Linux capatibility
37 //           1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for 
38 //                         multiple ports.
39 //
40
41 #include "ownet.h"
42 #include "ds2480.h"
43
44 // globally used 
45 uchar SerialNum[MAX_PORTNUM][8];
46
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];
51
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. 
59 //
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
66 //
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.
70 // 
71 int owFirst(int portnum, int do_reset, int alarm_only)
72 {
73    // reset the search state
74    LastDiscrepancy[portnum] = 0;
75    LastDevice[portnum] = FALSE;
76    LastFamilyDiscrepancy[portnum] = 0; 
77
78    return owNext(portnum, do_reset, alarm_only);
79 }
80
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. 
90 //
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
97 //
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.
103 // 
104 int owNext(int portnum, int do_reset, int alarm_only)
105 {
106    int i,tmp_last_desc,pos;
107    uchar tmp_serial_num[8];
108    uchar readbuffer[20],sendpacket[40];
109    int sendlen=0;
110    uchar lastcrc8;
111
112    // if the last call was the last one 
113    if (LastDevice[portnum])
114    {
115       // reset the search
116       LastDiscrepancy[portnum] = 0;
117       LastDevice[portnum] = FALSE;
118       LastFamilyDiscrepancy[portnum] = 0;  
119 #if DEBUG_OW_NETU
120       printf ("owNext: no (more) devices\n");
121 #endif
122       return FALSE;
123    }
124
125    // check if reset first is requested
126    if (do_reset)
127    {
128       // reset the 1-wire 
129       // if there are no parts on 1-wire, return FALSE
130       if (!owTouchReset(portnum))
131       {
132          // reset the search
133          LastDiscrepancy[portnum] = 0;        
134          LastFamilyDiscrepancy[portnum] = 0; 
135          return FALSE;
136       }
137    }
138
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)
143    {
144       UMode[portnum] = MODSEL_DATA;
145       sendpacket[sendlen++] = MODE_DATA;
146    }
147
148    // search command
149    if (alarm_only)
150       sendpacket[sendlen++] = 0xEC; // issue the alarming search command 
151    else
152       sendpacket[sendlen++] = 0xF0; // issue the search command 
153
154    // change back to command mode
155    UMode[portnum] = MODSEL_COMMAND;
156    sendpacket[sendlen++] = MODE_COMMAND;
157
158    // search mode on
159    sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHON | USpeed[portnum]);
160
161    // change back to data mode
162    UMode[portnum] = MODSEL_DATA;
163    sendpacket[sendlen++] = MODE_DATA;
164
165    // set the temp Last Descrep to none
166    tmp_last_desc = 0xFF;  
167
168    // add the 16 bytes of the search
169    pos = sendlen;
170    for (i = 0; i < 16; i++)
171       sendpacket[sendlen++] = 0;
172
173    // only modify bits if not the first search
174    if (LastDiscrepancy[portnum] != 0xFF)
175    {
176       // set the bits in the added buffer
177       for (i = 0; i < 64; i++)
178       {
179          // before last discrepancy
180          if (i < (LastDiscrepancy[portnum] - 1)) 
181                bitacc(WRITE_FUNCTION,
182                    bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]), 
183                    (short)(i * 2 + 1), 
184                    &sendpacket[pos]);
185          // at last discrepancy
186          else if (i == (LastDiscrepancy[portnum] - 1)) 
187                 bitacc(WRITE_FUNCTION,1, 
188                    (short)(i * 2 + 1), 
189                    &sendpacket[pos]);
190          // after last discrepancy so leave zeros
191       }
192    }
193
194    // change back to command mode
195    UMode[portnum] = MODSEL_COMMAND;
196    sendpacket[sendlen++] = MODE_COMMAND;
197
198    // search OFF
199    sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHOFF | USpeed[portnum]);
200
201    // flush the buffers
202    FlushCOM(portnum);
203
204    // send the packet 
205    if (WriteCOM(portnum,sendlen,sendpacket)) 
206    {
207       // read back the 1 byte response 
208       if (ReadCOM(portnum,17,readbuffer) == 17)
209       {
210          // interpret the bit stream
211          for (i = 0; i < 64; i++)
212          {
213             // get the SerialNum bit
214             bitacc(WRITE_FUNCTION,
215                    bitacc(READ_FUNCTION,0,(short)(i * 2 + 1),&readbuffer[1]),
216                    i,
217                    &tmp_serial_num[0]);
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))
221             {
222                tmp_last_desc = i + 1;  
223                // check LastFamilyDiscrepancy
224                if (i < 8)
225                   LastFamilyDiscrepancy[portnum] = i + 1; 
226             }
227          }
228
229          // do dowcrc
230          setcrc8(portnum,0);
231          for (i = 0; i < 8; i++)
232             lastcrc8 = docrc8(portnum,tmp_serial_num[i]);
233
234          // check results 
235          if ((lastcrc8 != 0) || (LastDiscrepancy[portnum] == 63) || (tmp_serial_num[0] == 0))
236          {
237             // error during search 
238             // reset the search
239             LastDiscrepancy[portnum] = 0;
240             LastDevice[portnum] = FALSE;
241             LastFamilyDiscrepancy[portnum] = 0;        
242 #if DEBUG_OW_NETU
243             printf ("owNext: check results failed\n");
244 #endif
245             return FALSE;
246          }
247          // successful search
248          else
249          {
250             // check for lastone
251             if ((tmp_last_desc == LastDiscrepancy[portnum]) || (tmp_last_desc == 0xFF))
252                LastDevice[portnum] = TRUE;
253
254             // copy the SerialNum to the buffer
255             for (i = 0; i < 8; i++)
256                SerialNum[portnum][i] = tmp_serial_num[i];
257          
258             // set the count
259             LastDiscrepancy[portnum] = tmp_last_desc;
260             return TRUE;
261          }
262       } else {
263 #if DEBUG_OW_NETU
264         printf ("owNext: ReadCOM failed\n");
265 #endif
266       }
267    } else {
268 #if DEBUG_OW_NETU
269      printf ("owNext: WriteCOM failed\n");
270 #endif
271    }
272
273    // an error occured so re-sync with DS2480
274    DS2480Detect(portnum);
275
276    // reset the search
277    LastDiscrepancy[portnum] = 0;
278    LastDevice[portnum] = FALSE;
279    LastFamilyDiscrepancy[portnum] = 0;          
280
281    return FALSE;
282 }
283
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.
293 //
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
300 //                   serial number.
301 //
302 void owSerialNum(int portnum, uchar *serialnum_buf, int do_read)
303 {
304    int i;
305
306    // read the internal buffer and place in 'serialnum_buf'
307    if (do_read)
308    {
309       for (i = 0; i < 8; i++)
310          serialnum_buf[i] = SerialNum[portnum][i];
311    }
312    // set the internal buffer from the data in 'serialnum_buf'
313    else
314    {
315       for (i = 0; i < 8; i++)
316          SerialNum[portnum][i] = serialnum_buf[i];
317    }
318 }
319
320 //--------------------------------------------------------------------------
321 // Setup the search algorithm to find a certain family of devices
322 // the next time a search function is called 'owNext'.
323 //
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
327 //                   next.
328 // 
329 void owFamilySearchSetup(int portnum, int search_family)
330 {
331    int i;
332
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;          
339 }
340
341 //--------------------------------------------------------------------------
342 // Set the current search state to skip the current family code.
343 //
344 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
345 //               OpenCOM to indicate the port number.
346 //
347 void owSkipFamily(int portnum)
348 {
349    // set the Last discrepancy to last family discrepancy
350    LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum];
351
352    // check for end of list
353    if (LastDiscrepancy[portnum] == 0) 
354       LastDevice[portnum] = TRUE;
355 }
356
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
361 // commands. 
362 //
363 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
364 //              OpenCOM to indicate the port number.
365 //
366 // Returns:   TRUE (1) : reset indicates present and device is ready
367 //                       for commands.
368 //            FALSE (0): reset does not indicate presence or echos 'writes'
369 //                       are not correct.
370 //
371 int owAccess(int portnum)
372 {
373    uchar sendpacket[9];
374    int i;
375
376    // reset the 1-wire 
377    if (owTouchReset(portnum))
378    {
379       // create a buffer to use with block function      
380       // match Serial Number command 0x55 
381       sendpacket[0] = 0x55; 
382       // Serial Number
383       for (i = 1; i < 9; i++)
384          sendpacket[i] = SerialNum[portnum][i-1];
385       
386       // send/recieve the transfer buffer   
387       if (owBlock(portnum,FALSE,sendpacket,9))
388       {
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])
392                return FALSE;
393          if (sendpacket[0] != 0x55)
394             return FALSE;
395          else
396             return TRUE;
397       }
398    }
399
400    // reset or match echo failed
401    return FALSE;
402 }
403
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. 
409 // 
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 
414 //                         command 0xF0. 
415 //
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.
426 // 
427 int owVerify(int portnum, int alarm_only)
428 {
429    int i,sendlen=0,goodbits=0,cnt=0,s,tst;
430    uchar sendpacket[50];
431    
432    // construct the search rom 
433    if (alarm_only)
434       sendpacket[sendlen++] = 0xEC; // issue the alarming search command 
435    else
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]);
443
444    // send/recieve the transfer buffer   
445    if (owBlock(portnum,TRUE,sendpacket,sendlen))
446    {
447       // check results to see if it was a success 
448       for (i = 0; i < 192; i += 3)
449       {
450          tst = (bitacc(READ_FUNCTION,0,i,&sendpacket[1]) << 1) |
451                 bitacc(READ_FUNCTION,0,(int)(i+1),&sendpacket[1]);
452
453          s = bitacc(READ_FUNCTION,0,cnt++,&SerialNum[portnum][0]);
454
455          if (tst == 0x03)  // no device on line 
456          {
457               goodbits = 0;    // number of good bits set to zero 
458               break;     // quit 
459          }
460
461          if (((s == 0x01) && (tst == 0x02)) ||
462              ((s == 0x00) && (tst == 0x01))    )  // correct bit 
463             goodbits++;  // count as a good bit 
464       }
465
466       // check too see if there were enough good bits to be successful 
467       if (goodbits >= 8) 
468          return TRUE;
469    }
470
471    // block fail or device not present
472    return FALSE;
473 }
474
475 //----------------------------------------------------------------------
476 // Perform a overdrive MATCH command to select the 1-Wire device with 
477 // the address in the ID data register.
478 //
479 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
480 //               OpenCOM to indicate the port number.
481 //
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.
485 //
486 //  *Note: This function could be converted to send DS2480
487 //         commands in one packet.  
488 //
489 int owOverdriveAccess(int portnum)
490 {
491    uchar sendpacket[8];
492    int i, bad_echo = FALSE;
493
494 #if DEBUG_OW_NETU
495    printf ("owOverdriveAccess\n");
496 #endif
497    // make sure normal level
498    owLevel(portnum,MODE_NORMAL);
499
500    // force to normal communication speed
501    owSpeed(portnum,MODE_NORMAL);
502
503    // call the 1-Wire Net reset function 
504    if (owTouchReset(portnum))
505    {
506       // send the match command 0x69
507       if (owWriteByte(portnum,0x69))
508       {
509          // switch to overdrive communication speed
510          owSpeed(portnum,MODE_OVERDRIVE);
511
512          // create a buffer to use with block function      
513          // Serial Number
514          for (i = 0; i < 8; i++)
515             sendpacket[i] = SerialNum[portnum][i];
516       
517          // send/recieve the transfer buffer   
518          if (owBlock(portnum,FALSE,sendpacket,8))
519          {
520             // verify that the echo of the writes was correct
521             for (i = 0; i < 8; i++)
522                if (sendpacket[i] != SerialNum[portnum][i])
523                   bad_echo = TRUE;
524             // if echo ok then success
525             if (!bad_echo) {
526 #if DEBUG_OW_NETU
527               printf ("owOverdriveAccess success\n");
528 #endif
529                return TRUE;               
530             }
531          }
532       }
533    }
534    
535    // failure, force back to normal communication speed
536    owSpeed(portnum,MODE_NORMAL);
537
538    return FALSE;
539 }
540
541 //--------------------------------------------------------------------------
542 // Bit utility to read and write a bit in the buffer 'buf'.
543 //
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
548 //           to read or write
549 //
550 // Returns: 1   if operation is set (1)
551 //          0/1 state of bit number 'loc' if operation is reading 
552 //
553 int bitacc(int op, int state, int loc, uchar *buf)
554 {
555    int nbyt,nbit;
556
557    nbyt = (loc / 8);
558    nbit = loc - (nbyt * 8);
559
560    if (op == WRITE_FUNCTION)
561    {
562       if (state)
563          buf[nbyt] |= (0x01 << nbit);
564       else
565          buf[nbyt] &= ~(0x01 << nbit);
566
567       return 1;
568    }
569    else
570       return ((buf[nbyt] >> nbit) & 0x01);
571 }