devmem工具源码

#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 */ //使用mmap映射一页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;
}

devmem使用方法

root@myzr:~# ./devmem
Usage:  ./devmem { address } [ type [ data ] ]address : memory address to act upontype    : access operation type : [b]yte, [h]alfword, [w]orddata    : data to be written

查看寄存器地址

root@myzr:~# ./devmem  0x20e05e0
/dev/mem opened.
Memory mapped at address 0x76f99000.
Value at address 0x20E05E0 (0x76f995e0): 0x1B8B1

写寄存器

root@myzr:~# ./devmem 0x209c000 b 1
/dev/mem opened.
Memory mapped at address 0x76f25000.
Value at address 0x209C000 (0x76f25000): 0x5C
Written 0x1; readback 0x5C
root@myzr:~#

【linux驱动】嵌入式 Linux 对内存的直接读写(devmem)相关推荐

  1. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之设备树模型

    文章目录 前言 1.设备树的作用 2.设备树的语法 2.1.设备树的逻辑图和dts文件.dtb文件 2.1.1.1Devicetree格式 1DTS文件的格式 node的格式 properties的格 ...

  2. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之按键驱动框架

    文章目录 前言 1.APP怎么读取按键值 1.1.查询方式 1.2.休眠-唤醒方式 1.3.poll方式 1.3.异步通知方式 1.5. 驱动程序提供能力,不提供策略 2.按键驱动程序框架--查询方式 ...

  3. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之驱动设计的思想:面向对象/分层/分离

    文章目录 前言 1.分离设计 驱动程序分析---程序分层 通用驱动程序---面向对象 个性化驱动程序---分离 APP 程序分析 前言 韦东山嵌入式Linux驱动开发基础知识学习笔记 文章中大多内容来 ...

  4. 嵌入式系统、linux和嵌入式linux的区别

    这几个东西比较容易混淆 嵌入式系统: IEEE(国际电气和电子工程师协会)对嵌入式系统的定义:"用于控制.监视或者辅助操作机器和设备的装置".原文为:Devices Used to ...

  5. 基于嵌入式linux电子相册设计,用于LINUX或者嵌入式LINUX的电子相册程序,基于QT开发...

    用于LINUX或者嵌入式LINUX的电子相册程序,基于QT开发,包含源代码和编译好的可执行程序 linux_project\album\album.pro .............\.....\al ...

  6. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之Pinctrl子系统和GPIO子系统的使用

    文章目录 前言 1.Pinctrl子系统 1.1.为什么有Pinctrl子系统 1.2.重要的概念 1.3.代码中怎么引用pinctrl 2.GPIO子系统 2.1.为什么有GPIO子系统 2.2.在 ...

  7. 嵌入式linux mtd,嵌入式Linux驱动设备之MTD技术详解

    原标题:嵌入式Linux驱动设备之MTD技术详解 MTD(memory technology device内存技术设备)是用于访问memory设备(ROM.flash)的Linux的子系统. MTD的 ...

  8. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之LED模板驱动程序的改造:设备树

    文章目录 前言 1.驱动的三种编写方法 2.怎么使用设备树写驱动程序 2.1.设备树节点要与platform_driver能匹配 2.2.修改platform_driver的源码 3.实验和调试技巧 ...

  9. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之总线设备驱动模型

    文章目录 前言 1.驱动编写的三种方法 1.1.传统写法 1.2.总线驱动模型 1.3.设备树驱动模型 2.Linux实现分离:Bus/Dev/Drv模型 2.1.Bus/Dev/Drv模型 2.2. ...

  10. 【嵌入式Linux】嵌入式Linux驱动开发基础知识之LED驱动框架--面向对象、分层设计思想

    文章目录 前言 1.LED驱动程序框架 1.1.对于LED驱动,我们想要什么样的接口? 1.2.LED驱动要怎么写,才能支持多个板子?分层写 1.3.程序分析 驱动程序 应用程序 Makefile 1 ...

最新文章

  1. 写给小白看的线程和进程,高手勿入
  2. hibernate加载持久化对象的两种方式---------------load方式和get方式
  3. php支付密码控件,vue支付密码的图文实例
  4. 13. python 类
  5. python兼职程序员工资一般多少-做Python程序员,工资一般多少?
  6. 自学python需要买书吗-Python入门到精通学习书籍推荐!
  7. 如何在windows7上安装启明星系统。
  8. Windows 10 下 Anaconda3 (Python 3.8) 配置 OpenCV-4.4.0
  9. 福建师范大学计算机组成原理期末试卷,福建师范大学2020年8月课程考试《计算机组成原理》作业考核试题...
  10. 前端学习(2046)vue之电商管理系统电商系统之通过externals加载外部资源
  11. Qt工作笔记-通过信号与槽实现定时器
  12. tomcat 更新class自动重启
  13. 推理集 —— 举一反三
  14. 高等数学(第七版)同济大学 总习题一 个人解答
  15. Jmeter-界面功能介绍
  16. ASCII码为0x01,0x02作为分隔符(这两个字符是键盘无法输入的)
  17. 使用FTP进行主机与Linux的文件传输
  18. 动手学习数据分析第一章内容
  19. SQL基础培训13-索引和优化
  20. JVM 栈分配与TLAB

热门文章

  1. 读后感和机翻《他们在看哪里,为什么看?在复杂的任务中共同推断人类的注意力和意图》
  2. keras 的 example 文件 mnist_swwae.py 解析
  3. error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. 的解决方法
  4. 设置VSCode显示聚焦到资源管理器NPM窗口快捷键Alt+N
  5. leetcode-53 最大子序和
  6. C++ 拷贝构造函数和重载赋值运算符的区别
  7. 让ubuntu下的eclipse支持GBK编码
  8. Qt——模态、非模态
  9. Bootstrap笔记
  10. 图书管理系统(源码)