snifer

[原创]AD22151霍尔元件使用实践

0
阅读(3266)

霍尔元件又叫干簧门磁。我用磁铁靠近霍尔开关,观察开关变化,同时模块上的 LED 灯会有开关状态显示,这个功能在很多地方都有用处,下面我写一下这个的使用过程。

$ cd hall/

 ls

 cd driver/

 ls

需要修改 Makfile 编译规则文件,在其中指定用户自己的内核源文件目录(用linux2.6.24内核,并保证该内核源码解压后至少编译过一次,才能正确编译内核驱动程序)
通过修改宏KERNELDIR ?变量来指定内核源码目录:使用make命令编译红外驱动程序
make  
ls
hall_switch.c  hall_switch.mod.c  hall_switch.o 
Module.symvers
hall_switch.ko  hall_switch.mod.o  Makefile
[sprife@localhost driver]$  
 
当前目录下生成驱动程序hall_switch.ko

编译应用程序.此时当前目录生成测试文件hall_switch_test

附驱动程序:#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

 

#define DEVICE_NAME     "hall_switch"  
#define hall_MAJOR    236       
struct hall_irq_desc {
    int irq;
    unsigned long flags;
    char *name;
};
static struct hall_irq_desc hall_irqs [] = {
    {IRQ_EINT4, IRQF_TRIGGER_RISING, "reed"},
    {IRQ_EINT6, IRQF_TRIGGER_RISING, "hall"},
};
static volatile int press_cnt [] = {0,0};
static DECLARE_WAIT_QUEUE_HEAD(hall_waitq);
static volatile int ev_press = 0;

static irqreturn_t hall_interrupt(int irq, void *dev_id)
{
    volatile int *press_cnt = (volatile int *)dev_id;
   
    *press_cnt = *press_cnt + 1;
    ev_press = 1;               
    wake_up_interruptible(&hall_waitq);
   
    return IRQ_RETVAL(IRQ_HANDLED);
}
static int hall_open(struct inode *inode, struct file *file)
{
    int i;
    int err;
   
    for (i = 0; i < sizeof(hall_irqs)/sizeof(hall_irqs[0]); i++) {
        err = request_irq(hall_irqs[i].irq, hall_interrupt, hall_irqs[i].flags, hall_irqs[i].name, (void *)&press_cnt[i]);
        if (err)
            break;
    }
    if (err) {
        i--;
        for (; i >= 0; i--)
            free_irq(hall_irqs[i].irq, (void *)&press_cnt[i]);
        return -EBUSY;
    }
   
    return 0;
}
static int hall_close(struct inode *inode, struct file *file)
{
    int i;
   
    for (i = 0; i < sizeof(hall_irqs)/sizeof(hall_irqs[0]); i++) {
        free_irq(hall_irqs[i].irq, (void *)&press_cnt[i]);
    }
    return 0;
}
static int hall_read(struct file *filp, char __user *buff,
                                         size_t count, loff_t *offp)
{
    unsigned long err;
   
    wait_event_interruptible(hall_waitq, ev_press);
    ev_press = 0;
    err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
    memset((void *)press_cnt, 0, sizeof(press_cnt));
    return err ? -EFAULT : 0;
}
static struct file_operations hall_fops = {
    .owner   =   THIS_MODULE,  
    .open    =   hall_open,
    .release =   hall_close,
    .read    =   hall_read,
};
static int __init hall_init(void)
{
    int ret;
    ret = register_chrdev(hall_MAJOR, DEVICE_NAME, &s3c24xx_hall_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }
   
    printk(DEVICE_NAME " initialized\n");
    return 0;
}
static void __exit hall_exit(void)
{
    unregister_chrdev(hall_MAJOR, DEVICE_NAME);
}
module_init(s3c24xx_hall_init);
module_exit(s3c24xx_hall_exit);
MODULE_AUTHOR("UP-TECH");            
MODULE_DESCRIPTION("UP-MAGIC HALL_SWITCH Driver");  
MODULE_LICENSE("GPL");      
                       
收获很大,最近很辛苦,公司催的要命啊