Project

General

Profile

Download (5.82 KB) Statistics
| Branch: | Tag: | Revision:
1 ae06ac82 (no author)
/* opcd_test - Low-Level test program for OpenPCD
2
 * (C) 2006 by Harald Welte <laforge@gnumonks.org>
3
 *
4
 *  This program is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License version 2 
6
 *  as published by the Free Software Foundation
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 */
17
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <unistd.h>
21
#include <string.h>
22
#define _GNU_SOURCE
23
#include <getopt.h>
24
#include <errno.h>
25
26
#include <sys/types.h>
27
#include <sys/stat.h>
28
#include <sys/mman.h>
29
#include <fcntl.h>
30
31
#include <usb.h>
32
33 3b41196c meri
#include <openpcd.h>
34 4c1c0deb (no author)
#include "opcd_usb.h"
35 ae06ac82 (no author)
36
static int get_number(const char *optarg, unsigned int min,
37
		      unsigned int max, unsigned int *num)
38
{
39
	char *endptr;
40 a607faca (no author)
	unsigned long nbr = strtoul(optarg, &endptr, 0);
41
	//fprintf(stderr, "trying to read `%s' as number\n", optarg);
42 ae06ac82 (no author)
43
	if (nbr == 0 && optarg == endptr)
44
		return -EINVAL;
45
46 a607faca (no author)
	if (nbr < min || nbr > max)
47 ae06ac82 (no author)
		return -ERANGE;
48
49
	*num = nbr;
50
	return 0;
51
}
52
53 a607faca (no author)
static void print_welcome(void)
54
{
55
	printf("opcd_test - OpenPCD Test and Debug Program\n"
56
	       "(C) 2006 by Harald Welte <laforge@gnumonks.org>\n\n");
57
}
58
static void print_help(void)
59
{
60
	printf( "\t-l\t--led-set\tled {0,1}\n"
61
		"\t-w\t--reg-write\treg value\n"
62
		"\t-r\t--reg-read\treg\n"
63
64 13b60901 laforge
		"\t-W\t--fifo-write\thex\n"
65
		"\t-R\t--fifo-read\thex\n"
66
67 a607faca (no author)
		"\t-s\t--set-bits\treg\tmask\n"
68 13b60901 laforge
		"\t-c\t--clear-bits\treg\tmask\n"
69
70
		"\t-u\t--usb-perf\txfer_size\n"
71
		);
72 a607faca (no author)
}
73
74 4c1c0deb (no author)
75 a607faca (no author)
static struct option opts[] = {
76
	{ "led-set", 1, 0, 'l' },
77
	{ "reg-write", 1, 0, 'w' },
78
	{ "reg-read", 1, 0, 'r' },
79
	{ "fifo-write", 1, 0, 'W' },
80
	{ "fifo-read", 1, 0, 'R' },
81
	{ "set-bits", 1, 0, 's' },
82
	{ "clear-bits", 1, 0, 'c' },
83 9865031f (no author)
	{ "usb-perf", 1, 0, 'u' },
84 5cc02937 (no author)
	{ "adc-read", 0, 0, 'a' },
85
	{ "adc-loop", 0, 0, 'A' },
86 9c00f1d5 (no author)
	{ "ssc-read", 0, 0, 'S' },
87
	{ "loop", 0, 0, 'L' },
88 13b60901 laforge
	{ "serial-number", 0, 0, 'n' },
89 a607faca (no author)
	{ "help", 0, 0, 'h'},
90
};	
91 ae06ac82 (no author)
92
int main(int argc, char **argv)
93
{
94 4c1c0deb (no author)
	struct opcd_handle *od;
95 13b60901 laforge
	int c, outfd, retlen;
96
	char *data;
97 4c1c0deb (no author)
	static char buf[8192];
98
	int buf_len = sizeof(buf);
99 ae06ac82 (no author)
100 a607faca (no author)
	print_welcome();
101
102 304a9dd4 laforge
	if (!strcmp(argv[0], "./opicc_test"))
103 13b60901 laforge
		od = opcd_init(1);
104
	else
105
		od = opcd_init(0);
106 ae06ac82 (no author)
107
	while (1) {
108
		int option_index = 0;
109
110 13b60901 laforge
		c = getopt_long(argc, argv, "l:r:w:R:W:s:c:h?u:aASLn", opts,
111 ae06ac82 (no author)
				&option_index);
112
113
		if (c == -1)
114
			break;
115
116
		switch (c) {
117
			unsigned int i,j;
118
		case 'l':
119
			if (get_number(optarg, 1, 2, &i) < 0)
120
				exit(2);
121
			if (get_number(argv[optind], 0, 1, &j) < 0)
122
				exit(2);
123 a607faca (no author)
			printf("setting LED %d to %s\n", i, j ? "on" : "off");
124 4c1c0deb (no author)
			opcd_send_command(od, OPENPCD_CMD_SET_LED, i, j, 0, NULL);
125 ae06ac82 (no author)
			break;
126
		case 'r':
127 a607faca (no author)
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0)
128 ae06ac82 (no author)
				exit(2);
129 13b60901 laforge
			printf("reading register 0x%02x: ", i);
130 4c1c0deb (no author)
			opcd_send_command(od, OPENPCD_CMD_READ_REG, i, 0, 0, NULL);
131
			opcd_recv_reply(od, buf, buf_len);
132 ae06ac82 (no author)
			break;
133
		case 'w':
134 a607faca (no author)
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0) {
135
				fprintf(stderr, "can't read register\n");
136 ae06ac82 (no author)
				exit(2);
137 a607faca (no author)
			}
138
			if (get_number(argv[optind], 0x00, 0xff, &j) < 0) {
139
				fprintf(stderr, "can't read value\n");
140 ae06ac82 (no author)
				exit(2);
141 a607faca (no author)
			}
142 ae06ac82 (no author)
			fprintf(stdout, "setting register 0x%02x to 0x%02x\n", i, j);
143 4c1c0deb (no author)
			opcd_send_command(od, OPENPCD_CMD_WRITE_REG, i, j, 0, NULL);
144 ae06ac82 (no author)
			break;
145
		case 'R':
146 a607faca (no author)
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0)
147 ae06ac82 (no author)
				exit(2);
148 4c1c0deb (no author)
			opcd_send_command(od, OPENPCD_CMD_READ_FIFO, 0, i, 0, NULL);
149
			opcd_recv_reply(od, buf, buf_len);
150 ae06ac82 (no author)
			break;
151
		case 'W':
152
			fprintf(stderr, "FIFO write not implemented yet\n");
153
			break;
154 a607faca (no author)
		case 's':
155
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0)
156
				exit(2);
157
			if (get_number(argv[optind], 0x00, 0xff, &j) < 0)
158
				exit(2);
159 4c1c0deb (no author)
			opcd_send_command(od, OPENPCD_CMD_REG_BITS_SET, i, j, 0, NULL);
160 a607faca (no author)
			break;
161
		case 'c':
162
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0)
163
				exit(2);
164
			if (get_number(argv[optind], 0x00, 0xff, &j) < 0)
165
				exit(2);
166 4c1c0deb (no author)
			opcd_send_command(od, OPENPCD_CMD_REG_BITS_CLEAR, i, j, 0, NULL);
167 a607faca (no author)
			break;
168 9865031f (no author)
		case 'u':
169
			if (get_number(optarg, 1, 255, &i) < 0)
170
				exit(2);
171
			opcd_usbperf(od, i);
172
			break;
173 5cc02937 (no author)
		case 'a':
174 9c00f1d5 (no author)
			opcd_send_command(od, OPENPCD_CMD_ADC_READ, 0, 1, 0, NULL);
175 5cc02937 (no author)
			opcd_recv_reply(od, buf, buf_len);
176
			/* FIXME: interpret and print ADC result */
177
			break;
178
		case 'A':
179
			while (1) {
180 9c00f1d5 (no author)
				opcd_send_command(od, OPENPCD_CMD_ADC_READ, 0, 1, 0, NULL);
181 5cc02937 (no author)
				opcd_recv_reply(od, buf, buf_len);
182
				/* FIXME: interpret and print ADC result */
183
			}
184
			break;
185 9c00f1d5 (no author)
		case 'S':
186
			opcd_send_command(od, OPENPCD_CMD_SSC_READ, 0, 1, 0, NULL);
187
			opcd_recv_reply(od, buf, buf_len);
188 13b60901 laforge
			/* FIXME: interpret and print SSC result */
189 9c00f1d5 (no author)
			break;
190
		case 'L':
191 13b60901 laforge
			outfd = open("/tmp/opcd_samples",
192
				     O_CREAT|O_WRONLY|O_APPEND, 0664);
193
			if (outfd < 0)
194
				exit(2);
195
			while (1) {
196
				data = buf + sizeof(struct openpcd_hdr);
197
				retlen = opcd_recv_reply(od, buf, buf_len);
198
				if (retlen < 0)
199
					break;
200
				printf("DATA: %s\n", opcd_hexdump(data, retlen-4));
201
#if 1
202
				write(outfd, data, retlen-4);
203
				fsync(outfd);
204
#endif
205
			}
206
			close(outfd);
207 9c00f1d5 (no author)
			break;
208 a607faca (no author)
		case 'h':
209
		case '?':
210
			print_help();
211
			exit(0);
212
			break;
213 13b60901 laforge
		case 'n':
214
			opcd_send_command(od, OPENPCD_CMD_GET_SERIAL, 0, 1, 4,
215
					  NULL);
216
			retlen = opcd_recv_reply(od, buf,
217
						 sizeof(struct openpcd_hdr)+4);
218
			if (retlen > 0) {
219
				data = buf + sizeof(struct openpcd_hdr);
220
				printf("SERIAL: %s\n", opcd_hexdump(data, retlen-4));
221
			} else
222
				printf("ERROR: %d, %s\n", retlen, usb_strerror());
223
			break;
224 ae06ac82 (no author)
		default:
225
			fprintf(stderr, "unknown key `%c'\n", c);
226 a607faca (no author)
			print_help();
227
			exit(2);
228 ae06ac82 (no author)
			break;
229
		}
230
	}
231
232 500fb4be (no author)
	sleep(1);
233
234 ae06ac82 (no author)
	exit(0);
235
}
Add picture from clipboard (Maximum size: 48.8 MB)