转自:http://blog.csdn.net/xyyangkun/article/details/7830149

版权声明:本文为博主原创文章,未经博主允许不得转载。

这是在mini6410上测试成功的,在没有驱动的情况下用程序直接控制了led灯test_mmap.c:

  1. /* Example how to access the value of the on-board DIP switches on
  2. * HiCO.SH7760. You can compile the program with command:
  3. *
  4. *   sh4-linux-gcc -Wall dip_switch.c -o dip_switch
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <sys/mman.h>
  13. #define MAP_SIZE 4096UL
  14. #define MAP_MASK (MAP_SIZE - 1)
  15. int main(void) {
  16. int fd;
  17. void *map_base, *virt_addr;
  18. /* Physical address of the DIP switch GPIO register on HiCO.SH7760 (See
  19. * the HiCO.SH7760 hardware manual
  20. *
  21. * _NOTE_: the dipswitches also define how the system boots (i.e. the value
  22. * is used by the bootloader at startup), so oafter testting, leave the
  23. * switches in the same position as they were */
  24. off_t target = 0x7F008800;  //GPKCON0 0x7F008800
  25. if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
  26. printf("/dev/mem could not be opened.\n");
  27. perror("open");
  28. exit(1);
  29. } else {
  30. printf("/dev/mem opened.\n");
  31. }
  32. /* Map one page */
  33. map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
  34. if(map_base == (void *) -1) {
  35. printf("Memory map failed.\n");
  36. perror("mmap");
  37. } else {
  38. printf("Memory mapped at address %p.\n", map_base);
  39. }
  40. virt_addr = map_base + (target & MAP_MASK);
  41. /* acess remapped region here */
  42. printf("Now try to change the value of the on-board DIP switche\n");
  43. *(unsigned int *)virt_addr=0x11110000;//0x1111<<16;//设定为输出状态
  44. int ab=*(unsigned int *)virt_addr;
  45. //*(unsigned int *)(virt_addr+8)=0xffffffff;
  46. printf("ab=%x\n",ab);
  47. #if 1
  48. while(1){
  49. *(volatile unsigned int *)(virt_addr+8)=0x00;   //一定要是virt_addr不能是map_base//置0亮
  50. printf("1value is 0x%x\n",*(unsigned int *)(virt_addr+8));
  51. printf("2value is 0x%x\n",*(unsigned int *)virt_addr);
  52. sleep(1);   //一定要有要不很快,人眼发现不了
  53. *(volatile unsigned int *)(virt_addr+8)=0xf0;                  //置1灭
  54. printf("3value is 0x%x\n",*(unsigned int *)(virt_addr+8));
  55. printf("4value is 0x%x\n",*(unsigned int *)virt_addr);
  56. sleep(1);   //一定要有要不很快,人眼发现不了
  57. }
  58. #endif
  59. /* we'll never get here in this example, but here's how the region is
  60. * unmapped. */
  61. if(munmap(map_base, MAP_SIZE) == -1) {
  62. printf("Memory unmap failed.\n");
  63. }
  64. close(fd);
  65. }

以上程序证明了在linux下用mmap函数可以在没有驱动的情况下也可以直接控制底层。

网上说只能控制gpio设备,我还可以直接控制其它设备,有待验证。

mmap直接控制底层【转】的更多相关文章

  1. JAVAEE——BOS物流项目10:权限概述、常见的权限控制方式、apache shiro框架简介、基于shiro框架进行认证操作

    1 学习计划 1.演示权限demo 2.权限概述 n 认证 n 授权 3.常见的权限控制方式 n url拦截权限控制 n 方法注解权限控制 4.创建权限数据模型 n 权限表 n 角色表 n 用户表 n ...

  2. 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示

    1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...

  3. C标准I/O库函数与Unbuffered I/O函数

    一.C标准I/O库函数.Unbuffered I/O函数 1. C标准I/O库函数是如何用系统调用的 fopen(3) 调用open(2)打开制定的文件,返回一个文件描述符(一个int类型的编号),分 ...

  4. C/C++ Learning

    目录 1. C/C++中的关键字2. C/C++中的标识符3. 编译选项MD(d).MT(d)编译选项的区别4. C++类模板.函数模板5. C++修饰符6. 调用约定7. 错误处理8. 环境表 9. ...

  5. FFmpeg详解

    认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几乎实现了所有当下常见的数据封装格式.多媒体传输协议以及音视频编解码器.因此,对于从事多媒体技术开发的工程师来说,深入研究FFM ...

  6. 最新FFMPEG解码流程

    FFMPEG解码流程: 1. 注册所有容器格式和CODEC:  av_register_all() 2. 打开文件:                    av_open_input_file() 3 ...

  7. JVM源码分析之一个Java进程究竟能创建多少线程

    JVM源码分析之一个Java进程究竟能创建多少线程 原创: 寒泉子 你假笨 2016-12-06 概述 虽然这篇文章的标题打着JVM源码分析的旗号,不过本文不仅仅从JVM源码角度来分析,更多的来自于L ...

  8. 摄像头bug查找工作总结

    近期花了很长时间在libcamera中查找和解决一个bug.下面将这段时间中的工作过程,以及对camera的认识总结如下: 首先是问题的发生,在UM2801中,摄像头的代码已经基本实现,并且相应功能也 ...

  9. ffmpeg 详解

    来源:http://blog.itpub.net/9399028/viewspace-1242300/ FFMPEG详解   认识FFMPEG FFMPEG堪称自由软件中最完备的一套多媒体支持库,它几 ...

随机推荐

  1. 什么是webshell?

    webshell是web入侵的脚本攻击工具. 简单的说来,webshell就是一个asp或php木马后门,黑客在入侵了一个网站后,常常在将这些 asp或php木马后门文件放置在网站服务器的web目录中 ...

  2. Android 回到底部和返回顶部实现

    效果 准备四张图片资源 首先XML布局 我们采用FrameLayout布局 代码: <?xml version="1.0" encoding="utf-8" ...

  3. web双机热备添加心跳检测ip的时候填了网关导致外网ip不能上网

    web双机热备添加心跳检测ip的时候填了网关导致外网ip不能上网 1 连接 机器其他机器, 通过机房做的服务器的局域网,ssh到这台的局域网ip,删除网卡配置文件的网关哪一行,重启网卡. 2 如果没有 ...

  4. Java遇见HTML——JSP篇之JSP基础语法

    一.JSP简介 JSP全名为Java Server Pages,Java服务器端页面,其根本是一个简化的Servlet设计,它实现了在Java中使用HTML标签.Jsp是一种动态网页技术标准,是在服务 ...

  5. windows 64位 dll文件 位置及python包rtree shapely安装

    位置 \Windows\System32 python包依赖包安装 rtree 依赖 spatialindex(spatialindex.dll   spatialindex_c.dll) shape ...

  6. Linux 源码安装httpd

    安装apr 下载解压apr-1.4.5 ./configure --prefix=/usr/local/apr make sudo make install 安装apr-util 下载解压apr-ut ...

  7. linux普通用户权限设置为超级用户权限方法、sudo不用登陆密码

    以用户zato为例 普通用户权限设置为超级用户权限 进入有超级用户权限的账号 添加文件可写(w)权限 sudo chmod u+x /etc/sudoers 编辑/etc/sudoers文件 添加语句 ...

  8. django 用户登陆注册

    注册登陆 views.py #!/usr/bin/env python # -*- coding:utf- -*- from django.shortcuts import render,render ...

  9. Git撤销操作命令

    使用Git的过程中,失误无法避免,虽然Git很强,但是有些失误,无法挽回.在这里我介绍一些Git的撤销命令. 撤销对文件的修改 如下图所示的情况,你修改了文件,但是不想保存对文件的修改. 根据具体情况 ...

  10. networkcommon v3开源消息框架资料

    http://www.cnblogs.com/csdev/category/870400.html