09.11.2012 Views

Contents - Raspberry PI Community Projects

Contents - Raspberry PI Community Projects

Contents - Raspberry PI Community Projects

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

G<strong>PI</strong>O Code examples<br />

G<strong>PI</strong>O Driving Example (C)<br />

Gert van Loo & Dom, has provided (http://www.raspberrypi.org/forum/educationalapplications/gertboard/page-4/#p31555)<br />

some tested code which accesses the G<strong>PI</strong>O pins<br />

through direct G<strong>PI</strong>O register manipulation in C-code. (Thanks to Dom for doing the<br />

difficult work of finding and testing the mapping.) Example G<strong>PI</strong>O code:<br />

//<br />

// How to access G<strong>PI</strong>O registers from C-code on the <strong>Raspberry</strong>-Pi<br />

// Example program<br />

// 15-January-2012<br />

// Dom and Gert<br />

//<br />

// Access from ARM Running Linux<br />

#define BCM2708_PERI_BASE 0x20000000<br />

#define G<strong>PI</strong>O_BASE (BCM2708_PERI_BASE + 0x200000) /* G<strong>PI</strong>O controller */<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#define PAGE_SIZE (4*1024)<br />

#define BLOCK_SIZE (4*1024)<br />

int mem_fd;<br />

char *gpio_mem, *gpio_map;<br />

char *spi0_mem, *spi0_map;<br />

// I/O access<br />

volatile unsigned *gpio;<br />

// G<strong>PI</strong>O setup macros. Always use INP_G<strong>PI</strong>O(x) before using OUT_G<strong>PI</strong>O(x) or SET_G<strong>PI</strong>O_ALT(x,y)<br />

#define INP_G<strong>PI</strong>O(g) *(gpio+((g)/10)) &= ~(7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!