LED驱动实例2

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>


#define DEVICE_NAME     "leds"  /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define LED_MAJOR       231     /* 主设备号 */

/* 应用程序执行ioctl(fd, cmd, arg)时的第2个参数 */
#define IOCTL_LED_ON    0
#define IOCTL_LED_OFF   1

/* 用来指定LED所用的GPIO引脚 */
static unsigned long led_table [] = {
    S3C2410_GPF7,
    S3C2410_GPF6,
    S3C2410_GPF5,
    S3C2410_GPF4,
};

/* 用来指定GPIO引脚的功能:输出 */
static unsigned int led_cfg_table [] = {
    S3C2410_GPF7_OUTP,
    S3C2410_GPF6_OUTP,
    S3C2410_GPF5_OUTP,
    S3C2410_GPF4_OUTP,
};

#undef NDEBUG
/*Comment/uncomment the following line to disable/enable debugging,
 OR define(or NOT) it in Makefile.
*/
#define NDEBUG

#undef PDEBUG
/* undef it, just in case */
#define VERBOSE

#ifdef NDEBUG
    #ifdef __KERNEL__
        #ifdef VERBOSE
            /* This one if debugging is on, and kernel space */
            #define PDEBUG(fmt, args...) printk( KERN_ERR "%s:%d " fmt, __FILE__, __LINE__, ## args)
        #else
            #define PDEBUG(fmt, args...) printk( KERN_ERR "kdebug: " fmt, ## args)
        #endif
    #else
         /* This one for user space */
        #define PDEBUG(fmt, args...) fprintf(stderr, "%s:%d " fmt,  __FILE__, __LINE__, ## args)
    #endif
#else
     /* not debugging: nothing */
    #define PDEBUG(fmt, args...) do {} while(0)
#endif


/* 应用程序对设备文件/dev/leds执行open(...)时,
 * 就会调用s3c24xx_leds_open函数
 */
static int s3c24xx_leds_open(struct inode *inode, struct file *file)
{
    int i;
   
    for (i = 0; i < 4; i++) {
        // 设置GPIO引脚的功能:本驱动中LED所涉及的GPIO引脚设为输出功能
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
     // 关闭内部上拉
     s3c2410_gpio_pullup(led_table[i], 1);
    }
    return 0;
}

/* 应用程序对设备文件/dev/leds执行ioclt(...)时,
 * 就会调用s3c24xx_leds_ioctl函数
 */
static int s3c24xx_leds_ioctl(
    struct inode *inode,
    struct file *file,
    unsigned int cmd,
    unsigned long arg)
{
    if (arg > 4) {

        return -EINVAL;
    }
   
    switch(cmd) {
    case IOCTL_LED_ON:
        // 设置指定引脚的输出电平为0V
        s3c2410_gpio_setpin(led_table[arg], 0);
        return 0;

    case IOCTL_LED_OFF:
        // 设置指定引脚的输出电平为3.3V
        s3c2410_gpio_setpin(led_table[arg], 1);
        return 0;

    default:
        return -EINVAL;
    }
}

static inline void s3c24xx_leds_proc_usage(char *exename)
{
    printk(KERN_ERR "Usage:\n");
    printk(KERN_ERR "echo \"<led_no> <on/off>\" > /proc/%s\n", exename);
    printk(KERN_ERR "led_no = 1, 2, 3 or 4\n");
}

static ssize_t s3c24xx_leds_proc_write (struct file *file,
   const char __user *buffer, size_t count, loff_t *offset)
{
 char *buf, *tok, **tokptrs;
 char *whitespace = " ,\t\r\n"; //空格和","分隔
 int ret = -EINVAL, leds_state, leds_no = -1, ntoks;
 int i = 0;

 if (!capable(CAP_SYS_ADMIN))
  return -EPERM;
 if (count == 0)
  return 0;

 if (!(buf = kmalloc(count + 1, GFP_KERNEL)))
  return -ENOMEM;
 if (copy_from_user((void *)buf, buffer, count)) {
  ret = -EFAULT;
  goto out0;
 }
 buf[count] = '\0';

 PDEBUG("s3c24xx_leds: buf= %s\n", buf);
 
 if (!(tokptrs = (char **)get_zeroed_page(GFP_KERNEL))) {
  ret = -ENOMEM;
  goto out1;
 }
 
 ret = -EINVAL;
 ntoks = 0;
 do {
  buf = buf + strspn(buf, whitespace);
  tok = strsep(&buf, whitespace);
  if (*tok == '\0') {
   if (ntoks == 0) {
    goto out1;
   } else
    break;
  }
  PDEBUG("in do_while loop%d buf=%s, tok=%s\n",ntoks, buf, tok);
  if (ntoks == (PAGE_SIZE / sizeof(char **)))
   goto out1;
  tokptrs[ntoks++] = tok;
 } while(buf);
 
 PDEBUG("s3c24xx_leds: ntoks= %d, buf2= %s\n", ntoks, buf);
 
 if (ntoks <2)
   goto out1;
 
 leds_state = -1;

 //The following two lines just for debug
 i= strcmp(tokptrs[ntoks-1], "on");
 PDEBUG("s3c24xx_leds states:%s,strcmp result=%d\n", tokptrs[ntoks-1],i);

 if (strcmp(tokptrs[ntoks-1], "on")==0) {
  leds_state = 1;
 } else if (strcmp(tokptrs[ntoks-1], "off") == 0) {
  leds_state = 0; 
 } else {
  goto out1;
 }
 for (i = 0; i< ntoks-1; i++) {
     leds_no = simple_strtoul(tokptrs[i], NULL, 0);
  PDEBUG("operate led-%d\n", leds_no);
  if(leds_no < 1 || leds_no >4) {
    PDEBUG("Error leds number %d\n", leds_no);
    s3c24xx_leds_proc_usage(DEVICE_NAME);
    continue;
  }
  s3c2410_gpio_setpin(led_table[leds_no-1], 1-leds_state); // LED ON or OFF
  ret = 0;
 }
out1:
 free_page((unsigned long)tokptrs);
out0:
 kfree(buf);
 if (ret == 0)
  return count;
 else {
   s3c24xx_leds_proc_usage(DEVICE_NAME); 
  return ret;
 }
}

/* 这个结构是字符设备驱动程序的核心
 * 当应用程序操作设备文件时所调用的open、read、write等函数,
 * 最终会调用这个结构中指定的对应函数
 */
static struct file_operations s3c24xx_leds_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   s3c24xx_leds_open,    
    .ioctl  =   s3c24xx_leds_ioctl,
};

static struct file_operations s3c24xx_leds_proc_ops = {
    .owner = THIS_MODULE,
    .write = s3c24xx_leds_proc_write,
};

/*
 * 执行“insmod s3c24xx_leds.ko”命令时就会调用这个函数
 */
static int __init s3c24xx_leds_init(void)
{
    int ret;
    struct proc_dir_entry *entry;
   
    /* 注册字符设备驱动程序
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为LED_MAJOR的设备文件时,就会调用s3c24xx_leds_fops中的相关成员函数
     * LED_MAJOR可以设为0,表示由内核自动分配主设备号
     */
    ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }
 
    /*创建proc接口*/
/*struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent)
这是最直接,包装最少的创建方法。
参数 name 是要创建的 proc 文件名。mode 是该文件权限值,例如 S_IRUGO,可传入0表示采用系统默认值?
parent 指定该文件的上层 proc 目录项,如果为 NULL,表示创建在 /proc 根目录下。
create_proc_entry() 完成的任务主要包括:检测 mode 值,分配 proc_dir_entry 结构,注册 proc_dir_entry*/

    entry = create_proc_entry(DEVICE_NAME, S_IFREG | S_IRUGO, NULL);
    if (!entry)
        return -ENOMEM;

    entry->proc_fops = &s3c24xx_leds_proc_ops;
   
    printk(DEVICE_NAME " initialized\n");
    PDEBUG("LEDS INIT\n");
 return 0;
}

/*
 * 执行”rmmod s3c24xx_leds.ko”命令时就会调用这个函数
 */
static void __exit s3c24xx_leds_exit(void)
{
   remove_proc_entry(DEVICE_NAME, NULL);  //与create_proc_entry函数相反
   /* 卸载驱动程序 */
   unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("StephenYee, Shenzhen Office, Farsight Inc.,");             // 驱动程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");   // 一些描述信息
MODULE_LICENSE("GPL");                              // 遵循的协议

 

 

应用程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define IOCTL_LED_ON    0
#define IOCTL_LED_OFF   1

void usage(char *exename)
{
    printf("Usage:\n");
    printf("    %s <led_no> <on/off>\n", exename);
    printf("    led_no = 1, 2, 3 or 4\n");
}

int main(int argc, char **argv)
{
    unsigned int led_no;
    int fd = -1;
   
    if (argc != 3)
        goto err;
       
    fd = open("/dev/leds", 0);  // 打开设备
    if (fd < 0) {
        printf("Can't open /dev/leds\n");
        return -1;
    }
   
    led_no = strtoul(argv[1], 0, 0) - 1;    // 操作哪个LED?
    if (led_no > 3)
        goto err;
   
    if (!strcmp(argv[2], "on")) {
        ioctl(fd, IOCTL_LED_ON, led_no);    // 点亮它
    } else if (!strcmp(argv[2], "off")) {
        ioctl(fd, IOCTL_LED_OFF, led_no);   // 熄灭它
    } else {
        goto err;
    }
   
    close(fd);
    return 0;
   
err:
    if (fd > 0)
        close(fd);
    usage(argv[0]);
    return -1;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值