int nrf52_gpio_direction_output(unsigned int offset, int value) { set_gpio_direction(offset, GPIO_DIR_OUT); nrf52_gpio_set(offset, value); return 0; }
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 gpio_direction_output(unsigned gpio, int value) { gpio_set_value(gpio, value); set_gpio_direction(gpio, 1); return 0; }
int ads_init_gpios(void) { //Init GPIOs if ( init_gpio(GPIO_DATA_READY) || set_gpio_direction(GPIO_DATA_READY, "in") ) { return -1; } #ifdef ADS1198 if ( init_gpio(GPIO_RESET) || set_gpio_direction(GPIO_RESET, "out") ) { return -1; } if ( init_gpio(GPIO_START) || set_gpio_direction(GPIO_START, "out") ) { return -1; } set_gpio_value(GPIO_START, 0); usleep(10000); #endif return 0; }
HT1632::HT1632(uint8_t bank, int8_t data, int8_t wr, int8_t cs, int8_t rd) { _data = data; _wr = wr; _cs = cs; _rd = rd; export_gpio(bank * 32 + _data); set_gpio_direction(bank * 32 + _data, "out"); export_gpio(bank * 32 + _wr); set_gpio_direction(bank * 32 + _wr, "out"); export_gpio(bank * 32 + _cs); set_gpio_direction(bank * 32 + _cs, "out"); export_gpio(bank * 32 + _rd); set_gpio_direction(bank * 32 + _rd, "out"); gpiobank = new GPIO_MMAP(bank); if (_cs > 0) { gpiobank->write(_cs, 1); } if (_wr > 0) { gpiobank->write(_wr, 1); } if (_data > 0) { gpiobank->write(_data, 1); } if (_rd > 0) { gpiobank->write(_rd, 1); } for (uint8_t i=0; i<48; i++) { ledmatrix[i] = 0; } ts.tv_sec = 0; ts.tv_nsec = DELAY; }
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); }
int main() { int core = 0; printf("GPIO%d interrupt test\n", core); gpio_init(core); // Set outputs to zero gpio_write(core, 0); // Bottom half out, rest in set_gpio_direction(core, 0x000000ff); gpio_set_up_pushbutton_int(core); // Now write 1 - this will come back on the pushbutton pin after a delay gpio_write(core, 1); int i=0, loop_limit = 30; while(1) { if (int_fired) { report(0x8000000d); break; } i++; if (i>=loop_limit) exit(1); } exit(0); }
bool init_fan_pins() { printf("starting...\n"); init_gpio(23); set_gpio_direction(23, "in"); init_gpio(15); set_gpio_direction(15, "in"); init_gpio(18); set_gpio_direction(18, "in"); set_gpio_direction(23, "out"); set_gpio(23, 1); set_gpio_direction(15, "out"); set_gpio(15, 1); set_gpio_direction(18, "out"); set_gpio(18, 1); init_gpio(25); set_gpio_direction(25, "out"); set_gpio(25, 0); return true; }
int gpio_direction_input(unsigned gpio) { set_gpio_direction(gpio, 0); return 0; }
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; }
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; }
int nrf52_gpio_direction_input(unsigned int offset) { set_gpio_direction(offset, GPIO_DIR_IN); return 0; }