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