EBC Exercise 11a Device Trees

The Device Tree (DT), and Device Tree Overlay are a way to describe hardware in a system. This introduction is heavily based on Adafruit's Introduction to the BeagleBone Black Device Tree using Derek Molloy's gpio example.

The purpose of this exercise is to learn how to set the pin mux and enable the pullup or pulldown resistors.

Pin Muxing
The am335x processor on the BeagleBone has more internal 'pins' than external pins. The solution is to have each external pin attached to a multiplexer that can be 'muxed' to one of eight internal pins. A nice chart that shows what can connect to where can be found by beagle$ git clone git://github.com/derekmolloy/boneDeviceTree.git beagle$ cd boneDeviceTree/docs beagle$ ls BeagleboneBlackP8HeaderTable.pdf BeagleboneBlackP9HeaderTable.pdf

These two pdf files show where each of the pins on the P8 and P9 headers can be muxed. The initial muxing is set at boot time using the Device Tree (DT).

Seeing What Pins are in Use
The HeaderTable pdfs give a nice overview of what the various pins can do, but they don't show what's currently in use. The BeagleBone has many gpio pins to choose from, but many are already in use and can't be changed. Here's how to see what's currently in use. First, do this once beagle$ export SLOTS=/sys/devices/bone_capemgr.*/slots beagle$ export PINS=/sys/kernel/debug/pinctrl/44e10800.pinmux/pins beagle$ export PINMUX=/sys/kernel/debug/pinctrl/44e10800.pinmux/pinmux-pins beagle$ export PINGROUPS=/sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups Now beagle$ cat $PINGROUPS registered pin groups: group: pinmux_userled_pins pin 21 (44e10854) pin 22 (44e10858) pin 23 (44e1085c) pin 24 (44e10860) group: pinmux_rstctl_pins pin 20 (44e10850) group: pinmux_i2c0_pins pin 98 (44e10988) pin 99 (44e1098c) ... What you see are the pins that are in use, listed by functional group. You can see what all the pins are doing with beagle$ cat $PINMUX Pinmux settings per pin Format: pin (name): mux_owner gpio_owner hog? pin 0 (44e10800): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 1 (44e10804): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 2 (44e10808): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 3 (44e1080c): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 4 (44e10810): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 5 (44e10814): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 6 (44e10818): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 7 (44e1081c): mmc.10 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins pin 8 (44e10820): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 9 (44e10824): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 10 (44e10828): (MUX UNCLAIMED) (GPIO UNCLAIMED) ... Notice the first 8 are using my the emmc. The next few are unused. You can see all the unused pins with beagle$ grep "(MUX UNCLAIMED) (GPIO UNCLAIMED)" $PINMUX pin 8 (44e10820): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 9 (44e10824): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 10 (44e10828): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 11 (44e1082c): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 12 (44e10830): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 13 (44e10834): (MUX UNCLAIMED) (GPIO UNCLAIMED) pin 14 (44e10838): (MUX UNCLAIMED) (GPIO UNCLAIMED) ... The problem is the pins are listed by an internal pin number and the hardware address (44e10820). The class repo has a couple of simple scripts that help with this. beagle$ cd exercises/gpio beagle$ ./freeGPIO P8_10 P8_11 P8_12 P8_13 P8_14 P8_15 P8_16 P8_17 P8_18 P8_19 P8_26 P8_7 P8_8 P8_9 P9_15 P9_16 P9_17 P9_18 P9_21 P9_22 P9_23 P9_24 P9_26 P9_27 P9_30 P9_41 P9_42 USR0 USR1 USR2 USR3 These are pins your are free to use. You can get information about a given pin using beagle$ ./findGPIO.js P9_16 { name: 'EHRPWM1B', gpio: 51, mux: 'gpmc_a3', eeprom: 35, pwm: { muxmode: 6, path: 'ehrpwm.1:1', name: 'EHRPWM1B' }, key: 'P9_16', muxRegOffset: '0x04c', options: [ 'gpmc_a3', 'gmii2_txd2', 'rgmii2_td2', 'mmc2_dat2', 'gpmc_a19', 'pr1_mii1_txd1', 'ehrpwm1B', 'gpio1_19' ] } P9_16 (gpio 51) mode: 7 (gpio1_19) 0x04c pulldown Receiver Active pin 19 (44e1084c): (MUX UNCLAIMED) (GPIO UNCLAIMED) This tells you how the mux and pullup/pulldown resistors are currently set, and how they could be set. You can get a quick summary for several pins using beagle$ ./findGPIO.js P9_15 P9_16 P9_17 P9_18 P9_16 (gpio 51) mode: 7 (gpio1_19) 0x04c pulldown Receiver Active P9_15 (gpio 48) mode: 0 (mii1_rxd3) 0x134 pulldown Receiver Active P9_17 (gpio 5) mode: 2 (i2c1_scl) 0x15c pulldown Receiver Active Slew Control Slow P9_18 (gpio 4) mode: 2 (i2c1_sda) 0x158 pulldown Receiver Active Slew Control Slow

Changing the Pin Mux
Let's set pins P9_15 P9_16 P9_17 P9_18 to all be mode 7 with a pullup resistor. To change the pin mux we'll get an overlay that works and edit it. beagle$ cd boneDeviceTree/overlay beagle$ cp DM-gpio-Test.dts ~/MAY-gpio-set.dts // Copy to a place of your choice // Use your initials instead of MAY beagle$ gedit MAY-gpio-SET.dts

To set P9_15 you need to look up its address offset. This is the hex value (0x134) reported by the findGPIO.js command. The following table shows what bits to set. Since we want mode 7, with pullup enabled and receiver active enabled use 0b11 0111 or 0x37. Here's the part of the file we'll edit /{   compatible = "ti,beaglebone", "ti,beaglebone-black"; part-number = "MAY-gpio-set"; version = "00A0"; fragment@0 { target = <&am33xx_pinmux>; __overlay__ { pinctrl_test: MAY-gpio-set { pinctrl-single,pins = < 0x134 0x37 // P9_15  48 INPUT  MODE7 pullup 0x04c 0x37 // P9_16  51 INPUT  MODE7 pullup 0x15c 0x37 // P9_17   5 INPUT  MODE7 pullup 0x158 0x37 // P9_18   4 INPUT  MODE7 pullup /* OUTPUT GPIO(mode7) 0x07 pulldown, 0x17 pullup, 0x?f no pullup/down */ /* INPUT  GPIO(mode7) 0x27 pulldown, 0x37 pullup, 0x?f no pullup/down */ >;      };      };    }; Notice the register/value pairs for each of the pins. P9_15 is at address 0x134 and gets a value 0x37. Use findGPIO.js and check to be sure the other pin addresses are correct.

Compile and Install
Compile the .dts file with beagle$ dtc -O dtb -o MAY-gpio-set-00A0.dtbo -b 0 -@ MAY-gpio-set.dts Compiling the overlay from .dts to .dtbo

Now you need to copy it to /lib/firmware so it can be found. beagle$ cp MAY-gpio-set-00A0.dtbo /lib/firmware

Finally make it active. Note, $SLOTS was defined earlier. beagle$ echo MAY-gpio-set > $SLOTS

Verify
Check to be sure it worked. First check for errors. If you picked a pin that's already in use you will get a message like this. beagle$ dmesg | tail [  83.795688] pinctrl-single 44e10800.pinmux: pin 44e10990 already requested by 48038000.mcasp; cannot claim for helper.14 [  83.807174] pinctrl-single 44e10800.pinmux: pin-100 (helper.14) status -22 [  83.814392] pinctrl-single 44e10800.pinmux: could not request pin 100 on device pinctrl-single [  83.823421] bone-pinmux-helper helper.14: Failed to select default state

Assuming all went well, check the slots. beagle$ cat $SLOTS 0: 54:PF--- 1: 55:PF--- 2: 56:PF--- 3: 57:PF--- 4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G 5: ff:P-O-L Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI 7: ff:P-O-L Override Board Name,00A0,Override Manuf,MAY-gpio-set

Now check with beagle$ cd exercises/gpio beagle$ .findGPIO.js P9_15 P9_16 P9_17 P9_18 P9_16 (gpio 51) mode: 7 (gpio1_19) 0x04c pullup Receiver Active P9_15 (gpio 48) mode: 7 (gpio2_18) 0x134 pullup Receiver Active P9_17 (gpio 5) mode: 7 (gpio0_5) 0x15c pullup Receiver Active P9_18 (gpio 4) mode: 7 (gpio0_4) 0x158 pullup Receiver Active

Looks like it worked, they are all mode 7 and are pullups.

Removing
It's appeared in slot 7. You can remove it with beagle$ echo -7 > $SLOTS Be careful though, the current versions appear unstable when removing this way.

Generally you need to reboot if you are going to change the pins again.