Changes between Version 34 and Version 35 of gpio


Ignore:
Timestamp:
09/12/2023 10:08:28 PM (8 months ago)
Author:
Blake Stewart
Comment:

Blink LED / GPIO DIO control, Spelling errors, updated for Venice

Legend:

Unmodified
Added
Removed
Modified
  • gpio

    v34 v35  
    275275Note that {{{gpioset}}} (and all libgpiod apps) will revert the state of a GPIO line back to its original value when it exits. For this reason if you want the state to persist you need to instruct {{{gpioset}}} to wait for a signal and optionally detach and run in the background.
    276276
    277 Examples:
    278  - Venice; use gpiofind/gpioget/gpioset on the GPIO named 'pci_wdis#'
     277Examples (Venice):
     278 - Use gpiofind/gpioget/gpioset on the GPIO named 'pci_wdis#'
    279279{{{#!bash
    280280# gpiofind pci_wdis#
     
    284284# gpioset --mode=signal --background gpiochip 3 7=0
    285285}}}
    286   - Note that once gpioset exits its process the GPIO's it changed will revert back to their prior state (by design) therefor we run it above with {{{--mode=signal}}} telling it to continue to run into it receives a signal and (optinally) {{{--background}}} telling it to detach from the controlling terminal to give our shell back.
    287  - Newport
    288 {{{#!bash
    289 root@bionic-newport:~# apt install gpiod
    290 ...
    291 root@bionic-newport:~# gpiodetect
    292 gpiochip0 [gpio_thunderx] (48 lines)
    293 gpiochip1 [pca9555] (16 lines)
    294 root@bionic-newport:~# gpioinfo gpio_thunderx
    295 gpiochip0 - 48 lines:
    296         line   0:      unnamed       unused   input  active-high
    297         line   1:      unnamed       unused   input  active-high
    298         line   2:      unnamed       unused   input  active-high
    299         line   3:      unnamed       unused   input  active-high
    300         line   4:      unnamed  "interrupt"   input  active-high [used]
    301         line   5:      unnamed       unused   input  active-high
    302         line   6:      unnamed       unused   input  active-high
    303         line   7:      unnamed       unused   input  active-high
    304         line   8:      unnamed "mmc_supply_3v3" output active-high [used]
    305         line   9:      unnamed       unused   input  active-high
    306         line  10:      unnamed       unused  output  active-high
    307         line  11:      unnamed       unused   input  active-high
    308         line  12:      unnamed       unused   input  active-high
    309         line  13:      unnamed       unused   input  active-high
    310         line  14:      unnamed      "user2"  output  active-high [used]
    311         line  15:      unnamed       unused  output  active-high
    312         line  16:      unnamed       unused  output  active-high
    313         line  17:      unnamed       unused  output  active-high
    314         line  18:      unnamed       unused  output  active-high
    315         line  19:      unnamed       unused  output  active-high
    316         line  20:      unnamed       unused   input  active-high
    317         line  21:      unnamed       unused   input  active-high
    318         line  22:      unnamed       unused   input  active-high
    319         line  23:      unnamed       unused  output  active-high
    320         line  24:      unnamed       unused   input  active-high
    321         line  25:      unnamed       unused   input  active-high
    322         line  26:      unnamed       unused   input  active-high
    323         line  27:      unnamed       unused   input  active-high
    324         line  28:      unnamed       unused  output  active-high
    325         line  29:      unnamed       unused   input  active-high
    326         line  30:      unnamed   "pps-gpio"   input  active-high [used]
    327         line  31:      unnamed      "user1"  output  active-high [used]
    328         line  32:      unnamed       unused   input  active-high
    329         line  33:      unnamed       unused   input  active-high
    330         line  34:      unnamed       unused   input  active-high
    331         line  35:      unnamed       unused   input  active-high
    332         line  36:      unnamed       unused   input  active-high
    333         line  37:      unnamed       unused   input  active-high
    334         line  38:      unnamed       unused   input  active-high
    335         line  39:      unnamed       unused   input  active-high
    336         line  40:      unnamed       unused   input  active-high
    337         line  41:      unnamed       unused   input  active-high
    338         line  42:      unnamed       unused   input  active-high
    339         line  43:      unnamed       unused   input  active-high
    340         line  44:      unnamed       unused   input  active-high
    341         line  45:      unnamed       unused   input  active-high
    342         line  46:      unnamed       unused   input  active-high
    343         line  47:      unnamed       unused   input  active-high
    344 root@bionic-newport:~# gpioget gpio_thunderx 24 # read gpio24 (DIO0)
    345 1
    346 root@bionic-newport:~# gpioset gpio_thunderx 24=0 # drive low gpio24 (DIO0)
    347 }}}
     286 
     287 - Finding DIO1 and setting it to output 1
     288{{{#!bash
     289# gpioinfo | grep dio
     290        line   7:       "dio0"       unused   input  active-high
     291        line   9:       "dio1"       unused   input  active-high
     292# gpiofind "dio1"
     293        gpiochip0 9
     294# gpioset --mode signal gpiochip0 9=1
     295}}}
     296
     297- Note that once gpioset exits its process the GPIO's it changed will revert back to their prior state (by design) therefore we run it above with {{{--mode=signal}}} telling it to continue to run into it receives a signal and (optinally) {{{--background}}} telling it to detach from the controlling terminal to give our shell back. Another process cannot take control of the GPIO if it is being controlled already.
    348298
    349299References:
    350300 * https://elinux.org/images/9/9b/GPIO_for_Engineers_and_Makers.pdf
     301 * https://kernel.googlesource.com/pub/scm/libs/libgpiod/libgpiod/+/v0.2.x/README.md
     302
     303=== Controlling DIO pins with a script or C
     304Gateworks SBCs have many exposed GPIO pins through headers, but only a few of them are DIO pins which can be controlled by a userspace program and connected to outside electronics, e.g. a LED.
     305
     306 
     307Note: Most of the exposed GPIO pins have special purposes to communicate with peripherals. Most of the internal GPIOs (from gpioinfo) are not connected internally and can be ignored.
     308
     309Before connecting any outside electronics to a GPIO pin, refer to the hardware manual for your exact device to avoid any damages. 
     310
     311Examples (Venice):
     312- Blink a LED using a script
     313{{{#!bash
     314#!/bin/bash
     315
     316#Use gpiofind to locate chip & line of DIO0
     317DIO0=$(gpiofind dio0)
     318status=0
     319
     320while true; do
     321        gpioset --mode=time --sec=1 $DIO0=$status #gpioset holds the line for 1 second
     322        status=$((1-status)) #Status (LED) is inverted
     323        echo $DIO0 is set to $status
     324done
     325}}}
     326
     327- Blink a LED using C (Board specific)
     328{{{#!c
     329#include <gpiod.h>  //Requires gpiod to compile
     330#include <stdio.h>
     331#include <unistd.h>
     332
     333int main(int argc, char **argv) {
     334        const char* chip_name = "gpiochip0";
     335        const int gpio_line = 7; //DIO0 on GW7201; see gpiofind command
     336        struct gpiod_chip* chip = gpiod_chip_open_by_name(chip_name);
     337        struct gpiod_line* line = gpiod_chip_get_line(chip, gpio_line);
     338
     339        gpiod_line_request_output(line, "blink", 0);
     340        int status = 0;
     341
     342        while(true){
     343                gpiod_line_set_value(line, status);
     344                printf("%s %d set to %d\n", chip_name, gpio_line, status);
     345                sleep(1);
     346                status = 1-status;
     347        }
     348        return 0;
     349}
     350
     351}}}
     352
     353- Both programs make the desired pin output 3.3v and 0v for a second each.
     354
     355- Refer to https://kernel.googlesource.com/pub/scm/libs/libgpiod/libgpiod/+/v0.2.x/README.md for more info.
    351356
    352357[=#sysfsgpio]