您的位置 首页 IOT

字符设备驱动-LED试验

驱动源码:includeincludeincludeincludeincludeincludeincludeincludeincludeincludeintmajor;staticstructclas

驱动源码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int major;
static struct class *leddrv_class;
static struct class_device *leddrv_class_dev;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int led_drv_open(struct inode *inode, struct file *file)
{
//printk(“first_drv_open\n”);
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
}
static ssize_t led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
char val = 0;
copy_from_user(&val,buf,count);
if(val == 1)
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
else if(val == 0)
*gpfdat |= ((1<<4) | (1<<5) | (1<<6));
//printk(“first_drv_write\n”);
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_dev = class_device_create(leddrv_class, NULL, MKDEV(major, 0), NULL, “xxx”);
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void led_drv_exit(void)
{
unregister_chrdev(major, “led_drv”); // 卸载
class_device_unregister(leddrv_class_dev);
class_destroy(leddrv_class);
iounmap(gpfcon);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE(“GPL”);
==================================================================================================
测验程序:
#include
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open(“/dev/xxx”,O_RDWR);
if(fd < 0)
{
printf(“cant open!\n”);
}
if(argc != 2)
{
printf(“Usage:\n”);
printf(“%s “,argv[0]);
return 0;
}
if(strcmp(argv[1],”on”) == 0)
val = 1;
else
val = 0;
write(fd,&val,4);
return 0;
}
==================================================================================================
注:在调用write(fd,&val,4);时依据不同的val值在调用驱动程序时做出判别:封闭、翻开led灯
copy_from_user(&val,buf,count); //从用户空间buf复制count长度的数据到内核空间val中
copy_to_user(buf,&val,,count); //从内核空间val复制count长度的数据到用户空间buf中
内核空间不能直接拜访物理地址,故要映射:
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部