int gpio_get_value(unsigned gpio) { unsigned long flags; if (unlikely(get_gpio_edge(gpio))) { int ret; local_irq_save(flags); set_gpio_edge(gpio, 0); ret = get_gpio_data(gpio); set_gpio_edge(gpio, 1); local_irq_restore(flags); return ret; } else return get_gpio_data(gpio); }
void RangeFinder::start() { nfds = 2; // Set the signal callback for Ctrl-C // signal(SIGINT, signal_handler); // gpio = atoi(argv[1]); export_gpio(); set_gpio_direction(0); set_gpio_edge("both"); // Can be rising, falling or both gpio_fd = gpio_fd_open(); threadRunning = 1; pthread_create(&rangeFinderThread_t, 0, &RangeFinder::start_thread, this); }
void init(void) { //Setup LED0 as GPIO OUT export_gpio(LED0); set_gpio_direction(LED0, "out"); set_gpio_value(LED0, led0_value); led0_fd = gpio_fd_open(LED0); //Setup MUX for PWM on LED1 set_mux_value(LED1_MUX, LED1_MUX_VAL); set_pwm(LED1_PWM, LED1_PWM_FREQ, read_ain(POT)/41); //Divide by 41 to put in the range [0, 99] //Setup button as GPIO IN export_gpio(BUTTON); set_gpio_direction(BUTTON, "in"); set_gpio_edge(BUTTON, "rising"); button_fd = gpio_fd_open(BUTTON); }
int main(int argc, char** argv){ //variable declarations struct pollfd fdset[1]; int nfds = 1; int timeout = 3000; int rc; char* buf[MAX_BUF]; int gpio1, gpio2; int gpio1_fd, gpio2_fd; int gpio2_value = 0; int pattern =0; int value =0; char *end; int res, i2cbus, address, size, file; int daddress; char filename[20]; //Setting the signal handler for Ctrl + C if (signal(SIGINT, signal_handler) == SIG_ERR) printf("\ncan't catch SIGINT\n"); //Argument checking for at least five if(argc < 6){ printf("Usage: %s <input-gpio> <output-gpio> <i2c-bus> <i2c-address> <register>\n", argv[0]); printf("polls input-gpio, and writes value to output-gpio\n"); fflush(stdout); return 1; } //Assigning gpio values gpio1 = atoi(argv[1]); gpio2 = atoi(argv[2]); //Input for Argument 1 export_gpio(gpio1); set_gpio_direction(gpio1, "in"); set_gpio_edge(gpio1, "falling"); gpio1_fd = gpio_fd_open(gpio1); //Output for Argument 2 export_gpio(gpio2); set_gpio_direction(gpio2, "out"); set_gpio_value(gpio2, gpio2_value); gpio2_fd = gpio_fd_open(gpio2); //Assigning I2C values i2cbus = atoi(argv[3]); address = atoi(argv[4]); daddress = atoi(argv[5]); size = I2C_SMBUS_BYTE; sprintf(filename, "/dev/i2c-%d", i2cbus); file = open(filename, O_RDWR); if (file<0) { if (errno == ENOENT) { fprintf(stderr, "Error: Could not open file " "/dev/i2c-%d: %s\n", i2cbus, strerror(ENOENT)); } else { fprintf(stderr, "Error: Could not open file " "`%s': %s\n", filename, strerror(errno)); if (errno == EACCES) fprintf(stderr, "Run as root?\n"); } exit(1); } if (ioctl(file, I2C_SLAVE, address) < 0) { fprintf(stderr, "Error: Could not set address to 0x%02x: %s\n", address, strerror(errno)); return -errno; } while(loop){ memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = gpio1_fd; fdset[0].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0){ printf("\npoll() failed!\n"); } if (rc == 0){ printf("."); } if((fdset[0].revents & POLLPRI) == POLLPRI) { read(fdset[0].fd, buf, MAX_BUF); printf("interrupt value=%c\n", buf[0]); pattern++; if(pattern == 4){ pattern = 0; } } printf("Case %d\n",pattern); switch(pattern){ // Blink led case 0: if(gpio2_value){ gpio2_value = 0; } else{ gpio2_value = 1; } set_gpio_value(gpio2, gpio2_value); break; // PWM output for blinking LED by frequency and duty cycle case 1: set_mux_value("gpmc_a2",6); set_pwm("ehrpwm.1:0",10,25); break; // Analog In for reading Voltage from POT case 2: unset_pwm("ehrpwm.1:0"); value = read_ain("ain6"); printf("Voltage: %d\n",value); break; // I2C Device reading temperature from the sensor case 3: printf("Case 3\n"); res = i2c_smbus_write_byte(file, daddress); if (res < 0) { fprintf(stderr, "Warning - write failed, filename=%s, daddress=%d\n", filename, daddress); } res = i2c_smbus_read_byte_data(file, daddress); if (res < 0) { fprintf(stderr, "Error: Read failed, res=%d\n", res); exit(2); } printf("Temperature: %d°C\n", res); break; default: break; } } close(file); gpio_fd_close(gpio1_fd); gpio_fd_close(gpio2_fd); unexport_gpio(gpio1); unexport_gpio(gpio2); fflush(stdout); return 0; }
int main(int argc, char** argv){ //variable declarations struct pollfd fdset[1]; int nfds = 1; int timeout = 100; int rc; char* buf[MAX_BUF]; int gpio1, gpio2, gpio_count1, gpio_count2; int gpio1_fd, gpio2_fd; int gpio2_value = 0; int program = 0; int ain = 0; float duty_cycle = 0; int i2c_read; //check that at least two arguments are passed in if(argc < 3){ printf("Usage: %s <input-gpio> <output-gpio>\n", argv[0]); printf("polls input-gpio, and writes value to output-gpio\n"); fflush(stdout); return 1; } //set signal handler if (signal(SIGINT, signal_handler) == SIG_ERR) printf("\ncan't catch SIGINT\n"); //assign gpio values gpio1 = atoi(argv[1]); gpio2 = atoi(argv[2]); //argument 1 will be input export_gpio(gpio1); set_gpio_direction(gpio1, "in"); set_gpio_edge(gpio1, "falling"); gpio1_fd = gpio_fd_open(gpio1); //argument 2 will be output export_gpio(gpio2); set_gpio_direction(gpio2, "out"); set_gpio_value(gpio2, gpio2_value); gpio2_fd = gpio_fd_open(gpio2); //prepare gpio_count1 and gpio_count2 gpio_count1 = 30; gpio_count2 = 31; export_gpio(gpio_count1); export_gpio(gpio_count2); set_mux_value("gpmc_wait0",7); set_gpio_direction(gpio_count1, "out"); set_gpio_direction(gpio_count2, "out"); //prepare i2c bus int file; while(keepgoing){ memset((void*)fdset, 0, sizeof(fdset)); fdset[0].fd = gpio1_fd; fdset[0].events = POLLPRI; //fdset[1].fd = gpio2_fd; //fdset[1].events = POLLPRI; rc = poll(fdset, nfds, timeout); if (rc < 0){ printf("\npoll() failed!\n"); } if((fdset[0].revents & POLLPRI) == POLLPRI) { read(fdset[0].fd, buf, MAX_BUF); usleep(100); gpio2_value = ~(gpio2_value)&1; set_gpio_value(gpio2, gpio2_value); program++; if(program > 3) program = 0; set_gpio_value(gpio_count1, program & 1); set_gpio_value(gpio_count2, program & 2); } switch(program){ case 0: duty_cycle = read_ain("ain6"); duty_cycle = duty_cycle/4095 * 100; set_mux_value("gpmc_a2", 6); set_pwm("ehrpwm.1:0", 1000, (int) duty_cycle); break; case 1: ain = read_ain("ain6"); printf("ain5 = %d\n", ain); unset_pwm("ehrpwm.1:0"); break; case 2: unset_pwm("ehrpwm.1:0"); break; case 3: if((file = open("/dev/i2c-3", O_RDWR)) < 0){ printf("failed to open i2c-3 bus\n"); return 1; } if (ioctl(file, I2C_SLAVE, 72) < 0){ printf("Could not set address for i2c\n"); return 1; } i2c_read = i2c_smbus_read_byte_data(file, 0); close(file); printf("0x%02x (%d)\n", i2c_read, i2c_read); break; default: break; } } gpio_fd_close(gpio1_fd); gpio_fd_close(gpio2_fd); unexport_gpio(gpio1); unexport_gpio(gpio2); unexport_gpio(gpio_count1); unexport_gpio(gpio_count2); unset_pwm("ehrpwm.1:0"); set_mux_value("gpmc_a2", 7); fflush(stdout); return 0; }