您的位置 首页 系统

字符设备驱动-使用次设备号完成多路LED操控

驱动源码:includeincludeintmajor;intminor=0;staticstructclass*leddrv_class;staticstructclass_device*leddr

驱动源码:

#include
.
.
#include
int major;
int minor = 0;
static struct class *leddrv_class;
static struct class_device *leddrv_class_devs[4];
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_drv_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
switch(minor)
{
case 0:
{
// 装备3引脚为输出
//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
*gpfcon &= ~(0x3<<(4*2));
*gpfcon |= (1<<(4*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
*gpfcon &= ~(0x3<<(5*2));
*gpfcon |= (1<<(5*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
*gpfcon &= ~(0x3<<(6*2));
*gpfcon |= (1<<(6*2));
// 都输出0
//s3c2410_gpio_setpin(S3C2410_GPF4, 0);
*gpfdat &= ~(1<<4);
//s3c2410_gpio_setpin(S3C2410_GPF5, 0);
*gpfdat &= ~(1<<5);
//s3c2410_gpio_setpin(S3C2410_GPF6, 0);
*gpfdat &= ~(1<<6);
break;
}
case 1:
{
s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF4, 0);
break;
}
case 2:
{
s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5, 0);
break;
}
case 3:
{
s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF6, 0);
break;
}
}
return 0;
}
static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
char val;
int minor = MINOR(file->f_dentry->d_inode->i_rdev);
copy_from_user(&val, buf, 1);
switch (minor)
{
case 0:
{
s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));
break;
}
case 1:
{
s3c2410_gpio_setpin(S3C2410_GPF4, val);
break;
}
case 2:
{
s3c2410_gpio_setpin(S3C2410_GPF5, val);
break;
}
case 3:
{
s3c2410_gpio_setpin(S3C2410_GPF6, val);
break;
}
}
return 0;
}
static struct file_operations led_drv_fops = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int led_drv_init(void)
{
major = register_chrdev(0, “led_drv”, &led_drv_fops); // 注册字符驱动, 告知内核
leddrv_class = class_create(THIS_MODULE, “leddrv”); //创建造备节点
leddrv_class_devs[0] = class_device_create(leddrv_class, NULL, MKDEV(major, 0), NULL, “leddrv”);
for (minor = 1; minor < 4; minor++)
{
leddrv_class_devs[minor] = class_device_create(leddrv_class, NULL, MKDEV(major, minor), NULL, “leddrv%d”, minor);
if (unlikely(IS_ERR(leddrv_class_devs[minor])))
return PTR_ERR(leddrv_class_devs[minor]);
}
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void led_drv_exit(void)
{
unregister_chrdev(major, “led_drv”); // 卸载
for (minor = 0; minor < 4; minor++)
{
class_device_unregister(leddrv_class_devs[minor]);
}
class_destroy(leddrv_class);
iounmap(gpfcon);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE(“GPL”);
==================================================================================================
测验程序:
#include
#include
#include
#include
void print_usage(char *file)
{
printf(“Usage:\n”);
printf(“%s \n”,file);
printf(“eg. \n”);
printf(“%s /dev/leds on\n”, file);
printf(“%s /dev/leds off\n”, file);
printf(“%s /dev/led1 on\n”, file);
printf(“%s /dev/led1 off\n”, file);
}
int main(int argc, char **argv)
{
int fd;
char* filename;
char val;
if (argc != 3)
{
print_usage(argv[0]);
return 0;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf(“error, cant open %s\n”, filename);
return 0;
}
if (!strcmp(“on”, argv[2]))
{
// 亮灯
val = 0;
write(fd, &val, 1);
}
else if (!strcmp(“off”, argv[2]))
{
// 灭灯
val = 1;
write(fd, &val, 1);
}
else
{
print_usage(argv[0]);
return 0;
}
}
=================================================================================================
试验:
ls -l /dev/leddrv
ls -l /dev/leddrv1
./leddrvTest /dev/leddrv on
./leddrvTest /dev/leddrv off
./leddrvTest /dev/leddrv1 on
./leddrvTest /dev/leddrv2 on
./leddrvTest /dev/leddrv2 off
./leddrvTest /dev/leddrv3 on
./leddrvTest /dev/leddrv3 off
运转程序时输入不同的/dev/leddrv、/dev/leddrv1、/dev/leddrv2、/dev/leddrv3得到的次设备号不同,驱动程序中依据不同的次设备号操控不同的灯,输入不同的on、off指令write时写入的值不同,依据不同的值履行开关动作。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/263367.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部