Mercurial > ~darius > hgwebdir.cgi > avr
comparison testugen2.c @ 27:d5a265299a4b
Test programs to talk to the hardware from a PC
author | darius |
---|---|
date | Mon, 10 Apr 2006 17:22:17 +0930 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
26:350e8655cbb7 | 27:d5a265299a4b |
---|---|
1 #include <sys/types.h> | |
2 #include <sys/ioctl.h> | |
3 #include <dev/usb/usb.h> | |
4 #include <sys/errno.h> | |
5 #include <sys/uio.h> | |
6 #include <unistd.h> | |
7 #include <fcntl.h> | |
8 #include <stdio.h> | |
9 #include <string.h> | |
10 #include <stdlib.h> | |
11 | |
12 /* Return codes for OWFirst()/OWNext() */ | |
13 #define OW_BADWIRE -3 | |
14 #define OW_BADCRC -2 | |
15 #define OW_NOPRESENCE -1 | |
16 #define OW_NOMODULES 0 | |
17 #define OW_FOUND 1 | |
18 | |
19 void docmd(int fd, uint8_t *buffer, int rlen, int wlen); | |
20 | |
21 int | |
22 main(int argc, char **argv) { | |
23 int endptfd, i; | |
24 char *endptname = "/dev/ugen0.2"; | |
25 uint8_t buffer[9]; | |
26 | |
27 if (argc < 2) { | |
28 fprintf(stderr, | |
29 "Bad usage:\n" | |
30 "\t%s cmd\n", argv[0]); | |
31 exit(1); | |
32 } | |
33 | |
34 if ((endptfd = open(endptname, O_RDWR)) == -1) { | |
35 fprintf(stderr, "Unable to open %s: %s\n", endptname, strerror(errno)); | |
36 exit(1); | |
37 } | |
38 | |
39 i = 1; | |
40 if (ioctl(endptfd, USB_SET_SHORT_XFER, &i) == -1) { | |
41 fprintf(stderr, "Unable to set short xfer on end point 1: %s\n", strerror(errno)); | |
42 exit(1); | |
43 } | |
44 | |
45 bzero(buffer, 9); | |
46 | |
47 if (!strcmp(argv[1], "reset")) { | |
48 buffer[0] = 0x00; | |
49 docmd(endptfd, buffer, 1, 1); | |
50 printf("OWTouchReset() reported %d\n", buffer[0]); | |
51 } else if (!strcmp(argv[1], "first")) { | |
52 buffer[0] = 0x01; | |
53 docmd(endptfd, buffer, 1, 9); | |
54 printf("OWFirst() = %d\n", buffer[0]); | |
55 if (buffer[0] == OW_FOUND) | |
56 printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", | |
57 buffer[1], buffer[2], buffer[3], buffer[4], | |
58 buffer[5], buffer[6], buffer[7], buffer[8]); | |
59 } else if (!strcmp(argv[1], "next")) { | |
60 if (scanf(argv[2], "%hh:%hh:%hh:%hh:%hh:%hh:%hh:%hh", &buffer[1], &buffer[2], &buffer[3], | |
61 &buffer[4], &buffer[5], &buffer[6], &buffer[7], &buffer[8]) != 8) { | |
62 fprintf(stderr, "Unable to parse ROM ID\n"); | |
63 exit(1); | |
64 } | |
65 buffer[0] = 0x02; | |
66 fprintf(stderr, "About to OWNext()\n"); | |
67 docmd(endptfd, buffer, 9, 9); | |
68 fprintf(stderr, "OWNext() = %d\n", buffer[0]); | |
69 if (buffer[0] == OW_FOUND) | |
70 printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", | |
71 buffer[1], buffer[2], buffer[3], buffer[4], | |
72 buffer[5], buffer[6], buffer[7], buffer[8]); | |
73 | |
74 } else if (!strcmp(argv[1], "scan")) { | |
75 buffer[0] = 0x00; | |
76 fprintf(stderr, "About to reset\n"); | |
77 docmd(endptfd, buffer, 1, 1); | |
78 printf("OWReset() reported %d\n", buffer[0]); | |
79 if (buffer[0] != 0) | |
80 exit(1); | |
81 | |
82 bzero(buffer, 9); | |
83 buffer[0] = 0x01; | |
84 fprintf(stderr, "About to OWFirst()\n"); | |
85 docmd(endptfd, buffer, 1, 9); | |
86 printf("OWFirst() = %d\n", buffer[0]); | |
87 | |
88 do { | |
89 switch (buffer[0]) { | |
90 case OW_BADWIRE: | |
91 printf("Bad wiring, either bus is held low, or a presence pulse was detected but no module found\n"); | |
92 break; | |
93 | |
94 case OW_NOPRESENCE: | |
95 printf("No presence pulse detected\n"); | |
96 break; | |
97 | |
98 case OW_BADCRC: | |
99 printf("Bad CRC\n"); | |
100 break; | |
101 | |
102 case OW_NOMODULES: | |
103 case OW_FOUND: | |
104 break; | |
105 | |
106 default: | |
107 printf("Unknown return code %d\n", buffer[0]); | |
108 break; | |
109 } | |
110 | |
111 if (buffer[0] != OW_FOUND) { | |
112 printf("No more modules\n"); | |
113 break; | |
114 } else { | |
115 printf("Found module %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", | |
116 buffer[1], buffer[2], buffer[3], buffer[4], | |
117 buffer[5], buffer[6], buffer[7], buffer[8]); | |
118 } | |
119 | |
120 buffer[0] = 0x02; | |
121 fprintf(stderr, "About to OWNext()\n"); | |
122 docmd(endptfd, buffer, 9, 9); | |
123 } while(1); | |
124 } else { | |
125 printf("Unknown command: %s\n", argv[1]); | |
126 exit(1); | |
127 } | |
128 | |
129 | |
130 exit(0); | |
131 } | |
132 | |
133 void | |
134 docmd(int fd, uint8_t *buffer, int wlen, int rlen) { | |
135 char blah[20]; | |
136 int i; | |
137 | |
138 fprintf(stderr, "About to write\n"); | |
139 gets(blah); | |
140 | |
141 if ((i = write(fd, buffer, wlen)) == -1) { | |
142 fprintf(stderr, "Unable to write end point: %s\n", strerror(errno)); | |
143 exit(1); | |
144 } | |
145 if (i != wlen) | |
146 printf("Warning, only wrote %d of %d bytes\n", i, wlen); | |
147 | |
148 fprintf(stderr, "About to read\n"); | |
149 gets(blah); | |
150 | |
151 if ((i = read(fd, buffer, rlen)) == -1) { | |
152 fprintf(stderr, "Unable to read from end point: %s\n", strerror(errno)); | |
153 exit(1); | |
154 } | |
155 if (i != rlen) | |
156 printf("Warning, only read %d of %d bytes\n", i, wlen); | |
157 } | |
158 | |
159 |