博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux下操作gpio寄存器的方法
阅读量:4664 次
发布时间:2019-06-09

本文共 2392 字,大约阅读时间需要 7 分钟。

一、 在驱动中:

  1. 用的时候映射端口:ioremap;

  #define GPIO_OFT(x) ((x) - 0x56000000)   #define GPFCON  (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))   gpio_va = ioremap(0x56000000, 0x100000); // 物理地址0x56000000, 映射区分配的大小0x100000字节   这样映射过后,就可以直接操作寄存器了:    配置3引脚为输出: GPFCON &= ~(0x3<<(4*2));   2. 在linux/include/asm-arm/arch-s3c2410/map.h映射, 在linux/include/asm-arm/arch-s3c2410/regs-gpio.h得到各种寄存器的地址,直接对这些地址进行操作即可。        http://blog.chinaunix.net/uid-25100840-id-271146.html            misccr = __raw_readl(S3C24XX_MISCCR);       misccr &= ~clear;       misccr ^= change;       __raw_writel(misccr, S3C24XX_MISCCR);          #define __raw_writel(v,a)    (__chk_io_ptr(a), *(volatile unsigned int __force   *)(a) = (v))        下面是内核代码提供的操作gpio寄存器的一些接口:

void s3c2410_gpio_cfgpin(unsigned int pin, unsigned int function);

unsigned int s3c2410_gpio_getcfg(unsigned int pin);

void s3c2410_gpio_pullup(unsigned int pin, unsigned int to);

void s3c2410_gpio_setpin(unsigned int pin, unsigned int to);
unsigned int s3c2410_gpio_getpin(unsigned int pin);
unsigned int s3c2410_modify_misccr(unsigned int clear, unsigned int change);
int s3c2410_gpio_getirq(unsigned int pin);

linux/include/asm-arm/io.h

#define __raw_writeb(v,a)    (__chk_io_ptr(a), *(volatile unsigned char __force  *)(a) = (v))

#define __raw_writew(v,a)    (__chk_io_ptr(a), *(volatile unsigned short __force *)(a) = (v))
#define __raw_writel(v,a)    (__chk_io_ptr(a), *(volatile unsigned int __force   *)(a) = (v))
#define __raw_readb(a)        (__chk_io_ptr(a), *(volatile unsigned char __force  *)(a))
#define __raw_readw(a)        (__chk_io_ptr(a), *(volatile unsigned short __force *)(a))
#define __raw_readl(a)        (__chk_io_ptr(a), *(volatile unsigned int __force   *)(a))

二、 在linux应用程序中:  mmap

http://www.cnblogs.com/cute/archive/2011/05/10/2042399.html

mmap的具体解释参考: http://www.cnblogs.com/lidabo/p/5319219.html

功能描述: 

mmap将一个文件或者其它对象映射进内存。文件被映射到多个页上,如果文件的大小不是所有页的大小之和,最后一个页不被使用的空间将会清零。munmap执行相反的操作,删除特定地址区域的对象映射。
基 于文件的映射,在mmap和munmap执行过程的任何时刻,被映射文件的st_atime可能被更新。如果st_atime字段在前述的情况下没有得到 更新,首次对映射区的第一个页索引时会更新该字段的值。用PROT_WRITE 和 MAP_SHARED标志建立起来的文件映射,其st_ctime 和 st_mtime
在对映射区写入之后,但在msync()通过MS_SYNC 和 MS_ASYNC两个标志调用之前会被更新。
用法:  
#include <sys/mman.h>
void *mmap(void *start, size_t length, int prot, int flags,
  int fd, off_t offset);

int munmap(void *start, size_t length);

 

============================

 

转载于:https://www.cnblogs.com/mylinux/p/5639264.html

你可能感兴趣的文章
背景 半透明问题 rgba + filter
查看>>
coredump配置、产生、分析以及分析示例
查看>>
Ubuntu安装JDK与环境变量配置
查看>>
常见html标签
查看>>
Spring和Spring MVC框架进行单元测试
查看>>
RTTI(运行时类型识别)
查看>>
tensorflow安装
查看>>
AGC001 E BBQ Hard
查看>>
js写的ajax
查看>>
解析器组合子
查看>>
AtCoder Regular Contest 097
查看>>
openssl 生成证书
查看>>
aspx页面生成xml数据
查看>>
转:do{...}while(0)的意义和用法
查看>>
nginx反向代理(动静分离)
查看>>
一些加快 程序运行速度的方法
查看>>
Go语言管道
查看>>
查看当前使用的shell
查看>>
基于 muse-ui 封装一个微信公众号上传插件 实现多图上传
查看>>
vue面试题!!!
查看>>