This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

please tell me which header file i have to include in my GPIO APIs applications.

Other Parts Discussed in Thread: AM1808, OMAPL138

Hi,

I  used gpio uaer APIs, writen this below code its getting error what is the problem which header file i have to include in my code to access gpio APIs.


/* Include the standard linux header files */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
//#include <asm/gpio.h>
//#include <asm-generic/gpio.h>


int main (int argc, char **argv)
{
 
    int err, value;    
    unsigned long num;        

/* Allocate memory to GPIO line, can be achieved by doing gpio_request() */
  err = gpio_request(110, "sample");
      if (err < 0)
     {
            printf("Failed to open gpio pin\n");
            return 1;
     }
/* you want to initialize a structure with an invalid GPIO number,A number that's not valid will be rejected by calls    which may request or free GPIOs*/
    if (gpio_is_valid(110))
    {
               printf("Failed to read gpio %ld \n", num);
            return -1;
    }
/* Depending on the requirement set GPIO as input or output pin then set gpio value as high or low. Setting the GPIO pin 110 as input set as input or output, returning 0 or negative errno */
err= gpio_direction_input(110);
    if (err < 0)
     {
            printf("Failed to open put number in input direction\n");
            return 1;
     }
/* GPIO INPUT:  return zero or nonzero */
//value=gpio_get_value(30);

/*Make pin 110 as output and set the value as high*/
gpio_direction_output(110, 1);

/*Exporting that particular pin (110) to sysfs entry then use this API */
gpio_export(110, 1);
printf("powerON for GPIO 110 line \n");
sleep(100);

/*Get value from GPIO pin */
value=gpio_get_value(110);
printf("Value for GPIO 110 line after passing 1=%d \n", value);

gpio_direction_output(110, 0);
printf("power OFF GPIO 110 \n");

value=gpio_get_value(110);
printf("Value for GPIO 110 line after passing 0=%d \n", value);
 
  
    return 0;
}

After compiling i am getting below  error, please tell me which header file i have to include in my applications.

main.c:8:24: error: linux/gpio.h: No such file or directory
main.c: In function 'main':
main.c:35: warning: implicit declaration of function 'gpio_request'
main.c:42: warning: implicit declaration of function 'gpio_is_valid'
main.c:48: warning: implicit declaration of function 'gpio_direction_input'
main.c:58: warning: implicit declaration of function 'gpio_direction_output'
main.c:61: warning: implicit declaration of function 'gpio_export'
main.c:63: warning: implicit declaration of function 'sleep'
main.c:66: warning: implicit declaration of function 'gpio_get_value'
make: *** [gpp] Error 1

  • Hi Ram,

    Build it as kernel module and test

    make -C <kernel source dir> M=<gpio test file dir> ARCH=arm CROSS_COMPILE=<tool chain prefix>

    Example:-

    $ make -C ../ M=$PWD ARCH=arm CROSS_COMPILE=arm_v5t_le-

    Regards

    AnilKumar

    Please mark this Forum post as answered via the Verify Answer button below if it helps answer your question.  Thanks!

  • Ram,

    More details can be found at "Documentation/kbuild/modules.txt"

    M indicate the make file path so create make file in gpio test dir and add below line

    obj-m += gpio_test.o

    Then proceed for build

    Regards

    AnilKumar

  • In addition to the Anil's comment above, you need to structure your code as kernel module. Special entry points must be used using some special macros. The usual "main()" is not allowed. However, judging from your other posts on this forum, you are trying to create a user space application. All those gpio functions are only available to kernel modules.

  • hello

    i am also have trouble in compiling my gpio code with make file

    can you please guide me where i am wrong

    here is my make file 

    gpiodriver.c is my c code file

     

     
    KERNEL_SRC_ABS_PATH =/home/harnek/DaVinci-PSP-SDK-03.20.00.14/src/kernel/
     
     
    MAKE_FLAGS = ARCH =arm CROSS_COMPILE=/home/harnek/toolchain/arm-2009q1/arm-none-linux-gnueabi-
     

     
    C_SOURCE_FILES =gpiodriver.c
     
    NAME_OF_THIS_MODULE =gpio_module


     
    O_FILES = $(C_SOURCE_FILES:.c=.o)
     
    O_DIRS = $(dir $(O_FILES) )
     
    O_CMD_FILES = $(addsuffix .*.o.cmd, $(O_DIRS))
     

     
    ifneq ($(KERNELRELEASE),)
     
          obj-m := $(NAME_OF_THIS_MODULE).o
          $(NAME_OF_THIS_MODULE)-objs := $(O_FILES)
     
     
    else
          KERNELDIR ?= $(KERNEL_SRC_ABS_PATH)
          PWD := $(shell pwd)
    all:
          $(MAKE) -C $(KERNELDIR) $(MAKE_FLAGS) M=$(PWD) modules       
    default: all
    endif

    distclean: clean
          rm -f $(O_FILES)
          rm -f $(O_CMD_FILES)  
          rm -f $(NAME_OF_THIS_MODULE)\.{ko,o,mod\.c,mod\.o}
          rm -f \.$(NAME_OF_THIS_MODULE)\.{ko\.cmd,o\.cmd,mod\.o\.cmd,}
          rm -f \.tmp_versions -r
         
    clean:
          rm -f $(O_FILES)
          rm -f $(NAME_OF_THIS_MODULE)\.{ko,o,mod\.c,mod\.o}
          rm -f Module\.symvers modules\.order
     

    thanks 

  • also this is the error comes on terminal when i run make

    harnek@ubuntu:~/calixto$ make
    make -C /home/harnek/DaVinci-PSP-SDK-03.20.00.14/src/kernel/ ARCH =arm CROSS_COMPILE=/home/harnek/toolchain/arm-2009q1/arm-none-linux-gnueabi- M=/home/harnek/calixto modules
    make: *** empty variable name. Stop.
    make: *** [all] Error 2

  • Dear Harnek,

    Can you try using the attached examples which come along with the PSP release DaVinci-PSP-SDK-03.21.00.04 for AM1808? You have to slightly modify the build file and you can use the following command to invoke make:

    make VERBOSE=1 platform=omapl138

    Thanks.

    7612.examples-03.21.00.04.tar.gz