平台:FL2440
内核版本linux 2.6.28
效果:没有按键时,程序进入休眠,每5秒打印超时信息。按键时响应中断,并输出是按下还是松开。
转载注明:http://blog.csdn.net/bbs598598/article/details/7742052
#include <linux/module.h> /*模块有关的*/
#include <linux/kernel.h>/*内核有关的*/#include <linux/fs.h>/*文件系统有关的*/
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>/*linux中断*/
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h> //copy_to_user
#include <mach/regs-gpio.h>/*寄存器设置*/S3C2410_GPF0等的定义
#include <mach/hardware.h>//s3c2410_gpio_getpin等的定义
#include <mach/irqs.h> //IRQ_EINT0等的定义
#include <asm/system.h>
static struct class_device*thirddrv_class_dev;
volatile unsigned long *gpfdat;//IO口数据寄存器
static unsigned char keyval;判断所用键值,返回用户空间,1、2、3、4为按下,81、82、83、84为松开
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);向内核初始化机构体button_waitq,read休眠用static volatile int ev_press=0;判断是否唤醒进程的标志参数
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};引脚相关结构体
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 0x04},
};引脚相关结构体,FL2440按键IO实例化
static irqreturn_t buttons_irq(int irq,void *dev_id)//按键中断服务函数
{
struct pin_desc *pindesc=(struct pin_desc*)dev_id;//引脚相关结构体参数传入,中断request_irq内核函数传入
unsigned int pinval;//按键引脚状态
pinval = s3c2410_gpio_getpin(pindesc->pin);获得按键引脚电平
if (pinval)//根据高低电平返回不同的键值
{
keyval=0x80|pindesc->key_val;
}
else
{
keyval=pindesc->key_val;
}
ev_press=1;进程唤醒参数
wake_up_interruptible(&button_waitq);唤醒睡眠的read进程
return IRQ_HANDLED;
}
static int third_drv_open(struct inode *inode, struct file *file)打开驱动设备文件
{
*gpfcon &= ~((0x2<<(0*2)) | (0x2<<(2*2))| (0x2<<(3*2))| (0x2<<(4*2)));设置按键引脚为中断状态
request_irq(IRQ_EINT0,buttons_irq,IRQF_TRIGGER_RISING,"s2",&pins_desc[0]);linux驱动中断设置函数,十分重要,设置中断号,中断引脚,中断服务程序,及中断触发方式,名称。需要查看按键原理图,0、2设置为上升沿触发,3、4设置为上升沿下降沿都触发。
request_irq(IRQ_EINT2,buttons_irq,IRQF_TRIGGER_RISING,"s3",&pins_desc[1]);
request_irq(IRQ_EINT3,buttons_irq,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"s4",&pins_desc[2]);
request_irq(IRQ_EINT4,buttons_irq,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"s5",&pins_desc[3]);
return 0;
}
ssize_t third_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)//读按键状态
{
if(size!=1)//只读一个状态,传入的
{
return -EINVAL;
}
wait_event_interruptible(button_waitq,ev_press);如果ev_press为0,则进程进入休眠状态copy_to_user(buf, &keyval, 1);当中断发生时,ev_press=1,唤醒进程。将按键键值返回用户空间
ev_press=0;设置参数归0
return sizeof(keyval);
}
static unsigned int third_drv_poll(struct file *file, poll_table *wait)//引入poll机制,类似select
{
unsigned int mask=0;
poll_wait(file,&button_waitq,wait);
//将进程挂载到等待队列中,此处不休眠,根据mask返回值决定休眠否(在schedule——timeout中)
if(ev_press)mask |= POLLIN|POLLRDNORM;中断发生,ev_press为1,设置mask为非零。唤醒休眠
return mask;
}
int third_drv_close(struct inode *inode, struct file *file)//关闭中断,释放中断引脚资源
{
free_irq(IRQ_EINT0,&pins_desc[0]);
free_irq(IRQ_EINT2,&pins_desc[0]);
free_irq(IRQ_EINT3,&pins_desc[0]);
free_irq(IRQ_EINT4,&pins_desc[0]);
return 0;
}
static struct file_operations sencod_drv_fops = {
.owner = THIS_MODULE,
.open = third_drv_open,
.read =third_drv_read,
.poll = third_drv_poll,
.release= third_drv_close,
};
int major;
static int third_drv_init(void)//向内核注册模块
{
major = register_chrdev(0, "third_drv", &sencod_drv_fops);
thirddrv_class = class_create(THIS_MODULE, "third_drv");
thirddrv_class_dev = device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */创建设备
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);//IO口引脚物理地址转为虚拟地址
gpfdat = gpfcon + 1;
return 0;
}
static void third_drv_exit(void)//从内核卸载模块
{
unregister_chrdev(major, "third_drv");
device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");
测试程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
/* thirddrvtest
*/
int main(int argc, char **argv)
{
int fd,ret;
unsigned char key_val;
struct pollfd fds[1];
fd = open("/dev/buttons", O_RDWR);打开设备
if (fd < 0)
{
printf("can't open!\n");
}
fds[0].fd=fd;
fds[0].events=POLLIN;
while (1)
{
ret=poll(fds,1,5000);//设置5S超时响应
if(ret ==0)
{
printf("time out for 5s .......\n");
}
else
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
}
return 0;
}
百度谷歌一起搜,我见过最好用的Chrome搜索插件。特此隆重推荐~
谷歌百度一键搜索
在Baidu里面搜Google,在Google里面搜百度!不用切换,松松搞定~~
有图有真相:
插件安装地址:http://t.cn/zWUv7wX
插件安装地址:http://t.cn/zWUv7wX
源码地址:https://github.com/raywill/BaiGoogleDu
在windowsphone7开发中,设置ListPicker的默认选中item需要注意一个问题,SelectedItems是int类型的,如果传入了例如long类型的数据,会导致设置失败。
今天因为没留意这个问题,导致了浪费时间在这方面代码检查。
直接上代码:
private void SetSelectedItems(List<long> selectedItems) { List<int> _selecteditems = new List<int>(); foreach (var item in selectedItems) { int dayOfWeek = (int)item; _selecteditems.Add(dayOfWeek); } DayOfWeeksLP.SelectedItems = _selecteditems; // DayOfWeeksLP.SelectedItems = selectedItems; // 这样设置是无效的 }