Hardware Reference
In-Depth Information
Solution
The C solution isn't as simple as the BoneScript or Python solution ( Recipe 5.20 ) , but it
does work and is much faster. libsoc is a “C library for interfacing with common SoC {sys-
tem on chip} peripherals through generic kernel interfaces” by Jack Mitchell . Here's how
to get and install it:
bone# git clone https://github.com/jackmitch/libsoc.git
bone# cd libsoc
bone# ./autogen.sh
# Takes about a minute
bone# ./configure
# Another minute
bone# make
# 30 seconds
bone# make install
This installs the libsoc library in /usr/local/lib . Add the code in Example 5-4 to a file
called blinkLED.c .
Example 5-4. Use C to blink an LED (blinkLED.c)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "libsoc_gpio.h"
#include "libsoc_debug.h"
// Blinks an LED attached to P9_14 (gpio1_18, 32+18=50)
#define GPIO_OUTPUT 50
int main ( void ) {
gpio * gpio_output ; // Create gpio pointer
libsoc_set_debug ( 1 ); // Enable debug output
// Request gpio
gpio_output = libsoc_gpio_request ( GPIO_OUTPUT , LS_SHARED );
// Set direction to OUTPUT
libsoc_gpio_set_direction ( gpio_output , OUTPUT );
libsoc_set_debug ( 0 );
// Turn off debug printing for fast toggle
int i ;
for ( i = 0 ; i < 1000000 ; i ++) { // Toggle the GPIO 100 times
libsoc_gpio_set_level ( gpio_output , HIGH );
Search WWH ::




Custom Search