iTop4412驱动开发之二:LED驱动

驱动代码:

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

#include <linux/gpio.h>
#include <plat/gpio-cfg.h>
#include <mach/gpio.h>
#include <mach/gpio-exynos4.h>

MODULE_LICENSE("GPL");

//设备节点
#define NODE_NAME   "leds_node"

static int leds_open(struct inode *node, struct file *fp)
{
    printk("leds open.\n");

    return 0;
}

static int leds_release(struct inode *node, struct file *fp)
{
    printk("leds release.\n");

    return 0;
}

static int leds_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
{
    printk("leds ioctl, cmd: %d, arg: %d\n", cmd, arg);

	switch (cmd)
	{
		case 0:
			gpio_direction_output(EXYNOS4_GPL2(0), arg);
			break;
		default:
			break;
	}

    return 0;
}

struct file_operations leds_ops = {
    .owner = THIS_MODULE,
    .open = leds_open,
    .release = leds_release,
    .unlocked_ioctl = leds_ioctl,
};

struct miscdevice leds_misc_dev = {
    .minor = MISC_DYNAMIC_MINOR,   //自动分配次设备号
    .name = NODE_NAME,
    .fops = &leds_ops,
};

static int leds_probe(struct platform_device *dev)
{
    printk("leds probe, device name: %s\n", dev->name);

	int ret = gpio_request(EXYNOS4_GPL2(0), "LEDS");
	if (ret)
	{
		printk("gpio request gpl0(2) failed.\n");

		return -1;
	}

	//gpio_direction_output(EXYNOS4_GPL2(0), 1);
	s3c_gpio_cfgpin(EXYNOS4_GPL2(0), GPIO_OUTPUT);

    misc_register(&leds_misc_dev);

    return 0;
}

static int leds_remove(struct platform_device *dev)
{
    printk("leds remove, device name: %s\n", dev->name);

	gpio_free(EXYNOS4_GPL2(0));

    misc_deregister(&leds_misc_dev);

    return 0;
}

struct platform_driver leds_driver = {
    .probe = leds_probe,
    .remove = leds_remove,
    .driver = {
        .name = "leds_ctl",
        .owner = THIS_MODULE,
    }
};

struct platform_device leds_dev = {
    .name = "leds_ctl",
    .id = -1,
};

static int leds_init()
{
    printk(KERN_EMERG "leds init.\n");

    platform_device_register(&leds_dev);
    platform_driver_register(&leds_driver);

    return 0;
}

static void leds_exit()
{
    printk(KERN_EMERG "leds exit.\n");
    platform_driver_unregister(&leds_driver);
    platform_device_unregister(&leds_dev);
}

module_init(leds_init);
module_exit(leds_exit);

应用代码:

#include <stdio.h>			//printf()
#include <stdlib.h>			//exit()
#include <string.h>			//strlen(), bzero();
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>


int main(int argc, char *argv[])
{

    int fd = open("/dev/leds_node", O_RDWR | O_NDELAY);

    while (1)
    {
        ioctl(fd, 0, 1);
        sleep(1);
        ioctl(fd, 0, 0);
        sleep(1);
    }
	close(fd);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值