资源简介
基于海思平台Hi3559AV00的韦根输入项目源码。包含C源代码,Makefile以及编译说明。
推荐使用WGNET转换器进行测试。
推荐使用WGNET转换器进行测试。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#define WIEGAND_MAJOR 250
#define TIMER_DELAY HZ/8
#define DEVICE_NAME “wiegand_in“
#define GPIO12_4 (12 * 8 + 4)
#define GPIO12_2 (12 * 8 + 2)
#define WG_DATA0 GPIO12_4
#define WG_DATA1 GPIO12_2
static int wiegand_dev_major = WIEGAND_MAJOR;
static bool dev_open_flag = false;
static bool overflow_flag = false;
struct wiegand_dev
{
char wiegand[34]; //wiegand-34
unsigned int data; //actual data
unsigned int count; //global counter
struct cdev cdev;
struct semaphore sem;
struct completion receive_completion;
struct timer_list wiegand_timer;
struct work_struct pen_event_work;
struct workqueue_struct *ts_workqueue;
int gpio_d0;
int gpio_d1;
int d0_irq;
int d1_irq;
};
struct wiegand_dev *wiegand_in_devp = NULL;
//使能中断
static void enable_irqs(void)
{
enable_irq(wiegand_in_devp->d0_irq);
enable_irq(wiegand_in_devp->d1_irq);
}
//禁止中断
static void disable_irqs(void)
{
disable_irq(wiegand_in_devp->d0_irq);
disable_irq(wiegand_in_devp->d1_irq);
}
static char convert_data(void)
{
int i;
int even = 0;
int odd = 0;
//偶校验
for(i=1; i<=16; i++)
{
if(wiegand_in_devp->wiegand[i] == 1)
even++;
}
//偶校验不通过的情况
//偶校验:当实际数据中1的个数为偶数的时候,这个校验位就是0,否则这个校验位就是1
if(even%2 != wiegand_in_devp->wiegand[0])
{
wiegand_in_devp->count = 0;
goto error;
}
//奇校验
for(i=17; i<=32; i++)
{
if(wiegand_in_devp->wiegand[i] == 1)
odd++;
}
//奇校验不通过的情况
//奇校验:当实际数据中1的个数为偶数的时候,这个校验位就是1,否则这个校验位就是0
if(odd%2 == wiegand_in_devp->wiegand[33])
{
wiegand_in_devp->count = 0;
goto error;
}
//奇偶校验通过,将韦根转换为实际数据
wiegand_in_devp->data = 0;
for(i=1; i<=32; i++)
{
wiegand_in_devp->data <<= 1;
wiegand_in_devp->data |= wiegand_in_devp->wiegand[i];
}
wiegand_in_devp->count = 0;
printk(“data is: %#x\n“ wiegand_in_devp->data);
return 0;
error:
printk(“parity efficacy error\n“);
return -1;
}
static void wiegand_do_timer(unsigned long arg)
{
int i = 0;
//虽然超时时间已到达,但还要等待维根数据传送完毕才进行转换
//wait_for_completion(&(wiegand_in_devp->receive_completion));
printk(“%d\n“ wiegand_in_devp->count);
for(i=0; icount; i++)
printk(“%d “ wiegand_in_devp->wiegand[i]);
printk(“\n“);
//如果不是数据位不是34(由于中断中有限制,实际上不会超过,只能小于34),则返回全F代表出错
if(wiegand_in_devp->count != 34)
{
wiegand_in_devp->count = 0;
wiegand_in_devp->data = 0xFFFFFFFF; //全F代表出错
up(&wiegand_in_devp->sem);
overflow_flag = false;
return;
}
//关闭外部中断,防止wiegand_data在转换期间发生变化
disable_irqs();
//如果转换错误,则返回全F代表出错
if(convert_data() != 0)
{
wiegand_in_devp->data = 0xFFFFFFFF; //全F代表出错
}
overflow_flag = false;
up(&wiegand_in_devp->sem);
//恢复中断
enable_irqs();
}
stat
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2253 2020-02-27 14:45 wiegand_in\drv\Makefile
文件 381 2020-02-27 14:48 wiegand_in\drv\readme.txt
文件 10702 2020-03-02 17:14 wiegand_in\drv\wiegand_in.c
目录 0 2020-03-02 17:46 wiegand_in\drv
目录 0 2020-02-27 14:45 wiegand_in
----------- --------- ---------- ----- ----
13336 5
- 上一篇:用QT做的LINUX下串口通信测试可用
- 下一篇:标准的CAD电气块图,经典
评论
共有 条评论