Skip to content

Instantly share code, notes, and snippets.

@piranha32
Last active Dec 18, 2015
Embed
What would you like to do?
mmapped GPIO test
/*
* memmapped_oe_test.c
*
* Created on: Jun 11, 2013
* Author: jacek
* Compile the code:
* gcc memmapped_oe_test.c -o memmapped_oe_test -std=c99
*/
#include <stdint.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#define DATA_OUT_REG 0x13C
#define DATA_IN_REG 0x138
#define GPIO_OE_REG 0x134
int main()
{
const uint32_t gpioAddrs[] =
{ 0x44E07000, 0x4804C000, 0x481AC000, 0x481AE000 };
uint32_t *gpios[4];
int gpioFd;
gpioFd = open("/dev/mem", O_RDWR | O_SYNC);
for (int i = 0; i < 4; i++)
{
gpios[i] = (uint32_t *) mmap(NULL, 0xfff,
PROT_READ | PROT_WRITE, MAP_SHARED, gpioFd, gpioAddrs[i]);
printf("gpio[%i] at address 0x%08x mapped at 0x%08x\n",i,gpioAddrs[i],gpios[i]);
}
int i;
i=0; //Bus error
//i=1; //OK
//i=2; //Bus error
//i=3; //Bus error
printf("i=%i\n",i);
printf("OUT[%i]=0x%08x\n",i,gpios[i][DATA_OUT_REG/4]);
printf("IN[%i]=0x%08x\n",i,gpios[i][DATA_IN_REG/4]);
printf("OE[%i]=0x%08x\n",i,gpios[i][GPIO_OE_REG/4]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment