DMA 驱动:简单实例

    xiaoxiao2025-08-01  16

    1. 简介:

    1)dma 直接内存访问。

    2)DMA控制器操作的内存区域要求是物理地址连续的,因此需要使用内核中提供的特定函数进行内存申请。

    3)把源地址和目的地址告诉芯片内部相关寄存器,根据 DMA 中断是不是发生来判断是不是操作完。

    char *src = dma_alloc_writecombine(NULL, BUF_SIZE, &src_phys, GFP_KERNEL);

    函数返回虚拟地址,第三个参数返回物理地址。

    用户操作的时候使用 虚拟地址。

    传给控制器,控制器操作的是 物理地址

    4)2440 有四个 DMA 通道。下表表示的是触发 DMA 的源

    5)DMA 的两种模式,从图上可以看到 Demand Mode 是在 XnXDREQ 一直是低的时候,XnXDACK一直进行数据的读写,而 Handshake Mode 是 XnXDREQ 变低触发一次读写。

    6)内存是在 AHB总线上, 串口/i2s 等是在外设总线上。

    7)寄存器中

    DSZ:读写数据的size:1,2,4 byte

    TSZ:单词传输:R/W 一次

            burst传输:R/W 四次

    TC:传输次数

    总的长度 = TC X TSZ(1/4) X DSZ

    8)DMA 通道的选择,需要查看 /proc/interrupts 中是不是已经用了。

     

    2. 驱动例子:

    dma.c  --- 基于linux3.4.112

    #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/irq.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <mach/regs-gpio.h> #include <mach/hardware.h> #include <linux/poll.h> #include <linux/dma-mapping.h> #include <linux/wait.h> #include <linux/irqreturn.h> #include <linux/interrupt.h> #include <linux/sched.h> #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 struct s3c_dma_regs { unsigned long disrc; unsigned long disrcc; unsigned long didst; unsigned long didstc; unsigned long dcon; unsigned long dstat; unsigned long dcsrc; unsigned long dcdst; unsigned long dmasktrig; }; static int major = 0; static char *src; static u32 src_phys; static char *dst; static u32 dst_phys; static struct class *cls; static volatile struct s3c_dma_regs *dma_regs; static DECLARE_WAIT_QUEUE_HEAD(dma_waitq); /* 中断事件标志, 中断服务程序将它置1,ioctl将它清0 */ static volatile int ev_dma = 0; static long s3c_dma_ioctl(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; i < BUF_SIZE; i++) dst[i] = src[i]; if (memcmp(src, dst, BUF_SIZE) == 0) { printk("MEM_CPY_NO_DMA OK\n"); } else { printk("MEM_CPY_DMA ERROR\n"); } break; } case MEM_CPY_DMA : { ev_dma = 0; /* 把源,目的,长度告诉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); /* 如何知道DMA什么时候完成? */ /* 休眠 */ wait_event_interruptible(dma_waitq, ev_dma); if (memcmp(src, dst, BUF_SIZE) == 0) { printk("MEM_CPY_DMA OK\n"); } else { printk("MEM_CPY_DMA ERROR\n"); } break; } } return 0; } static struct file_operations dma_fops = { .owner = THIS_MODULE, .unlocked_ioctl = s3c_dma_ioctl, }; static irqreturn_t s3c_dma_irq(int irq, void *devid) { /* 唤醒 */ ev_dma = 1; wake_up_interruptible(&dma_waitq); /* 唤醒休眠的进程 */ return IRQ_HANDLED; } static int s3c_dma_init(void) { /* IRQ_DMA3表示第3个通道(从0开始) */ if (request_irq(IRQ_DMA3, s3c_dma_irq, 0, "s3c_dma", NULL)) { printk("can't request_irq for DMA\n"); return -EBUSY; } /* 分配SRC, DST对应的缓冲区 */ src = dma_alloc_writecombine(NULL, BUF_SIZE, &src_phys, GFP_KERNEL); if (NULL == src) { printk("can't alloc buffer for src\n"); free_irq(IRQ_DMA3, NULL); return -ENOMEM; } dst = dma_alloc_writecombine(NULL, BUF_SIZE, &dst_phys, GFP_KERNEL); if (NULL == dst) { free_irq(IRQ_DMA3, NULL); dma_free_writecombine(NULL, BUF_SIZE, src, src_phys); printk("can't alloc buffer for dst\n"); return -ENOMEM; } major = register_chrdev(0, "s3c_dma", &dma_fops); /* 为了自动创建设备节点 */ cls = class_create(THIS_MODULE, "s3c_dma"); device_create(cls, NULL, MKDEV(major, 0), NULL, "dma"); /* /dev/dma */ dma_regs = ioremap(DMA3_BASE_ADDR, sizeof(struct s3c_dma_regs)); return 0; } static void s3c_dma_exit(void) { iounmap(dma_regs); device_destroy(cls, MKDEV(major, 0)); class_destroy(cls); 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, NULL); } module_init(s3c_dma_init); module_exit(s3c_dma_exit); MODULE_LICENSE("GPL");

    Makefile

    KERN_DIR = ~/wor_lip/linux-3.4.112 all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order obj-m += dma.o

    解释: 无,,

    3. 用户层操作代码:

    dma_test.c

    #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ioctl.h> #include <string.h> /* ./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 <nodma | dma>\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; }

    编译此函数

    arm-none-linux-gnueabi-gcc -march=armv4t dma_test.c -o dma_test

    将生成的文件拷贝到开发板上,开发板上运行,看运行结果。---- 这里的前提是加载了上边的 dma 驱动。

    ./dma_test dma &

    top

    ./dma_test nodma &

    top

    最新回复(0)