pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS

Difference between
modified post 226d8e2 by Anonymous on Fri 13th Dec 2019 14:55
original post 8c4e by Anonymous on Wed 11th Dec 2019 15:56
Download diff
Show old version | new version | both versions

    
11
/*
22
 * qaweep1.c - Quantron beeper-weeper demo V0.0.1
33
 *
44
 * Written by Roland Mainz <r.mainz@eckelmann.de>
55
 */
66
77
/*
88
 * Compile with
99
 * $ /opt/OSELAS.Toolchain-2012.12.0/arm-v5te-linux-gnueabi/gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized/bin/arm-v5te-linux-gnueabi-gcc -std=gnu99 -g -Wall -fno-use-linker-plugin qaweep1.c
1010
 */
1111
#include <errno.h>
1212
#include <fcntl.h>
1313
#include <stdio.h>
1414
#include <stdlib.h>
1515
#include <sys/stat.h>
1616
#include <sys/types.h>
1717
#include <unistd.h>
1818
1919
#define NUM_ELEMENTS_IN_ARRAY(ar) (sizeof(ar) / sizeof (ar[0]))
2020
const double freqs[] = {
2121
	440.0000,
2222
	466.1638,
2323
	493.8833,
2424
	523.2511,
2525
	554.3653,
2626
	587.3295,
2727
	622.2540,
2828
	659.2551,
2929
	698.4565,
3030
	739.9888,
3131
	783.9909,
3232
	830.6094,
3333
	880.0000,
3434
	932.3275,
3535
	987.7666,
3636
	1046.502,
3737
	1108.731,
3838
	1174.659,
3939
	1244.508
4040
};
4141
4242
#define MICROSECONDS_PER_SEC (1000000L)
4343
44-
int main(int ac, char *av[])
44+
static
45+
int cycle_loop(int fd, long cycles, long low_delay, long high_delay)
4546
{
46-
	int	retval = EXIT_FAILURE;
47+
	int retval = 0;
47-
	int	fd,
48+
	int i;
4949
50-
	fd = open("/sys/class/gpio/export", O_WRONLY);
50+
	for (i = 0; i < cycles; i++) {
51-
	if (fd == -1) {
51+
		if (write(fd, "1", 1) != 1) {
5352
		goto exit_ioerr;
5453
	}
5554
56-
	if (write(fd, "11", 2) != 2) {
55+
		(void)usleep(low_delay);
6056
61-
	(void)close(fd);
57+
		if (write(fd, "0", 1) != 1) {
58+
			goto exit_ioerr;
6260
63-
	gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY);
61+
		(void)usleep(high_delay);
6762
	}
6863
69-
	fd = openat(gpiodirfd, "direction", O_WRONLY);
64+
	retval = 1;
70-
	if (fd == -1) {
65+
exit_ioerr:
7466
75-
	if (write(fd, "out", 3) != 3) {
67+
	return retval;
7868
	}
7969
80-
	(void)close(fd);
70+
int main(int ac, char *av[])
71+
{
8175
82-
	fd = openat(gpiodirfd, "value", O_WRONLY);
76+
	fd = open("/sys/class/gpio/export", O_WRONLY);
8377
	if (fd == -1) {
84-
		perror("Unable to open value");
78+
		perror("Unable to open /sys/class/gpio/export");
8579
		goto exit_ioerr;
8680
	}
8781
88-
	int i, j;
82+
	if (write(fd, "11", 2) != 2) {
89-
	for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++)
83+
		perror("Error writing to /sys/class/gpio/export");
90-
	{
84+
		goto exit_ioerr;
91-
		double f = freqs[j];
85+
	}
9486
95-
		(void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n",
87+
	(void)close(fd);
9988
100-
		for (i = 0; i < cycles; i++) {
89+
	gpiodirfd = open("/sys/class/gpio/gpio11/", O_DIRECTORY);
101-
			if (write(fd, "1", 1) != 1) {
90+
	if (gpiodirfd == -1) {
102-
				perror("Error writing 0 to value");
91+
		perror("Unable to open /sys/class/gpio/gpio11/");
10392
				goto exit_ioerr;
10493
			}
10594
106-
			(void)usleep(delay/2);
95+
	fd = openat(gpiodirfd, "direction", O_WRONLY);
96+
	if (fd == -1) {
107100
108-
			if (write(fd, "0", 1) != 1) {
101+
	if (write(fd, "out", 3) != 3) {
109-
				perror("Error writing 1 to value");
102+
		perror("Error writing to /sys/class/gpio/gpio11/direction");
110103
				goto exit_ioerr;
111104
			}
112105
119-
106+
	(void)close(fd);
115107
116-
		/* Wait 1/10 second */
108+
	fd = openat(gpiodirfd, "value", O_WRONLY);
109+
	if (fd == -1) {
118112
	}
119113
120-
	(void)close(fd);
114+
	int j;
115+
	for (j=0 ; j < NUM_ELEMENTS_IN_ARRAY(freqs) ; j++)
121120
122-
	fd = open("/sys/class/gpio/unexport", O_WRONLY);
121+
		(void)printf("freq=%4.4f\tdelay=%ld\tcycles=%ld\n",
123-
	if (fd == -1) {
122+
			f,
124-
		perror("Unable to open /sys/class/gpio/unexport");
123+
			delay,
124+
			cycles);
127125
128-
	if (write(fd, "11", 2) != 2) {
126+
		if (!cycle_loop(fd, cycles, delay/2, delay/2))
129-
		perror("Error writing to /sys/class/gpio/unexport");
127+
		{
128+
			perror("Error writing 0 to value");
130129
		goto exit_ioerr;
131130
	}
132131
133-
	retval = EXIT_SUCCESS;
132+
		/* Wait 1/4 second */
134-
exit_ioerr:
133+
		(void)usleep(MICROSECONDS_PER_SEC/4);
134+
	}
136136
		(void)close(fd);
139137
140-
	return retval;
138+
	fd = open("/sys/class/gpio/unexport", O_WRONLY);
141-
}
139+
	if (fd == -1) {
142-
140+
		perror("Unable to open /sys/class/gpio/unexport");
143-
141+
		goto exit_ioerr;
144-
142+
	}
145143
146-
144+
	if (write(fd, "11", 2) != 2) {
147-
145+
		perror("Error writing to /sys/class/gpio/unexport");
148-
146+
		goto exit_ioerr;
149-
147+
	}
150148
151-
149+
	retval = EXIT_SUCCESS;
152-
150+
exit_ioerr:
153-
151+
	if (fd >=0)
154-
152+
		(void)close(fd);
155-
153+
	if (gpiodirfd >=0)
156-
154+
		(void)close(gpiodirfd);
157155
158-
156+
	return retval;
159-
157+
}
160158

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at