打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How to access GPIO ports and physical memory from user-space

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

other examples:

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);}
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
针对Linux BSP开发的Petalinux,如何实现硬件工程导入
PCIe Spec View
matlab高级编程之内存管理
JAVA Memory Concept
linux virtual memory layout by moniskiller upload [读书笔记]
What You Need to Know About Swap Partitions on Linux
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服