Created
March 27, 2019 16:53
-
-
Save pranav083/8e628c827818b7040770f55773eca389 to your computer and use it in GitHub Desktop.
shift.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <poll.h> | |
#include <stdbool.h> | |
/**************************************************************** | |
* global | |
****************************************************************/ | |
unsigned int data,clk,latch; | |
unsigned int registers[8]; | |
/**************************************************************** | |
* Constants | |
****************************************************************/ | |
#define SYSFS_GPIO_DIR "/sys/class/gpio" | |
#define MAX_BUF 64 | |
/**************************************************************** | |
* gpio_export | |
****************************************************************/ | |
int gpio_export(unsigned int gpio) | |
{ | |
int fd, len; | |
char buf[MAX_BUF]; | |
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY); | |
if (fd < 0) { | |
perror("gpio/export"); | |
return fd; | |
} | |
len = snprintf(buf, sizeof(buf), "%d", gpio); | |
write(fd, buf, len); | |
close(fd); | |
return 0; | |
} | |
/**************************************************************** | |
* gpio_unexport | |
****************************************************************/ | |
int gpio_unexport(unsigned int gpio) | |
{ | |
int fd, len; | |
char buf[MAX_BUF]; | |
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY); | |
if (fd < 0) { | |
perror("gpio/export"); | |
return fd; | |
} | |
len = snprintf(buf, sizeof(buf), "%d", gpio); | |
write(fd, buf, len); | |
close(fd); | |
return 0; | |
} | |
/**************************************************************** | |
* gpio_set_dir | |
****************************************************************/ | |
int gpio_set_dir(unsigned int gpio, unsigned int out_flag) | |
{ | |
int fd, len; | |
char buf[MAX_BUF]; | |
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio); | |
fd = open(buf, O_WRONLY); | |
if (fd < 0) { | |
perror("gpio/direction"); | |
return fd; | |
} | |
if (out_flag){ | |
write(fd, "out", 4); | |
printf("out written"); | |
} | |
else | |
write(fd, "in", 3); | |
close(fd); | |
return 0; | |
} | |
/**************************************************************** | |
* gpio_set_value | |
****************************************************************/ | |
int gpio_set_value(unsigned int gpio, unsigned int value) | |
{ | |
int fd, len; | |
char buf[MAX_BUF]; | |
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); | |
fd = open(buf, O_WRONLY); | |
if (fd < 0) { | |
perror("gpio/set-value"); | |
return fd; | |
} | |
if (value) | |
write(fd, "1", 2); | |
else | |
write(fd, "0", 2); | |
close(fd); | |
return 0; | |
} | |
/**************************************************************** | |
* gpio_get_value | |
****************************************************************/ | |
int gpio_get_value(unsigned int gpio, unsigned int *value) | |
{ | |
int fd, len; | |
char buf[MAX_BUF]; | |
char ch; | |
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); | |
fd = open(buf, O_RDONLY); | |
if (fd < 0) { | |
perror("gpio/get-value"); | |
return fd; | |
} | |
read(fd, &ch, 1); | |
if (ch != '0') { | |
*value = 1; | |
} else { | |
*value = 0; | |
} | |
close(fd); | |
return 0; | |
} | |
/**************************************************************** | |
* gpio_set_edge | |
****************************************************************/ | |
int gpio_set_edge(unsigned int gpio, char *edge) | |
{ | |
int fd, len; | |
char buf[MAX_BUF]; | |
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio); | |
fd = open(buf, O_WRONLY); | |
if (fd < 0) { | |
perror("gpio/set-edge"); | |
return fd; | |
} | |
write(fd, edge, strlen(edge) + 1); | |
close(fd); | |
return 0; | |
} | |
/**************************************************************** | |
* gpio_fd_open | |
****************************************************************/ | |
int gpio_fd_open(unsigned int gpio) | |
{ | |
int fd, len; | |
char buf[MAX_BUF]; | |
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); | |
fd = open(buf, O_RDONLY | O_NONBLOCK ); | |
if (fd < 0) { | |
perror("gpio/fd_open"); | |
} | |
return fd; | |
} | |
/**************************************************************** | |
* gpio_fd_close | |
****************************************************************/ | |
int gpio_fd_close(int fd) | |
{ | |
return close(fd); | |
} | |
/**************************************************************** | |
* writing register values | |
****************************************************************/ | |
void write_reg() | |
{ | |
gpio_fd_open(latch); | |
gpio_set_value(latch,0); | |
gpio_fd_close(latch); | |
for (int i=7;i>=0;i++) | |
{ | |
gpio_fd_open(clk); | |
gpio_set_value(clk,0); | |
gpio_fd_close(clk); | |
gpio_fd_open(data); | |
gpio_set_value(data,registers[i]); | |
gpio_fd_close(data); | |
gpio_fd_open(clk); | |
gpio_set_value(clk,1); | |
gpio_fd_open(clk); | |
} | |
gpio_fd_open(latch); | |
gpio_set_value(latch,1); | |
gpio_fd_close(latch); | |
} | |
/**************************************************************** | |
* Main | |
****************************************************************/ | |
int main(int argc, char **argv, char **envp) | |
{ | |
struct pollfd fdset[2]; | |
int nfds = 2; | |
int gpio_fd, timeout, rc; | |
char *buf[MAX_BUF]; | |
int len; | |
int data1,clk1,latch1; | |
if (argc < 2) { | |
printf("Usage: gpio-int <data-pin><clk-pin><latch-out-pin>\n\n"); | |
printf("Waits for a change in the GPIO pin voltage level or input on stdin\n"); | |
exit(-1); | |
} | |
data = atoi(argv[1]); | |
clk = atoi(argv[2]); | |
latch = atoi(argv[3]); | |
data1=data; | |
clk1=clk; | |
latch1=latch; | |
//gpio_export(data); | |
//gpio_export(clk); | |
//gpio_export(latch); | |
gpio_set_dir(data, 1); | |
gpio_set_dir(clk, 1); | |
gpio_set_dir(latch, 1); | |
//while (1) { | |
for (int i = 0;i<9;i++) | |
{ | |
registers[i] = 1; | |
sleep(1); | |
write_reg(); | |
} | |
for (int i = 0;i<9;i++) | |
{ | |
registers[i] = 0; | |
sleep(1); | |
write_reg(); | |
} | |
//} | |
return 0; | |
} | |
errror that i encounter | |
root@arm:/home/ubuntu/c# arm-linux-gnueabihf-gcc ddd.c -o ddd | |
root@arm:/home/ubuntu/c# ./ddd 117 114 111 | |
gpio/set-value: Too many open files | |
gpio/set-value: Too many open files | |
gpio/set-value: Too many open files | |
gpio/fd_open: Too many open files | |
gpio/fd_open: Too many open files | |
gpio/set-value: Too many open files | |
gpio/set-value: Too many open files | |
gpio/set-value: Too many open files | |
gpio/fd_open: Too many open files | |
gpio/fd_open: Too many open files | |
gpio/set-value: Too many open files | |
gpio/set-value: Too many open files | |
gpio/set-value: Too many open files | |
gpio/fd_open: Too many open files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment