查看: 17403|回复: 0

【fireflyRK3288/firefly RK3399开发板】GPIO控制

[复制链接]

404

主题

245

回帖

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
20384
发表于 2018-6-11 15:49:27 | 显示全部楼层 |阅读模式
Python例子:
模块安装请参考:http://developer.t-firefly.com/thread-10503-1-2.html

android例子:
http://dev.t-firefly.com/forum.p ... =396&highlight=GPIO
fireflyRK3399驱动中调用GPIO:
http://www.t-firefly.com/doc/product/info/id/88.html



调试方法IO指令
GPIO调试有一个很好用的工具,那就是IO指令,Firefly-RK3399的Android系统默认已经内置了IO指令,使用IO指令可以实时读取或写入每个IO口的状态,这里简单介绍IO指令的使用。 首先查看 io 指令的帮助:
  1. #io --help
  2. Unknown option: ?
  3. Raw memory i/o utility - $Revision: 1.5 $

  4. io -v -1|2|4 -r|w [-l <len>] [-f <file>] <addr> [<value>]

  5.    -v         Verbose, asks for confirmation
  6.    -1|2|4     Sets memory access size in bytes (default byte)
  7.    -l <len>   Length in bytes of area to access (defaults to
  8.               one access, or whole file length)
  9.    -r|w       Read from or Write to memory (default read)
  10.    -f <file>  File to write on memory read, or
  11.               to read on memory write
  12.    <addr>     The memory address to access
  13.    <val>      The value to write (implies -w)

  14. Examples:
  15.    io 0x1000                  Reads one byte from 0x1000
  16.    io 0x1000 0x12             Writes 0x12 to location 0x1000
  17.    io -2 -l 8 0x1000          Reads 8 words from 0x1000
  18.    io -r -f dmp -l 100 200    Reads 100 bytes from addr 200 to file
  19.    io -w -f img 0x10000       Writes the whole of file to memory

  20. Note access size (-1|2|4) does not apply to file based accesses.
复制代码

从帮助上可以看出,如果要读或者写一个寄存器,可以用:
  1. io -4 -r 0x1000 //读从0x1000起的4位寄存器的值
  2. io -4 -w 0x1000 //写从0x1000起的4位寄存器的值
复制代码
使用示例:
  • 查看GPIO1_B3引脚的复用情况

1. 从主控的datasheet查到GPIO1对应寄存器基地址为:0xff320000
2. 从主控的datasheet查到GPIO1B_IOMUX的偏移量为:0x00014
3. GPIO1_B3的iomux寄存器地址为:基址(Operational Base) + 偏移量(offset)=0xff320000+0x00014=0xff320014  
4. 用以下指令查看GPIO1_B3的复用情况:
  1. # io -4 -r 0xff320014ff320014:  0000816a
复制代码

5. 从datasheet查到[7:6]:
  1. gpio1b3_sel
  2. GPIO1B[3] iomux select  
  3. 2'b00: gpio
  4. 2'b01: i2c4sensor_sda
  5. 2'b10: reserved
  6. 2'b11: reserved
复制代码

因此可以确定该GPIO被复用为 i2c4sensor_sda。

6. 如果想复用为GPIO,可以使用以下指令设置:

  1. # io -4 -w 0xff320014 0x0000812a
复制代码


GPIO调试接口
Debugfs文件系统目的是为开发人员提供更多内核数据,方便调试。 这里GPIO的调试也可以用Debugfs文件系统,获得更多的内核信息。 GPIO在Debugfs文件系统中的接口为 /sys/kernel/debug/gpio,可以这样读取该接口的信息:
  1. # cat /sys/kernel/debug/gpio
  2. GPIOs 0-31, platform/pinctrl, gpio0:
  3. gpio-2   (                    |vcc3v3_3g           ) out hi   
  4. gpio-4   (                    |bt_default_wake_host) in  lo   
  5. gpio-5   (                    |power               ) in  hi   
  6. gpio-9   (                    |bt_default_reset    ) out lo   
  7. gpio-10  (                    |reset               ) out lo   
  8. gpio-13  (                    |?                   ) out lo   

  9. GPIOs 32-63, platform/pinctrl, gpio1:
  10. gpio-32  (                    |vcc5v0_host         ) out hi   
  11. gpio-34  (                    |int-n               ) in  hi   
  12. gpio-35  (                    |vbus-5v             ) out lo   
  13. gpio-45  (                    |pmic-hold-gpio      ) out hi   
  14. gpio-49  (                    |vcc3v3_pcie         ) out hi   
  15. gpio-54  (                    |mpu6500             ) out hi   
  16. gpio-56  (                    |pmic-stby-gpio      ) out hi   

  17. GPIOs 64-95, platform/pinctrl, gpio2:
  18. gpio-83  (                    |bt_default_rts      ) in  hi   
  19. gpio-90  (                    |bt_default_wake     ) in  lo   
  20. gpio-91  (                    |?                   ) out hi   

  21. GPIOs 96-127, platform/pinctrl, gpio3:
  22. gpio-111 (                    |mdio-reset          ) out hi   

  23. GPIOs 128-159, platform/pinctrl, gpio4:
  24. gpio-149 (                    |hp-con-gpio         ) out lo
复制代码

从读取到的信息中可以知道,内核把GPIO当前的状态都列出来了,以GPIO0组为例,gpio-2(GPIO0_A2)作为3G模块的电源控制脚(vcc3v3_3g),输出高电平(out hi)。
FAQsQ1: 如何将PIN的MUX值切换为一般的GPIO?
A1: 当使用GPIO request时候,会将该PIN的MUX值强制切换为GPIO,所以使用该pin脚为GPIO功能的时候确保该pin脚没有被其他模块所使用。
Q2: 为什么我用IO指令读出来的值都是0x00000000?
A2: 如果用IO命令读某个GPIO的寄存器,读出来的值异常,如 0x00000000或0xffffffff等,请确认该GPIO的CLK是不是被关了,GPIO的CLK是由CRU控制,可以通过读取datasheet下面CRU_CLKGATE_CON* 寄存器来查到CLK是否开启,如果没有开启可以用io命令设置对应的寄存器,从而打开对应的CLK,打开CLK之后应该就可以读到正确的寄存器值了。
Q3: 测量到PIN脚的电压不对应该怎么查?
A3: 测量该PIN脚的电压不对时,如果排除了外部因素,可以确认下该pin所在的io电压源是否正确,以及IO-Domain配置是否正确。
Q4: gpio_set_value()与gpio_direction_output()有什么区别?
A4: 如果使用该GPIO时,不会动态的切换输入输出,建议在开始时就设置好GPIO 输出方向,后面拉高拉低时使用gpio_set_value()接口,而不建议使用gpio_direction_output(), 因为gpio_direction_output接口里面有mutex锁,对中断上下文调用会有错误异常,且相比 gpio_set_value,gpio_direction_output 所做事情更多,浪费。



风火轮微信公众号
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|风火轮WIKI|手机版|小黑屋|深圳风火轮团队 ( 粤ICP备17095099号 )

GMT+8, 2024-3-29 20:16 , Processed in 0.051333 second(s), 19 queries .

快速回复 返回顶部 返回列表
 
【客服1】 商务合作 15289193
【客服2】 业务洽谈 13257599
【客服3】 售前咨询 510313198
【邮箱】
smartfire@smartfire.cn