嵌入式linux驱动-DMA笔记

一、开发环境

1、内核:Linux 2.6.22.6;

2、JZ2440v3

3、ubuntu 9.10

二、实现使用dma把一块数据存储到另一块上。实现过程:

1.创建字符设备驱动。包括定义file_operations结构变量,注册字符设备register_chrdev,使用class_create、class_device_create自动创建设备节点。

2.关于DMA的设置。

(1)分配中断。如:request_irq(IRQ_DMA3, s3c_dma_irq, 0, "s3c_dma", 1)。编写中断函数。


(2)分配源、目的缓冲区。

/* 分配SRC, DST对应的缓冲区 */
src = dma_alloc_writecombine(NULL, BUF_SIZE, &src_phys, GFP_KERNEL);
dst = dma_alloc_writecombine(NULL, BUF_SIZE, &dst_phys, GFP_KERNEL);

(3)地址重映射。

dma_regs = ioremap(DMA3_BASE_ADDR, sizeof(struct s3c_dma_regs));

(4)设置DMA的寄存器。

如: /* 把源,目的,长度告诉DMA */
dma_regs->disrc      = src_phys;        /* 源的物理地址 */
dma_regs->disrcc     = (0<<1) | (0<<0); /* 源位于AHB总线, 源地址递增 */
dma_regs->didst      = dst_phys;        /* 目的的物理地址 */
dma_regs->didstc     = (0<<2) | (0<<1) | (0<<0); /* 目的位于AHB总线, 目的地址递增 */
dma_regs->dcon       = (1<<30)|(1<<29)|(0<<28)|(1<<27)|(0<<23)|(0<<20)|(BUF_SIZE<<0);  /* 使能中断,单个传输,软件触发, */
/* 启动DMA */
dma_regs->dmasktrig  = (1<<1) | (1<<0);

3.为实现使用dma把一块数据存储到另一块上。在启动DMA后,进入休眠。DMA传输完成会产生中断,再唤醒休眠。

三、完整程序

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MEM_CPY_NO_DMA 0
#define MEM_CPY_DMA 1
#define BUF_SIZE (512*1024)

#define DMA0_BASE_ADDR 0x4B000000
#define DMA1_BASE_ADDR 0x4B000040
#define DMA2_BASE_ADDR 0x4B000080
#define DMA3_BASE_ADDR 0x4B0000C0

static DECLARE_WAIT_QUEUE_HEAD(dma_wq);
static volatile int condition=0;

struct s3c_dma_regs{
	unsigned long disrc0;
	unsigned long disrcc0;
	unsigned long didst0;
	unsigned long didstc0;
	unsigned long dcon0;
	unsigned long dstat0;
	unsigned long dcsrc0;
	unsigned long dcdst0;
	unsigned long dmasktrig0;
};
static volatile struct s3c_dma_regs *dma_regs;

static char *src;
static u32  src_phys;

static char *dst;
static u32  dst_phys;

static int major=0;
static struct  class *class;

static int s3c_dma_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	int i;

	memset(src, 0xaa, BUF_SIZE);
	memset(dst, 0x55, BUF_SIZE);
	
	switch(cmd)
	{
		case MEM_CPY_NO_DMA: 
		{
			for(i=0;idisrc0=src_phys;
			dma_regs->disrcc0=(0<<1)|(0<<0);
			dma_regs->didst0 =dst_phys;
			dma_regs->didstc0=(0<<2)|(0<<1)|(0<<0);
			dma_regs->didstc0=(1<<29)|(0<<28)|(0<<23)|(0<<21)|(BUF_SIZE<<19);
			//启动DMA
			dma_regs->dmasktrig0=(1<<1)|(1<<0);
			//如何知道什么时候完成传输
			
			//休眠
			wait_event_interruptible(dma_wq, condition);
		
			if(memcmp(src, dst, BUF_SIZE)==0)
			{
				printk("MEM_CPY_DMA ok\n");
			}
			else
			{
				printk("MEM_CPY_DMA erro\n");
			}			
			break;
		}

	}
	return 0;
}


static struct file_operations dma_fops={
	.owner=THIS_MODULE,
	.ioctl  =s3c_dma_ioctl,

};



static irqreturn_t s3c_dma_irq(int irq, void *devid)
{
	condition=1;
	//唤醒
	wake_up_interruptible(&dma_wq);

	return IRQ_HANDLED;
}

static int s3c_dma_init(void)
{
	if(request_irq(IRQ_DMA3, s3c_dma_irq, 0,"s3c_dma",1))
		{
			free_irq(IRQ_DMA3, 1);
			printk("can't request irq for dma \n");
			return -ENOMEM;
	}

//分配缓冲区
	src=dma_alloc_writecombine(NULL,BUF_SIZE, &src_phys, GFP_KERNEL);
	if(src==NULL)
	{	
		dma_free_writecombine(NULL,BUF_SIZE, src,src_phys);
		printk("can't alloc buffer for src \n");
		return -ENOMEM;
	}
	dst=dma_alloc_writecombine(NULL,BUF_SIZE, &dst_phys, GFP_KERNEL); 
	if(dst==NULL)
	{	
		dma_free_writecombine(NULL,BUF_SIZE, dst,dst_phys);
		printk("can't alloc buffer for dst \n");
		return -ENOMEM;
	}

	major=register_chrdev(0,"s3c_dma",&dma_fops);
	class=class_create(THIS_MODULE, "sc3_dma");
	class_device_create(class,NULL,MKDEV(major, 0),NULL,"dma");

	dma_regs=ioremap(DMA3_BASE_ADDR, sizeof(struct s3c_dma_regs));
	
	return 0;
}

static void s3c_dma_exit(void)
{
	iounmap(dma_regs);
	class_device_destroy(class,MKDEV(major, 0));
	class_destroy(class);
	unregister_chrdev(major,"s3c_dma");
	dma_free_writecombine(NULL,BUF_SIZE, src,src_phys);
	dma_free_writecombine(NULL,BUF_SIZE, dst,dst_phys);
	free_irq(IRQ_DMA3, 1);
}

module_init(s3c_dma_init);
module_exit(s3c_dma_exit);

MODULE_LICENSE("GPL");


四、测试程序

#include 
#include 
#include 
#include 
#include 
#include 

/* ./dma_test nodma
 * ./dma_test dma
 */
#define MEM_CPY_NO_DMA  0
#define MEM_CPY_DMA     1

void print_usage(char *name)
{
	printf("Usage:\n");
	printf("%s \n", name);
}


int main(int argc, char **argv)
{
	int fd;
	
 	if (argc != 2)
	{
		print_usage(argv[0]);
		return -1;
	}

	fd = open("/dev/dma", O_RDWR);
	if (fd < 0)
	{
		printf("can't open /dev/dma\n");
		return -1;
	}

	if (strcmp(argv[1], "nodma") == 0)
	{
		while (1)
		{
			ioctl(fd, MEM_CPY_NO_DMA);
		}
	}
	else if (strcmp(argv[1], "dma") == 0)
	{
		while (1)
		{
			ioctl(fd, MEM_CPY_DMA);
		}
	}
	else
	{
		print_usage(argv[0]);
		return -1;
	}
	return 0; 	
}
五、结果

嵌入式linux驱动-DMA笔记_第1张图片





你可能感兴趣的:(嵌入式linux驱动)