backlight misc驱动范例 及应用程序范例
生活随笔
收集整理的這篇文章主要介紹了
backlight misc驱动范例 及应用程序范例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.編寫背光驅動。文件名為my2440_backlight.c
(1)
#include?<linux/errno.h>#include?<linux/kernel.h>
#include?<linux/module.h>
#include?<linux/slab.h>
#include?<linux/input.h>
#include?<linux/init.h>
#include?<linux/serio.h>
#include?<linux/delay.h>
#include?<linux/clk.h>
#include?<linux/miscdevice.h>
#include?<asm/io.h>
#include?<asm/irq.h>
#include?<asm/uaccess.h>
#include?<mach/regs-clock.h>
#include?<plat/regs-timer.h>
#include?<mach/regs-gpio.h>
#include?<linux/cdev.h>
#define?DEVICE_NAME?"backlight"?//設備名稱
#define?DEVICE_MINOR?5?//次設備號,這里我們將設備注冊為misc設備,這種設備的主設備號都為10
static?int?my2440_backlight_ioctl(struct?inode?*inode,?
??????????????????????????????????struct?file?*file,?
??????????????????????????????????unsigned?int?cmd,?
??????????????????????????????????unsigned?long?arg)
{
????switch(cmd)
????{
????????case?0:
????????????//當接收的命令為0時,就將GPG4引腳設為低電平,關閉背光
????????????s3c2410_gpio_setpin(S3C2410_GPG4,?0);?
????????????printk(DEVICE_NAME?"?turn?off!\n");
????????????return?0;
????????case?1:
????????????//當接收的命令為1時,就將GPG4引腳設為高電平,開啟背光
????????????s3c2410_gpio_setpin(S3C2410_GPG4,?1);?
????????????printk(DEVICE_NAME?"?turn?on!\n");
????????????return?0;
????????default:
????????????return?-EINVAL;
????}
}
static?struct?file_operations?dev_fops?=?
{
????.owner?=?THIS_MODULE,
????.ioctl?=?my2440_backlight_ioctl,?//這里只使用控制IO口的方式來控制背光
};
static?struct?miscdevice?misc?=
{
????.minor?=?DEVICE_MINOR,
????.name?=?DEVICE_NAME,
????.fops?=?&dev_fops,
};
static?int?__init?dev_init(void)
{
????int?ret;
????ret?=?misc_register(&misc);?//注冊成misc設備
????if(ret?<?0)
????{
????????printk("Register?misc?device?fiald!");
????????return?ret;
????}
????//將GPG4口配置成輸出口
????s3c2410_gpio_cfgpin(S3C2410_GPG4,?S3C2410_GPG4_OUTP);?
????return?ret;
}
static?void?__exit?dev_exit(void)
{
????misc_deregister(&misc);?//注銷該misc設備
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Huang?Gang");
MODULE_DESCRIPTION("Backlight?control?for?my2440");
?
(2)制作Makefile,內容為:obj-m := my2440_backlight.o
(3)make -C /root/2410kernels/linux-2.6.25.20_v1.0.4/ M=$(pwd) modules
2.背光應用程序:
?
#include?<stdio.h>#include?<stdlib.h>
#include?<fcntl.h>
#include?<sys/ioctl.h>
int?main(int?argc,?char?**argv)
{
????int?turn;
????int?fd;
????
????//檢測命令后面帶的參數
????if(argc?==?1?||?argc?>?2)
????{
????????printf("Usage:?backlight?on|off!\n");
????????exit(1);
????}
????
????//打開背光設備
????fd?=?open("/dev/backlight",?O_RDWR);
????
????if(fd?<?0)
????{
????????printf("Open?Backlight?Device?Faild!\n");
????????exit(1);
????}
????
????//判斷輸入的參數
????if(strcmp(argv[1],?"on")?==?0)
????{
????????turn?=?1;
????}
????else?if(strcmp(argv[1],?"off")?==?0)
????{
????????turn?=?0;
????}
????else
????{
????????printf("Usage:?backlight?on|off!\n");
????????exit(1);
????}
????
????//進行IO控制
????ioctl(fd,?turn);
????//關閉背光設備
????close(fd);
????return?0;
}
?
編譯:arm-linux-gcc -o backlight_test backlight_test.c
?
總結
以上是生活随笔為你收集整理的backlight misc驱动范例 及应用程序范例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux驱动学习笔记之并发控制
- 下一篇: 嵌入式逻辑分析仪SignalTap II