User Tools

Site Tools


user:jcavalu3:portfolio:datacomm1

Project #1: The Command Line

In this project, I was given a few links to websites that I needed to visit to begin accessing the GPIO pins on the Raspberry Pi. The first website was used to access the GPIO pins on the command line:

First, in order to access any GPIO pins, the user must use SuperUser or just sudo before any command accessing the pins.

According to the website, when writing to the ./export file in the /sys/class/gpio/ subdirectory, a file is created with a GPIO structure that is set up according to the input from the user. For this example, the user was told to create this file for GPIO11 so the user can access that pin and handle the LED.

To create the GPIO file, the user must input the following:

echo 11 > /sys/class/gpio/export

Next, the Pin Direction must be set as Input or Output:

echo out > /sys/class/gpio/gpio11/direction

Lastly, a value of '1' must be given to turn on the LED:

echo 1 > /sys/class/gpio/gpio11/value

This should have done it!

To turn it off, just repeat the step that turned the LED on, replacing the '1' with a '0'.

echo 0 > /sys/class/gpio/gpio11/value

To delete the file access, repeat the step that first created the file (export), but instead accessing the 'unexport' file.

echo 11 > /sys/class/gpio/unexport

Project #2: The C Code

This tutorial used C code to access the GPIO pins, which is what most of (if not the rest) of this class will focus on, the C code method of accessing the pins.

From the second website I found the code to access the pins through the C language.

The necessary code is as follows:

rpi.h

  1 #include <stdio.h>
  2 
  3 #include <sys/mman.h>
  4 #include <sys/types.h>
  5 #include <sys/stat.h>
  6 
  7 #include <fcntl.h>
  8 
  9 #include <unistd.h>
 10 
 11 #define BCM2708_PERI_BASE   0x20000000
 12 #define GPIO_BASE       (BCM2708_PERI_BASE + 0X200000) // GPIO controller
 13 
 14 #define BLOCK_SIZE      (4*1024)
 15 
 16 //IO Access
 17 struct bcm2835_peripheral {
 18     unsigned long addr_p;
 19     int mem_fd;
 20     void *map;
 21     volatile unsigned int *addr;
 22 
 23 };
 24 
 25 // struct bcm2835_peripheral gpio = {GPIO_BASE};
 26 
 27 extern struct bcm2835_peripheral gpio; // They have to be found somewhere, b    ut can't be in the header
 28 
 29 
 30 // GPIO setup macros. Always use INP_GPIO(X) before using OUT_GPIO(x)
 31 #define INP_GPIO(g)         *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3))
 32 #define OUT_GPIO(g)         *(gpio.addr + ((g)/10)) |=  (1<<(((g)%10)*3))
 33 #define SET_GPI_ALT(G,a)    *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a    )==4?3:2)<<(((g)%10)*3))
 34 
 35 #define GPIO_SET            *(gpio.addr + 7)    // sets   bits which are 1 i    gnores bits which are 0
 36 #define GPIO_CLR            *(gpio.addr + 10)   // chears bits which are 1 i    gnores bits which are 0
 37 
 38 #define GPIO_READ(g)        *(gpio.addr + 13) &= (1<<(g))

rpi.c

  1 #include "rpi.h"
  2 
  3 struct bcm2835_peripheral gpio = {GPIO_BASE};
  4 
  5 // Exposes the physical address defined in the passed structure using mmap o    n /dev/mem
  6 int map_peripheral(struct bcm2835_peripheral *p)
  7 {
  8     // Open /dev/mem
  9     if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
 10         printf("Failed to open /dev/mem, try checking permissions.\n");
 11         return -1;
 12     }
 13 
 14     p->map = mmap(
 15         NULL,
 16         BLOCK_SIZE,
 17         PROT_READ|PROT_WRITE,
 18         MAP_SHARED,
 19         p->mem_fd,      // File descriptor to physical memory virtual file '    /dev/mem'
 20         p->addr_p       // Address in physical map that we want this memory     block to expose
 21         );
 22 
 23     if (p->map == MAP_FAILED) {
 24         perror("mmap");
 25         return -1;
 26     }
 27 
 28     p->addr = (volatile unsigned int *)p->map;
 29 
 30     return 0;
 31 }
 32 
 33 void unmap_peripheral(struct bcm2835_peripheral *p) {
 34 
 35     munmap(p->map, BLOCK_SIZE);
 36     close(p->mem_fd);
 37 }

The code that is seen above is what is necessary for the program that turns on the LED to work.

main.c

  1 #include "rpi.h"
  2 
  3 int main()
  4 {
  5     if(map_peripheral(&gpio) == -1)
  6     {
  7         printf("Failed to map the physical GPIO registers into the virtual m    emory space.\n");
  8         return -1;
  9     }
 10 
 11     // Define pin 7 as output
 12     INP_GPIO(7); // Sets GPIO pin as input
 13     OUT_GPIO(7); // Sets GPIO pin as output
 14 
 15     while(1)
 16     {
 17         // Toggle pin 7 (blink a LED!)
 18         GPIO_SET = 1 << 7; // Sets the GPIO pin on
 19         usleep(1000000); // sleep for one second
 20 
 21         GPIO_CLR = 1 << 7; // Clears the GPIO pin/turns it off
 22         usleep(1000000); // sleep for one second
 23     }
 24     return 0;
 25 }

The program has been run and successfully turns the LED on and off.

user/jcavalu3/portfolio/datacomm1.txt · Last modified: 2013/10/31 01:12 by jcavalu3