#define GPIO1_BASE 0x4804C000 // Start Address
#define GPIO1_END 0x4804D000 // End Address
#define GPIO1_SIZE GPIO_END - GPIO_BASE
#define GPIO_OE 0x0134 // Offset
#define GPIO_DATAOUT 0x013C // Offset
#define GPIO1_16 16
int main(int argc, char** argv)
{
volatile unsigned int *reg_base, *reg_oe, *reg_out;
unsigned int value;
int fd;
//open mem file for Read and Write
fd = open("/dev/mem", O_RDWR);
if (fd == -1)
{
perror("Unable to open /dev/mem");
exit(EXIT_FAILURE);
}
/* LED Setup and Turn-on */
reg_base = mmap(0, GPIO1_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);
//Set GPIO to Output
reg_oe = (volatile unsigned int*)(reg_base + GPIO_OE);
value = *(reg_oe);
value &= ~(1 << GPIO1_16);
*(reg_oe) = value;
//turn on GPIO
reg_out = (volatile unsigned int*)(GPIO1_16 + GPIO_DATAOUT);
value = *(reg_out);
value |= (1 << GPIO1_16);
*(reg_out) = value;
return 0;
}--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
See
https://github.com/nomel/beaglebone/tree/master/gpio-header
If you don't want to use it directly, you can use the generated files as an example.
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <sys/mman.h>#include "gpio.h"using namespace std;// The number is indicating which GPIO Module(0-3) of// each pin belongs to.//const int BeagleGPIO::pinBank[] ={-1, -1, 1, 1, 1, // P8_1 -> P8_51, 2, 2, 2, 2, // P8_6 -> P8_101, 1, 0, 0, 1, // P8_11 -> P8_151, 0, 2, 0, 1, // P8_16 -> P8_201, 1, 1, 1, 1, // P8_21 -> P8_251, 2, 2, 2, 2, // P8_26 -> P8_300, 0, 0, 2, 0, // P8_31 -> P9_352, 2, 2, 2, 2, // P8_36 -> P8_402, 2, 2, 2, 2, // P8_41 -> P8_452, // P8_46-1, -1, -1, -1, -1, // P9_1 -> P9_5-1, -1, -1, -1, -1, // P9_6 -> P9_100, 1, 0, 1, 1, // P9_11 -> P9_151, 0, 0, 0, 0, // P9_16 -> P9_200, 0, 1, 0, 3, // P9_21 -> P9_250, 3, 3, 3, 3, // P9_26 -> P9_303, -1, -1, -1, -1, // P9_31 -> P9_35-1, -1, -1, -1, -1, // P9_36 -> P9_400, 0, -1, -1, -1, // P9_41 -> P9_45-1 // P9_46};// Pin ID: if it is -1, it means that it is not a GPIO pin// If it is a GPIO Pin, the number is indicating the position// of GPIO_DATAIN and GPIO_DATAOUT registers.const int BeagleGPIO::pinId[] ={-1, -1, 6, 7, 2, // P8_1 -> P8_53, 2, 3, 5, 4, // P8_6 -> P8_1013, 12, 23, 26, 15, // P8_11 -> P8_1514, 27, 1, 22, 31, // P8_16 -> P8_2030, 5, 4, 1, 0, // P8_21 -> P8_2529, 22, 24, 23, 25, // P8_26 -> P8_3010, 11, 9, 17, 8, // P8_31 -> P9_3516, 14, 15, 12, 13, // P8_36 -> P8_4010, 11, 8, 9, 6, // P8_41 -> P8_457, // P8_46-1, -1, -1, -1, -1, // P9_1 -> P9_5-1, -1, -1, -1, -1, // P9_6 -> P9_1030, 28, 31, 18, 16, // P9_11 -> P9_1519, 5, 4, 13, 12, // P9_16 -> P9_203, 2, 17, 15, 21, // P9_21 -> P9_2514, 19, 17, 15, 16, // P9_26 -> P9_3014, -1, -1, -1, -1, // P9_31 -> P9_35-1, -1, -1, -1, -1, // P9_36 -> P9_4020, 7, -1, -1, -1, // P9_41 -> P9_45-1 // P9_46};// Pad Control Register// The addresses of registers, which control the Pin Mux of// each GPIO Pinsconst unsigned long BeagleGPIO::padControl[] ={0x0000, 0x0000, 0x0818, 0x081C, 0x0808, // P8_1 -> P8_50x080C, 0x0890, 0x0894, 0x089C, 0x0898, // P8_6 -> P8_100x0834, 0x0830, 0x0824, 0x0828, 0x083C, // P8_11 -> P8_150x0838, 0x082C, 0x088C, 0x0820, 0x0884, // P8_16 -> P8_200x0880, 0x0814, 0x0810, 0x0804, 0x0800, // P8_21 -> P8_250x087C, 0x08E0, 0x08E8, 0x08E4, 0x08EC, // P8_26 -> P8_300x08D8, 0x08DC, 0x08D4, 0x08CC, 0x08D0, // P8_31 -> P8_350x08C8, 0x08C0, 0x08C4, 0x08B8, 0x08BC, // P8_36 -> P8_400x08B0, 0x08B4, 0x08A8, 0x08AC, 0x08A0, // P8_41 -> P8_450x08A4, // P8_460x0000, 0x0000, 0x0000, 0x0000, 0x0000, // P9_1 -> P9_50x0000, 0x0000, 0x0000, 0x0000, 0x0000, // P9_6 -> P9_100x0870, 0x0878, 0x0874, 0x0848, 0x0840, // P9_11 -> P9_150x084C, 0x095C, 0x0958, 0x097C, 0x0978, // P9_16 -> P9_200x0954, 0x0950, 0x0844, 0x0984, 0x09AC, // P9_21 -> P9_250x0980, 0x09A4, 0x099C, 0x0994, 0x0998, // P9_26 -> P9_300x0990, 0x0000, 0x0000, 0x0000, 0x0000, // P9_31 -> P9_350x0000, 0x0000, 0x0000, 0x0000, 0x0000, // P9_36 -> P9_400x09B4, 0x0964, 0x0000, 0x0000, 0x0000, // P9_41 -> P9_450x0000 // P9_46};// Control Module Registers: Please refer to Memory Map of// AM335x MCUconst unsigned long BeagleGPIO::ctrlModuleReg = 0x44E10000;// GPIO Module registersconst unsigned long BeagleGPIO::GpioBaseReg[] ={0x44E07000, // GPIO00x4804C000, // GPIO10x481AC000, // GPIO20x481AE000 // GPIO3};//=======================================================//=======================================================BeagleGPIO::BeagleGPIO(){// Not initialized by defaultmActive = false;system("echo 5 > /sys/class/gpio/export > /dev/null");system("echo 65 > /sys/class/gpio/export > /dev/null");system("echo 105 > /sys/class/gpio/export > /dev/null");// Opening /dev/mem firstm_gpio_fd = open( "/dev/mem", O_RDWR | O_SYNC );if ( m_gpio_fd < 0 ){GPIO_ERROR( "Cannot open /dev/mem" );return;}// Map Control ModulemControlModule = (unsigned long *)mmap( NULL, 0x1FFF, PROT_READ | PROT_WRITE, MAP_SHARED, m_gpio_fd, ctrlModuleReg );if ( mControlModule == MAP_FAILED ){GPIO_ERROR( "Control Module Mapping failed" );return;}// Now mapping the GPIO registersfor ( int i=0; i<4; ++i){// Map a GPIO bankm_gpio[i] = (unsigned long *)mmap( NULL, 0xFFF, PROT_READ | PROT_WRITE, MAP_SHARED, m_gpio_fd, GpioBaseReg[i] );if ( m_gpio[i] == MAP_FAILED ){GPIO_ERROR( "GPIO Mapping failed for GPIO Module " << i );return;}}// Init complete and successfullmActive = true;}//-------------------------------------------------------////Disstructor////-------------------------------------------------------BeagleGPIO::~BeagleGPIO(){if ( mActive && m_gpio_fd)close( m_gpio_fd );}//-------------------------------------------------------//// Configure pin as input/output////-------------------------------------------------------BeagleGPIO::Status BeagleGPIO::configPin( unsigned short _pin, Direction _direction ){if ( !mActive )return kFail;//assert(pinBank[_pin]>=0);//assert(pinId[_pin]>=0);// Set Pin as GPIO on the pad controlmControlModule[padControl[_pin]/4] |= 0x07;unsigned long v = 0x1 << pinId[_pin];if ( _direction == kINPUT){m_gpio[pinBank[_pin]][kOE/4] |= v;}else{m_gpio[pinBank[_pin]][kOE/4] &= ~v;}// Disable Interrupts by defaultm_gpio[pinBank[_pin]][kIRQSTATUS_CLR_0/4] |= v;m_gpio[pinBank[_pin]][kIRQSTATUS_CLR_1/4] |= v;return kSuccess;}//-------------------------------------------------------////! Write a value to a Pin////! return status////-------------------------------------------------------BeagleGPIO::Status BeagleGPIO::writePin( unsigned short _pin, unsigned char _value ){//assert(pinBank[_pin]>=0);//assert(pinId[_pin]>=0);unsigned long v = (_value & 0x01) << pinId[_pin];unsigned long mask = 0x1 << pinId[_pin];// Remove bitm_gpio[pinBank[_pin]][kDATAOUT/4] &= ~mask;// Assign new bit valuem_gpio[pinBank[_pin]][kDATAOUT/4] |= v;return kSuccess;}//-------------------------------------------------------//// Read a value from a pin////-------------------------------------------------------unsigned char BeagleGPIO::readPin( unsigned short _pin ){//assert(pinBank[_pin]>=0);//assert(pinId[_pin]>=0);unsigned long bit = pinId[_pin];return (m_gpio[pinBank[_pin]][kDATAIN/4] & (0x1 << bit)) >> bit;}