Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / readmac / readmac.c
1 // readmac.c
2 //
3 // Uses the internal 1-wire access routines in ow.c to read the MAC address
4 // from the 2502 on the TINI.
5
6
7 #include <stdio.h>
8
9 #include "ow.h"
10 #include "crcutil.h"
11
12 void main(void)
13 {
14    unsigned char serial[32];   
15    unsigned char mac[6];
16    unsigned char myCRC;
17    unsigned char rc = 0;
18    unsigned char i;
19
20    printf("Probing internal one-wire bus...\n"); 
21
22    if (owTouchReset())
23    {
24        printf("No presence pulse.\n");  
25        rc = 1;
26    }
27
28    if (!rc && !owFirst(0, 0))
29    {
30        printf("Nothing on internal 1-wire bus.\n");
31        rc = 1;
32    }
33
34    if (!rc)
35    {
36         // Find first device of type 89 on internal 1-wire.
37         {
38             owSerialNum(serial, 1);
39        
40             #ifdef NOISY
41             printf("found 1-wire device: %02x%02x%02x%02x%02x%02x%02x%02x\n",
42                    serial[0], serial[1], serial[2], serial[3],
43                    serial[4], serial[5], serial[6], serial[7]);
44             #endif
45         } while (serial[0] != 0x89 && owNext(0, 0));
46
47         owTouchReset();
48        
49         if (serial[0] != 0x89)
50         {
51             printf("No DS2502 found on internal 1-wire bus.\n");
52             rc = 1;
53         }
54    }
55     
56    if (!rc)
57    {
58         printf("DS2502 located (ID %02x%02x%02x%02x%02x%02x%02x%02x).\n",
59                    serial[0], serial[1], serial[2], serial[3],
60                    serial[4], serial[5], serial[6], serial[7]);       
61         /* Build command packet. */
62         serial[0] = 0xCC;       /* Skip ROM. */
63         serial[1] = 0xF0;       /* Read memory. */
64         serial[2] = 0x00;       /* Start address least significant byte. */
65         serial[3] = 0x00;       /* start address MSB. */
66         serial[4] = 0xFF;       /* listen slot for CRC8 of 1-3. */
67         
68         owBlock(0, serial, 5);
69         
70         myCRC = docrc8(0, serial[1]);
71         myCRC = docrc8(myCRC, serial[2]);
72         myCRC = docrc8(myCRC, serial[3]);
73         if (myCRC != serial[4])
74         {
75             printf("read failed: bogus CRC: %x != %x\n", myCRC, serial[4]);
76             rc = 1;
77         }
78     }
79
80     if (!rc)
81     {
82         for (i = 0; i < 32; i++)
83         {
84             serial[i] = 0xFF;
85         }
86         owBlock(0, serial, 32);
87          
88         #ifdef NOISY
89         printf("DS2502 memory: ");
90         for (i = 0; i < 32; i++)
91         {
92             printf("%02x ", serial[i]);
93         }       
94         printf("\n");
95         #endif
96         {
97             unsigned char *mptr = mac;
98             unsigned char *sptr = &(serial[10]);
99             
100             for (i = 0; i < 6; i++)
101             {
102                 *mptr++ = *sptr--;
103             }
104         }
105         
106         printf("MAC address: %02x%02x%02x%02x%02x%02x\n",
107                 mac[0], mac[1], mac[2],
108                 mac[3], mac[4], mac[5]);
109         owTouchReset();
110     }
111 }