日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mmap直接操作底层,相当于驱动

發(fā)布時(shí)間:2024/9/5 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mmap直接操作底层,相当于驱动 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在2.6的內(nèi)核中,上面?zhèn)€程序測試出錯(cuò),下面?zhèn)€測試可用


1、http://www.codeforge.cn/read/99759/pxa270_gpio_led.c__html

pxa270_gpio_led.c?in pxa270_gpio_led.rar

/********************************************************* copyright (C), 1999-2009, YILIDA Tech. Co., Ltd. File name: gpio.c Author: Owen Version: 1.0 Date:09/12/2009 Description: Others: Function List: 1. 2. History: 1. Date:Author:Modification: *********************************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <sys/file.h>#define UINT unsigned int #define GPIO_BASE 0x40E00000 #define GPIO *(volatile UINT *) #define GPDR1_DIR 0x0010 #define GPSR1_OUT 0x001C #define GPCR1_ZERO 0x0028int main(int argc,char *argv[]){int i;int fd;char *start;//open /dev/mem with read and write modefd = open ("/dev/mem", O_RDWR);if (fd < 0){printf("cannot open /dev/mem.");return -1;}//map physical memory 0-10 bytes start = (char *)mmap(0, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);GPIO(start + GPDR1_DIR) = (1 << 21);while(1){GPIO(start + GPSR1_OUT) = (1 << 21);sleep(2);GPIO(start + GPCR1_ZERO) = (1 << 21);sleep(2);}munmap(start, 10); //destroy map memoryclose(fd); //close filereturn 0; } ...

2、http://www.support.emtrion.de/doku.php?id=linux:how-to-access-gpio how_to_access_gpio_ports_and_physical_memory_from_user-space

Normally all hardware access is done by a kernel driver. There are, however cases where you just need to change a value in some register without worrying buffering, interrupts etc. For example if you just need to access some GPIO ports to change a value of a led.

GPIO ports and physical memory need to be mapped into the user-space before they can be accessed. Following document discusses this and provides an example program?devmem2

  • http://www.simtec.co.uk/appnotes/AN0014/

other examples:

  • http://wiki.gp2x.org/wiki/GPIO_Programming_Example

Here is an example how to read the on-board DIP switch value on HiCO.SH7760

/* Example how to access the value of the on-board DIP switches on* HiCO.SH7760. You can compile the program with command:** sh4-linux-gcc -Wall dip_switch.c -o dip_switch*/ #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #include <sys/mman.h>#define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1)int main(void) {int fd;void *map_base, *virt_addr;/* Physical address of the DIP switch GPIO register on HiCO.SH7760 (See* the HiCO.SH7760 hardware manual ** _NOTE_: the dipswitches also define how the system boots (i.e. the value* is used by the bootloader at startup), so oafter testting, leave the* switches in the same position as they were */off_t target = 0xA40000A0;if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {printf("/dev/mem could not be opened.\n");perror("open");exit(1);} else {printf("/dev/mem opened.\n");}/* Map one page */map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);if(map_base == (void *) -1) {printf("Memory map failed.\n");perror("mmap");} else {printf("Memory mapped at address %p.\n", map_base);}virt_addr = map_base + (target & MAP_MASK);/* acess remapped region here */printf("Now try to change the value of the on-board DIP switche\n");while(1){printf("value is 0x%02x\n",*(unsigned char *)virt_addr);sleep(1);}/* we'll never get here in this example, but here's how the region is* unmapped. */if(munmap(map_base, MAP_SIZE) == -1) {printf("Memory unmap failed.\n"); }close(fd); }


總結(jié)

以上是生活随笔為你收集整理的mmap直接操作底层,相当于驱动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。