Difference between revisions of "PB:Enabling and disabling I/O"

From Klaus' wiki
Jump to: navigation, search
(Setting up and Enabling PWM)
(Setting up a GPIO)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
=Introduction=
 +
 
Over at [https://learn.adafruit.com/introduction-to-the-beaglebone-black-device-tree Adafruit] is a great introduction to the Device Tree, that is used on BeagleBones.
 
Over at [https://learn.adafruit.com/introduction-to-the-beaglebone-black-device-tree Adafruit] is a great introduction to the Device Tree, that is used on BeagleBones.
  
Line 13: Line 15:
 
The IO is available through a virtual filesystem starting in /sys.
 
The IO is available through a virtual filesystem starting in /sys.
  
==Setting up and Enabling PWM==
+
'''Be sure to execute the shown commands as root.'''
  
Switch to user '''root''' (if you have given your root-login a password - otherwise use sudo's).
+
=Setting up a GPIO=
<source lang=bash>
+
] $ su -
+
</source>
+
 
+
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 two PWM chips on board. Chip0 and Chip1 each carrying two PWM channels.
+
 
+
In order to send pwm output from PWM A on Chip0 through the pinmux to pin 1,36 issue these commands:
+
 
+
<source lang=bash>
+
[ ✓ root@beaglebone [~] $  config-pin p1_36 pwm
+
[ ✓ root@beaglebone [~] $  config-pin -q p1_36
+
P1_36 Mode: pwm
+
</source>
+
The output using the -q option show you the current configuration.
+
 
+
After the config-pin utility has been executed change to this directory
+
 
+
<source lang=bash>
+
] $ cd /sys/class/pwm/pwmchip0
+
</source>
+
 
+
In order to enable Chip0 perform
+
<source lang=bash>
+
] $ echo 0 > export
+
</source>
+
Now the chip0 is enabled which will reveal a '''pwm0''' directory in current directory.
+
 
+
Change into the directory '''pwm0''' and list the files available.
+
 
+
<source lang=bash>
+
[ ✗ root@beaglebone [pwm0] $  ll
+
total 0
+
drwxr-xr-x 3 root root    0 Mar  9 10:51 .
+
drwxrwxr-x 4 root pwm    0 Mar  9 10:50 ..
+
-r--r--r-- 1 root root 4.0K Mar  9 10:51 capture
+
-rw-r--r-- 1 root root 4.0K Mar  9 11:05 duty_cycle
+
-rw-r--r-- 1 root root 4.0K Mar  9 10:56 enable
+
-rw-r--r-- 1 root root 4.0K Mar  9 11:04 period
+
-rw-r--r-- 1 root root 4.0K Mar  9 10:51 polarity
+
drwxr-xr-x 2 root root    0 Mar  9 10:51 power
+
-rw-r--r-- 1 root root 4.0K Mar  9 10:51 uevent
+
</source>
+
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.
+
 
+
===Examples===
+
An example:
+
<source lang=bash>
+
[ ✓ root@beaglebone [pwm0] $  echo 1000000000 > period
+
[ ✓ root@beaglebone [pwm0] $  echo 500000000 > duty_cycle
+
[ ✓ root@beaglebone [pwm0] $  echo 1 > enable
+
</source>
+
The is the result:
+
 
+
[[File:PWM 1 second period 50 percent duty cycle.png|600 px]]
+
 
+
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.
+
 
+
[[File:PWM 1 second period 20 percent duty cycle.png|600px]]
+
 
+
==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.
 
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.
Line 93: Line 25:
 
Issuing this command:
 
Issuing this command:
 
<source lang=bash>
 
<source lang=bash>
] $ config-pin -l P2-01
+
] $ config-pin -l P2_01
 
</source>
 
</source>
 
gives this output:
 
gives this output:
Line 100: Line 32:
 
The output tells us the the pin can be set into GPIO mode. This can be done by issuing
 
The output tells us the the pin can be set into GPIO mode. This can be done by issuing
 
<source lang=bash>
 
<source lang=bash>
] $ config-pin P2-01 out
+
] $ config-pin P2_01 gpio
 
</source>
 
</source>
  
 
query the pin setting by issuing
 
query the pin setting by issuing
 
<source lang=bash>
 
<source lang=bash>
] $ config-pin -q P2-01
+
] $ config-pin -q P2_01
 
</source>
 
</source>
 
and get a response like this
 
and get a response like this
Line 113: Line 45:
 
Issuing an other option the config-pin we can get information about the pin
 
Issuing an other option the config-pin we can get information about the pin
 
<source lang=bash>
 
<source lang=bash>
] $ config-pin -i P2-01
+
] $ config-pin -i P2_01
 
</source>
 
</source>
  
Line 148: Line 80:
 
The file '''value''' is of interest as are the '''edge''' and a few others.
 
The file '''value''' is of interest as are the '''edge''' and a few others.
  
 +
==Example==
 
<source lang=bash>
 
<source lang=bash>
] $ cat direction
+
[ ✓ root@beaglebone [~] $ cat direction
 
out
 
out
] $ echo "1" > value
+
[ ✓ root@beaglebone [~] $ echo "1" > value
 
</source>
 
</source>
 
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.
 
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.
Line 158: Line 91:
  
 
''Figure shows the result of echo "2" > value''
 
''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).
 +
<source lang=bash>
 +
] $ su -
 +
</source>
 +
 +
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:
 +
 +
<source lang=bash>
 +
[ ✓ root@beaglebone [~] $  config-pin p1_36 pwm
 +
[ ✓ root@beaglebone [~] $  config-pin -q p1_36
 +
P1_36 Mode: pwm
 +
</source>
 +
The output using the -q option show you the current configuration.
 +
 +
After the config-pin utility has been executed change to this directory
 +
 +
<source lang=bash>
 +
] $ cd /sys/class/pwm/pwmchip0
 +
</source>
 +
 +
In order to enable Chip0 perform
 +
<source lang=bash>
 +
] $ echo 0 > export
 +
</source>
 +
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.
 +
 +
<source lang=bash>
 +
[ ✗ 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
 +
</source>
 +
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:
 +
<source lang=bash>
 +
[ ✓ root@beaglebone [pwm0] $  echo 1000000000 > period
 +
[ ✓ root@beaglebone [pwm0] $  echo 500000000 > duty_cycle
 +
[ ✓ root@beaglebone [pwm0] $  echo 1 > enable
 +
</source>
 +
The is the result:
 +
 +
[[File:PWM 1 second period 50 percent duty cycle.png|600 px]]
 +
 +
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.
 +
 +
[[File:PWM 1 second period 20 percent duty cycle.png|600px]]

Latest revision as of 08:36, 4 March 2021

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 gpio

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

[ ✓ root@beaglebone [~] $ cat direction
out
[ ✓ root@beaglebone [~] $ 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