作者:李浩
硬件平台:OMAP-L138
软件平台:linux2.6.32
自己编写的一个简单的字符驱动,具体的功能实现没有写出:
fpga driver:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/compiler.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/device.h>
//******************************************************************************************
MODULE_AUTHOR("LiHao <[email protected]>");
MODULE_DESCRIPTION("FPGA Driver");
MODULE_LICENSE("GPL");
#define DEVICE_NAME "FPGA"
#define FPGA_MAJOR 231
static struct cdev cdev_fpga;
struct class *fpga_class;
//******************************************************************************************
static int FPGA_open(struct inode *inode,struct file *filp);
static ssize_t FPGA_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos);
static ssize_t FPGA_write(struct file *filp,char __user *buf,size_t count,loff_t *f_pos);
static int FPGA_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
static int FPGA_release(struct inode *inode,struct file *filp);
//******************************************************************************************
static struct file_operations FPGA_fops = {
.owner = THIS_MODULE,
.open = FPGA_open,
.read = FPGA_read,
.write = FPGA_write,
.ioctl = FPGA_ioctl,
.release = FPGA_release,
};
static int __init fpga_init(void)
{
int ret;
dev_t devno;
printk("fpga_init start.../n");
devno = MKDEV(FPGA_MAJOR,0);
ret = register_chrdev_region(devno,1,DEVICE_NAME);
if(ret < 0)
{
printk("<>fpga_init::register_chrdev_region error!/n");
return ret;
}
cdev_init(&cdev_fpga,&FPGA_fops);
cdev_fpga.owner = THIS_MODULE;
ret = cdev_add(&cdev_fpga,devno,1);
if(ret)
{
printk("<>fpga_init::can not add fpga device!/n");
return ret;
}
fpga_class = class_create(THIS_MODULE,"fpga_class");
if(IS_ERR(fpga_class))
{
printk("<>fpga_init::class_create error!/n");
return -1;
}
device_create(fpga_class,NULL,MKDEV(FPGA_MAJOR,0),NULL,DEVICE_NAME);
printk("fpga_init end.../n");
return 0;
}
static void __exit fpga_exit(void)
{
printk("fpga_exit start..../n");
cdev_del(&cdev_fpga);
unregister_chrdev_region(MKDEV(FPGA_MAJOR,0),1);
device_destroy(fpga_class,MKDEV(FPGA_MAJOR,0));
class_destroy(fpga_class);
printk("fpga_exit..../n");
}
static int FPGA_open(struct inode *inode,struct file *filp)
{
printk("FPGA_open start../n");
printk("FPGA_open end../n");
return 0;
}
static ssize_t FPGA_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
{
printk("FPGA_read start../n");
printk("FPGA_read end../n");
return 0;
}
static ssize_t FPGA_write(struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
{
printk("FPGA_write start../n");
printk("FPGA_write end../n");
return 0;
}
static int FPGA_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
printk("FPGA_ioctl start../n");
switch(cmd)
{
case 0:
break;
case 1:
break;
}
printk("FPGA_ioctl end../n");
return 0;
}
static int FPGA_release(struct inode *inode,struct file *filp)
{
printk("FPGA_release start../n");
printk("FPGA_release end../n");
return 0;
}
module_init(fpga_init);
module_exit(fpga_exit);
**********************************************************************************************
**********************************************************************************************
应用程序调用:
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <termios.h>
#include <time.h>
int main(int argc,char **argv)
{
int fpga;
int file_len=10;
unsigned char read_buf[512]={0};
unsigned char recv_buf[512]={0};
fpga = open("/dev/FPGA",O_RDWR,S_IRUSR | S_IWUSR);
if(fpga > 0)
{
read(fpga,read_buf,file_len);
write(fpga,recv_buf,file_len);
close(fpga);
return 0;
}
printf("open fpga error!/n");
return 1;
}