]> git.gag.com Git - fw/sdcc/blob - device/examples/ds390/ow390/ownetu.c
added tstow.c
[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 // globally used 
41 uchar SerialNum[MAX_PORTNUM][8];
42
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];
47
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. 
55 //
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
62 //
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.
66 // 
67 int owFirst(int portnum, int do_reset, int alarm_only)
68 {
69    // reset the search state
70    LastDiscrepancy[portnum] = 0;
71    LastDevice[portnum] = FALSE;
72    LastFamilyDiscrepancy[portnum] = 0; 
73
74    return owNext(portnum, do_reset, alarm_only);
75 }
76
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. 
86 //
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
93 //
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.
99 // 
100 int owNext(int portnum, int do_reset, int alarm_only)
101 {
102    int i,tmp_last_desc,pos;
103    uchar tmp_serial_num[8];
104    uchar readbuffer[20],sendpacket[40];
105    int sendlen=0;
106    uchar lastcrc8;
107
108    // if the last call was the last one 
109    if (LastDevice[portnum])
110    {
111       // reset the search
112       LastDiscrepancy[portnum] = 0;
113       LastDevice[portnum] = FALSE;
114       LastFamilyDiscrepancy[portnum] = 0;          
115       return FALSE;
116    }
117
118    // check if reset first is requested
119    if (do_reset)
120    {
121       // reset the 1-wire 
122       // if there are no parts on 1-wire, return FALSE
123       if (!owTouchReset(portnum))
124       {
125          // reset the search
126          LastDiscrepancy[portnum] = 0;        
127          LastFamilyDiscrepancy[portnum] = 0; 
128          return FALSE;
129       }
130    }
131
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)
136    {
137       UMode[portnum] = MODSEL_DATA;
138       sendpacket[sendlen++] = MODE_DATA;
139    }
140
141    // search command
142    if (alarm_only)
143       sendpacket[sendlen++] = 0xEC; // issue the alarming search command 
144    else
145       sendpacket[sendlen++] = 0xF0; // issue the search command 
146
147    // change back to command mode
148    UMode[portnum] = MODSEL_COMMAND;
149    sendpacket[sendlen++] = MODE_COMMAND;
150
151    // search mode on
152    sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHON | USpeed[portnum]);
153
154    // change back to data mode
155    UMode[portnum] = MODSEL_DATA;
156    sendpacket[sendlen++] = MODE_DATA;
157
158    // set the temp Last Descrep to none
159    tmp_last_desc = 0xFF;  
160
161    // add the 16 bytes of the search
162    pos = sendlen;
163    for (i = 0; i < 16; i++)
164       sendpacket[sendlen++] = 0;
165
166    // only modify bits if not the first search
167    if (LastDiscrepancy[portnum] != 0xFF)
168    {
169       // set the bits in the added buffer
170       for (i = 0; i < 64; i++)
171       {
172          // before last discrepancy
173          if (i < (LastDiscrepancy[portnum] - 1)) 
174                bitacc(WRITE_FUNCTION,
175                    bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]), 
176                    (short)(i * 2 + 1), 
177                    &sendpacket[pos]);
178          // at last discrepancy
179          else if (i == (LastDiscrepancy[portnum] - 1)) 
180                 bitacc(WRITE_FUNCTION,1, 
181                    (short)(i * 2 + 1), 
182                    &sendpacket[pos]);
183          // after last discrepancy so leave zeros
184       }
185    }
186
187    // change back to command mode
188    UMode[portnum] = MODSEL_COMMAND;
189    sendpacket[sendlen++] = MODE_COMMAND;
190
191    // search OFF
192    sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_SEARCHOFF | USpeed[portnum]);
193
194    // flush the buffers
195    FlushCOM(portnum);
196
197    // send the packet 
198    if (WriteCOM(portnum,sendlen,sendpacket)) 
199    {
200       // read back the 1 byte response 
201       if (ReadCOM(portnum,17,readbuffer) == 17)
202       {
203          // interpret the bit stream
204          for (i = 0; i < 64; i++)
205          {
206             // get the SerialNum bit
207             bitacc(WRITE_FUNCTION,
208                    bitacc(READ_FUNCTION,0,(short)(i * 2 + 1),&readbuffer[1]),
209                    i,
210                    &tmp_serial_num[0]);
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))
214             {
215                tmp_last_desc = i + 1;  
216                // check LastFamilyDiscrepancy
217                if (i < 8)
218                   LastFamilyDiscrepancy[portnum] = i + 1; 
219             }
220          }
221
222          // do dowcrc
223          setcrc8(portnum,0);
224          for (i = 0; i < 8; i++)
225             lastcrc8 = docrc8(portnum,tmp_serial_num[i]);
226
227          // check results 
228          if ((lastcrc8 != 0) || (LastDiscrepancy[portnum] == 63) || (tmp_serial_num[0] == 0))
229          {
230             // error during search 
231             // reset the search
232             LastDiscrepancy[portnum] = 0;
233             LastDevice[portnum] = FALSE;
234             LastFamilyDiscrepancy[portnum] = 0;          
235             return FALSE;
236          }
237          // successful search
238          else
239          {
240             // check for lastone
241             if ((tmp_last_desc == LastDiscrepancy[portnum]) || (tmp_last_desc == 0xFF))
242                LastDevice[portnum] = TRUE;
243
244             // copy the SerialNum to the buffer
245             for (i = 0; i < 8; i++)
246                SerialNum[portnum][i] = tmp_serial_num[i];
247          
248             // set the count
249             LastDiscrepancy[portnum] = tmp_last_desc;
250             return TRUE;
251          }
252       }
253    }
254
255    // an error occured so re-sync with DS2480
256    DS2480Detect(portnum);
257
258    // reset the search
259    LastDiscrepancy[portnum] = 0;
260    LastDevice[portnum] = FALSE;
261    LastFamilyDiscrepancy[portnum] = 0;          
262
263    return FALSE;
264 }
265
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.
275 //
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
282 //                   serial number.
283 //
284 void owSerialNum(int portnum, uchar *serialnum_buf, int do_read)
285 {
286    int i;
287
288    // read the internal buffer and place in 'serialnum_buf'
289    if (do_read)
290    {
291       for (i = 0; i < 8; i++)
292          serialnum_buf[i] = SerialNum[portnum][i];
293    }
294    // set the internal buffer from the data in 'serialnum_buf'
295    else
296    {
297       for (i = 0; i < 8; i++)
298          SerialNum[portnum][i] = serialnum_buf[i];
299    }
300 }
301
302 //--------------------------------------------------------------------------
303 // Setup the search algorithm to find a certain family of devices
304 // the next time a search function is called 'owNext'.
305 //
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
309 //                   next.
310 // 
311 void owFamilySearchSetup(int portnum, int search_family)
312 {
313    int i;
314
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;          
321 }
322
323 //--------------------------------------------------------------------------
324 // Set the current search state to skip the current family code.
325 //
326 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
327 //               OpenCOM to indicate the port number.
328 //
329 void owSkipFamily(int portnum)
330 {
331    // set the Last discrepancy to last family discrepancy
332    LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum];
333
334    // check for end of list
335    if (LastDiscrepancy[portnum] == 0) 
336       LastDevice[portnum] = TRUE;
337 }
338
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
343 // commands. 
344 //
345 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
346 //              OpenCOM to indicate the port number.
347 //
348 // Returns:   TRUE (1) : reset indicates present and device is ready
349 //                       for commands.
350 //            FALSE (0): reset does not indicate presence or echos 'writes'
351 //                       are not correct.
352 //
353 int owAccess(int portnum)
354 {
355    uchar sendpacket[9];
356    int i;
357
358    // reset the 1-wire 
359    if (owTouchReset(portnum))
360    {
361       // create a buffer to use with block function      
362       // match Serial Number command 0x55 
363       sendpacket[0] = 0x55; 
364       // Serial Number
365       for (i = 1; i < 9; i++)
366          sendpacket[i] = SerialNum[portnum][i-1];
367       
368       // send/recieve the transfer buffer   
369       if (owBlock(portnum,FALSE,sendpacket,9))
370       {
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])
374                return FALSE;
375          if (sendpacket[0] != 0x55)
376             return FALSE;
377          else
378             return TRUE;
379       }
380    }
381
382    // reset or match echo failed
383    return FALSE;
384 }
385
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. 
391 // 
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 
396 //                         command 0xF0. 
397 //
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.
408 // 
409 int owVerify(int portnum, int alarm_only)
410 {
411    int i,sendlen=0,goodbits=0,cnt=0,s,tst;
412    uchar sendpacket[50];
413    
414    // construct the search rom 
415    if (alarm_only)
416       sendpacket[sendlen++] = 0xEC; // issue the alarming search command 
417    else
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]);
425
426    // send/recieve the transfer buffer   
427    if (owBlock(portnum,TRUE,sendpacket,sendlen))
428    {
429       // check results to see if it was a success 
430       for (i = 0; i < 192; i += 3)
431       {
432          tst = (bitacc(READ_FUNCTION,0,i,&sendpacket[1]) << 1) |
433                 bitacc(READ_FUNCTION,0,(int)(i+1),&sendpacket[1]);
434
435          s = bitacc(READ_FUNCTION,0,cnt++,&SerialNum[portnum][0]);
436
437          if (tst == 0x03)  // no device on line 
438          {
439               goodbits = 0;    // number of good bits set to zero 
440               break;     // quit 
441          }
442
443          if (((s == 0x01) && (tst == 0x02)) ||
444              ((s == 0x00) && (tst == 0x01))    )  // correct bit 
445             goodbits++;  // count as a good bit 
446       }
447
448       // check too see if there were enough good bits to be successful 
449       if (goodbits >= 8) 
450          return TRUE;
451    }
452
453    // block fail or device not present
454    return FALSE;
455 }
456
457 //----------------------------------------------------------------------
458 // Perform a overdrive MATCH command to select the 1-Wire device with 
459 // the address in the ID data register.
460 //
461 // 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
462 //               OpenCOM to indicate the port number.
463 //
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.
467 //
468 //  *Note: This function could be converted to send DS2480
469 //         commands in one packet.  
470 //
471 int owOverdriveAccess(int portnum)
472 {
473    uchar sendpacket[8];
474    int i, bad_echo = FALSE;
475
476    // make sure normal level
477    owLevel(portnum,MODE_NORMAL);
478
479    // force to normal communication speed
480    owSpeed(portnum,MODE_NORMAL);
481
482    // call the 1-Wire Net reset function 
483    if (owTouchReset(portnum))
484    {
485       // send the match command 0x69
486       if (owWriteByte(portnum,0x69))
487       {
488          // switch to overdrive communication speed
489          owSpeed(portnum,MODE_OVERDRIVE);
490
491          // create a buffer to use with block function      
492          // Serial Number
493          for (i = 0; i < 8; i++)
494             sendpacket[i] = SerialNum[portnum][i];
495       
496          // send/recieve the transfer buffer   
497          if (owBlock(portnum,FALSE,sendpacket,8))
498          {
499             // verify that the echo of the writes was correct
500             for (i = 0; i < 8; i++)
501                if (sendpacket[i] != SerialNum[portnum][i])
502                   bad_echo = TRUE;
503             // if echo ok then success
504             if (!bad_echo)
505                return TRUE;               
506          }
507       }
508    }
509    
510    // failure, force back to normal communication speed
511    owSpeed(portnum,MODE_NORMAL);
512
513    return FALSE;
514 }
515
516 //--------------------------------------------------------------------------
517 // Bit utility to read and write a bit in the buffer 'buf'.
518 //
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
523 //           to read or write
524 //
525 // Returns: 1   if operation is set (1)
526 //          0/1 state of bit number 'loc' if operation is reading 
527 //
528 int bitacc(int op, int state, int loc, uchar *buf)
529 {
530    int nbyt,nbit;
531
532    nbyt = (loc / 8);
533    nbit = loc - (nbyt * 8);
534
535    if (op == WRITE_FUNCTION)
536    {
537       if (state)
538          buf[nbyt] |= (0x01 << nbit);
539       else
540          buf[nbyt] &= ~(0x01 << nbit);
541
542       return 1;
543    }
544    else
545       return ((buf[nbyt] >> nbit) & 0x01);
546 }