Hardware Reference
In-Depth Information
Solution
Recipe 8.1 shows how to control an LED with a pushbutton using BoneScript. This recipe
accomplishes the same thing using C and libsoc . Recipe 5.21 shows how to use C and
libsoc to access the GPIO pins.
Wire up the pushbutton and LED as shown in Figure 8-1 . Follow the instructions in Recipe
5.21 to install libsoc . Then add the code in Example 8-2 to a file named pushLED.c .
Example 8-2. Code for reading a switch and blinking an LED using libsoc (pushLED.c)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "libsoc_gpio.h"
#include "libsoc_debug.h"
/**
*
* This gpio_test is intended to be run on BeagleBone
* and reads pin P9_42 (gpio7) and write the value to P9_12 (gpio60).
*
* The GPIO_OUTPUT and INPUT defines can be changed to support any two
pins.
*
*/
#define GPIO_OUTPUT 60
#define GPIO_INPUT 7
// Create both gpio pointers
gpio * gpio_output , * gpio_input ;
static int interrupt_count = 0 ;
int callback_test ( void * arg ) {
int * tmp_count = ( int *) arg ;
int value ;
* tmp_count = * tmp_count + 1 ;
value = libsoc_gpio_get_level ( gpio_input );
libsoc_gpio_set_level ( gpio_output , value );
// Comment out the following line to make the code respond faster
Search WWH ::




Custom Search