Linux驱动开发常用调试工具 devmem之代码分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leon_zeng0/article/details/83099809

本文先列出代码,然后分析,最后是使用情况介绍,命令控制led 点亮的方法。

在Linux开发中着实用的调试工具并不是很多。devmem的方式是提供给驱动开发人员,在应用层能够读写内存地址,外设地址。也有类似叫 devmem2的,功能类似。在我用的petalinux 2018.2 中,其名字是devmem。

这个代码是开源的,linux 的一部分。 我这里的原始代码来自 : https://github.com/hackndev/tools/blob/master/devmem2.c

我们可以借鉴这段代码开发我们linux 下的应用,也不再需要什么驱动开发了。我们可以在我们的系统下编译使用,一般linux包含这个程序,也就不用自己编译。但自己有特别需要,可以修改,编译使用这段代码。

/*
 * 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.
 *
 *
 * 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';
	
	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]);


    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);
			break;
		case 'h':
			read_result = *((unsigned short *) virt_addr);
			break;
		case 'w':
			read_result = *((unsigned long *) virt_addr);
			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);

	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;
}

最开始段是作者信息,包含头文件,至41行。

-47,3个宏定义。

然后 main ()开始是主程序。

55-62 如果输入参数少于2,则告诉你使用方法。

63  target 获取从第2个参数,支持0x 这种16进制方式。这个是地址的数据。

65-66 有3个参数的话,3参数是地址宽度定义,b,h,w 为选项。只取第一个字符。

-79 打开设备并映像地址。

-96 读取地址处的数据。

-115 如果参数有4个的话,第4个就是写的数据(支持0x 这种16进制方式),并完成写的操作。

-结尾 关闭设备,并关闭映像。

以下是我在zynq 7010 的 petalinux 下的编译命令:

liwenz@ubuntu:~/tmp$ vim devmem.c
liwenz@ubuntu:~/tmp$ arm-linux-gnueabihf-gcc devmem.c -o devmem

下面是我在petalinux 运行devmem 的情况,测试我的4个led, 1,2,4,8 分别点亮4个灯, 0x0f 点亮所有4个灯。

第3个参数控制读写的宽度只能是 w(32位),  h(16位), b(8位)。

猜你喜欢

转载自blog.csdn.net/leon_zeng0/article/details/83099809
今日推荐