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