LINUX内核下跑单片机按键,S3C2440下linux按键驱动编写及测试程序

module_init(dev_init);//模块初始化,仅当使用 insmod/podprobe 命令加载时有用,如果设备不是通过模块方式加载,此处将不会被调用

module_exit(dev_exit);//卸载模块,当该设备通过模块方式加载后,可以通过 rmmod 命令卸载,将调用此函数

MODULE_LICENSE("GPL");//版权信息

MODULE_AUTHOR("tang");//作者名字

测试程序btn_test.c

#include #include #include #include #include #include #include #include #include #include

int main(void)

{

int buttons_fd;

char buttons[6] = {'0', '0', '0', '0', '0', '0'}; //定义按键值变量,对于驱动函数中的 key_values 数组

/*打开按键设备/dev/buttons*/

buttons_fd = open("/dev/buttons", 0);

if (buttons_fd < 0) {

/*打开失败则退出*/

perror("open device buttons");

exit(1);

}

/*永读按键并打印键值和状态*/

for (;;) {

char current_buttons[6];

int count_of_changed_key;

int i;

/*使用 read 函数读取一组按键值(6 个)*/

if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {

perror("read buttons:");

exit(1);

}

/*逐个分析读取到的按键值*/

for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {

if (buttons[i] != current_buttons[i]) {

buttons[i] = current_buttons[i];

/*打印按键值,并标明按键按下/抬起的状态*/

printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" :

"down");

count_of_changed_key++;

}

}

if (count_of_changed_key) {

printf("\n");

}

}

/*关闭按键设备文件*/

close(buttons_fd);

return 0;

}

arm-linux-gcc -o buttons_test buttons_test.c

以下是成功加载范例jd2440_buttons.c,位于linux-2.x.xx/drivers/char下

cp -f jd2440_buttons.c /linux-2.x.xx/drivers/char //把驱动源码复制到内核驱动的字符设备下

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include

#define DEVICE_NAME     "buttons"

struct button_irq_desc {

int irq;

int pin;

int pin_setting;

int number;

char *name;

};

static struct button_irq_desc button_irqs [] = {

{IRQ_EINT11,S3C2410_GPG3,  S3C2410_GPG3_EINT11, 0, "KEY0"}, //key_right

{IRQ_EINT8, S3C2410_GPG0,  S3C2410_GPG0_EINT8,  1, "KEY1"}, //key_down

{IRQ_EINT3, S3C2410_GPF3,  S3C2410_GPF3_EINT3,  2, "KEY2"}, //key_enter

{IRQ_EINT9, S3C2410_GPG1,  S3C2410_GPG1_EINT9,  3, "KEY3"}, //key_left

{IRQ_EINT1, S3C2410_GPF1,  S3C2410_GPF1_EINT1,  4, "KEY4"}, //key_up

{IRQ_EINT5, S3C2410_GPF5,  S3C2410_GPF5_EINT5,  5, "KEY5"}, //usb wakeup

{       -1,           -1,                  -1,  6, "KEY6"},

};

static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;

static irqreturn_t buttons_interrupt(int irq, void *dev_id)

{

struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;

int down;

down = !s3c2410_gpio_getpin(button_irqs->pin);

if (down != (key_values[button_irqs->number] & 1)) {

key_values[button_irqs->number] = '0' + down;

ev_press = 1;

wake_up_interruptible(&button_waitq);

}

return IRQ_RETVAL(IRQ_HANDLED);

}

static int s3c24xx_buttons_open(struct inode *inode, struct file *file)

{

int i;

int err = 0;

for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {

if (button_irqs[i].irq < 0) {

continue;

}

err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH,

button_irqs[i].name, (void *)&button_irqs[i]);

if (err)

break;

}

if (err) {

i--;

for (; i >= 0; i--) {

if (button_irqs[i].irq < 0) {

continue;

}

disable_irq(button_irqs[i].irq);

free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);

}

return -EBUSY;

}

ev_press = 1;

return 0;

}

static int s3c24xx_buttons_close(struct inode *inode, struct file *file)

{

int i;

for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {

if (button_irqs[i].irq < 0) {

continue;

}

free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);

}

return 0;

}

static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)

{

unsigned long err;

if (!ev_press) {

if (filp->f_flags & O_NONBLOCK)

return -EAGAIN;

else

wait_event_interruptible(button_waitq, ev_press);

}

ev_press = 0;

err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));

return err ? -EFAULT : min(sizeof(key_values), count);

}

static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)

{

unsigned int mask = 0;

poll_wait(file, &button_waitq, wait);

if (ev_press)

mask |= POLLIN | POLLRDNORM;

return mask;

}

static struct file_operations dev_fops = {

.owner   =   THIS_MODULE,

.open    =   s3c24xx_buttons_open,

.release =   s3c24xx_buttons_close,

.read    =   s3c24xx_buttons_read,

.poll    =   s3c24xx_buttons_poll,

};

static struct miscdevice misc = {

.minor = MISC_DYNAMIC_MINOR,

.name = DEVICE_NAME,

.fops = &dev_fops,

};

static int __init dev_init(void)

{

int ret;

ret = misc_register(&misc);

printk (DEVICE_NAME"\tinitialized\n");

return ret;

}

static void __exit dev_exit(void)

{

misc_deregister(&misc);

}

module_init(dev_init);

module_exit(dev_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("modelsim");

c2c9ed493cd281aa86d8a6f5178c4c01.gif [1] [2] [3] 610626052e95c7fbe3d254abc769d9ad.gif

本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值