Working with GPIO on Raspberry Pi with FreeBSD

Raspberry Pi is based on Broadcom BCM2835 chip.

We can find description of Broadcom BCM2835 Peripherals here.
There are 54 general-purpose I/O (GPIO) lines split into two banks.

Here is instruction how to work with GPIO on Raspberry Pi with FreeBSD.

We can find good description of Raspberry Pi low-level peripherals here.
The production Raspberry Pi board has a 26-pin 2.54 mm expansion header, marked as P1, arranged in a 2×13 strip.
They provide 8 GPIO pins plus access to I²C, SPI, UART, as well as +3.3V, +5V and GND supply lines.

raspberry leaf r2

During the FreeBSD boot we can see messages about GPIO:
It sees 54 pins.

We can use gpioctl(1) utility to work with GPIO.

After boot some of pins are in IN status and some of them are in OUT status:

Let’s try to play with GPIO.

I connected LED to pin 23 for OUT and photocell to pin 25 for IN.

Usually we need to connect photocell to analog input, not to digital input.
But in our case we will get the rounded value: 0 without light and 1 with light.

I will light LED and check photocell status.

First we need to put pin 23 to OUT state.
Then we can change it status to HIGH (1) or LOW (0) value:

When we set the status of pin 23 to HIGHT (1) our LED will light:

Let’s play with pin 23 and check status of pin 25:

We can see that when turn LED (pin 23) on (gpioctl 23 1) pin 25 returns HIGH (1 in last string).
And when we turn LED off (gpioctl 23 0) pin 25 returns LOW (0 in last string).

We also can play with GPIO from C Languages.
Next small programm just put pin in OUT status and set in value to HIGH or LOW value (I used source code of gpioctl):

#include <sys/cdefs.h>

#include <fcntl.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/gpio.h>

void usage ( void )  {
  fprintf ( stderr, "Usage: pin [0|1]\n" );

int main ( int argc, char **argv )  {

  int pin, val, fd;
  char ctlfile[] = _PATH_DEVGPIOC "0";
  struct gpio_pin gpiopin;
  struct gpio_req gpioreq;

  if ( argc != 3 )  usage();
  sscanf( argv[1], "%d", &pin );
  sscanf( argv[2], "%d", &val );

  if ( (val != 0) && (val != 1) )  {
    fprintf ( stderr, "Invalid pin value: %d\n", val );

  printf ( "pin .. %d, val .. %d\n", pin, val );

  if ( (fd = open(ctlfile, O_RDONLY)) < 0 )  {

  gpiopin.gp_pin = pin;
  gpiopin.gp_flags = GPIO_PIN_OUTPUT;
  if ( ioctl(fd, GPIOSETCONFIG, &gpiopin) < 0 )  {

  gpioreq.gp_pin = pin;
  gpioreq.gp_value = val;
  if ( ioctl(fd, GPIOSET, &gpioreq) < 0 )  {




6 thoughts on “Working with GPIO on Raspberry Pi with FreeBSD

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s