unsigned char get_keys_pressed(unsigned char* key) { (*key) = 0; if (gpio_read(HOME_BUTTON) == 0) (*key) |= N_KEY; if (gpio_read(POWER_BUTTON) == 1) (*key) |= POWER_KEY; if (gpio_read(ROW0) == 0) (*key) |= VOLUP_KEY; if (gpio_read(ROW1) == 0) (*key) |= VOLDOWN_KEY; return (*key); }
/** * uint32_t suli_pin_pulse_in(IO_T *, what_state, timeout) */ uint32_t suli_pin_pulse_in(IO_T *pio, int state, uint32_t timeout) { //TODO: more efficient implementation uint32_t t = us_ticker_read(); while (gpio_read(pio) != state) { if (timeout > 0 && (us_ticker_read() - t) > timeout) return 0; } uint32_t t1 = us_ticker_read(); while (gpio_read(pio) == state) { if (timeout > 0 && (us_ticker_read() - t) > timeout) return 0; } return us_ticker_read() - t1 /*- ??? some wasting code consumes some time */; }
bool adxl362_read_pin1_level(void) { bool value = 0; gpio_read(adxl362_s.int_pin_gpio, ADXL362_INT1_PIN, &value); return value; }
//int main_app(IN u16 argc, IN u8 *argv[]) void main(void) { gpio_t gpio_led; gpio_t gpio_btn; // Init LED control pin gpio_init(&gpio_led, GPIO_LED_PIN); gpio_dir(&gpio_led, PIN_OUTPUT); // Direction: Output gpio_mode(&gpio_led, PullNone); // No pull // Initial Push Button pin gpio_init(&gpio_btn, GPIO_PUSHBT_PIN); gpio_dir(&gpio_btn, PIN_INPUT); // Direction: Input gpio_mode(&gpio_btn, PullUp); // Pull-High while(1){ if (gpio_read(&gpio_btn)) { // turn off LED gpio_write(&gpio_led, 0); } else { // turn on LED gpio_write(&gpio_led, 1); } } }
int spi_byte(uint8_t txbyte) { uint8_t rxbyte = 0; uint8_t bitno; uint8_t bit ; //TODO: Implement CPHA1 for (bitno=0; bitno<8; bitno++) { /* Transmit MSB first */ bit = ((txbyte & 0x80) != 0x00); txbyte <<= 1; gpio_write(config.mosi, bit); delayus(config.tSettle); CLOCK_ACTIVE(); delayus(config.tHold); delayus(config.tFreq); /* Read MSB first */ bit = gpio_read(config.miso); rxbyte = (rxbyte<<1) | bit; CLOCK_IDLE(); delayus(config.tFreq); } return rxbyte; }
static bool cpld_xc2c_jtag_clock(const jtag_t* const jtag, const uint32_t tms, const uint32_t tdi) { // 8 ns TMS/TDI to TCK setup gpio_write(jtag->gpio->gpio_tdi, tdi); gpio_write(jtag->gpio->gpio_tms, tms); // 20 ns TCK high time __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); gpio_clear(jtag->gpio->gpio_tck); // 25 ns TCK falling edge to TDO valid // 20 ns TCK low time __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); gpio_set(jtag->gpio->gpio_tck); // 15 ns TCK to TMS/TDI hold time __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); return gpio_read(jtag->gpio->gpio_tdo); }
static int subcommand_gpio_write(int argc, const char **argv) { /* > gpio write $pin $value(0 or 1) */ uint8_t pin; bool value; if (argc < 3) { return -1; } pin = (uint8_t)atoi(argv[1]); if (pin >= MAX_PIN_NUMBER) { return -1; } value = !!atoi(argv[2]); gpio_write(pin, value); uart_put_line("gpio write: pin="); uart_put_char(ascii_num_to_char(pin/10)); uart_put_char(ascii_num_to_char(pin%10)); uart_put_line(" value="); uart_put_char(ascii_num_to_char(gpio_read(pin))); uart_put_char('\n'); return 0; }
void gpio_toggle(uint8_t port, uint8_t pin) { if (gpio_read(port, pin)) { gpio_clear(port, pin); } else { gpio_set(port, pin); } }
int main(int argc, char **argv) { int fd; int retval; void *map_base; fd = open("/dev/mem", O_RDONLY | O_SYNC); if (fd < 0) exit(255); map_base = mmap(0, MAP_SIZE, PROT_READ, MAP_SHARED, fd, GPIO_BASE); if(map_base == (void *) -1) exit(255); switch(gpio_read(map_base,GPIO)) { case 0: /* battery */ retval = 0; break; case 1: /* mains */ retval = 1; break; default: /* will never reach here unless something has gone terribly wrong */ retval = 255; } if(munmap(map_base, MAP_SIZE) == -1) exit(255) ; close(fd); return retval; }
void hall_effect(void) { const unsigned vout= GPIO_PIN4; gpio_set_function(vout, GPIO_FUNC_INPUT); gpio_set_pullup(vout); for(int i = 0; i < 10; i++) { while(gpio_read(vout) == 1) {} printf("magnet close!\n"); while(gpio_read(vout) == 0) {} printf("magnet out of range!\n"); } //reboot(); }
static void handle_outbound(struct aura_node *node, struct aura_object *o, struct aura_buffer *buf) { int ret = -EIO; if (OPCODE("export")) { int gpio = aura_buffer_get_u32(buf); slog(4, SLOG_DEBUG, "gpio: export %d", gpio); ret = gpio_export(gpio); } else if (OPCODE("write")) { int gpio = aura_buffer_get_u32(buf); int value = aura_buffer_get_u32(buf); slog(4, SLOG_DEBUG, "gpio: write gpio %d value %d", gpio, value); ret = gpio_write(gpio, value); } else if (OPCODE("in")) { int gpio = aura_buffer_get_u32(buf); ret = gpio_in(gpio); } else if (OPCODE("out")) { int gpio = aura_buffer_get_u32(buf); ret = gpio_out(gpio); } else if (OPCODE("read")) { int gpio = aura_buffer_get_u32(buf); ret = gpio_read(gpio, &gpio); aura_buffer_rewind(buf); aura_buffer_put_u32(buf, gpio); } slog(0, SLOG_DEBUG, "gpio ret = %d", ret); if (ret) { aura_call_fail(node, o); return; } aura_queue_buffer(&node->inbound_buffers, buf); }
void keyboard_int_handler(unsigned pc){ if (gpio_check_and_clear_event(CLK)){ int bit = gpio_read(DATA); //Check start, parity and stop bit if ( (cnt == 0 && bit == 1) || (cnt == 9 && (numOfOne & 1) && bit) || (cnt == 9 && (!(numOfOne & 1) && !bit)) || (cnt == 10 && bit == 0) ){ cir_push(0); currScancode = 0; cnt = 0; return; } if (cnt > 0 && cnt < 9){ if (bit) numOfOne ++; currScancode |= (bit << (cnt-1)); } PS2Code[cnt++] = bit; //Package received if (cnt == 11){ cir_push(currScancode); /*printf("%08x\n", currScancode); */ cnt = 0; currScancode = 0; } } else{ timer_int_handler(pc); } }
int test_gpio(void) { static k = 0, j = 0, flag = 0; // while (1) { for (k = 0; k < 3; k++) { if (gpio_read(key_port[k]) == 1) { gpio_set(led_port[k], GPIO_HIGH); } else { gpio_set(led_port[k], GPIO_LOW); } } j++; if (j % 10 == 0) { if (flag) gpio_set(191, GPIO_HIGH); else gpio_set(191, GPIO_LOW); flag = 1 - flag; } gpio_delay(10); } return 0; }
int lidstate() { int fd; int retval; void *map_base; fd = open("/dev/mem", O_RDONLY | O_SYNC); if (fd < 0) {printf("Please run as root"); exit(1);} map_base = mmap(0, MAP_SIZE, PROT_READ, MAP_SHARED, fd, GPIO_BASE); if(map_base == (void *) -1) exit(255); switch(gpio_read(map_base,98)) { case 0: /* lid is closed */ retval = LID_CLOSED; break; case 1: /* lid is open */ retval = LID_OPEN; break; default: retval = LID_UNKNOWN; } if(munmap(map_base, MAP_SIZE) == -1) exit(255) ; close(fd); return retval; }
int powerstate() { int fd; int retval; void *map_base; fd = open("/dev/mem", O_RDONLY | O_SYNC); if (fd < 0) {printf("Please run as root"); exit(1);} map_base = mmap(0, MAP_SIZE, PROT_READ, MAP_SHARED, fd, GPIO_BASE); if(map_base == (void *) -1) exit(255); switch(gpio_read(map_base,0)) { case 0: /* battery */ retval = PWR_BATTERY; break; case 1: /* mains */ retval = PWR_AC_CORD; break; default: retval = PWR_UNKNOWN; } if(munmap(map_base, MAP_SIZE) == -1) exit(255) ; close(fd); return retval; }
static void *btn_handler_function (void *arg) { uint32_t last = xtimer_now(); int i; flag_state = 0; while (1) { i = gpio_read(BTN_B1_PIN); if (i != 0) { if ( flag_state == 0 ) { LED6_ON; flag_state = 1; xtimer_usleep_until(&last, 1000000); } else { LED6_OFF; flag_state = 0; xtimer_usleep_until(&last, 1000000); } } xtimer_usleep_until(&last, 100000); } return NULL; }
/* * waits for the Commit value * The function expexts the commandid auf the corresponding command * to determiante, wether a long timeout is needed or not. (Take Photo) */ int mdc800_rs232_waitForCommit (char commandid) { char ch[1]; if (mdc800_io_device_handle == 0) { printCError ("(mdc800_rs232_waitForCommit) Camera is not open !\n"); return 0; } gpio_set_timeout (mdc800_io_device_handle, mdc800_io_getCommandTimeout (commandid) ); if (gpio_read (mdc800_io_device_handle,ch,1) != GPIO_OK) { printCError ("(mdc800_rs232_waitForCommit) Error receiving commit !\n"); return 0; } if (ch[0] != ANSWER_COMMIT ) { printCError ("(mdc800_rs232_waitForCommit) Byte \"%i\" was not the commit !\n",ch[0]); return 0; } return (1); }
void button_scan(void * args) { if(gpio_read(BUTTON)) { button_state2 = 1; } else { button_state2 = 0; } if((button_state1 == 1) && (button_state2 == 0)) {//fall if(button_count == 0) { button_count ++; run_after_delay(button_event_one_two, NULL, 500); } else { button_count ++; } } button_state1 = button_state2; run_after_delay(button_scan, NULL, 50); }
bitbang_pin_value_en spi_pin_f( bitbang_pin_type_en pin, bitbang_pin_value_en value, void *context) { spi_context_st *ctx = (spi_context_st *)context; gpio_st *gpio; bitbang_pin_value_en res; switch (pin) { case BITBANG_CLK_PIN: gpio = &ctx->sc_clk; break; case BITBANG_DATA_IN: gpio = &ctx->sc_miso; break; case BITBANG_DATA_OUT: gpio = &ctx->sc_mosi; break; } if (pin == BITBANG_DATA_IN) { res = gpio_read(gpio) ? BITBANG_PIN_HIGH : BITBANG_PIN_LOW; } else { res = value; gpio_write(gpio, ((res == BITBANG_PIN_HIGH) ? GPIO_VALUE_HIGH : GPIO_VALUE_LOW)); } return res; }
int launcher_shoot_next(void) { uint8_t i = 0; for (; i < 10; i++) { if (_loaded[i] != 0) { break; } } if (i == 10) { return -1; } _loaded[i] = 0; gpio_write(_chamber[i], 1); usleep(10000); gpio_write(_chamber[i], 0); if (i == 9) { gpio_write(_reload_led, 1); while (gpio_read(_feedback) == 1) { gpio_write(_disabled, 0); usleep(10); gpio_write(_disabled, 1); usleep(10); } } return 0; }
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds * to 3 minutes in length, but must be called at least a few dozen microseconds * before the start of the pulse. */ extern uint32_t pulseIn( uint32_t ulPin, uint32_t state, uint32_t timeout ) { // cache the port and bit of the pin in order to speed up the // pulse width measuring loop and achieve finer resolution. calling // digitalRead() instead yields much coarser resolution. gpio_t *pGpio_t; uint32_t start_ticks, cur_ticks; if ( ulPin < 0 || ulPin > 13 ) return 0; /* Handle */ if ( g_APinDescription[ulPin].ulPinType != PIO_GPIO ) { return 0; } pGpio_t = (gpio_t *)gpio_pin_struct[ulPin]; // wait for any previous pulse to end start_ticks = us_ticker_read(); while (gpio_read(pGpio_t) == state) { cur_ticks = us_ticker_read(); if ( cur_ticks - start_ticks > timeout ) return 0; } // wait for the pulse to start while (gpio_read(pGpio_t) != state) { cur_ticks = us_ticker_read(); if ( cur_ticks - start_ticks > timeout ) return 0; } // wait for the pulse to stop start_ticks = us_ticker_read(); while (gpio_read(pGpio_t) == state) { cur_ticks = us_ticker_read(); if ( cur_ticks - start_ticks > timeout ) return 0; } cur_ticks = us_ticker_read(); // convert the reading to microseconds. The loop has been determined // to be 52 clock cycles long and have about 16 clocks between the edge // and the start of the loop. There will be some error introduced by // the interrupt handlers. return cur_ticks - start_ticks; }
int main() { gpio_init_out(&led, LED1); while(1) { gpio_write(&led, !gpio_read(&led)); HAL_Delay(150); } }
int gpio_toggle(gpio_t dev) { if (gpio_read(dev)) { return gpio_clear(dev); } else { return gpio_set(dev); } }
void gpio_toggle(gpio_t dev) { if (gpio_read(dev)) { gpio_clear(dev); } else { gpio_set(dev); } }
/** * gpio toggle * * Toggles the specified pin * * @param pin Pin number to toggle */ void gpio_toggle(int pin) { if (gpio_read(pin)) { gpio_clear(pin); } else { gpio_set(pin); } }
int main() { int ret = 0; uint32_t n, wdata, mask; int fd = 0; /* Open the UIO device file */ fd = open("/dev/uio0", O_RDWR); if (fd < 1) { perror("failed opening /dev/uio0"); return -1; } /* mmap the UIO device */ regmap = (volatile unsigned *)mmap(NULL, GPIO_MAP_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (!regmap) { perror("failed mmap-ing /dev/uio0"); return -1; } /* Only 27 pins are looped. */ mask = (1 << 27) - 1; /* walking 1 */ for(n = 0; n < 27; n++) { wdata = 1 << n; gpio_write(wdata); gpio_wait(); if (gpio_read(n, wdata, mask) != 0) ret = 1; } /* walking 0 */ for(n = 0; n < 27; n++) { wdata = 1 << n; wdata = ~wdata; gpio_write(wdata); gpio_wait(); if (gpio_read(n, wdata, mask) != 0) ret = 1; } munmap((void*)regmap, GPIO_MAP_SIZE); return ret; }
void button_is_long_push(void * args) { if((!gpio_read(BUTTON)) && (button_count == 1)) {// button_long_push(); } button_count = 0; }
static int tegra_gpio_attach(device_t dev) { struct tegra_gpio_softc *sc; int i, rid; sc = device_get_softc(dev); mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); /* Allocate bus_space resources. */ rid = 0; sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); if (sc->mem_res == NULL) { device_printf(dev, "Cannot allocate memory resources\n"); tegra_gpio_detach(dev); return (ENXIO); } rid = 0; sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); if (sc->irq_res == NULL) { device_printf(dev, "Cannot allocate IRQ resources\n"); tegra_gpio_detach(dev); return (ENXIO); } sc->dev = dev; sc->gpio_npins = NGPIO; if ((bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC, tegra_gpio_intr, NULL, sc, &sc->gpio_ih))) { device_printf(dev, "WARNING: unable to register interrupt handler\n"); tegra_gpio_detach(dev); return (ENXIO); } for (i = 0; i < sc->gpio_npins; i++) { sc->gpio_pins[i].gp_pin = i; sc->gpio_pins[i].gp_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT; snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME, "gpio_%s.%d", tegra_gpio_port_names[ i / GPIO_PINS_IN_REG], i % GPIO_PINS_IN_REG); sc->gpio_pins[i].gp_flags = gpio_read(sc, GPIO_OE, &sc->gpio_pins[i]) != 0 ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT; } sc->sc_busdev = gpiobus_attach_bus(dev); if (sc->sc_busdev == NULL) { tegra_gpio_detach(dev); return (ENXIO); } return (bus_generic_attach(dev)); }
static int gpio_opb_pin_read(void *arg, int pin) { struct gpio_opb_softc * const sc = arg; const u_int p = (pin % GPIO_NPINS) + 1; uint32_t reg_ir = gpio_read(sc, GPIO_IR); return (reg_ir >> GPIO_PIN_SHIFT(p)) & 0x01; }
int digitalRead(int pin) { if (gpio_read(arduino_pinmap[pin])) { return HIGH; } else { return LOW; } }