N32905音视频学习笔记-驱动调试工具devmem2

目录

准备工作:

devmem2简介

2.devmem2应用程序分析

3.编译

4.烧录运行

本例程将在鸭嘴兽wifi音视频开发板上学习驱动调试工具devmem2的源码及使用方法。

我们都知道Linux是一款安全性很高的系统,驱动层和用户层有不同的地址空间,应用程序是不能直接访问底层硬件的。但是有一个特例就是devmem,我们利用它可以对物理内存直接读写操作,如查看CPU寄存器的值,这给驱动开发带来了极大的便利。

准备工作:

wifi音视频开发板一块

  1. devmem2简介

在Linux开发中常用的调试工具不是很多,devmem2是驱动开发人员常用的调试工具,能够在应用层能够侦测内存地址中的数据变化,以此来检测驱动中对内存或者相关配置的正确性验证。

驱动开发devmem2使用方法:

devmem2 { address } [ type [ data ] ]

address : 物理地址

type :要访问的数据类型 : [b]yte, [h]alfword, [w]ord

data :想要写入的数据,若为读取操作则省略此参数,若为写入,则必须含有此参数。

 

2.devmem2应用程序分析

devmem2源代码在\duckbill\N32905\applications\devmem2\devmem2.c,源码如下:

/*
 * devmem2.c: Simple program to read/write from/to any location in memory.
 *
 *  Copyright (C) 2000, Jan-Derk Bakker ([email protected])
 *
 *
 * This software has been developed for the LART computing board
 * (http://www.lart.tudelft.nl/). The development has been sponsored by
 * the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
 * and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
 * projects.
 *
 * The author can be reached at:
 *
 *  Jan-Derk Bakker
 *  Information and Communication Theory Group
 *  Faculty of Information Technology and Systems
 *  Delft University of Technology
 *  P.O. Box 5031
 *  2600 GA Delft
 *  The Netherlands
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
  
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
 
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

int main(int argc, char **argv) {
    int fd;
    void *map_base, *virt_addr; 
	unsigned long read_result, writeval;
	off_t target;
	int access_type = 'w';
	
	//小于2个参数,则打印devmen2的usage
	if(argc < 2) {
		fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
			"\taddress : memory address to act upon\n"
			"\ttype    : access operation type : [b]yte, [h]alfword, [w]ord\n"
			"\tdata    : data to be written\n\n",
			argv[0]);
		exit(1);
	}
	target = strtoul(argv[1], 0, 0); //获取访问内存的物理地址

	if(argc > 2)
		access_type = tolower(argv[2][0]);   //获取数据类型

	//打开设备节点/dev/mem
    if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
    printf("/dev/mem opened.\n"); 
    fflush(stdout);
    
	//将内核空间映射到用户空间
    /* Map one page */
    map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
    if(map_base == (void *) -1) FATAL;
    printf("Memory mapped at address %p.\n", map_base); 
    fflush(stdout);
    
    virt_addr = map_base + (target & MAP_MASK);  //计算出虚拟地址
	//根据不同的参数获取不同类型的内存数据
    switch(access_type) {
		case 'b':
			read_result = *((unsigned char *) virt_addr);	//读8位的数据
			break;
		case 'h':
			read_result = *((unsigned short *) virt_addr);  //读16位的数据
			break;
		case 'w':
			read_result = *((unsigned long *) virt_addr);	//读32位的数据
			break;
		default:
			fprintf(stderr, "Illegal data type '%c'.\n", access_type);
			exit(2);
	}
    printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result); 
    fflush(stdout);

	//若参数个数大于3,则表示写入操作
	if(argc > 3) {
		writeval = strtoul(argv[3], 0, 0);  //获取写入的值
		switch(access_type) {
			case 'b':
				*((unsigned char *) virt_addr) = writeval;
				read_result = *((unsigned char *) virt_addr);
				break;
			case 'h':
				*((unsigned short *) virt_addr) = writeval;
				read_result = *((unsigned short *) virt_addr);
				break;
			case 'w':
				*((unsigned long *) virt_addr) = writeval;
				read_result = *((unsigned long *) virt_addr);
				break;
		}
		printf("Written 0x%X; readback 0x%X\n", writeval, read_result); 
		fflush(stdout);
	}
	
	if(munmap(map_base, MAP_SIZE) == -1) FATAL;
    close(fd);
    return 0;
}

其实这个工具的源码也比较简单,就是应用程序通过mmap函数将物理内存映射到用户空间虚拟地址,用户层就可以实现对内存的某个地址读写操作了。

3.编译

在ubuntu下切换路径至/duckbill/N32905/applications/devmem2。

执行make,编译生成可执行文件devmem2,devmem2将会自动拷贝至例程文件系统目录 /duckbill/N32905/usr/TEST_mini905/mkFilesys下。

执行例程文件系统TEST_mini905/test_mini905/目录下的脚本mkjffs2.sh,生成我们所需的jffs2文件系统。

4.烧录运行

WIFI-Mini905开发板与电脑之间连接好usb电源线(也充当下载线)、usb转串口线,将拨码开关S1拨向Rec位,按下自锁开关K1,开发板通电,N32905进入烧录模式。

使用TurboWriter依次烧录 开发板光盘资料\Mini905光盘资料\BIN\基础例程下的loader(SpiLoader_905.bin)、内核(Kernel.bin)以及刚刚生成的文件系统(TEST_mini905.jffs2.summary),烧录步骤与例程1一致。

烧录完成后将拨码开关S1拨向Nor位,开发板重新通电,电源指示灯亮,N32905进入正常启动模式,等待系统运行起来。

以下是直接往内存的物理地址0x1直接读写操作。

①读操作

在串口超级终端输入./devmem2 0x1 w,从物理内存地址0x1读出32位的数据为0x1000000。

②写操作

输入./devmem2 0x1 w 0x0,往物理内存地址0x1写0x0。

③读操作

输入./devmem2 0x1 w,从物理内存地址0x1读出的数据为0x0,由此可知,第2步的写操作是正确的。

 

猜你喜欢

转载自blog.csdn.net/chenzhe805/article/details/81708359