#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#define INT_DEV_NAME "int_dev"
#define INT_DEV_MAJOR 240
#define INT_WRITE_ADDR 0x378
#define INT_READ_ADDR 0x379
#define INT_CTRL_ADDR 0x37A
#define PRINT_IRQ 3
#define PRINT_IRQ_ENABLE_MASK 0x10
#define INT_BUFF_MAX 64
typedef struct {
unsigned long time;
}__attribute__ ((packed)) R_INT_INFO;
R_INT_INFO intbuffer [INT_BUFF_MAX];
int intcount = 0;
/*清除缓存*/
void int_clear (void)
{
int lp;
for (lp = 0; lp < INT_BUFF_MAX; lp++) {
intbuffer[lp].time = 0;
}
intcount = 0;
}
irqreturn_t int_interrupt (int irq, void *dev_id)
{
printk ("come in interrupt\n");
if (intcount < INT_BUFF_MAX) {
intbuffer[intcount].time = get_jiffies_64();
intcount++;
}
return IRQ_HANDLED;
}
int int_open (struct inode *inode, struct file *filp)
{
set_irq_type(PRINT_IRQ, IRQ_TYPE_LEVEL_LOW); /*设置中断为低电平触发*/
if (!request_irq (PRINT_IRQ, int_interrupt, IRQF_DISABLED, INT_DEV_NAME, NULL)) {
printk ("register ok\n");
outb (PRINT_IRQ_ENABLE_MASK, INT_CTRL_ADDR); /*使能IRQ控制*/
}
int_clear();
return 0;
}
ssize_t int_read (struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
int readcount;
char *ptrdata;
int loop;
readcount = count / sizeof (R_INT_INFO); /*读以sizeof(R_INT_INFO)为单位的内容个数 */
if (readcount > intcount) /*读取字节数超出则读取所有现存的数据*/
readcount = intcount;
ptrdata = (char *) &intbuffer[0];
for (loop = 0; loop < readcount * sizeof(R_INT_INFO); loop++) {
printk ("loop_time = %d\n", loop);
put_user (ptrdata[loop], (char *) &buf[loop]); /*把中断时读取的数据写往用户空间*/
}
return (readcount * sizeof(R_INT_INFO));
}
ssize_t int_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
unsigned char status;
int loop;
int_clear();
for (loop = 0; loop < count; loop++) {
get_user (status, (char *) buf);
outb (status, INT_WRITE_ADDR);
}
return count;
}
int int_release (struct inode *inode, struct file *filp)
{
outb (0x00, INT_CTRL_ADDR); /*禁止中断*/
free_irq (PRINT_IRQ, NULL); /*从内核中注销中断服务函数*/
return 0;
}
struct file_operations int_fops = {
.owner = THIS_MODULE,
.read = int_read,
.write = int_write,
.open = int_open,
.release = int_release,
};
int int_init (void)
{
int result;
result = register_chrdev (INT_DEV_MAJOR, INT_DEV_NAME, &int_fops);
if (result < 0) return result;
return 0;
}
void int_exit (void)
{
unregister_chrdev (INT_DEV_MAJOR, INT_DEV_NAME);
}
module_init (int_init);
module_exit (int_exit);
MODULE_LICENSE ("Dual BSD/GPL");
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_FILENAME "/dev/int_dev"
typedef struct {
unsigned long time;
}__attribute__ ((packed)) R_INT_INFO;
#define INT_BUFF_MAX 64
int main()
{
int dev;
R_INT_INFO intbuffer[INT_BUFF_MAX];
int intcount;
char buff[128];
int loop = 0;
dev = open (DEVICE_FILENAME, O_RDWR | O_NDELAY);
if (dev >= 0) {
printf ("start...\n");
buff[0] = 0x00;
write (dev, buff, 1); /*关闭LED*/
sleep(1);
buff[0] = 0xFF;
write (dev, buff, 1); /*打开LED*/
sleep (1);
buff[0] = 0x00;
write (dev, buff, 1); /*关闭LED*/
sleep (1);
buff[0] = 0xFF;
write (dev, buff, 1); /*打开LED*/
printf ("wait...input\n");
while (1) {
memset (intbuffer, 0, sizeof (intbuffer));
intcount = read (dev, (char *)&intbuffer[0], sizeof(R_INT_INFO)) / sizeof(R_INT_INFO);
if (intcount) {
while (intbuffer [loop].time != 0) {
printf ("intbuffer [%d] = %d\n", loop, intbuffer [loop]);
loop++;
}
break;
}
}
printf ("input ok...\n");
sleep (1);
memset (intbuffer, 0, sizeof(intbuffer));
printf ("read interrupt times\n");
intcount = read (dev, (char *)intbuffer, sizeof(intbuffer)) / sizeof(R_INT_INFO);
for (loop = 0; loop < intcount; loop++)
printf ("index = %d time = %ld\n", loop, intbuffer[loop].time);
printf ("led flashing...\n");
for (loop = 0; loop < 5; loop++) {
buff[0] = 0xFF;
write (dev, buff, 1);
sleep (1);
buff[0] = 0x00;
write (dev, buff, 1);
sleep (1);
}
close (dev);
} else {
printf ("open int_dev failed\n");
}
return 0;
}
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |