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:
4
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:
34

Let’s try to play with GPIO.

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

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.
SDIM6531

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

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

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

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>
__FBSDID("$FreeBSD$");

#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" );
  exit(1);
}

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 );
    exit(1);
  }

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

  if ( (fd = open(ctlfile, O_RDONLY)) < 0 )  {
    perror("open");
    exit(1);
  }

  gpiopin.gp_pin = pin;
  gpiopin.gp_flags = GPIO_PIN_OUTPUT;
  if ( ioctl(fd, GPIOSETCONFIG, &gpiopin) < 0 )  {
    perror("ioctl(GPIOSETCONFIG)");
    exit(1);
  }

  gpioreq.gp_pin = pin;
  gpioreq.gp_value = val;
  if ( ioctl(fd, GPIOSET, &gpioreq) < 0 )  {
    perror("ioctl(GPIOSET)");
    exit(1);
  }

  close(fd);
  exit(0);
}

35

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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