Linux GPIO Enumeration

Discussion in 'UDOO QUAD' started by Drevose, Mar 12, 2019.

  1. Drevose

    Drevose New Member

    Joined:
    Mar 12, 2019
    Messages:
    4
    Likes Received:
    0
    I would like pins 20 and 21 to be used only for I2C. I used the device tree configuration tool to enable I2C on these pins and I2C seems to work just fine. However, I noticed that users can still go into /sys/class/gpio and control pins 20 and 21 (GPIO92 and GPIO85) as GPIOs.

    I modified imx6qdl-udoo-externalpins.dtsi so that EIM_D28 and EIM_D21 were no longer configured as GPIOs under hoggrp-2. I enabled i2c1 and made sure that EIM_D28 and EIM_D21 were configured for I2C in the other relevant device tree source files. Again, I2C seems to work just fine with these changes, but I can still control pins 20 and 21 as GPIOs under /sys/class/gpio.

    Is there a way for pins 20 and 21 to not be enumerated under /sys/class/gpio if they are configured to be used for I2C?
     
  2. fetcher

    fetcher Member

    Joined:
    Mar 9, 2014
    Messages:
    166
    Likes Received:
    20
    First, beyond the clutter factor, it doesn't hurt to have those extraneous GPIOs present, since with I2C enabled they're disconnected from the actual pin via pinmux switches inside the i.MX6 chip - writing to them will have no effect.

    The presence of specific GPIOs under /sys/class/gpio is controlled not by the device tree (which affects whether or not these are bound to actual pins), but from user space by writing their numbers, one by one to /sys/class/gpio/export to add, or /sys/class/gpio/unexport to remove. The specific two unwanted ones you mention could be removed with
    Code:
    for x in 85 92; do echo $x >/sys/class/gpio/unexport; done
    which can go in rc.local to run at startup time.

    Of course, it'd be cleaner to not create them in the first place. In Udoobuntu, if you're using that, there is a run-time script that automatically adds these from a master list stored in the filesystem, but I can't remember its name or location - will check later when I have a chance to boot that up on a test Udoo. I normally run Debian instead, and just set up the gpio's using my own 1-line script loop in rc.local that reads from a text file:
    Code:
    for g in `cat /rw/etc/udoo-gpio-list |sed 's/#.*//' |xargs`; do echo $g >/sys/class/gpio/export ; done
    With the contents of that udoo-gpio-list file as follows for one specific board (lines with leading # marks represent disabled GPIOs and are skipped):
    Code:
    0       #sam3x-reset
    117     #sam3x-erase
    15      # SD2_DAT0 / TOUCH_RST -> hard-drive relay
    36      # PWR_EN_5V regulator control (1 -> disable)
    #116    #[0]  sam3x RX0
    #112    #[1]  sam3x TX0
    #20     #[2]  sd1 clk
    #16     #[3]  sd1 dat0
    #17     #[4]  sd1 dat1
    #18     #[5]  sd1 cmd
    41      #[6]
    42      #[7]  rightmost in front before GND
    
    #21     #[8]  sd1 dat3
    #19     #[9]  sd1 dat2
    1       #[10]
    9       #[11]
    3       #[12]
    40      #[13]
    
    150     #[14]  moving R->L toward double hdr
    162     #[15]
    160     #[16]
    161     #[17]
    158     #[18]
    159     #[19]
    #92     #[20]  i2c0 SDA
    #85     #[21]  i2c0 SCL
    
    # 123   #[22]  # lirc - front of double hdr
    124     #[23]
    125     #[24]
    126     #[25]
    127     #[26]
    133     #[27]
    134     #[28]
    135     #[29]
    136     #[30]
    137     #[31]
    138     #[32]
    139     #[33]
    # 140   #[34]  # lirc
    141     #[35]
    142     #[36]
    143     #[37]
    54      #[38]
    205     #[39]
    32      #[40]
    35      #[41]
    34      #[42]
    33      #[43]
    #101    #[44]  spdif-out
    144     #[45]
    145     #[46]
    #89     #[47]  uart3 rx
    #105    #[48]  uart5 rx
    #104    #[49]  uart5 tx
    57      #[50]
    56      #[51]
    55      #[52]
    #88     #[53]  uart3 tx
    
    #8      #[69] /CANTX - sam3x wx
    #7      #[68] /CANRX - sam3x wx
    
    0       #sam3x-reset
    117     #sam3x-erase
    15      # SD2_DAT0 / TOUCH_RST -> hard-drive relay
    36      # PWR_EN_5V regulator control (1 -> disable)
    #116    #[0]  sam3x RX0
    #112    #[1]  sam3x TX0
    #20     #[2]  sd1 clk
    #16     #[3]  sd1 dat0
    #17     #[4]  sd1 dat1
    #18     #[5]  sd1 cmd
    41      #[6]
    42      #[7]  rightmost in front before GND
    
    #21     #[8]  sd1 dat3
    #19     #[9]  sd1 dat2
    1       #[10]
    9       #[11]
    3       #[12]
    40      #[13]
    
    150     #[14]  moving R->L toward double hdr
    162     #[15]
    160     #[16]
    161     #[17]
    158     #[18]
    159     #[19]
    #92     #[20]  i2c0 SDA
    #85     #[21]  i2c0 SCL
    
    # 123   #[22]  # lirc - front of double hdr
    124     #[23]
    125     #[24]
    126     #[25]
    127     #[26]
    133     #[27]
    134     #[28]
     
    Drevose likes this.
  3. fetcher

    fetcher Member

    Joined:
    Mar 9, 2014
    Messages:
    166
    Likes Received:
    20
    Just as a demonstration of how the stray gpio's have no effect on alternate pin functions... if they were still connected in parallel, this would short the I2C bus to ground and prevent any devices from being detected:
    Code:
    root@imp:~# echo 85 >/sys/class/gpio/export
    root@imp:~# echo 92 >/sys/class/gpio/export
    
    root@imp:~# echo out >/sys/class/gpio/gpio92/direction
    root@imp:~# echo out >/sys/class/gpio/gpio85/direction
    
    root@imp:~# echo 0 >/sys/class/gpio/gpio85/value
    root@imp:~# echo 0 >/sys/class/gpio/gpio92/value
    
    root@imp:~# i2cdetect -y 0
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- --
    20: -- -- -- 23 -- -- 26 -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- 41 -- -- 44 45 -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- 75 76 77
    
     
    Drevose likes this.
  4. Drevose

    Drevose New Member

    Joined:
    Mar 12, 2019
    Messages:
    4
    Likes Received:
    0
    Hello Fetcher,

    Thank you very much for the quick reply! After I posted, I wondered if writing to registers in the GPIO module would not cause the pin output to change since the pins were multiplexed to the I2C module. Your answer seems to confirm this. I am using Udoobuntu 2.2.0 and will search for that script and list.

    Thank you so much for your help!
     
  5. Drevose

    Drevose New Member

    Joined:
    Mar 12, 2019
    Messages:
    4
    Likes Received:
    0
    Ah, yes, there is a script call udoo-gpio-export in /usr/bin that gets called in an initialization script. I can see the list of GPIOs that are always listed in /sys/class/gpio. Thanks again!
     
  6. fetcher

    fetcher Member

    Joined:
    Mar 9, 2014
    Messages:
    166
    Likes Received:
    20
    Glad you found it!

    I just realized that in copying the custom list from my main Udoo, I double-pasted part of it - apologies if that confused anyone - and also used an earlier version that wasn't well-commented. Here's my current list, which shows some of the possible alternate, non-GPIO pin functions. I keep a few still enabled as GPIO inputs even when in use on the SAM3X side, or for IMX6 on-chip peripherals, as this allows monitoring the pin-state independently.
    Code:
    #special GPIOs not exposed on Arduino headers:
    #
    0       # sam3x-reset
    117     # sam3x-erase
    15      # SD2_DAT0 / TOUCH_RST on LVDS socket -> hard-drive relay
    36      # PWR_EN_5V regulator control (1 -> disable)
    
    #front-row Arduino-Uno footprint pins, working L-R from SAM3X UART
    #
    # 116   #[0]  sam3x RX0
    # 112   #[1]  sam3x TX0
    # 20    #[2]  sd1 clk
    # 16    #[3]  sd1 dat0
    # 17    #[4]  sd1 dat1
    # 18    #[5]  sd1 cmd
    41      #[6]  (FREE)
    42      #[7]  (FREE)
    
    # 21    #[8]  sd1 dat3
    # 19    #[9]  sd1 dat2
    1       #[10] (FREE)
    9       #[11] (FREE)
    3       #[12] OUT: gs108ev3 pwr/reset ctrl
    40      #[13] OUT: gs108ev3 EN_8051         (rightmost in front, before GND)
    
    # moving now R->L in front, toward double header, last 8 front pins:
    #
    150     #[14]  LED (blue): rootfs sd mounted R/W
    162     #[15]  LED (red):  LTE-1 tether up
    160     #[16]  (FREE)  sam3x TX2
    161     #[17]  (FREE)  sam3x RX2
    158     #[18]  (FREE)  sam3x TX1
    159     #[19]  (FREE)  sam3x RX1
    # 92    #[20]  i2c-0 SDA
    # 85    #[21]  i2c-0 SCL
    
    # large double-row header on left, staggered even/odd (evens closer to center):
    #
    # 123   #[22]   lirc IR rcv (front of double hdr near +5V)
    124     #[23]  OUT: /1OE output-enable to 2*74hct241 level-shifters
    125     #[24]  PWM LED: (remote, 2n2222 -> analog header) email/net-status
    126     #[25]  PWM LED: 5mm green, network-status
    127     #[26]  OUT: latching relay control, 24V -> Unifi AP
    133     #[27]  OUT: opto trigger -> 12V PSU failover latch
    134     #[28]  LED: 1W/i2c/serial data-gathering cronjob running
    135     #[29]  OUT: latching relay control, 12V -> VoIP ATA #1
    # 136   #[30]  IN: sdcard #1 cd-gpio (grabbed in DTS usdhc@02190000)
    137     #[31]   (SAM3x-Arduino WKUP3) - MCP23017 IRQ from keypad
    138     #[32]  OUT: latching relay control -> PoE port #2
    139     #[33]  CM17A X10 Firecracker transmitter, "DTR" signal+power
    # 140   #[34]   AUD4 I2S RXFS
    # 141   #[35]   AUD4 I2S RXC
    # 142   #[36]   AUD4 I2S TXC
    # 143   #[37]   AUD4 I2S TXD
    # 54    #[38]   lirc IR transmitter
    205     #[39]  LED: (red):  LTE-2 or LTE-3 tether up OR rc.local running
    32      #[40]  CM17A X10 Firecracker transmitter, "RTS" signal+power
    35      #[41]  OUT: latching relay control -> 12V to CM
    34      #[42]  (FREE)
    33      #[43]  (FREE)
    # 101   #[44]   SPDIF-out via hct241 gate-pair
    # 144   #[45]   AUD4 I2S TXFS
    # 145   #[46]   AUD4 I2S RXD
    # 89    #[47]   uart3 rx
    # 105   #[48]   uart5 rx
    # 104   #[49]   uart5 tx
    # 57    #[50]   ECSPI2 MISO
    # 56    #[51]   ECSPI2 MOSI
    # 55    #[52]   ECSPI2 SCLK
    # 88    #[53]   uart3 tx
    
    # last two shared pins at rear-left before SAM3X-dedicated analog-ins:
    #
    # 8     #[69] /CANTX - (SAM3X-Arduino WKUP0) rain gauge pulse-counter
    # 7     #[68] /CANRX - (SAM3X-Arduino)       anemometer serial data
     
    waltervl likes this.

Share This Page