GPIO

OpenBSD provides access to general purpose input output via the gpio devices and gpioctl.

dmesg

When the computer boots, look at the gpio devices found. Or check dmesg after boot.

pc2$ dmesg | grep gpio
gpio0 at sxipio0: 32 pins
gpio1 at sxipio0: 32 pins
gpio2 at sxipio0: 32 pins
gpio3 at sxipio0: 32 pins
gpio4 at sxipio0: 32 pins
gpio5 at sxipio0: 32 pins
gpio6 at sxipio0: 32 pins
gpio7 at sxipio1: 32 pins
gpioleds0 at mainbus0: "orangepi:green:pwr"
gpiokeys0 at mainbus0: "sw4"

By default only in /dev only gpio0, gpio1, and gpio2 are created. If your computer has more gpio available, you will need to manually create the device entries using mknod.

pc2$ ls -l /dev/gpio*
crw-------  1 root  wheel  88, 0 May  2  2024 /dev/gpio0
crw-------  1 root  wheel  88, 1 May  2  2024 /dev/gpio1
crw-------  1 root  wheel  88, 2 May  2  2024 /dev/gpio2

mknod

Looking in /dev/MAKEDEV you can see how the gpio devices are made.

pc2$ grep gpio MAKEDEV                                                         
#	gpio*	General Purpose Input/Output
gpio*)
	M gpio$U c 88 $U 600
	R efi ipmi0 dri gpio0 gpio1 gpio2 bktr0 vnd0 vnd1 vnd2 vnd3

To create /dev/gpio3 we use the command:

pc2$ doas mknod gpio3 c 88 3

Once all the matching devices have been created, change the permissions to 660 so they can be accessed without needing doas.

pc2$ doas chmod 660 /dev/gpio*
pc2$ ls -l /dev/gp*
crw-rw----  1 root  wheel  88, 0 May  2  2024 /dev/gpio0
crw-rw----  1 root  wheel  88, 1 May  2  2024 /dev/gpio1
crw-rw----  1 root  wheel  88, 2 May  2  2024 /dev/gpio2
crw-rw----  1 root  wheel  88, 3 Sep  5 08:53 /dev/gpio3
crw-rw----  1 root  wheel  88, 4 Sep  5 08:54 /dev/gpio4
crw-rw----  1 root  wheel  88, 5 Sep  5 08:54 /dev/gpio5
crw-rw----  1 root  wheel  88, 6 Sep  5 08:54 /dev/gpio6
crw-rw----  1 root  wheel  88, 7 Sep  5 08:54 /dev/gpio7

Notice the gpio major number is always 88, and the minor number is the device number. Now the devices are all there, we can check what each device has available.

pc2$ gpioctl gpio0
/dev/gpio0: 20 pins
pc2$ gpioctl gpio1 
/dev/gpio1: 0 pins
pc2$ gpioctl gpio2 
/dev/gpio2: 17 pins
pc2$ gpioctl gpio3 
/dev/gpio3: 3 pins
pc2$ gpioctl gpio4 
/dev/gpio4: 16 pins
pc2$ gpioctl gpio5 
/dev/gpio5: 1 pins
pc2$ gpioctl gpio6 
/dev/gpio6: 14 pins
pc2$ gpioctl gpio7 
/dev/gpio7: 10 pins

That is showing the number of pins that are useable with each gpio device. Now we need to figure out which of the 32 pins they are.

To figure that out I edit /etc/rc.securelevel to make all the pins outputs, and reboot.

gpioctl gpio0 0 set out
gpioctl gpio0 1 set out
gpioctl gpio0 2 set out
gpioctl gpio0 3 set out
gpioctl gpio0 4 set out
gpioctl gpio0 5 set out
gpioctl gpio0 6 set out
gpioctl gpio0 7 set out
gpioctl gpio0 8 set out
gpioctl gpio0 9 set out
gpioctl gpio0 10 set out
gpioctl gpio0 11 set out
gpioctl gpio0 12 set out
gpioctl gpio0 13 set out
gpioctl gpio0 14 set out
gpioctl gpio0 15 set out
gpioctl gpio0 16 set out
gpioctl gpio0 17 set out
gpioctl gpio0 18 set out
gpioctl gpio0 19 set out
gpioctl gpio0 20 set out
gpioctl gpio0 21 set out
gpioctl gpio0 22 set out
gpioctl gpio0 23 set out
gpioctl gpio0 24 set out
gpioctl gpio0 25 set out
gpioctl gpio0 26 set out
gpioctl gpio0 27 set out
gpioctl gpio0 28 set out
gpioctl gpio0 29 set out
gpioctl gpio0 30 set out
gpioctl gpio0 31 set out

The pins that set are good, the ones that return errors are not. Delete the gpio pins that return errors. Repeat the exercise for each of the gpio devices. Some gpio pins may cause the boot to hang, which is a problem. To recover from this remove the SD card, mount it in another computer, and edit the rc.securelevel file to not set the gpio pin that caused the failure. Replace the card and reboot. On the Orange Pi PC2 gpio7 pins 8 and 9 caused a lockup.

Now all the pins are set to output they can be tested with a LED and a 220ohm resistor. On the PC2 gpio0 pin 20 is a red LED next to the power LED on the board.

pc2$ gpioctl gpio0 20 on
pin 20: state 0 -> 1
pc2$ gpioctl gpio0 20 off
pin 20: state 1 -> 0

To find the 40 pin connector mapping I connected a LED to a potential pin and cycled through all the available gpio device pins until the LED lit up. After I had done this I noticed that the pin descriptors in the OrangePi documentation refer to the pins as 'PA12' and 'PD11', etc. These where gpio0 pin 12 and gpio3 pin 11.