mini2440驱动学习第二课————LED

经过上一篇的hello world 驱动,熟悉了驱动程序大体的框架,这次第二课的驱动,轮到LED了!要注意它是怎么操作GPIO的,又是怎么和用户层(应用层)联系起来的。附上代码:

 

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>


#define DEVICE_NAME "leds"                     //驱动名字

 

static unsigned long led_table [] =

{                      //设置相应GPIO端口到一个数组里,方便做for循环
    S3C2410_GPB(5),
    S3C2410_GPB(6),
    S3C2410_GPB(7),
    S3C2410_GPB(8),
};

 

static unsigned int led_cfg_table [] =

{                     //GPIO口输出模式
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
};

 

static int sbc2440_leds_ioctl(struct inode *inode,  struct file *file,  unsigned int cmd,  unsigned long arg)

{                     //对GPIO实施操作的函数
    switch(cmd)

    {
        case 0:
        case 1:  if (arg > 4)

                     {
                          return -EINVAL;
                     }
                     s3c2410_gpio_setpin(led_table[arg], !cmd);        //在led_table[arg]管脚上输出(!cmd)
                     return 0;                                                               //其中cmd控制LED开关(0、1), arg控

                                                                                                  //制第几个LED(0、1、2、3)       

         default:  return -EINVAL;
     }
}

 

static struct file_operations dev_fops =

{                     //把驱动程序里的函数(功能)与应用层联系起来

                       //常用的还有open、close、read、write等等

                       //应用层能够用到的函数,都在这里给出
    .owner = THIS_MODULE,
    .ioctl = sbc2440_leds_ioctl,
};

 

static struct miscdevice misc =

{                     //杂项设备,一种主编号使用10的字符设备
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &dev_fops,
};

 

static int __init dev_init(void)
{
    int ret;

    int i;
 
    for (i = 0; i < 4; i++)

   {
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);          //设置GPIO功能
        s3c2410_gpio_setpin(led_table[i], 0);                                //初始化输出为低电平
   }

    ret = misc_register(&misc);                              //misc_register就是用主标号10调用register_chrdev()

                                                                            //而register_chrdev()是一个字符设备注册函数

    printk (DEVICE_NAME"/tinitialized/n");

    return ret;
}

 

static void __exit dev_exit(void)
{
    misc_deregister(&misc);                                   //同上
}

 

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");                                    //所遵守协议信息
MODULE_AUTHOR("FriendlyARM Inc.");                 //代码作者信息

 

注意:

1.static struct file_operations dev_fops = {.........}

   这个很关键,在高层应用程序上调用的驱动函数,都必须在这里定义好。

 

2.ioctl()函数 (本点出自http://hi.baidu.com/760159/blog/item/75c225f3dea26d19b17ec525.html

   static int sbc2440_leds_ioctl(struct inode *inode, struct file *file,   unsigned int cmd, unsigned long arg)

   ioctl函数是文件结构中的一个属性分量。ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率、马达的转速等等。
     struct inode *inode,是设备节点号。fd就是用户程序打开设备时使用open函数返回的文件标示符,cmd就是用户程序对设备的控制命令,unsigned long arg是控制命令的个数。
      驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数控制设备的I/O通道。如果函数返回一个非负值,那么该值会被返回给调用程序,表示成功。韩式一般通过switch{case}对设备的一些特性进行控制。switch{case}结构,每一个case对应一个命令码,做出一些相应的操作。在本例中的cmd有两个可选项0和1.0表示灯灭,1表示灯亮。所以case 0,1都要进行操作。由于实际的硬件连接中,是低电平灯亮。所以在对引脚赋值时要取反。 s3c2410_gpio_setpin(led_table[arg], !cmd)

 

3.设备函数注册(在__init 中)与卸载(在__exit 中)一般成对出现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值