mini2440 led驱动

led驱动代码如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/ioctl.h>
#include <linux/gpio.h>

#include <mach/regs-gpio.h>

#include "led.h"

static int led_open(struct inode *inode, struct file *file)
{
	s3c2410_gpio_cfgpin(S3C2410_GPB(5), S3C2410_GPIO_OUTPUT);
	s3c2410_gpio_setpin(S3C2410_GPB(5), 1);

	return 0;
}

static int led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case LED_ON:
		s3c2410_gpio_setpin(S3C2410_GPB(5), 0);
		return 0;
	case LED_OFF:
		s3c2410_gpio_setpin(S3C2410_GPB(5), 1);
		return 0;
	default:
		return -EINVAL;
	}
}

static struct file_operations led_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	.ioctl = led_ioctl,
};

static struct miscdevice led_misc = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "led",
	.fops = &led_fops,
};

static int led_init(void)
{
	return misc_register(&led_misc);
}

static void led_exit(void)
{
	misc_deregister(&led_misc);
}

MODULE_LICENSE("Dual BSD/GPL");

module_init(led_init);
module_exit(led_exit);


这里使用了s3c2410_gpio_cfgpin和s3c2410_gpio_setpin函数来操作GPIO口,访问硬件是不能直接操作物理地址的,需要使用iounmap将物理地址映射成虚拟地址,而s3c2410_gpio_cfgpin和s3c2410_gpio_setpin这两个函数也同样满足这个条件,同样操作的是虚拟地址。led.h定义ioctl函数中的命令,代码如下:

#ifndef __LED_H
#define __LED_H

#define LED_ON		_IO('l', 0)
#define LED_OFF		_IO('l', 1)

#endif /* __LED_H */


上层应用程序代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>

#include "led.h"

int main(void)
{
        int fd;

        fd = open("/dev/led", O_RDWR);

        if (fd < 0) {
                printf("No such device!\n");
                return -1;
        }

        while (1) {
                ioctl(fd, LED_ON);
                sleep(1);
                ioctl(fd, LED_OFF);
                sleep(1);
        }

        close(fd);

        return 0;
}

你可能感兴趣的:(mini2440 led驱动)