Ktam3874/pitx software guide, Distribution, needs root rights) – Kontron KTAM3874-pITX User Manual
Page 66

KTD-S0057-I
Page 62 Linux® Programming Examples (DebianTM)
KTAM3874/pITX Software Guide
The program below generates a square wave (ratio of 1:1) as an output demonstration and subsequently it
reads the voltage level from the same pin as an input example (compiled on Ubuntu
TM
distribution, needs
root rights).
/* GPIO test program
* Copyright (c) 2013 Kontron Technology A/S
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*/
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#define MAX_GPIO
136
#define GPIO_IN
0
#define GPIO_OUT
1
#define DIR_OUT
"out"
#define DIR_IN
"in"
#define GP2_17
81
/* J2105 Pin 38 */
#define MAX_CNT
1000
#define SLP_TIME
100
static const char *dev_export
= "/sys/class/gpio/export";
static const char *dev_unexport =
"/sys/class/gpio/unexport";
static const char *dev_dir
= "/sys/class/gpio/gpio%d/direction";
static const char *dev_val
= "/sys/class/gpio/gpio%d/value";
static int fd [MAX_GPIO];
int gpio_export (int gpio_pin)
{
int
fd_tmp;
char
str[8];
fd_tmp = open (dev_export, O_WRONLY);
if (fd_tmp < 0)
return
-1;
sprintf (str, "%d", gpio_pin);
if (write (fd_tmp, str, strlen (str)) != strlen (str))
{
close
(fd_tmp);
return
-1;
}
close
(fd_tmp);
return
0;
}