PB:Enabling and disabling I/O

From Klaus' wiki
Jump to: navigation, search

Introduction

Over at Adafruit is a great introduction to the Device Tree, that is used on BeagleBones.

BeagleBone black originally came with a kernel 3.8 which had a Device Tree Overlay (DTO) and a capemanager. But in the newer kernels, at time of writing 4.9, the DTO has been completely reorganised and there is no need for at capemanager any more. Find your kernel version by typing in uname -r.

If you are running kernel 3.8.x please follow these guidelines Example One and Example two and the instructions given in Derek Molloys book Exploring BeagleBone Black.

For kernels newer or equal to 4.9 follow the guideline below.

Rober Nelson has written a short guide to enable a few of the most used IO's. The rest should be deductable if you combine the pin configuration seen below and the commands given.

PocketBeagle pinout.png

The IO is available through a virtual filesystem starting in /sys.

Be sure to execute the shown commands as root.

Setting up a GPIO

The config-pin utility and the figure above can be used to figure out which pin to use and to put it into the correct mode.

First find a suitable pin to use for GPIO. I choose P2 pin 1 from the figure.

Issuing this command:

] $ config-pin -l P2-01

gives this output:

default gpio gpio_pu gpio_pd gpio_input pwm

The output tells us the the pin can be set into GPIO mode. This can be done by issuing

] $ config-pin P2-01 out

query the pin setting by issuing

] $ config-pin -q P2-01

and get a response like this

P2_01 Mode: gpio Direction: out Value: 0

From this we can tell the the pin is in gpio mode as output.

Issuing an other option the config-pin we can get information about the pin

] $ config-pin -i P2-01
Pin name: P2_01
Function if no cape loaded: pwm
Function if cape loaded: default gpio gpio_pu gpio_pd gpio_input pwm
Function information: ehrpwm1a default gpio1_18 gpio1_18 gpio1_18 gpio1_18 ehrpwm1a
Kernel GPIO id: 50
PRU GPIO id: 82

In order to locate the virtual files that controls the gpio there are more possibilities. But from the above we the the number 50, which enables us to use:

] $ cd /sys/class/gpio/gpio50

because it is the gpio that is in question here.

In this directory there are some files:

] $ ls -la
total 0
drwxrwxr-x  3 root gpio    0 Jan  1  2000 .
drwxrwxr-x 16 root gpio    0 Jan  1  2000 ..
-rw-rw-r--  1 root gpio 4096 Jan  1  2000 active_low
lrwxrwxrwx  1 root gpio    0 Nov 11 11:24 device -> ../../../gpiochip1
-rw-rw-r--  1 root gpio 4096 Nov 11 12:58 direction
-rw-rw-r--  1 root gpio 4096 Jan  1  2000 edge
-rw-rw-r--  1 root gpio 4096 Jan  1  2000 label
drwxrwxr-x  2 root gpio    0 Jan  1  2000 power
lrwxrwxrwx  1 root gpio    0 Nov 11 11:24 subsystem -> ../../../../../../../class/gpio
-rw-rw-r--  1 root gpio 4096 Jan  1  2000 uevent
-rw-rw-r--  1 root gpio 4096 Nov 11 13:04 value

The file value is of interest as are the edge and a few others.

Example

] $ cat direction
out
] $ echo "1" > value

The direction file tells the direction of the gpio and by echoing a 1 or a 0 to value it is possible to set or unset the output.

Screenshot 20181111 130743.png

Figure shows the result of echo "2" > value

Setting up and Enabling PWM

Switch to user root (if you have given your root-login a password - otherwise use sudo's).

] $ su -

The utility config-pin can setup the configuration. Use config-pin --help to get an overview of the possibilities with the command.

The PB has three PWM chips on board. Chip0, Chip2 and Chip4 each carrying two PWM channels. From the above table/figure one can see that Chip0 controls two pins p1-36 (A channel) and p1-33 (B channel).

In order to send pwm output from PWM A on Chip0 through the pinmux to pin 1,36 issue these commands:

[ ✓ root@beaglebone [~] $  config-pin p1_36 pwm
[ ✓ root@beaglebone [~] $  config-pin -q p1_36 
P1_36 Mode: pwm

The output using the -q option show you the current configuration.

After the config-pin utility has been executed change to this directory

] $ cd /sys/class/pwm/pwmchip0

In order to enable Chip0 perform

] $ echo 0 > export

Now the chip0 is enabled which will reveal a pwm0 directory in current directory.

Change into the directory pwm-0:0 and list the files available.

[ ✗ root@beaglebone [pwm0] $  /sys/class/pwm/pwmchip0/pwm-0:0# ll
total 0
drwxrwxr-x 3 root pwm    0 Nov 11 15:10 .
drwxrwxr-x 4 root pwm    0 Nov 11 15:10 ..
-rw-rw-r-- 1 root pwm 4.0K Nov 11 15:10 capture
lrwxrwxrwx 1 root pwm    0 Nov 11 15:10 device -> ../../pwmchip0
-rw-rw-r-- 1 root pwm 4.0K Nov 11 15:10 duty_cycle
-rw-rw-r-- 1 root pwm 4.0K Nov 11 15:10 enable
-rw-rw-r-- 1 root pwm 4.0K Nov 11 15:10 period
-rw-rw-r-- 1 root pwm 4.0K Nov 11 15:10 polarity
drwxrwxr-x 2 root pwm    0 Nov 11 15:10 power
lrwxrwxrwx 1 root pwm    0 Nov 11 15:10 subsystem -> ../../../../../../../../class/pwm
-rw-rw-r-- 1 root pwm 4.0K Nov 11 15:10 uevent

provided you've made an alias called ll in root home directory in the file .bashrc - otherwise use ls -al

Write the desired period time for the pwm signal into period

Write the desired duty cycle into duty_cycle

Write a "1" into the enable file in order to enable the pwm. Use 0 (Zero) to disable the pwm again.

Example

An example:

[ ✓ root@beaglebone [pwm0] $  echo 1000000000 > period 
[ ✓ root@beaglebone [pwm0] $  echo 500000000 > duty_cycle 
[ ✓ root@beaglebone [pwm0] $  echo 1 > enable

The is the result:

PWM 1 second period 50 percent duty cycle.png

Of course the enabling can happen at any time desirable after you've set-up period and duty cycle, so you can set-up the polarity, period and duty cycle when ever convenient and enable when needed. Writing a 0 into enable disables the output again.

NOTICE: The two PWM outputs from one PWM chip shall run with the same period, but can run with different duty cycle. Below is P1,36 and P1,33 running the same period but different duty cycle. The period was set on pwm0 in this case and it cannot be changed in pwm1.

PWM 1 second period 20 percent duty cycle.png