Project

General

Profile

Download (5.82 KB) Statistics
| Branch: | Tag: | Revision:
1
/* 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
#include <openpcd.h>
34
#include "opcd_usb.h"
35

    
36
static int get_number(const char *optarg, unsigned int min,
37
		      unsigned int max, unsigned int *num)
38
{
39
	char *endptr;
40
	unsigned long nbr = strtoul(optarg, &endptr, 0);
41
	//fprintf(stderr, "trying to read `%s' as number\n", optarg);
42

    
43
	if (nbr == 0 && optarg == endptr)
44
		return -EINVAL;
45

    
46
	if (nbr < min || nbr > max)
47
		return -ERANGE;
48

    
49
	*num = nbr;
50
	return 0;
51
}
52

    
53
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
		"\t-W\t--fifo-write\thex\n"
65
		"\t-R\t--fifo-read\thex\n"
66

    
67
		"\t-s\t--set-bits\treg\tmask\n"
68
		"\t-c\t--clear-bits\treg\tmask\n"
69

    
70
		"\t-u\t--usb-perf\txfer_size\n"
71
		);
72
}
73

    
74

    
75
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
	{ "usb-perf", 1, 0, 'u' },
84
	{ "adc-read", 0, 0, 'a' },
85
	{ "adc-loop", 0, 0, 'A' },
86
	{ "ssc-read", 0, 0, 'S' },
87
	{ "loop", 0, 0, 'L' },
88
	{ "serial-number", 0, 0, 'n' },
89
	{ "help", 0, 0, 'h'},
90
};	
91

    
92
int main(int argc, char **argv)
93
{
94
	struct opcd_handle *od;
95
	int c, outfd, retlen;
96
	char *data;
97
	static char buf[8192];
98
	int buf_len = sizeof(buf);
99

    
100
	print_welcome();
101

    
102
	if (!strcmp(argv[0], "./opicc_test"))
103
		od = opcd_init(1);
104
	else
105
		od = opcd_init(0);
106

    
107
	while (1) {
108
		int option_index = 0;
109

    
110
		c = getopt_long(argc, argv, "l:r:w:R:W:s:c:h?u:aASLn", opts,
111
				&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
			printf("setting LED %d to %s\n", i, j ? "on" : "off");
124
			opcd_send_command(od, OPENPCD_CMD_SET_LED, i, j, 0, NULL);
125
			break;
126
		case 'r':
127
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0)
128
				exit(2);
129
			printf("reading register 0x%02x: ", i);
130
			opcd_send_command(od, OPENPCD_CMD_READ_REG, i, 0, 0, NULL);
131
			opcd_recv_reply(od, buf, buf_len);
132
			break;
133
		case 'w':
134
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0) {
135
				fprintf(stderr, "can't read register\n");
136
				exit(2);
137
			}
138
			if (get_number(argv[optind], 0x00, 0xff, &j) < 0) {
139
				fprintf(stderr, "can't read value\n");
140
				exit(2);
141
			}
142
			fprintf(stdout, "setting register 0x%02x to 0x%02x\n", i, j);
143
			opcd_send_command(od, OPENPCD_CMD_WRITE_REG, i, j, 0, NULL);
144
			break;
145
		case 'R':
146
			if (get_number(optarg, 0x00, OPENPCD_REG_MAX, &i) < 0)
147
				exit(2);
148
			opcd_send_command(od, OPENPCD_CMD_READ_FIFO, 0, i, 0, NULL);
149
			opcd_recv_reply(od, buf, buf_len);
150
			break;
151
		case 'W':
152
			fprintf(stderr, "FIFO write not implemented yet\n");
153
			break;
154
		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
			opcd_send_command(od, OPENPCD_CMD_REG_BITS_SET, i, j, 0, NULL);
160
			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
			opcd_send_command(od, OPENPCD_CMD_REG_BITS_CLEAR, i, j, 0, NULL);
167
			break;
168
		case 'u':
169
			if (get_number(optarg, 1, 255, &i) < 0)
170
				exit(2);
171
			opcd_usbperf(od, i);
172
			break;
173
		case 'a':
174
			opcd_send_command(od, OPENPCD_CMD_ADC_READ, 0, 1, 0, NULL);
175
			opcd_recv_reply(od, buf, buf_len);
176
			/* FIXME: interpret and print ADC result */
177
			break;
178
		case 'A':
179
			while (1) {
180
				opcd_send_command(od, OPENPCD_CMD_ADC_READ, 0, 1, 0, NULL);
181
				opcd_recv_reply(od, buf, buf_len);
182
				/* FIXME: interpret and print ADC result */
183
			}
184
			break;
185
		case 'S':
186
			opcd_send_command(od, OPENPCD_CMD_SSC_READ, 0, 1, 0, NULL);
187
			opcd_recv_reply(od, buf, buf_len);
188
			/* FIXME: interpret and print SSC result */
189
			break;
190
		case 'L':
191
			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
			break;
208
		case 'h':
209
		case '?':
210
			print_help();
211
			exit(0);
212
			break;
213
		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
		default:
225
			fprintf(stderr, "unknown key `%c'\n", c);
226
			print_help();
227
			exit(2);
228
			break;
229
		}
230
	}
231

    
232
	sleep(1);
233

    
234
	exit(0);
235
}
(5-5/7)
Add picture from clipboard (Maximum size: 48.8 MB)