一个linux上注册的led类打印设备及操作方法.docx

上传人:小飞机 文档编号:1938851 上传时间:2022-12-27 格式:DOCX 页数:8 大小:240.94KB
返回 下载 相关 举报
一个linux上注册的led类打印设备及操作方法.docx_第1页
第1页 / 共8页
一个linux上注册的led类打印设备及操作方法.docx_第2页
第2页 / 共8页
一个linux上注册的led类打印设备及操作方法.docx_第3页
第3页 / 共8页
一个linux上注册的led类打印设备及操作方法.docx_第4页
第4页 / 共8页
一个linux上注册的led类打印设备及操作方法.docx_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《一个linux上注册的led类打印设备及操作方法.docx》由会员分享,可在线阅读,更多相关《一个linux上注册的led类打印设备及操作方法.docx(8页珍藏版)》请在三一办公上搜索。

1、一个在PC上注册的led类打印设备及操作方法/*leds_w380.c*/#include #include #include #include #include #include #include #include /* * Blue led. * (it can only be blue flashing led) */static void w380_blueled_set(struct led_classdev *led_dev, enum led_brightness value)/s3c_gpio_cfgpin(S5PV210_GPJ3(4), S3C_GPIO_SFN(2);/s

2、3c_gpio_setpull(S5PV210_GPJ3(4), 0);switch (value) case LED_HALF:/s3c_gpio_setpin(S5PV210_GPJ3(4), 1);printk(w380leds_onn); break;case LED_FULL:/s3c_gpio_setpin(S5PV210_GPJ3(4), 1);printk(w380leds_onn); break;default:case LED_OFF:/s3c_gpio_setpin(S5PV210_GPJ3(4), 0);printk(w380leds_offn); break;/*st

3、atic ssize_t w380_blue_show(struct device *dev, struct device_attribute *attr, char *buf)struct led_classdev *bled =container_of(dev,struct led_classdev,dev );/w380_blueled_set(bled,1);return sprintf(buf, %un, 0);static ssize_t w380_blue_store(struct device *dev, struct device_attribute *attr, const

4、 char *buf, size_t count)struct led_classdev *bled =container_of(dev,struct led_classdev,dev );w380_blueled_set(bled,255);return 0;static DEVICE_ATTR(blue, 0644, w380_blue_show, w380_blue_store);static struct attribute *w380led_attributes = &dev_attr_blue.attr,NULL;static struct attribute_group w380

5、led_attribute_group = .attrs = w380led_attributes;static struct led_classdev w380_blueled = .name= w380:blue,.brightness_set= w380_blueled_set,;*/ static int w380leds_probe(struct platform_device *pdev)int ret;printk(w380leds_proben); ret = led_classdev_register(&pdev-dev, &w380_blueled);if (ret)got

6、o err_blue;printk(w380leds_probe_device_create_filen); /ret = device_create_file(&pdev-dev, &dev_attr_blue);/ret = sysfs_create_group(&pdev-dev.kobj, &w380led_attribute_group);printk(w380leds_probe_device_create_fileOOOOOn); if (ret != 0)dev_err(&pdev-dev,No source control for LED: %dn, ret);return

7、0;err_blue:dev_err(&pdev-dev, led_classdev_register failedn);printk(w380leds_probefailedn); led_classdev_unregister(&w380_blueled);static int w380leds_remove(struct platform_device *pdev)led_classdev_unregister(&w380_blueled);return 0;static struct platform_driver w380leds_driver = .driver= .name= w

8、380_leds,.owner= THIS_MODULE,.probe= w380leds_probe,.remove= w380leds_remove,;static struct platform_device w380leds_device = .name= w380_leds,;static int _init w380leds_init(void)printk(w380leds_initn); printk(w380leds_initn); int ret;ret=platform_driver_register(&w380leds_driver);if (!ret) printk(

9、platform_device_registern); ret = platform_device_register(&w380leds_device);if (ret)printk(platform_driver_unregistern); platform_driver_unregister(&w380leds_driver);return ret;static void _exit w380leds_exit(void)printk(w380leds_exitn); platform_device_register(&w380leds_device);platform_driver_un

10、register(&w380leds_driver);module_init(w380leds_init);module_exit(w380leds_exit);MODULE_AUTHOR(wang tao );MODULE_DESCRIPTION(LED driver for the w380);MODULE_LICENSE(GPL);MODULE_ALIAS(platform:w380_leds);其Makefile文件如下:注意命令的开头是一个tabifneq ($(KERNELRELEASE),)#kbuild syntax. dependency relationshsip of f

11、iles and target modules are listed here.mymodule-objs := leds_w380.oobj-m := leds_w380.o elsePWD := $(shell pwd)KVER ?= $(shell uname -r)KDIR := /lib/modules/$(KVER)/buildall:$(MAKE) -C $(KDIR) M=$(PWD)clean:rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versionsendif步骤一:编译,make一下生成leds_w380.o和leds_w380.ko等步骤二

12、:复制,将leds_w380.ko复制到/lib/module/2.6.32-21-generic/kernel/drivers/leds下步骤三:加载sudo modprobe leds_w380 Sudo rmmod leds_w380.ko Sudo insmod leds_w380.ko这样反复的目的是利用modprobe的依赖关系。步骤四:成功验证,Sudo lsmod以下是测试代码:/*ledtest.c*/#include #include #include /#include /#include #define DEVNAME /sys/class/leds/w380:blue

13、/brightness/*需要注意的是这个宏可以针对操作不同的设备而修改*/static int write_int(char const* path, int value) int fd; static int already_warned = 0; fd = open(path, O_RDWR); if (fd = 0) char buffer20; int bytes = sprintf(buffer, %dn, value); printf(buffer:%sn, buffer); int amt = write(fd, buffer, bytes); close(fd); retur

14、n amt = -1 ? 1: 0; else if (already_warned = 0) printf(write_int failed to open %sn, path); already_warned = 1; return -1; int main(int argc, char *argv) int turn; /检测命令后面带的参数 if(argc = 1 | argc 2) printf(Usage: Led on|off!n); exit(1); /判断输入的参数 if(strcmp(argv1, on) = 0) turn = 255; printf(Usage: Led on!n); else if(strcmp(argv1, off) = 0) turn = 0; printf(Usage: Led off!n); else printf(Usage: Led on|off!n); exit(1); write_int(DEVNAME,turn); return 0;进入ledtest目录,编译生成ledtest,执行./ ledtest on执行dmesg查看结果:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号