linux下的led驱动程序

/*
 * Copyright (c) 2009-~ Lan Peng
 *
 * This source code is released for free distribution under the terms of the
 * GNU General Public License
 *
 * Author: Lan Peng<lanpeng722@gmail.com>
 * Created Time: 2009年09月14日 星期一 18时02分17秒
 * File Name: led.c
 *
 * Description:
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/irq.h>
#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <mach/hardware.h>
#include <mach/platform.h>
#include <mach/lpc32xx_gpio.h>

#define GPO_5_SET     (*(volatile unsigned int __force *)0xf4028004)//虚拟地址
#define GPO_5_CLR     (*(volatile unsigned int __force *)0xf4028008)//虚拟地址
#define GPO_5         (1<<5) //寄存器的第五位控制led灯
#define DEV_NAME    "ledlan"//设备文件名
#define IO_SET        1
#define IO_CLR        0

static int lan_led_open(struct inode *inode, struct file *file);
static int lan_led_release(struct inode *inode, struct file *file);
static ssize_t lan_led_write(struct file *file, const char __user *buff, ssize_t count, loff_t *ppos);
static ssize_t lan_led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);

static int lan_led_open(struct inode *inode, struct file *file)
{
    printk("Open......\n");
    try_module_get(THIS_MODULE);
    GPO_5_SET |= GPO_5;
    return 0;
}

static int lan_led_release(struct inode *inode, struct file *file)
{
    printk("Release......\n");
    module_put(THIS_MODULE);
    return 0;
}

static ssize_t lan_led_write(struct file *file, const char __user *buff, ssize_t count, loff_t *ppos)
{
    int i;
    unsigned char ctrl=0;
    if(count > 1)
        return -1;
    printk("write......\n");
    get_user(ctrl, (u8 *)buff);
    i = (ctrl - 0x30)&0x03;
    if(i == 0)
        GPO_5_CLR |= GPO_5;
    else if(i == 1)
        GPO_5_SET |= GPO_5;
    return count;
}

static ssize_t lan_led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    switch(cmd)
    {
        case IO_SET:
            GPO_5_SET |= GPO_5;
        break;
        case IO_CLR:
            GPO_5_CLR |= GPO_5;
        break;
        default:
            GPO_5_SET |= GPO_5;
        break;    
    }
    return 0;
}

static struct file_operations lan_led_fops = {
    .owner = THIS_MODULE,
    .write = lan_led_write,
    .ioctl     = lan_led_ioctl,
    .open = lan_led_open,
    .release = lan_led_release,
};

static struct miscdevice lan_led_miscdev = { /*杂设备*/
    .minor     = MISC_DYNAMIC_MINOR,
    .name     = DEV_NAME,
    .fops    = &lan_led_fops,
};

static int __init lan_init(void)
{
    int ret;
    printk("init......\n");
    ret = misc_register(&lan_led_miscdev);
    if(ret)
        printk("Failed to register miscdev");
    return 0;
}

static void __exit lan_exit(void)
{
    printk("byebye......\n");
    misc_deregister(&lan_led_miscdev);
}

module_init(lan_init);
module_exit(lan_exit);
MODULE_LICENSE("GPL");


//用户态测试程序:test_led.c:

/*
 * Copyright (c) 2009-~ Lan Peng
 *
 * This source code is released for free distribution under the terms of the
 * GNU General Public License
 *
 * Author:       Lan Peng<lanpeng722@gmail.com>
 * Created Time: 2009年09月14日 星期一 20时21分18秒
 * File Name:    test_led.c
 *
 * Description: 
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int args, char *argv[])
{
    int fd;
    int i;
    char buf;
    fd = open("/dev/ledlan", O_RDWR);
    if(fd == -1)
    {
        printf("Open Error!\n");
        exit(1);
    }
#if 1   
    for(i = 0; i < 10; i++){
        buf = '1';
        write(fd, &buf, 1);
        usleep(300000);
        buf = '0';
        write(fd, &buf, 1);
        usleep(300000);
    }
#endif

#if 1
    printf("ioctl......");
    for(i = 0; i < 10; i++){
        ioctl(fd, 0, NULL);
        usleep(300000);
        ioctl(fd, 1, NULL);
        usleep(300000);
    }
#endif   
    return 0;
}



<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(640) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值