summaryrefslogtreecommitdiffstats
path: root/meta-xilinx-demos/recipes-examples/gpio-demo/files/gpio-demo.c
blob: 4e17779daf07978bb79ed4d6c7efca1e5e0dc7f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
*
* gpio-demo app
*
* Copyright (C) 2013 - 2016  Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in this
* Software without prior written authorization from Xilinx.
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>

#define GPIO_ROOT "/sys/class/gpio"
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

static enum {NONE, IN, OUT, CYLON, KIT} gpio_opt = NONE;

static const unsigned long cylon[] = {
	0x00000080, 0x00000040, 0x00000020, 0x00000010,
	0x00000008, 0x00000004, 0x00000002, 0x00000001,
		    0x00000002, 0x00000004, 0x00000008,
	0x00000010, 0x00000020, 0x00000040, 0x00000080,
};

static const unsigned long kit[] = {
	0x000000e0, 0x00000070, 0x00000038, 0x0000001c,
	0x0000000e, 0x00000007, 0x00000003, 0x00000001,
		    0x00000003, 0x00000007, 0x0000000e,
	0x0000001c, 0x00000038, 0x00000070, 0x000000e0,
};

static int gl_gpio_base = 0;

static void usage (char *argv0)
{
	char *basename = strrchr(argv0, '/');
	if (!basename)
		basename = argv0;

	fprintf(stderr,
		"Usage: %s [-g GPIO_BASE] COMMAND\n"
		"\twhere COMMAND is one of:\n"
		"\t\t-i\t\tInput value from GPIO and print it\n"
		"\t\t-o\tVALUE\tOutput value to GPIO\n"
		"\t\t-c\t\tCylon test pattern\n"
		"\t\t-k\t\t KIT test pattern\n"
		"\tGPIO_BASE indicates which GPIO chip to talk to (The number can be \n"
		"\tfound at /sys/class/gpio/gpiochipN).\n"
		"\tThe highest gpiochipN is the first gpio listed in the dts file, \n"
		"\tand the lowest gpiochipN is the last gpio listed in the dts file.\n"
		"\tE.g.If the gpiochip240 is the LED_8bit gpio, and I want to output '1' \n"
		"\tto the LED_8bit gpio, the command should be:\n"
		"\t\tgpio-demo -g 240 -o 1\n"
		"\n"
		"\tgpio-demo written by Xilinx Inc.\n"
		"\n"
		,  basename);
	exit(-2);
}

static int open_gpio_channel(int gpio_base)
{
	char gpio_nchan_file[128];
	int gpio_nchan_fd;
	int gpio_max;
	int nchannel;
	char nchannel_str[5];
	char *cptr;
	int c;
	char channel_str[5];

	char *gpio_export_file = "/sys/class/gpio/export";
	int export_fd=0;

	/* Check how many channels the GPIO chip has */
	sprintf(gpio_nchan_file, "%s/gpiochip%d/ngpio", GPIO_ROOT, gpio_base);
	gpio_nchan_fd = open(gpio_nchan_file, O_RDONLY);
	if (gpio_nchan_fd < 0) {
		fprintf(stderr, "Failed to open %s: %s\n", gpio_nchan_file, strerror(errno)); 
		return -1;
	}
	read(gpio_nchan_fd, nchannel_str, sizeof(nchannel_str));
	close(gpio_nchan_fd);
	nchannel=(int)strtoul(nchannel_str, &cptr, 0);
	if (cptr == nchannel_str) {
		fprintf(stderr, "Failed to change %s into GPIO channel number\n", nchannel_str);
		exit(1);
	}

	/* Open files for each GPIO channel */
	export_fd=open(gpio_export_file, O_WRONLY);
	if (export_fd < 0) {
		fprintf(stderr, "Cannot open GPIO to export %d\n", gpio_base);
		return -1;
	}

	gpio_max = gpio_base + nchannel;	
	for(c = gpio_base; c < gpio_max; c++) {
		sprintf(channel_str, "%d", c);
		write(export_fd, channel_str, (strlen(channel_str)+1));
	}
	close(export_fd);
	return nchannel;
}

static int close_gpio_channel(int gpio_base)
{
	char gpio_nchan_file[128];
	int gpio_nchan_fd;
	int gpio_max;
	int nchannel;
	char nchannel_str[5];
	char *cptr;
	int c;
	char channel_str[5];

	char *gpio_unexport_file = "/sys/class/gpio/unexport";
	int unexport_fd=0;

	/* Check how many channels the GPIO chip has */
	sprintf(gpio_nchan_file, "%s/gpiochip%d/ngpio", GPIO_ROOT, gpio_base);
	gpio_nchan_fd = open(gpio_nchan_file, O_RDONLY);
	if (gpio_nchan_fd < 0) {
		fprintf(stderr, "Failed to open %s: %s\n", gpio_nchan_file, strerror(errno)); 
		return -1;
	}
	read(gpio_nchan_fd, nchannel_str, sizeof(nchannel_str));
	close(gpio_nchan_fd);
	nchannel=(int)strtoul(nchannel_str, &cptr, 0);
	if (cptr == nchannel_str) {
		fprintf(stderr, "Failed to change %s into GPIO channel number\n", nchannel_str);
		exit(1);
	}

	/* Close opened files for each GPIO channel */
	unexport_fd=open(gpio_unexport_file, O_WRONLY);
	if (unexport_fd < 0) {
		fprintf(stderr, "Cannot close GPIO by writing unexport %d\n", gpio_base);
		return -1;
	}

	gpio_max = gpio_base + nchannel;	
	for(c = gpio_base; c < gpio_max; c++) {
		sprintf(channel_str, "%d", c);
		write(unexport_fd, channel_str, (strlen(channel_str)+1));
	}
	close(unexport_fd);
	return 0;
}

static int set_gpio_direction(int gpio_base, int nchannel, char *direction)
{
	char gpio_dir_file[128];
	int direction_fd=0;
	int gpio_max;
	int c;

	gpio_max = gpio_base + nchannel;
	for(c = gpio_base; c < gpio_max; c++) {
		sprintf(gpio_dir_file, "/sys/class/gpio/gpio%d/direction",c);
		direction_fd=open(gpio_dir_file, O_RDWR);
		if (direction_fd < 0) {
			fprintf(stderr, "Cannot open the direction file for GPIO %d\n", c);
			return 1;
		}
		write(direction_fd, direction, (strlen(direction)+1));
		close(direction_fd);
	}
	return 0;
}

static int set_gpio_value(int gpio_base, int nchannel, int value)
{
	char gpio_val_file[128];
	int val_fd=0;
	int gpio_max;
	char val_str[2];
	int c;

	gpio_max = gpio_base + nchannel;

	for(c = gpio_base; c < gpio_max; c++) {
		sprintf(gpio_val_file, "/sys/class/gpio/gpio%d/value",c);
		val_fd=open(gpio_val_file, O_RDWR);
		if (val_fd < 0) {
			fprintf(stderr, "Cannot open the value file of GPIO %d\n", c);
			return -1;
		}
		sprintf(val_str,"%d", (value & 1));
		write(val_fd, val_str, sizeof(val_str));
		close(val_fd);
		value >>= 1;
	}
	return 0;
}

static int get_gpio_value(int gpio_base, int nchannel)
{
	char gpio_val_file[128];
	int val_fd=0;
	int gpio_max;
	char val_str[2];
	char *cptr;
	int value = 0;
	int c;

	gpio_max = gpio_base + nchannel;

	for(c = gpio_max-1; c >= gpio_base; c--) {
		sprintf(gpio_val_file, "/sys/class/gpio/gpio%d/value",c);
		val_fd=open(gpio_val_file, O_RDWR);
		if (val_fd < 0) {
			fprintf(stderr, "Cannot open GPIO to export %d\n", c);
			return -1;
		}
		read(val_fd, val_str, sizeof(val_str));
		value <<= 1;
		value += (int)strtoul(val_str, &cptr, 0);
		if (cptr == optarg) {
			fprintf(stderr, "Failed to change %s into integer", val_str);
		}
		close(val_fd);
	}
	return value;
}

void signal_handler(int sig)
{
	switch (sig) {
		case SIGTERM:
		case SIGHUP:
		case SIGQUIT:
		case SIGINT:
			close_gpio_channel(gl_gpio_base);
			exit(0) ;
		default:
			break;
	}
}

int main(int argc, char *argv[])
{
	extern char *optarg;
	char *cptr;
	int gpio_value = 0;
	int nchannel = 0;

	int c;
	int i;

	opterr = 0;
	
	while ((c = getopt(argc, argv, "g:io:ck")) != -1) {
		switch (c) {
			case 'g':
				gl_gpio_base = (int)strtoul(optarg, &cptr, 0);
				if (cptr == optarg)
					usage(argv[0]);
				break;
			case 'i':
				gpio_opt = IN;
				break;
			case 'o':
				gpio_opt = OUT;
				gpio_value = (int)strtoul(optarg, &cptr, 0);
				if (cptr == optarg)
					usage(argv[0]);
				break;
			case 'c':
				gpio_opt = CYLON;
				break;
			case 'k':
				gpio_opt = KIT;
				break;
			case '?':
				usage(argv[0]);
			default:
				usage(argv[0]);
				
		}
	}

	if (gl_gpio_base == 0) {
		usage(argv[0]);
	}

	nchannel = open_gpio_channel(gl_gpio_base);
	signal(SIGTERM, signal_handler); /* catch kill signal */
	signal(SIGHUP, signal_handler); /* catch hang up signal */
	signal(SIGQUIT, signal_handler); /* catch quit signal */
	signal(SIGINT, signal_handler); /* catch a CTRL-c signal */
	switch (gpio_opt) {
		case IN:
			set_gpio_direction(gl_gpio_base, nchannel, "in");
			gpio_value=get_gpio_value(gl_gpio_base, nchannel);
			fprintf(stdout,"0x%08X\n", gpio_value);
			break;
		case OUT:
			set_gpio_direction(gl_gpio_base, nchannel, "out");
			set_gpio_value(gl_gpio_base, nchannel, gpio_value);
			break;
		case CYLON:
#define CYLON_DELAY_USECS (10000)
			set_gpio_direction(gl_gpio_base, nchannel, "out");
			for (;;) {
				for(i=0; i < ARRAY_SIZE(cylon); i++) {
					gpio_value=(int)cylon[i];
					set_gpio_value(gl_gpio_base, nchannel, gpio_value);
				}
				usleep(CYLON_DELAY_USECS);
			}
		case KIT:
#define KIT_DELAY_USECS (10000)
			set_gpio_direction(gl_gpio_base, nchannel, "out");
			for (;;) {
				for (i=0; i<ARRAY_SIZE(kit); i++) {
					gpio_value=(int)kit[i];
					set_gpio_value(gl_gpio_base, nchannel, gpio_value);
				}
				usleep(KIT_DELAY_USECS);
			}
		default:
			break;
	}
	close_gpio_channel(gl_gpio_base);
	return 0;
}