LED驱动程序---字符设备控制技术

系统调用函数

大部分驱动程序除了需要提供读写设备的能力外,还需要具备控制设备的能力。比如:改变波特率。

在用户空间,使用* ioctl系统调用 *来控制设备,原型如下:

int ioctl(int fd,unsigned long cmd,...);
//fd : 要控制的设备文件描述符。
//cmd : 发送给设备的控制命令。
//… : 第3个参数是可选的参数,存在与否是依赖于控
制命令(第 2 个参数 )。

应当注意系统的版本:
这里写图片描述

定义控制命令

命令从其实质而言就是一个整数, 但为了让这个整数具备更好的可读性,我们通常会把这个整数分为几个段:类型(8位),序号,参数传送方向,参数长度。

1、Type(类型/幻数):表明这是属于哪个设备的命令。
2、Number( )序号 :用来区分同一设备的不同命令
3、Direction:参数传送的方向,可能的值是 _ IOC_ NONE (没有数据传输),_ IOC_ READ,_ IOC _WRITE (向设备写入参数)。
4、Size: 参数长度

Linux系统提供了下面的宏来帮助定义命令:
_IO(type,nr):不带参数的命令。
_IOR(type,nr,datatype):从设备中读参数的命令。
_IOW(type,nr,datatype):向设备写入参数的命令。
例:

#define MEM_MAGIC ‘m’ //定义幻数
#define MEM_SET _IOW(MEM_MAGIC, 0, int)

unlocked_ioctl 函数的实现通常是根据命令执行的一个switch语句。但是,当命令号不能匹配任何一个设备所支持的命令时,返回 -EINVAL。
编程模型:

switch (cmd)
{
    case 命令A:
    //执行A对应的操作
    case 命令B:
    //执行B对应的操作
    default:
    // return -EINVAL
}

LED控制驱动程序

驱动程序:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <mach/gpio-bank-k.h>
#include "led.h"

#define LEDCON 0x7f008800
#define LEDDAT 0x7f008808
unsigned int *led_config; 
unsigned int *led_data; 

struct cdev cdev;
dev_t devno;

int led_open(struct inode *node, struct file *filp)
{
    led_config = ioremap(LEDCON,4);
    writel(0x11110000,led_config);

    led_data = ioremap(LEDDAT,4);

    return 0;
}

long led_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    switch (cmd)
    {
        case LED_ON:
            writel(0x00,led_data);
            return 0;

        case LED_OFF:
            writel(0xff,led_data);
            return 0;

        default:
            return -EINVAL;
    }
}

//指定初始化方式
static struct file_operations led_fops =
{
    .open = led_open,
    .unlocked_ioctl = led_ioctl,
};

static int led_init()
{
    cdev_init(&cdev,&led_fops);

    alloc_chrdev_region(&devno, 0 , 1 , "myled");
    cdev_add(&cdev, devno, 1);

    return 0;   
}

static void led_exit()
{
    cdev_del(&cdev);
    unregister_chrdev_region(devno,1);
}

module_init(led_init);
module_exit(led_exit);

应用程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "led.h"

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

     if (argc <2 )
     {
         printf("please enter the second para!\n");
         return 0;  
     }

     cmd = atoi(argv[1]); 

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

     if (cmd == 1)
         ioctl(fd,LED_ON);
     else
         ioctl(fd,LED_OFF); 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值