1、
使用例子: https://blog.csdn.net/maizaozao/article/details/32712123
AXI定义了一个寄存器32位
rd 0-4
rst 5-8
rfull 9-12
rempty 13-16
data1-data8 17-20
#include<linux/init.h>
#include<linux/platform_device.h>
#include<linux/module.h>
#include<linux/miscdevice.h>
#include<linux/ioport.h>
#include<linux/of.h>
#include<linux/fs.h>
#include<asm/io.h>
#include <linux/ioctl.h>
#include<linux/slab.h>
#include<asm/uaccess.h>
#define DEVICE_NAME "ADS1278"
#define MY_GPIO_PHY_ADDR1 0x43c00000
#define MY_GPIO_PHY_ADDR2 0x43c10000
#define MY_GPIO_REG_NUM 12 //寄存器编号
#define MY_GPIO_REG_WIDTH 32
#define CTL_MAGIC 'k'
#define reset _IOR(CTL_MAGIC, 0, unsigned long)
#define rempty _IOR(CTL_MAGIC, 1, unsigned long)
#define rfull _IOR(CTL_MAGIC, 2, unsigned long)
static void __iomem *GPIO_Regs1;
static void __iomem *GPIO_Regs2;
static int my_gpio_open(struct inode *inode,struct file *filp)
{
return 0;
}
static int my_gpio_release(struct inode *inode, struct file *filp)
{
return 0;
}
static int my_gpio_read(struct file *filp,char __user *buffer,size_t count,loff_t *offset)
{
int ret;
char kbuff[48];
char empty;
int j;
empty = ioread8(GPIO_Regs1+12);
while(empty ==1)
{
empty = ioread8(GPIO_Regs1+12);
}
//read ADS1278_1
iowrite32(0x00000001,GPIO_Regs1+0);
for(j = 0;j<8;j++)
{
kbuff[j*3+2] = ioread8(GPIO_Regs1+16+j*4+0);
kbuff[j*3+1] = ioread8(GPIO_Regs1+16+j*4+1);
kbuff[j*3+0] = ioread8(GPIO_Regs1+16+j*4+2);
}
iowrite32(0x00000000,GPIO_Regs1+0);
//read ADS1278_2
iowrite32(0x00000001,GPIO_Regs2+0);
for(j = 0;j<8;j++)
{
kbuff[24+j*3+2] = ioread8(GPIO_Regs2+16+j*4+0);
kbuff[24+j*3+1] = ioread8(GPIO_Regs2+16+j*4+1);
kbuff[24+j*3+0] = ioread8(GPIO_Regs2+16+j*4+2);
}
iowrite32(0x00000000,GPIO_Regs2+0);
ret=copy_to_user(buffer,(char *)&kbuff,48);
if(ret!=0)
{
printk("read_data_dev:[ERROR]fail to copy data to user space!");
return -1;
}
return 48;
}
static int my_gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int full;
int empty;
if(cmd==reset)
{
iowrite32(0x00000001, GPIO_Regs1+4);
iowrite32(0x00000001, GPIO_Regs2+4);
iowrite32(0x00000000, GPIO_Regs1+4);
iowrite32(0x00000000, GPIO_Regs2+4);
}
if(cmd == rfull)
{
full = ioread8(GPIO_Regs1+8)|ioread8(GPIO_Regs2+8);
return full;
}
if(cmd == rempty)
{
empty = ioread8(GPIO_Regs1+12);
return empty;
}
return -1;
}
static const struct file_operations my_gpio_fops =
{
.owner = THIS_MODULE,
.open = my_gpio_open,
.release = my_gpio_release,
.read = my_gpio_read,
.unlocked_ioctl = my_gpio_ioctl,
};
static struct miscdevice fifo_dev =
{
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &my_gpio_fops,
};
int __init fifo_init(void)
{
int ret;
fifo_Regs1 = ioremap(MY_GPIO_PHY_ADDR1,0xFFFF);
fifo_Regs2 = ioremap(MY_GPIO_PHY_ADDR2,0xFFFF);
if(fifo_Regs1==NULL||fifo_Regs1==NULL)
{
printk("read_data_dev:[ERROR] Access address is NULL!\n");
return -EIO;
}
ret = misc_register(&fifo_dev);
if(ret)
{
printk("read_data_dev:[ERROR] Misc device register failed\n");
return ret;
}
printk("read_data_dev: Tata! Module init complete\n");
return 0;
}
void __exit my_gpio_exit(void)
{
iounmap(GPIO_Regs1);
iounmap(GPIO_Regs2);
misc_deregister(&my_gpio_dev);
printk("read_data_dev: Module exit\n");
}
module_init(my_gpio_init);
module_exit(my_gpio_exit);
MODULE_AUTHOR("Peng Liu");
MODULE_ALIAS("my_gpio");
MODULE_DESCRIPTION("zedboard read_data_dev module");
MODULE_LICENSE("GPL");