Linux example, 2 linux example, Kta55/pitx software guide – Kontron KTA55-pITX User Manual

Page 21

Advertising
background image

KTD-S0043-A

Page 18

GPIO Interface


KTA55/pITX Software Guide

7.2

Linux Example

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>


int main (void)
{
int

i;

int fd = open ("/dev/mem", O_RDWR);

// open device "mem"

volatile unsigned char *ptr;


if (fd < 0)

// is mem device present ?

{

printf ("\nCould not open memory device\n");

return

-1;

}

// map 4k page to address 0xFED80000

ptr = (unsigned char*) mmap (NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0xFED80000);

if (ptr == MAP_FAILED)

// did mapping failed ?

{

printf ("\nMapping failed\n");

close

(fd);

return

-1;

}

for (i = 0; i < 8; i++)

printf ("GPIO%d pin status: %0x02X\n", i, ((ptr[0x0100+i] & 0x80) >> 7));

// read and mask GPIO register

close

(fd);

return

0;

}


Note: You must have administrator rights to execute this example. You can also use 'sudo' if you have no
administrator rights.

Advertising
This manual is related to the following products: