static int davinci_dm644x_gpio_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
//const char *tag;
intgpio5_value1;
intgpio5_value2;
intgpio6_value1;
intgpio6_value2;
intgpio7_value1;
intgpio7_value2;
switch(cmd)
{
case 0://gpio = 0
gpio_direction_output(ZX_GPIO5, 0);
gpio_direction_output(ZX_GPIO6, 0);
gpio_direction_output(ZX_GPIO7, 0);
gpio5_value1 = gpio_get_value(ZX_GPIO5);
gpio6_value1 = gpio_get_value(ZX_GPIO6);
gpio7_value1 = gpio_get_value(ZX_GPIO7);
printk("gpio5_value1 = %d, gpio6_value1 = %d, gpio7_value1 = %d/n", gpio5_value1, gpio6_value1,gpio7_value1);
//}
break;
case 1://gpio = 1
//if(0==arg)
//{
//gpio_request(ZX_GPIO5, tag);
//printk("cmd = 1, ZX_GPIO5= %d/n", ZX_GPIO5);
gpio_direction_output(ZX_GPIO5, 1);
gpio_direction_output(ZX_GPIO6, 1);
gpio_direction_output(ZX_GPIO7, 1);
gpio5_value2 = gpio_get_value(ZX_GPIO5);
gpio6_value2 = gpio_get_value(ZX_GPIO6);
gpio7_value2 = gpio_get_value(ZX_GPIO7);
printk("gpio5_value2 = %d, gpio6_value2 =%d, gpio7_value2=%d/n", gpio5_value2, gpio6_value2, gpio7_value2);
//}
break;
default:
return -EINVAL;
}
}
static const struct file_operations davinci_dm644x_gpio_fileops = {
.owner= THIS_MODULE,
.open= davinci_dm644x_gpio_open,
.ioctl= davinci_dm644x_gpio_ioctl,
.release = gpio_release
};
static int __init davinci_dm644x_gpio_init(void)
{
int ret;
ret = register_chrdev(GPIO_MAJOR, DEVICE_NAME, &davinci_dm644x_gpio_fileops);
if(ret < 0)
{
printk(DEVICE_NAME " register falid!/n");
return ret;
}
printk (DEVICE_NAME" initialized/n");
return ret;
}
static void __exit davinci_dm644x_gpio_exit(void)
{
unregister_chrdev(GPIO_MAJOR, DEVICE_NAME);
}
module_init(davinci_dm644x_gpio_init);
module_exit(davinci_dm644x_gpio_exit);
MODULE_AUTHOR("xxx <>");
MODULE_DESCRIPTION("Davinci DM644x gpio driver");
MODULE_LICENSE("GPL");本文引用地址:http://m.butianyuan.cn/article/201611/321880.htm下面是應(yīng)用程序
#include
#include
#include
#include
#defineon(1)
#defineoff(0)
int main(void)
{
int gpio_number = 0;
int fd;
fd = open("/dev/dm644x_gpios", 0);
if (fd < 0)
{
perror("open device /dev/dm644x_gpios");
exit(1);
}
printf("open gpio, here is app/n");
sleep(1);
printf("gpio_number = %d/n", gpio_number);
sleep(1);
while (1) {
ioctl(fd, 1, 0);
sleep(5);
ioctl(fd, 0, gpio_number);
sleep(5);
}
close(fd);
return 0;
}
下面是Makefile
#
# Makefile for the skeleton device drivers.
#
KDIR=/root/work/linux-2.6.18_pro500# 內(nèi)核目錄
PWD := $(shell pwd)#是指要編譯的內(nèi)核模塊的源程序在哪個(gè)目錄下,$(PWD)指與Makefile在同一目錄下,也就是當(dāng)前目錄。
ifeq ($(KERNELRELEASE), )
modules:
$(MAKE) -C $(KDIR) M=$(PWD) modules#編譯模塊的規(guī)則,
arm_v5t_le-gcc -o gpio_test app.c#編譯應(yīng)用程序
cp -f *.ko gpio_test /nfsroot/dm644x/filesys#把編譯好的驅(qū)動和應(yīng)用程序復(fù)制到扳子的文件系統(tǒng)中去
#scp char-reg.kozlh@192.168.1.160:/home/zlh/opt/filesystem
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:#刪除生成的所有文件
rm -rf gpio_test *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
#obj-m += skeleton_dev_class.o
obj-m += gpio_driver.o#你要編譯的模塊名稱,這里必須和$(PWD)目錄下C程序文件的名稱相對應(yīng),如果C程序文件為world.c,那么必須修改成obj-m = world.o,也可
#Obj-m = hello.o
#Hello-objs = hello1.o, hello2.o編譯多個(gè)文件生成hello.o
endif
直接make 就行了如果不行的話make ARCH=arm CROSS_COMPILE=arm_v5t_le-加上編譯工具
超級終端操作:
insmodgpio_drive.ko
mknod/dev/dm644x_gpiosc1990
./gpio_test
評論