1. 程式人生 > >字元裝置驅動(五)按鍵優化休眠

字元裝置驅動(五)按鍵優化休眠

目錄


title: 字元裝置驅動(五)按鍵優化
tags: linux
date: 2018-11-23 17:56:57
toc: true
---

字元裝置驅動(五)按鍵優化

按鍵值讀取

Linux內部有系統函式s3c2410_gpio_getpin能夠讀取GPIO的值

unsigned int s3c2410_gpio_getpin(unsigned int pin)
{
    void __iomem *base = S3C24XX_GPIO_BASE(pin);
    unsigned long offs = S3C2410_GPIO_OFFSET(pin);

    return __raw_readl(base + 0x04) & (1<< offs);
}

在中斷處理函式中有個引數是dev_id,這個是由request_irq初始化的,處理函式可以用這個結構體傳遞引數

static irqreturn_t buttons_irq(int irq, void *dev_id) 

也就是說一箇中斷綁定了一個dev_id,可用作傳遞引數,解除安裝中斷函式

request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
free_irq(IRQ_EINT0, &pins_desc[0]);

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
    struct pin_desc * pindesc = (struct pin_desc *)dev_id;
    unsigned int pinval;
    
    pinval = s3c2410_gpio_getpin(pindesc->pin); //這裡就讀取到了
}

休眠讀取

程式設計

程式設計目的:App去讀取按鍵值,如果有按鍵中斷觸發(鍵值有改變)則列印,否則休眠.

read(key)
    if change 
        printf
    else
        sleep

也就是說App函式依然不變,但是需要修改驅動的讀取函式,驅動的讀取函式中有休眠的動作

// app.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int fd;
    unsigned char key_val;
    fd = open("/dev/xyz0", O_RDWR);
    if (fd < 0)
    {
        printf("can't open!\n");
    }
    while (1)
    {
        read(fd, &key_val, 1);
        printf("key_val = 0x%x\n", key_val);
    }
    return 0;
}

驅動程式中需要設計休眠,中斷髮生來喚醒首先定義一個等待佇列,下述是一個巨集

//生成一個等待佇列頭wait_queue_head_t,名字為name
DECLARE_WAIT_QUEUE_HEAD(name) 

// 定義一個名為`button_waitq`的佇列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

休眠函式如下,condition=0才休眠,定義在include/linux/wait.h

#define wait_event_interruptible(wq, condition)             \
({                                  \
    int __ret = 0;                          \
    if (!(condition))                       \
        __wait_event_interruptible(wq, condition, __ret);   \
    __ret;                              \
})

喚醒也是一個巨集,引數是等待佇列,放置在中斷函式處理中,定義在include/linux/wait.h

wake_up_interruptible(&button_waitq);   /* 喚醒休眠的程序 */

mark

完整程式碼如下

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
//#include <linux/interrupt.h>

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

static struct class *drv_class;
static struct class_device  *drv_class_dev;

// 定義一個名為`button_waitq`的佇列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
// flag=1 means irq happened and need to update
int flag=0;

struct pin_desc{
    unsigned int pin;
    unsigned int key_val;
};

/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 */
/* 鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;

struct pin_desc pins_desc[4] = {
    {S3C2410_GPF0, 0x01},
    {S3C2410_GPF2, 0x02},
    {S3C2410_GPG3, 0x03},
    {S3C2410_GPG11, 0x04},
};

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
    printk("irq%d\r\n",irq);

    struct pin_desc * pindesc = (struct pin_desc *)dev_id;
    unsigned int pinval;
    
    pinval = s3c2410_gpio_getpin(pindesc->pin);

    if (pinval)
    {
        /* 鬆開 */
        key_val = 0x80 | pindesc->key_val;
    }
    else
    {
        /* 按下 */
        key_val = pindesc->key_val;
    }

    wake_up_interruptible(&button_waitq);   /* 喚醒休眠的程序 */
    flag=1;

    return IRQ_RETVAL(IRQ_HANDLED);
}

static int drv_open(struct inode *inode, struct file *file)
{
    /* 配置GPF0,2為輸入引腳 */
    /* 配置GPG3,11為輸入引腳 */
    request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
    request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
    request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
    request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);   
    return 0;
}

int drv_close(struct inode *inode, struct file *file)
{
    free_irq(IRQ_EINT0, &pins_desc[0]);
    free_irq(IRQ_EINT2, &pins_desc[1]);
    free_irq(IRQ_EINT11,&pins_desc[2]);
    free_irq(IRQ_EINT19,&pins_desc[3]);
    return 0;
}


static ssize_t drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    //int minor =  MINOR(file->f_dentry->d_inode->i_rdev);
    //printk("drv_write=%d\n",minor);
    return 0;
}

static ssize_t drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
    if (size != 1)
    return -EINVAL;

    /* 如果沒有按鍵動作, 休眠 */
    wait_event_interruptible(button_waitq, flag);

    /* 如果有按鍵動作, 返回鍵值 */
    copy_to_user(buf, &key_val, 1);
    flag = 0;
    
    return 1;
}


static struct file_operations drv_fops = {
    .owner  =   THIS_MODULE,    /* 這是一個巨集,推向編譯模組時自動建立的__this_module變數 */
    .open   =   drv_open,     
    .write  =   drv_write,
    .read   =   drv_read,    
    .release =  drv_close,  
};

static int major;
static int drv_init(void)
{
    int minor=0;
    major=register_chrdev(0, "drv", &drv_fops); // 註冊, 告訴核心
    drv_class = class_create(THIS_MODULE, "drv");
    drv_class_dev = class_device_create(drv_class, NULL, MKDEV(major, 0), NULL, "xyz%d", minor);

    gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    gpfdat = gpfcon + 1;
    gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
    gpgdat = gpgcon + 1;
    return 0;
}

static void drv_exit(void)
{
    unregister_chrdev(major, "drv"); // 解除安裝
    class_device_unregister(drv_class_dev);
    class_destroy(drv_class);
    iounmap(gpfcon);
    iounmap(gpgcon);
}

module_init(drv_init);
module_exit(drv_exit);
MODULE_AUTHOR("xxx");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");
MODULE_LICENSE("GPL");

測試

測試執行./text /dev/xyz0 & 後臺執行

# ./test /dev/xyz0 &
# irq55
key_val = 0x3
irq55
key_val = 0x83
irq18
key_val = 0x2
irq18
key_val = 0x82
irq16
key_val = 0x1
irq16
key_val = 0x81
irq63
key_val = 0x4
irq63
key_val = 0x84

使用top檢視佔用

Load average: 0.00 0.01 0.00
  PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND
  782   770 0        R     3096   5%   0% top
  770     1 0        S     3096   5%   0% -sh
  781   770 0        S     1312   2%   0% ./test /dev/xyz0

使用ps檢視任務為S狀態 休眠狀態

# ps
  PID  Uid        VSZ Stat Command
    1 0          3092 S   init
  781 0          1312 S   ./test /dev/xyz0
  783 0          3096 R   ps