int main() { int value; if(map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } // Define pin 17 as output INP_GPIO(2); OUT_GPIO(17); while(1) { // Toggle pin 17 (blink a led!) GPIO_SET = 1 << 17; sleep(1); value = GPIO_READ(2); while(!value) { // delay(20); value = GPIO_READ(2); } GPIO_CLR = 1 << 17; sleep(1); } return 0; }
struct tegra_gpio_pin * tegra_gpio_acquire(const char *pinname, u_int flags) { struct tegra_gpio_bank bank; struct tegra_gpio_pin *gpin; int pin; device_t dev; dev = device_find_by_driver_unit("tegragpio", 0); if (dev == NULL) return NULL; bank.bank_sc = device_private(dev); bank.bank_pb = tegra_gpio_pin_lookup(pinname, &pin); if (bank.bank_pb == NULL) return NULL; const uint32_t cnf = GPIO_READ(&bank, GPIO_CNF_REG); if ((cnf & __BIT(pin)) == 0) GPIO_WRITE(&bank, GPIO_CNF_REG, cnf | __BIT(pin)); gpin = kmem_alloc(sizeof(*gpin), KM_SLEEP); gpin->pin_bank = bank; gpin->pin_no = pin; gpin->pin_flags = flags; tegra_gpio_pin_ctl(&gpin->pin_bank, gpin->pin_no, gpin->pin_flags); return gpin; }
unsigned char ReadByte(void) { unsigned char Mask=1; int loop; unsigned char data=0; int loop2; for(loop=0;loop<8;loop++) { // set output INP_GPIO(DS_PIN); OUT_GPIO(DS_PIN); // PIN LOW GPIO_CLR= 1<<DS_PIN; DELAY1US // set input INP_GPIO(DS_PIN); // Wait 2 us DelayMicrosecondsNoSleep(2); if(GPIO_READ(DS_PIN)!=0) data |= Mask; Mask*=2; DelayMicrosecondsNoSleep(60); } return data; }
int gpio_read(int g) { #ifndef GPIO_SIMULATED return GPIO_READ(g); #else return 0; /* always low in simulation */ #endif }
static void uda1341_output_high(struct uda1341_softc *sc) { int cr; GPIO_WRITE(sc, SAGPIO_PSR, (L3_DATA | L3_MODE | L3_CLK)); cr = GPIO_READ(sc, SAGPIO_PDR) | (L3_DATA | L3_MODE | L3_CLK); GPIO_WRITE(sc, SAGPIO_PDR, cr); }
static void uda1341_output_low(struct uda1341_softc *sc) { int cr; cr = GPIO_READ(sc, SAGPIO_PDR); cr &= ~(L3_DATA | L3_MODE | L3_CLK); GPIO_WRITE(sc, SAGPIO_PDR, cr); }
static int tegra_gpio_pin_read(void *priv, int pin) { struct tegra_gpio_bank *bank = priv; const uint32_t v = GPIO_READ(bank, GPIO_IN_REG); return (v >> pin) & 1; }
int tegra_gpio_read(struct tegra_gpio_pin *gpin) { if (gpin->pin_flags & GPIO_PIN_INPUT) { return tegra_gpio_pin_read(&gpin->pin_bank, gpin->pin_no); } else { const uint32_t v = GPIO_READ(&gpin->pin_bank, GPIO_OUT_REG); return (v >> gpin->pin_no) & 1; } }
static void uda1341_init(struct uda1341_softc *sc) { int cr; /* GPIO initialize */ cr = GPIO_READ(sc, SAGPIO_AFR); cr &= ~(GPIO_ALT_SSP_TXD | GPIO_ALT_SSP_RXD | GPIO_ALT_SSP_SCLK | GPIO_ALT_SSP_SFRM); cr |= GPIO_ALT_SSP_CLK; GPIO_WRITE(sc, SAGPIO_AFR, cr); cr = GPIO_READ(sc, SAGPIO_PDR); cr &= ~GPIO_ALT_SSP_CLK; GPIO_WRITE(sc, SAGPIO_PDR, cr); /* SSP initialize & enable */ SSP_WRITE(sc, SASSP_CR1, CR1_ECS); cr = 0xF | (CR0_FRF_MASK & (1<<4)) | (CR0_SCR_MASK & (3<<8)) | CR0_SSE; SSP_WRITE(sc, SASSP_CR0, cr); /* Enable the audio power */ sc->sc_parent->ipaq_egpio |= (EGPIO_H3600_AUD_PWRON | EGPIO_H3600_AUD_ON); sc->sc_parent->ipaq_egpio &= ~(EGPIO_H3600_CODEC_RESET | EGPIO_H3600_QMUTE); EGPIO_WRITE(sc); /* external clock configured for 44100 samples/sec */ cr = GPIO_READ(sc, SAGPIO_PDR); cr |= (GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1); GPIO_WRITE(sc, SAGPIO_PDR, cr); GPIO_WRITE(sc, SAGPIO_PSR, GPIO_H3600_CLK_SET0); GPIO_WRITE(sc, SAGPIO_PCR, GPIO_H3600_CLK_SET1); /* wait for power on */ delay(100*1000); sc->sc_parent->ipaq_egpio |= EGPIO_H3600_CODEC_RESET; EGPIO_WRITE(sc); /* Wait for the UDA1341 to wake up */ delay(100*1000); }
LOCAL void ICACHE_FLASH_ATTR buttonTask(void *pvParameters) { static uint32_t messageId = 0; uint8_t loopIndex; uint8_t currentValue; uint8_t remoteRequest[50] = {0}; uint32_t remoteRequestLength; DBGINF(("Button task initiated")); vTaskDelay(100); DBGINF(("Button task running")); for (loopIndex = 0; loopIndex < CTNC_MAX_NUMBER_OF_RESOURCE; loopIndex++) { if (CTNC_RES_TYPE_BINARY_INPUT == fResource[loopIndex].type) { PIN_FUNC_SELECT(fResource[loopIndex].gpioName, fResource[loopIndex].gpioFunc); PIN_PULLUP_EN(fResource[loopIndex].gpioName); //GPIO_DIS_OUTPUT(GPIO_ID_PIN(fResource[loopIndex].gpioNumber)); gpio_output_set(0, BIT(GPIO_ID_PIN(fResource[loopIndex].gpioNumber)), 0,0); } } DBGINF(("All resource output set to current values.")); while (1) { for (loopIndex = 0; loopIndex < CTNC_MAX_NUMBER_OF_RESOURCE; loopIndex++) { fResource[loopIndex].lacheValue++; currentValue = GPIO_READ(fResource[loopIndex].gpioNumber); if ( (currentValue != fResource[loopIndex].value) || (fResource[loopIndex].lacheValue >= 5000) ) { fResource[loopIndex].value = currentValue; fResource[loopIndex].lacheValue = 0; messageId++; remoteRequestLength = snprintf( &remoteRequest[0], 50, "/%d/%s/%s/uresval/%d/%d", messageId, MESH_getNodeID(), MESH_getNodeID(), fResource[loopIndex].id, fResource[loopIndex].value); for (loopIndex = 0; loopIndex < 4; loopIndex++) { sendUDPData(remoteRequest, remoteRequestLength); vTaskDelay(50); } break; } } vTaskDelay(CTNC_BTN_TASK_SLEEP); } DBGERR(("QUITING BUTTON TASK, This is never expected")); vTaskDelete(NULL); }
/** * checkGpioThread_fn() - Function is runned as lon as thread isn't stopped * * Function print to kernel_info the value on the input pin of the RPI2 */ int checkGpioThread_fn(void *data) { while (!kthread_should_stop()) { msleep(1000); printk(KERN_INFO "%i\n",GPIO_READ(GPIO_KEY)); //TODO: Write values to User-space } return 0; }
unsigned char ReadBit(void) { unsigned char rvalue=0; INP_GPIO(DS_PIN); OUT_GPIO(DS_PIN); // PIN LOW GPIO_CLR= 1 << DS_PIN; DELAY1US // set INPUT INP_GPIO(DS_PIN); DelayMicrosecondsNoSleep(2); if(GPIO_READ(DS_PIN)!=0) rvalue=1; DelayMicrosecondsNoSleep(60); return rvalue; }
static int L3_getbit(struct uda1341_softc *sc) { int cr, data; GPIO_WRITE(sc, SAGPIO_PCR, L3_CLK); /* Clock down */ delay(L3_CLK_LOW); cr = GPIO_READ(sc, SAGPIO_PLR); data = (cr & L3_DATA) ? 1 : 0; GPIO_WRITE(sc, SAGPIO_PSR, L3_CLK); /* Clock up */ delay(L3_CLK_HIGH); return (data); }
int main(void) { if (map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } INP_GPIO(TRIGGER); OUT_GPIO(TRIGGER); INP_GPIO(ECHO); struct timespec pulse = {0, 10000}, // 0.10 ms wait = {0, 5000}, // 0.05 ms loop = {0, 25000000}; // 25.00 ms GPIO_CLR = 1 << TRIGGER; printf("sleeping\n"); nanosleep(&loop, NULL); while(1) { // Set the trigger and wait 0.1 ms GPIO_SET = 1 << TRIGGER; nanosleep(&pulse, NULL); GPIO_CLR = 1 << TRIGGER; // Record the current time, wait for an echo struct timespec tstart={0,0}, tend={0,0}; clock_gettime(CLOCK_MONOTONIC, &tstart); while (!GPIO_READ(ECHO)) nanosleep(&wait, NULL); // Get the end time and calculate the difference clock_gettime(CLOCK_MONOTONIC, &tend); int usecs = (tend.tv_sec - tstart.tv_sec) * 1000000 + (tend.tv_nsec - tstart.tv_nsec) / 1000; // Convert to meters double r = (340.29 * (double) usecs) / 1000000; printf("%f meters\n", r); nanosleep(&loop, NULL); } }
static int ar71xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val) { struct ar71xx_gpio_softc *sc = device_get_softc(dev); int i; for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); *val = (GPIO_READ(sc, AR71XX_GPIO_IN) & (1 << pin)) ? 1 : 0; return (0); }
/** * device-read() - Called when a process, which already opened the * device file, attempts to read from it. */ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ char *buffer, /* buffer to fill with data */ size_t length, /* length of the buffer */ loff_t * offset) { //Power on the leds shows if button is reacting on the signal if(GPIO_READ(GPIO_LED)) { printk(KERN_INFO "POWER = HIGH\n"); put_user(1, buffer); } else { printk(KERN_INFO "POWER = LOW\n"); put_user(0, buffer); } return 0; }
int main() { if (map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return -1; } // Define pin 7 as output INP_GPIO(4); int count = 0; while (1) { printf("about to read gpio 4..\n"); int in = GPIO_READ(4); printf("%i - in : %i\n", count++,in); sleep(1); } return 0; }
/** * device-read() - Called when a process, which already opened the * device file, attempts to read from it. */ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ char *buffer, /* buffer to fill with data */ size_t length, /* length of the buffer */ loff_t * offset) { if(GPIO_READ(GPIO_LED)) { printk(KERN_INFO "LED state is on\n"); put_user(1, buffer); } else { printk(KERN_INFO "LED state is off\n"); put_user(0, buffer); } return 0; }
static int L3_read(struct uda1341_softc *sc, uint8_t addr, uint8_t *data, int len) { int cr, mode; mode = 0; uda1341_output_high(sc); L3_sendbyte(sc, addr, mode++); cr = GPIO_READ(sc, SAGPIO_PDR); cr &= ~(L3_DATA); GPIO_WRITE(sc, SAGPIO_PDR, cr); while(len--) *data++ = L3_getbyte(sc, mode++); uda1341_output_low(sc); return len; }
/** * r_irq_handler() - IRQ handler fired on interrupt */ static irqreturn_t r_irq_handler(int irq, void *dev_id, struct pt_regs *regs) { unsigned long flags; // disable hard interrupts (remember them in flag 'flags') local_irq_save(flags); // Set output if (GPIO_READ(GPIO_PWM1)) { GPIO_SET(GPIO_OUTP); } else { GPIO_CLR(GPIO_OUTP); } // restore hard interrupts local_irq_restore(flags); return (IRQ_HANDLED); }
static int ar71xx_gpio_pin_toggle(device_t dev, uint32_t pin) { int res, i; struct ar71xx_gpio_softc *sc = device_get_softc(dev); for (i = 0; i < sc->gpio_npins; i++) { if (sc->gpio_pins[i].gp_pin == pin) break; } if (i >= sc->gpio_npins) return (EINVAL); res = (GPIO_READ(sc, AR71XX_GPIO_IN) & (1 << pin)) ? 1 : 0; if (res) GPIO_WRITE(sc, AR71XX_GPIO_CLEAR, (1 << pin)); else GPIO_WRITE(sc, AR71XX_GPIO_SET, (1 << pin)); return (0); }
static void tegra_gpio_attach_bank(struct tegra_gpio_softc *sc, u_int bankno) { struct tegra_gpio_bank *bank = &sc->sc_banks[bankno]; struct gpiobus_attach_args gba; u_int pin; bank->bank_sc = sc; bank->bank_pb = &tegra_gpio_pinbanks[bankno]; bank->bank_gc.gp_cookie = bank; bank->bank_gc.gp_pin_read = tegra_gpio_pin_read; bank->bank_gc.gp_pin_write = tegra_gpio_pin_write; bank->bank_gc.gp_pin_ctl = tegra_gpio_pin_ctl; const uint32_t cnf = GPIO_READ(bank, GPIO_CNF_REG); for (pin = 0; pin < __arraycount(bank->bank_pins); pin++) { bank->bank_pins[pin].pin_num = pin; /* skip pins in SFIO mode */ if ((cnf & __BIT(pin)) == 0) continue; bank->bank_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE; bank->bank_pins[pin].pin_state = tegra_gpio_pin_read(bank, pin); } memset(&gba, 0, sizeof(gba)); gba.gba_gc = &bank->bank_gc; gba.gba_pins = bank->bank_pins; gba.gba_npins = __arraycount(bank->bank_pins); bank->bank_dev = config_found_ia(sc->sc_dev, "gpiobus", &gba, tegra_gpio_cfprint); }
int DoReset(void) { int loop; INP_GPIO(DS_PIN); DelayMicrosecondsNoSleep(10); INP_GPIO(DS_PIN); OUT_GPIO(DS_PIN); // pin low for 480 us GPIO_CLR=1<<DS_PIN; usleep(480); INP_GPIO(DS_PIN); DelayMicrosecondsNoSleep(60); if(GPIO_READ(DS_PIN)==0) { DelayMicrosecondsNoSleep(420); return 1; } return 0; }
int read_jtag_tdo() { return ( GPIO_READ(JTAG_TDO) ) ? 1 : 0; }
void *blink(int *terminate) { int i, j, k, switchscan, tmp; // Find gpio address (different for Pi 2) ---------- gpio.addr_p = bcm_host_get_peripheral_address() + +0x200000; if (gpio.addr_p == 0x20200000) printf("RPi Plus detected\n"); else printf("RPi 2 detected\n"); // printf("Priority max SCHED_FIFO = %u\n",sched_get_priority_max(SCHED_FIFO) ); // set thread to real time priority ----------------- struct sched_param sp; sp.sched_priority = 98; // maybe 99, 32, 31? if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp)) { fprintf(stderr, "warning: failed to set RT priority\n"); } // -------------------------------------------------- if (map_peripheral(&gpio) == -1) { printf("Failed to map the physical GPIO registers into the virtual memory space.\n"); return (void *) -1; } // initialise GPIO (all pins used as inputs, with pull-ups enabled on cols) // INSERT CODE HERE TO SET GPIO 14 AND 15 TO I/O INSTEAD OF ALT 0. // AT THE MOMENT, USE "sudo ./gpio mode 14 in" and "sudo ./gpio mode 15 in". "sudo ./gpio readall" to verify. for (i = 0; i < 8; i++) { // Define ledrows as input INP_GPIO(ledrows[i]); GPIO_CLR = 1 << ledrows[i]; // so go to Low when switched to output } for (i = 0; i < 12; i++) // Define cols as input { INP_GPIO(cols[i]); } for (i = 0; i < 3; i++) // Define rows as input { INP_GPIO(rows[i]); } // BCM2835 ARM Peripherals PDF p 101 & elinux.org/RPi_Low-level_peripherals#Internal_Pull-Ups_.26_Pull-Downs GPIO_PULL = 2; // pull-up short_wait(); // must wait 150 cycles #ifdef SERIALSETUP GPIO_PULLCLK0 = 0x03ffc; // selects GPIO pins 2..13 (frees up serial port on 14 & 15) #else GPIO_PULLCLK0 = 0x0fff0; // selects GPIO pins 4..15 (assumes we avoid pins 2 and 3!) #endif short_wait(); GPIO_PULL = 0; // reset GPPUD register short_wait(); GPIO_PULLCLK0 = 0; // remove clock short_wait(); // probably unnecessary // BCM2835 ARM Peripherals PDF p 101 & elinux.org/RPi_Low-level_peripherals#Internal_Pull-Ups_.26_Pull-Downs GPIO_PULL = 0; // no pull-up no pull-down just float short_wait(); // must wait 150 cycles GPIO_PULLCLK0 = 0x0ff00000; // selects GPIO pins 20..27 short_wait(); GPIO_PULL = 0; // reset GPPUD register short_wait(); GPIO_PULLCLK0 = 0; // remove clock short_wait(); // probably unnecessary // BCM2835 ARM Peripherals PDF p 101 & elinux.org/RPi_Low-level_peripherals#Internal_Pull-Ups_.26_Pull-Downs GPIO_PULL = 0; // no pull-up no pull down just float // not the reason for flashes it seems: //GPIO_PULL = 2; // pull-up - letf in but does not the reason for flashes short_wait(); // must wait 150 cycles GPIO_PULLCLK0 = 0x070000; // selects GPIO pins 16..18 short_wait(); GPIO_PULL = 0; // reset GPPUD register short_wait(); GPIO_PULLCLK0 = 0; // remove clock short_wait(); // probably unnecessary // -------------------------------------------------- printf("\nFP on\n"); while (*terminate == 0) { unsigned phase; // if ((loopcount++ % 500) == 0) printf("1\n"); // visual heart beat // display all phases circular for (phase = 0; phase < GPIOPATTERN_LED_BRIGHTNESS_PHASES; phase++) { // each phase must be eact same duration, so include switch scanning here // the original gpio_ledstatus[8] runs trough all phases volatile uint32_t *gpio_ledstatus = gpiopattern_ledstatus_phases[gpiopattern_ledstatus_phases_readidx][phase]; // prepare for lighting LEDs by setting col pins to output for (i = 0; i < 12; i++) { INP_GPIO(cols[i]); // OUT_GPIO(cols[i]); // Define cols as output } // light up 8 rows of 12 LEDs each for (i = 0; i < 8; i++) { // Toggle columns for this ledrow (which LEDs should be on (CLR = on)) for (k = 0; k < 12; k++) { if ((gpio_ledstatus[i] & (1 << k)) == 0) GPIO_SET = 1 << cols[k]; else GPIO_CLR = 1 << cols[k]; } // Toggle this ledrow on INP_GPIO(ledrows[i]); GPIO_SET = 1 << ledrows[i]; // test for flash problem OUT_GPIO(ledrows[i]); /*test* / GPIO_SET = 1 << ledrows[i]; /**/ nanosleep((struct timespec[] ) { { 0, intervl}}, NULL); // Toggle ledrow off GPIO_CLR = 1 << ledrows[i]; // superstition INP_GPIO(ledrows[i]); usleep(10); // waste of cpu cycles but may help against udn2981 ghosting, not flashes though } //nanosleep ((struct timespec[]){{0, intervl}}, NULL); // test // prepare for reading switches for (i = 0; i < 12; i++) INP_GPIO(cols[i]); // flip columns to input. Need internal pull-ups enabled. // read three rows of switches for (i = 0; i < 3; i++) { INP_GPIO(rows[i]); // GPIO_CLR = 1 << rows[i]; // and output 0V to overrule built-in pull-up from column input pin OUT_GPIO(rows[i]); // turn on one switch row GPIO_CLR = 1 << rows[i]; // and output 0V to overrule built-in pull-up from column input pin nanosleep((struct timespec[] ) { { 0, intervl / 100}}, NULL); // probably unnecessary long wait, maybe put above this loop also switchscan = 0; for (j = 0; j < 12; j++) // 12 switches in each row { tmp = GPIO_READ(cols[j]); if (tmp != 0) switchscan += 1 << j; } INP_GPIO(rows[i]); // stop sinking current from this row of switches gpio_switchstatus[i] = switchscan; } }
bool QGpio::bitValue (int bit) { return GPIO_READ (bit); }
int read_gpio(int port) { return GPIO_READ(port) ? 1 : 0; }