GPIO

The phyCORE-AM65x SOM brings out a selection of GPIOs to the connectors. On the development kit you can access many of these signals through the PHYTEC Expansion Board (PCM-957). This guide walks through the basic steps of toggling and reading the state of these IO interfaces.

Connect to the GPIOs

GPIO1_50 and GPIO0_73 have been selected for this example, and are accessible at the expansion connector. The table below gives a brief description of the two selected signals:

Connecting GPIOs

GPIO

Expansion Board Pin #

Description

X_GPIO1_50

21C

Expansion_button (in device tree)

Digital input, internal pull down resistor

X_GPIO0_73

25C

Expansion_led (in device tree, needs updating)

Digital output, 1.8V when set HIGH

Set up Hardware Configuration

You can wire up a test circuit on the expansion board such as the following:

Expansion Board (PCM-957) Circuit Drawing

Set up Software Configuration

In order to be accessible in Linux, the GPIOs need to be configured in the Linux device tree. PHYTEC has provided a sample device tree overlay, in the boot directory of the root file system, which configures expansion connector interfaces used in this Peripheral Guide. To enable this device tree overlay:

  • The device tree overlay is set in U-Boot.

  • To enter U-Boot, hit any key within 3 seconds of powering on the hardware to halt autoboot.

  • Clear the “default_overlays” variable and modify the “extra_overlays” variable to point to the sample dtbo: “k3-am65xx-phytec-expansion-sample.dtbo”. The “default_overlays” variable needs to be cleared because it is set to k3-am65xx-phytec-lcd-018.dtbo which conflicts with the sample. (the code will need to be updated to use GPIO_73)

    Target (U-Boot)
    setenv default_overlays
    setenv extra_overlays k3-am65xx-phytec-expansion-sample.dtbo
    saveenv
    
  • To exit U-Boot and continue booting into Linux, type boot

    Target (U-Boot)
    boot
    

Warning

The Expansion Sample overlay k3-am65xx-phytec-expansion-sample.dtbo conflicts with the LCD-018 overlay due to the PWM being used for backlight in the LCD-018. Make sure to disconnect the display when using this overlay before powering on the development kit.

Access GPIOs in Linux

Button

  • Use the following command to poll the state of the button.

    Target (Linux)
    cat /sys/kernel/debug/gpio | grep 'expansion_button'
    
  • Try running the above command a few times while alternating between holding down and letting go of the button.

LED

  • Set the pin high and turn on the LED:

    Target (Linux)
    echo 1 > /sys/class/leds/expansion_led/brightness
    
  • Set the pin low and turn off the LED

    Target (Linux)
    echo 0 > /sys/class/leds/expansion_led/brightness
    

Advanced steps to impress your mom!

  • To create a script that automatically blinks the LED, open a text editor:

    Target (Linux)
    vi ~/blink.sh
    
  • Edit the contents of the new file to reflect the code below and save the file: .. tip:: The vi text editor begins in “Command Mode” and you must first hit the ‘i’ key in order to enter “Insert Mode”. Using the arrow keys to navigate, make the necessary changes and then hit ESC to go back to “Command mode”. Now enter “:wq” to write the file and quit.

    Pro Tip: Once in “Insert Mode”, copy and paste the file contents to save you some effort!

Target (Linux)
  #!/bin/sh

  for i in `seq 1 10`; do
      echo 1 > /sys/class/leds/expansion_led/brightness
      sleep 1
      echo 0 > /sys/class/leds/expansion_led/brightness
      sleep 1
  done
  • Make your file executable:

    Target (Linux)
    chmod +x ~/blink.sh
    
  • Now run the script:

    Target (Linux)
    ~/blink.sh
    

Revert to the Default Software Configuration

This guide requires disabling the LCD-018 and enabling a sample expansion overlay. In order to revert back to the default overlay settings, the following needs to be run in U-Boot:

  • To enter U-Boot, hit any key within 3 seconds of powering on the hardware to halt autoboot.

    Target (U-Boot)
    env default -f -a
    saveenv