在linux下控制gpio可以先用简单的命令行去控制下看看,

1. devmem 0x41200000 32

0x00000005

2.devicetree的结构如下

dip0: gpio_dip_sw@41200000 {

compatible = "generic-uio","uio";

reg =<0x41200000 0x1000>;

text_data = "GPIO_DIP_SWITCH"; //for test

bin_data=; //for test

};

需要注意的点是 reg=后面的一个参数必须要大于等于0x1000 ;

3. c语言源码

fd_led=open(filename,O_RDONLY);

if(fd_led<0)

return 1;

base_address = mmap(NULL, 0x10, PROT_READ | PROT_WRITE, MAP_SHARED, fd_led, 0);

if(base_address==MAP_FAILED)

{perror("mmap");

}

总结一下:

下面是完整的API函数

#include

#include

#include

// The specific GPIO being used must be setup and replaced thru

// this code. The GPIO of 240 is in the path of most the sys dirs

// and in the export write.

//

// Figuring out the exact GPIO was not totally obvious when there

// were multiple GPIOs in the system. One way to do is to go into

// the gpiochips in /sys/class/gpio and view the label as it should

// reflect the address of the GPIO in the system. The name of the

// the chip appears to be the 1st GPIO of the controller.

//

// The export causes the gpio240 dir to appear in /sys/class/gpio.

// Then the direction and value can be changed by writing to them.

// The performance of this is pretty good, using a nfs mount,

// running on open source linux, on the ML507 reference system,

// the GPIO can be toggled about every 4 usec.

// The following commands from the console setup the GPIO to be

// exported, set the direction of it to an output and write a 1

// to the GPIO.

//

// bash> echo 240 > /sys/class/gpio/export

// bash> echo out > /sys/class/gpio/gpio240/direction

// bash> echo 1 > /sys/class/gpio/gpio240/value

// if sysfs is not mounted on your system, the you need to mount it

// bash> mount -t sysfs sysfs /sys

// the following bash script to toggle the gpio is also handy for

// testing

//

// while [ 1 ]; do

// echo 1 > /sys/class/gpio/gpio240/value

// echo 0 > /sys/class/gpio/gpio240/value

// done

// to compile this, use the following command

// gcc gpio.c -o gpio

// The kernel needs the following configuration to make this work.

//

// CONFIG_GPIO_SYSFS=y

// CONFIG_SYSFS=y

// CONFIG_EXPERIMENTAL=y

// CONFIG_GPIO_XILINX=y

int main()

{

int valuefd, exportfd, directionfd;

printf("GPIO test running...\n");

// The GPIO has to be exported to be able to see it

// in sysfs

exportfd = open("/sys/class/gpio/export", O_WRONLY);

if (exportfd < 0)

{

printf("Cannot open GPIO to export it\n");

exit(1);

}

write(exportfd, "240", 4);

close(exportfd);

printf("GPIO exported successfully\n");

// Update the direction of the GPIO to be an output

directionfd = open("/sys/class/gpio/gpio240/direction", O_RDWR);

if (directionfd < 0)

{

printf("Cannot open GPIO direction it\n");

exit(1);

}

write(directionfd, "out", 4);

close(directionfd);

printf("GPIO direction set as output successfully\n");

// Get the GPIO value ready to be toggled

valuefd = open("/sys/class/gpio/gpio240/value", O_RDWR);

if (valuefd < 0)

{

printf("Cannot open GPIO value\n");

exit(1);

}

printf("GPIO value opened, now toggling...\n");

// toggle the GPIO as fast a possible forever, a control c is needed

// to stop it

while (1)

{

write(valuefd,"1", 2);

write(valuefd,"0", 2);

}

}

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

linux下使用gpio控制代码,zynq linux 下控制gpio的c代码相关推荐

  1. ZYNQ LINUX开发笔记——windows下用xilinx SDK编译zynq linux app小程序

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 ZYNQ LINUX开发笔记--windows下用xilinx SDK编译zynq linux app小程序 前言 操作方法 总结 前 ...

  2. 61-20210407华为海思Hi3516DV300的linux系统下控制GPIO口(标准linux模式点亮LED灯)

    61-20210407华为海思Hi3516DV300的linux系统下控制GPIO口(标准linux模式点亮LED灯) 2021/4/7 15:13 https://xueqiu.com/797071 ...

  3. Linux内核中的GPIO系统之(3):pin controller driver代码分析

    一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道,pin control subsystem也不例外,被它驱动的硬件叫做pin controller(一般ARM soc的datash ...

  4. 基于arm下的Linux控制,基于ARMuCLinux的网络控制系统设计与实现

    引言    随着网络和通信技术的发展,嵌入式系统现已进入高速发展阶段.并在社会各个领域得到了广泛的应用.本文介绍了一种采用ARM+uCLinux作为开发平台.实现基于TCP/IP的远程系统监控.从而取 ...

  5. am335x linux修改ip,Linux 修改代码以支持LED 控制(board-am335xevm)

    Linux 修改代码以支持LED 控制(board-am335xevm) Note: All the codes are changed at the file board-am335xevm.c d ...

  6. Linux下CGroup进行CPU、内存等资源控制

    留存 from: http://www.cnblogs.com/kevingrace/p/5685433.html  and  http://www.cnblogs.com/wang_yb/p/394 ...

  7. 米尔科技ZYNQ -Linux下的DMA驱动

    一.目标 在米尔科技的z-turn板上实现linux下的DMA驱动,同时对DMA中断进行测试. 二.分析 ZYNQ的AXIDMA有Direct Register Mode和Scatter/Gather ...

  8. android gpio驱动实例,Android/Linux 驱动层对GPIO口的操作方法和相关代码

    实例: unsigned int otg_en_pin=952; ----->高通的gpio的基础值为902,这里我们时间的gpio为50,即这里定义值是要设置为952. //young.yan ...

  9. unity延迟执行下一行代码_Python代码在Linux环境下执行错误异常

    import happybase import time from multiprocessing import Pool# 设置IP地址 hbase_ip = '20.88.0.84' start_ ...

最新文章

  1. 读后感和机翻《人类因果迁移:深度强化学习的挑战》
  2. bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者*
  3. [导入]通过简易的前台代码实现无限二级域名转向(来自无忧 biyuan老矣)
  4. PHP源码之数组的内部实现
  5. mysql如何和qt连接使用_Qt5学习:连接MySQL数据库
  6. ACL'21 | debug完的神经网络,如何测试是否仍然存在bug?
  7. 软件工程 敏捷的酒后问答
  8. 【英语学习】【Level 07】U01 Making friends L1 New kid on the block
  9. 计算机应用有那些技校好,技校都有哪些专业? 就业前景好的有哪些
  10. mysql创建用户unix,MySQL,无法创建UNIX套接字(12)
  11. 听说有人快收权限掉了
  12. 电商网站详情页系统架构
  13. 记录-使用TM1640 数码管驱动芯片
  14. 解决WINDOWS2008上不识别EMULEX COE 11102万兆卡的问题
  15. 更精确的新旧中国居民身份证号码验证算法
  16. 为什么oracle打不开,oracle-Ora-01081_数据库打不开_错误解决方法
  17. 华为防火墙查看日志命令_华为防火墙命令
  18. Apple补丁解决iOS、iPadOS和macOS中的0day漏洞
  19. 联想服务器维护 - System X 3650 M5
  20. STM32F107的时钟设置由25M修改成8M

热门文章

  1. 界面无小事(六):来做个好看得侧拉菜单!
  2. 微信App支付全解析
  3. mongodb java驱动_Java操作MongoDB之mongodb-driver(一)
  4. springboot读取src下文件_java(包括springboot)读取resources下文件方式
  5. Java大对象lob_JavaEE JDBC 读写LOB大对象
  6. iOS开发之UITableView全面解析
  7. Linux系统与网络服务管理技术大全(第2版)
  8. 安装JDK_Tomcat
  9. 转 安装PHP出现make: *** [sapi/cli/php] Error 1 解决办法
  10. linux登录pg数据库密码,PostgreSQL:修改数据库用户的密码