Linux驱动开发十一:分层分离LED子系统
来源:互联网 发布:汇编数组 编辑:程序博客网 时间:2024/05/19 01:07
本例根据韦东山视频第二期的第14课
分层分离而来,用QT6410实践
本例为点亮LED:
最简单的led驱动就是从端口输出0或1来关闭或点亮灯。而我们这里讲的led子系统,主要是对led事件进行了分装和优化,这里我们主要讲的是可以实现跨平台的led驱动。不管你是使用三星的平台,还是Atmel的平台,你只要知道如何在你的BSP中添加平台数据,并且知道如何在应用程序中使用这个驱动,那么你就不用因为新的平台而再次编写led驱动。
代码如下:
led_dev.c
#include <linux/module.h>#include <linux/version.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/interrupt.h>#include <linux/list.h>#include <linux/timer.h>#include <linux/init.h>#include <linux/serial_core.h>#include <linux/platform_device.h>/* 分配/设置/注册一个platform_device */static struct resource led_resource[] = { [0] = { .start = 0x7F008820, .end = 0x7F008820 + 16 - 1, .flags = IORESOURCE_MEM, }, [1] = { .start = 0, .end = 0, .flags = IORESOURCE_IRQ, }};static void led_release(struct device * dev){}static struct platform_device led_dev = { .name = "myled", .id = -1, .num_resources = ARRAY_SIZE(led_resource), .resource = led_resource, .dev = { .release = led_release, },};static int led_dev_init(void){platform_device_register(&led_dev);return 0;}static void led_dev_exit(void){platform_device_unregister(&led_dev);}module_init(led_dev_init);module_exit(led_dev_exit);MODULE_LICENSE("GPL");
led_drv.c
/* 分配/设置/注册一个platform_driver */#include <linux/module.h>#include <linux/version.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/interrupt.h>#include <linux/irq.h>#include <linux/sched.h>#include <linux/pm.h>#include <linux/sysctl.h>#include <linux/proc_fs.h>#include <linux/delay.h>#include <linux/platform_device.h>#include <linux/input.h>#include <linux/irq.h>#include <asm/uaccess.h>#include <asm/io.h>static int major;static struct class *cls;static struct class_device *led_cls;static volatile unsigned long *gpio_con;static volatile unsigned long *gpio_dat;static int pin;static int led_open(struct inode *inode, struct file *file){//printk("first_drv_open\n");/* 配置为输出 */*gpio_con &= ~(0xf<<(pin*4));*gpio_con |= (0x1<<(pin*4));return 0;}static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){int val;//printk("first_drv_write\n");copy_from_user(&val, buf, count); //copy_to_user();if (val == 1){// 点灯*gpio_dat &= ~(1<<pin);}else{// 灭灯*gpio_dat |= (1<<pin);}return 0;}static struct file_operations led_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = led_open, .write=led_write, };static int led_probe(struct platform_device *pdev){struct resource*res;/* 根据platform_device的资源进行ioremap */res = platform_get_resource(pdev, IORESOURCE_MEM, 0);gpio_con = ioremap(res->start, res->end - res->start + 1);gpio_dat = gpio_con + 1;res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);pin = res->start;/* 注册字符设备驱动程序 */printk("led_probe, found led\n");major = register_chrdev(0, "myled", &led_fops);cls = class_create(THIS_MODULE, "myled");led_cls = device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */return 0;}static int led_remove(struct platform_device *pdev){/* 卸载字符设备驱动程序 *//* iounmap */printk("led_remove, remove led\n");device_unregister(led_cls);class_destroy(cls);unregister_chrdev(major, "myled");iounmap(gpio_con);return 0;}struct platform_driver led_drv = {.probe= led_probe,.remove= led_remove,.driver= {.name= "myled",}};static int led_drv_init(void){platform_driver_register(&led_drv);return 0;}static void led_drv_exit(void){platform_driver_unregister(&led_drv);}module_init(led_drv_init);module_exit(led_drv_exit);MODULE_LICENSE("GPL");
Makefile:
KERN_DIR = /work/work/linuxall:make -C $(KERN_DIR) M=`pwd` modules cp *.ko /work/nfsrootclean:make -C $(KERN_DIR) M=`pwd` modules cleanobj-m+= led_drv.oobj-m+= led_dev.o
led_test.c
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>/* led_test on * led_test off */int main(int argc, char **argv){int fd;int val = 1;fd = open("/dev/led", O_RDWR);if (fd < 0){printf("can't open!\n");}if (argc != 2){printf("Usage :\n");printf("%s <on|off>\n", argv[0]);return 0;}if (strcmp(argv[1], "on") == 0){val = 1;}else{val = 0;}write(fd, &val, 4);return 0;}
执行
./led_test on
时点亮第一盏灯;
执行
./led_test off
时熄灭第一盏灯;
0 0
- Linux驱动开发十一:分层分离LED子系统
- 《Linux驱动》分层分离
- Android Led驱动[Linux Led子系统]
- Linux 设备驱动开发思想 —— 驱动分层与驱动分离
- Linux 设备驱动开发思想 —— 驱动分层与驱动分离
- 嵌入式Linux设备驱动开发思想进阶之驱动分层与驱动分离
- linux设备驱动之led子系统<一>
- linux设备驱动之led子系统<二>
- linux 驱动之led子系统(一)
- Linux下的LED子系统驱动
- linux设备驱动之led子系统<一>
- linux驱动之分离分层的概念
- Linux设备驱动模型--分离分层思想
- Linux驱动之分离分层的概念
- led子系统实现led驱动
- 驱动分离分层
- 嵌入式--驱动分离分层
- Linux输入子系统(6):设备驱动层之LED驱动
- 程序员选择公司的8个标准
- IOS开发专题---转化解析JSON格式为字符格式(原生类库解析)
- 怎样用模拟登陆查询ou屏蔽的视图
- TCL语言(二)
- 微信硬件设备接入接口协议
- Linux驱动开发十一:分层分离LED子系统
- VS中监视窗口,即时窗口和输出窗口的使用
- 叠片过滤器:叠片式过滤器所具备的优势
- 一些关于机器学习的网站总结
- 蝶恋花 . 记得隔窗初见面
- Activity水平切换效果(overridePendingTransition)
- 微信硬件平台框架说明及接入流程
- sql的limit
- 黑马程序员——JAVA基础之 == 和equals区别