文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>驱动开发板上GPIO上的led

驱动开发板上GPIO上的led

时间:2007-01-02  来源:基地小头目

(一)驱动源程序
 
  led.c

    #ifndef __KERNEL__
    #define __KERNEL__
    #endif
    #ifndef MODULE
    #define MODULE
    #endif

    #include<linux/fs.h>
    #include<linux/module.h>
    #include<asm/hardware.h>
    #include<linux/kernel.h>
     #include<asm/arch/S3C2410.h>
 
    MODULE_LICENSE("GPL");

    #define IOPORT_MAJOR 220

    typedef char ioport_device_t;

    long port_addr;

    static ioport_device_t gpio_devices[257];

    int gpio_open(struct inode*, struct file *);
    int gpio_release(struct inode*, struct file *);
    int gpio_ctl_ioctl(struct inode *, struct file *, unsigned int, unsigned long);

    static struct file_operations gpio_ctl_fops=
     {
       ioctl: gpio_ctl_ioctl,
       open: gpio_open,
       release: gpio_release,
     };

    int gpio_open(struct inode *inode, struct file *filp)
    {
        int minor;
        minor = MINOR(inode->i_rdev);
        set_gpio_ctrl(GPIO_MODE_OUT|GPIO_F6);
        gpio_devices[minor]++;
        return 0;
    }

    int gpio_release(struct inode *inode, struct file *filp)
    {
        int minor;
        minor = MINOR(inode->i_rdev);
        if(gpio_devices[minor])
        gpio_devices[minor]--;
        return 0;
    }
    int gpio_ctl_ioctl(struct inode *inode,struct file *flip,unsigned int command,unsigned long arg)
    {
       int err=0;
       switch(command)
       {
            case 0:
            write_gpio_bit(GPIO_MODE_OUT|GPIO_F6,1);
            return 0;
            case 1:
            write_gpio_bit(GPIO_MODE_OUT|GPIO_F6,0);
            return 0;
        }
        return err;
    }

    static devfs_handle_t devfs_handle;

    int init_module(void)
    {
        int ret;
        printk("hello \n");
        ret=register_chrdev(IOPORT_MAJOR,"gpiotest",&gpio_ctl_fops);
        devfs_handle = devfs_register(NULL,"gpiotest", DEVFS_FL_DEFAULT,
        IOPORT_MAJOR,0,S_IFCHR|S_IRUSR|S_IWUSR,&gpio_ctl_fops,NULL);
        return 0;
    }

    void cleanup_module(void)
    {
        devfs_unregister(devfs_handle);
        unregister_chrdev(IOPORT_MAJOR,"gpiotest");
    }


(二)测试源程序

test.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define DEVICE_GPIOTEST "/dev/gpiotest"

int main()
{
int fd;
int val=-1;
if((fd=open(DEVICE_GPIOTEST,O_RDONLY|O_NONBLOCK))<0)
{
perror("can not open device");
exit(1);
}
while(1)
{
printf("0:set ,1:clear,2: quit :");
scanf("%d",&val);

if(val==0)
ioctl(fd,0,0);
else if(val==1)
ioctl(fd,1,0);
else if(val==2)
{
  printf("close \n");
close(fd);
 break;
}
}
}


(三)makefile

    CC=arm-linux-gcc
    CFLAG := -Wall -DMODULE -D__KERNEL__ -DDEBUG_PRINTK
            -I/usr/local/arm/2.4.18/include/
    OBJ=led
        $(OBJ).o:$(OBJ).c
        $(CC) $(CFLAG) -c $(OBJ).c
    clean:
        rm -f *.o

(四)关于程序的说明

    1、devfs_register()在/dev文件夹下创建gpiotest文件
    2、对GPIO口的操作宏定义说明(在/asm/arch/S3C2410.h)
        set_gpio_ctrl(GPIO_MODE_OUT|GPIO_F6) 设置相应GPIO位是输入还是输出
        GPIO_MODE_OUT  --为输出
        GPIO_MODE_IN   --为输入
       

        write_gpio_bit(GPIO_MODE_OUT|GPIO_F6,1)向相应的GPIO位写数据
      
          
   
排行榜 更多 +
香焦金刚(Banana Kong)

香焦金刚(Banana Kong)

冒险解谜 下载
命运主宰者

命运主宰者

冒险解谜 下载
高中迷恋装扮

高中迷恋装扮

休闲益智 下载