三种方式控制GPIO

Posted ricks

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三种方式控制GPIO相关的知识,希望对你有一定的参考价值。

BBB为REV C,emmc4G版本,系统为Debian 7.9 wheezy (2015.11.12),内核为Linux 3.8.13.使用命令cat /etc/dogtag查看

技术分享图片

查看系统信息的四种方式

uname -a

lsb_release -a

cat /proc/version

sudo cat /etc/dogtag

技术分享图片

cd /opt/scripts/tools

./version.sh 

技术分享图片

这次先从GPIO控制开始,目标是user led。板子上四个led的定义为:

技术分享图片

  • USR0 is configured at boot to blink in a heartbeat pattern-------------------------running
  • USR1 is configured at boot to light during microSD card accesses-------------micro SD
  • USR2 is configured at boot to light during CPU activity----------------------------CPU
  • USR3 is configured at boot to light during eMMC accesses----------------------emmc

 

1. 命令行控制

 

以GPIO user led 3为例

技术分享图片

这样就可以控制usr LED3的亮灭了。

quectel模块net status led灯控制举例:

[email protected]:/sys/class/leds# ls
net_status
[email protected]-perf:/sys/class/leds# cd net_status/
[email protected]-perf:/sys/devices/soc:leds_gpio/leds/net_status# ls
brightness      delay_on        max_brightness  subsystem       uevent
delay_off       device          power           trigger
[email protected]-perf:/sys/devices/soc:leds_gpio/leds/net_status# ls -AlF
total 0
-rw-r--r--    1 root     root          4096 Jan  1  1970 brightness
-rw-r--r--    1 root     root          4096 Jan  6 00:12 delay_off
-rw-r--r--    1 root     root          4096 Jan  6 00:12 delay_on
lrwxrwxrwx    1 root     root             0 Jan  1  1970 device -> ../../../soc:leds_gpio/
-rw-r--r--    1 root     root          4096 Jan  1  1970 max_brightness
drwxr-xr-x    2 root     root             0 Jan  1  1970 power/
lrwxrwxrwx    1 root     root             0 Jan  1  1970 subsystem -> ../../../../class/leds/
-rw-r--r--    1 root     root          4096 Jan  1  1970 trigger
-rw-r--r--    1 root     root          4096 Jan  1  1970 uevent
[email protected]-perf:/sys/devices/soc:leds_gpio/leds/net_status# cat trigger
none usb-online rfkill0 [timer]
[email protected]-perf:/sys/devices/soc:leds_gpio/leds/net_status# echo none>trigger
[email protected]-perf:/sys/devices/soc:leds_gpio/leds/net_status# echo 1 > brightnes
s
[email protected]-perf:/sys/devices/soc:leds_gpio/leds/net_status# echo 0 > brightnes

这样就可以控制net status led的亮灭了。

它的System Reference Manual (SRM)文档在Hardware 部分下载p66页中

技术分享图片

1.1 查看占用的引脚

我们查看目前已经使用的引脚及其寄存器地址:

 

root:/sys/kernel/debug/pinctrl/44e10800.pinmux# cat pingroups
registered pin groups:
group: pinmux_userled_pins
pin 21 (44e10854)
pin 22 (44e10858)
pin 23 (44e1085c)
pin 24 (44e10860)

group: pinmux_rstctl_pins
pin 20 (44e10850)

group: pinmux_i2c0_pins
pin 98 (44e10988)
pin 99 (44e1098c)

group: pinmux_i2c2_pins
pin 94 (44e10978)
pin 95 (44e1097c)

group: pinmux_mmc1_pins
pin 88 (44e10960)!!!!!!!!!not have this one

group: pinmux_emmc2_pins
pin 32 (44e10880)
pin 33 (44e10884)
pin 0 (44e10800)
pin 1 (44e10804)
pin 2 (44e10808)
pin 3 (44e1080c)
pin 4 (44e10810)
pin 5 (44e10814)
pin 6 (44e10818)
pin 7 (44e1081c)

group: pinmux_userled_pins
pin 21 (44e10854)
pin 22 (44e10858)
pin 23 (44e1085c)
pin 24 (44e10860)

group: mcasp0_pins
pin 107 (44e109ac)
pin 103 (44e1099c)
pin 101 (44e10994)
pin 100 (44e10990)
pin 27 (44e1086c)!!!!!!!!!pin 106 (44e109a8)

group: nxp_hdmi_bonelt_pins
pin 108 (44e109b0)
pin 40 (44e108a0)
pin 41 (44e108a4)
pin 42 (44e108a8)
pin 43 (44e108ac)
pin 44 (44e108b0)
pin 45 (44e108b4)
pin 46 (44e108b8)
pin 47 (44e108bc)
pin 48 (44e108c0)
pin 49 (44e108c4)
pin 50 (44e108c8)
pin 51 (44e108cc)
pin 52 (44e108d0)
pin 53 (44e108d4)
pin 54 (44e108d8)
pin 55 (44e108dc)
pin 56 (44e108e0)
pin 57 (44e108e4)
pin 58 (44e108e8)
pin 59 (44e108ec)

group: nxp_hdmi_bonelt_off_pins
pin 108 (44e109b0)

1.2 查看所有引脚

所有引脚的功能

root:/sys/kernel/debug/pinctrl/44e10800.pinmux# cat pinmux-pins
Pinmux settings per pin
Format: pin (name): mux_owner gpio_owner hog?
pin 0 (44e10800): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 1 (44e10804): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 2 (44e10808): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 3 (44e1080c): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 4 (44e10810): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 5 (44e10814): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 6 (44e10818): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 7 (44e1081c): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 8 (44e10820): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 9 (44e10824): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 10 (44e10828): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 11 (44e1082c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 12 (44e10830): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 13 (44e10834): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 14 (44e10838): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 15 (44e1083c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 16 (44e10840): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 17 (44e10844): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 18 (44e10848): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 19 (44e1084c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 20 (44e10850): rstctl.4 (GPIO UNCLAIMED) function pinmux_rstctl_pins group pinmux_rstctl_pins
pin 21 (44e10854): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 22 (44e10858): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 23 (44e1085c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 24 (44e10860): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 25 (44e10864): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 26 (44e10868): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 27 (44e1086c): 48038000.mcasp (GPIO UNCLAIMED) function mcasp0_pins group mcasp0_pins
pin 28 (44e10870): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 29 (44e10874): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 30 (44e10878): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 31 (44e1087c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 32 (44e10880): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 33 (44e10884): mmc.11 (GPIO UNCLAIMED) function pinmux_emmc2_pins group pinmux_emmc2_pins
pin 34 (44e10888): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 35 (44e1088c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 36 (44e10890): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 37 (44e10894): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 38 (44e10898): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 39 (44e1089c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 40 (44e108a0): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 41 (44e108a4): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 42 (44e108a8): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 43 (44e108ac): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 44 (44e108b0): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 45 (44e108b4): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 46 (44e108b8): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 47 (44e108bc): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 48 (44e108c0): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 49 (44e108c4): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 50 (44e108c8): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 51 (44e108cc): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 52 (44e108d0): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 53 (44e108d4): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 54 (44e108d8): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 55 (44e108dc): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 56 (44e108e0): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 57 (44e108e4): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 58 (44e108e8): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 59 (44e108ec): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 60 (44e108f0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 61 (44e108f4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 62 (44e108f8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 63 (44e108fc): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 64 (44e10900): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 65 (44e10904): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 66 (44e10908): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 67 (44e1090c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 68 (44e10910): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 69 (44e10914): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 70 (44e10918): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 71 (44e1091c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 72 (44e10920): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 73 (44e10924): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 74 (44e10928): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 75 (44e1092c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 76 (44e10930): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 77 (44e10934): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 78 (44e10938): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 79 (44e1093c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 80 (44e10940): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 81 (44e10944): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 82 (44e10948): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 83 (44e1094c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 84 (44e10950): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 85 (44e10954): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 86 (44e10958): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 87 (44e1095c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 88 (44e10960): mmc.5 (GPIO UNCLAIMED) function pinmux_mmc1_pins group pinmux_mmc1_pins
pin 89 (44e10964): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 90 (44e10968): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 91 (44e1096c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 92 (44e10970): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 93 (44e10974): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 94 (44e10978): 4819c000.i2c (GPIO UNCLAIMED) function pinmux_i2c2_pins group pinmux_i2c2_pins
pin 95 (44e1097c): 4819c000.i2c (GPIO UNCLAIMED) function pinmux_i2c2_pins group pinmux_i2c2_pins
pin 96 (44e10980): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 97 (44e10984): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 98 (44e10988): 44e0b000.i2c (GPIO UNCLAIMED) function pinmux_i2c0_pins group pinmux_i2c0_pins
pin 99 (44e1098c): 44e0b000.i2c (GPIO UNCLAIMED) function pinmux_i2c0_pins group pinmux_i2c0_pins
pin 100 (44e10990): 48038000.mcasp (GPIO UNCLAIMED) function mcasp0_pins group mcasp0_pins
pin 101 (44e10994): 48038000.mcasp (GPIO UNCLAIMED) function mcasp0_pins group mcasp0_pins
pin 102 (44e10998): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 103 (44e1099c): 48038000.mcasp (GPIO UNCLAIMED) function mcasp0_pins group mcasp0_pins
pin 104 (44e109a0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 105 (44e109a4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 106 (44e109a8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 107 (44e109ac): 48038000.mcasp (GPIO UNCLAIMED) function mcasp0_pins group mcasp0_pins
pin 108 (44e109b0): hdmi.13 (GPIO UNCLAIMED) function nxp_hdmi_bonelt_pins group nxp_hdmi_bonelt_pins
pin 109 (44e109b4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 110 (44e109b8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 111 (44e109bc): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 112 (44e109c0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 113 (44e109c4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 114 (44e109c8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 115 (44e109cc): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 116 (44e109d0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 117 (44e109d4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 118 (44e109d8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 119 (44e109dc): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 120 (44e109e0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 121 (44e109e4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 122 (44e109e8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 123 (44e109ec): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 124 (44e109f0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 125 (44e109f4): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 126 (44e109f8): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 127 (44e109fc): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 128 (44e10a00): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 129 (44e10a04): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 130 (44e10a08): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 131 (44e10a0c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 132 (44e10a10): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 133 (44e10a14): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 134 (44e10a18): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 135 (44e10a1c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 136 (44e10a20): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 137 (44e10a24): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 138 (44e10a28): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 139 (44e10a2c): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 140 (44e10a30): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 141 (44e10a34): (MUX UNCLAIMED) (GPIO UNCLAIMED)

因为系统已经占用了很多引脚,为了方便,我们选择下图中带有GPIO的引脚来操作

 

 技术分享图片

P9-15 GPIO1_16   = 1 x 32 + 16 = GPIO 48

技术分享图片

如果在P9-15接个led就会点亮它。使用完毕以后记得unexported。

技术分享图片

https://elinux.org/EBC_Exercise_07_Updating_an_Old_U-Boot

cd /opt/scripts/tools

config-pin -q P9_15

 技术分享图片

 

1.3 引脚模式详解

(查表知道P9-15 GPIO1_16 GPIO48 Offset 0x040)
注意:GPIO 48 is not PIN 48!!!

GPIO地址为44e10840 #pinmux_pins中的16号

技术分享图片

事实上我们看到GPIO 48它对应于pin 16,所以不要想当然。

这里发现引脚模式0x27为0010 0111,它是什么意思呢?

我们看AM3358的技术手册,AM335x Sitara Processors Technical Reference Manual (Rev. M).pdf点击打开链接 搜索关键字“conf_<module>”Page 1426

技术分享图片

010 0111, Fast, Receiver enable, pull down selected, pull up/pull down enable, mux select mode7, 所以整个含义就是INPUT GPIO(mode7) pulldown, 下拉输入。

BBB_SRM Page 86页

技术分享图片

0x07 means 000 0111 = Fast, Disable Receiver, Pulldown type, enabled, mux mode 7.下拉输出

0x17 means 001 0111 = Fast, Disable Receiver, Pullup type, enabled, mux mode 7.上拉输出

 

0x27 means 010 0111 = Fast, Enable Receiver, Pulldown type, enabled, mux mode 7.下拉输入

0x37 means 011 0111 = Fast, Enable Receiver, Pullup type, enabled, mux mode 7. 上拉输入

注意这也不是绝对的,例如pins GPIO2_6 到 GPIO2_14 都有 42.2k 下拉电阻 到 GND 并且 100k 上拉电阻。SRM Page 74

技术分享图片

2. C++程序控制

这里仍旧控制Usr Led3,程序如下:

 

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

#define LED3_PATH "/sys/class/leds/beaglebone:green:usr3"

void writeLED(string filename, string value){
   fstream fs;
   string path(LED3_PATH);
   fs.open((path + filename).c_str(), fstream::out);
   fs << value;
   fs.close();
}

void removeTrigger(){
  writeLED("/trigger", "none");
}

int main(int argc, char* argv[]){
   if(argc!=2){
	cout << "Usage is makeLED and one of: " << endl;
        cout << "   on, off, flash or status" << endl;
	cout << " e.g. makeLED flash" << endl;
        return 2;
   }
   string cmd(argv[1]);
   cout << "Starting the makeLED program" << endl;
   cout << "The current LED Path is: " << LED3_PATH << endl;

   // select whether command is on, off, flash or status
   if(cmd=="on"){
        cout << "Turning the LED on" << endl;
	removeTrigger();
        writeLED("/brightness", "1");
   }
   else if (cmd=="off"){
        cout << "Turning the LED off" << endl;
	removeTrigger();
        writeLED("/brightness", "0");
   }
   else if (cmd=="flash"){
        cout << "Flashing the LED" << endl;
        writeLED("/trigger", "timer");
        writeLED("/delay_on", "50");
        writeLED("/delay_off", "50");
   }
   else if (cmd=="status"){
	// display the current trigger details
        std::fstream fs;
	fs.open( LED3_PATH "/trigger", std::fstream::in);
	string line;
	while(getline(fs,line)) cout << line << endl;
	fs.close();
   }
   else{
	cout << "Invalid command!" << endl;
   }
   cout << "Finished the makeLED Program" << endl;
   return 0;
}

技术分享图片

 

 

 

2.1 利用软链接,根据指令来执行程序

主程序makeLEDmulti.cpp

 

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

#define LED3_PATH "/sys/class/leds/beaglebone:green:usr3"

void writeLED(string filename, string value){
   fstream fs;
   string path(LED3_PATH);
   fs.open((path + filename).c_str(), fstream::out);
   fs << value;
   fs.close();
}

void removeTrigger(){
  writeLED("/trigger", "none");
}

bool endsWith(string const &in, string const &comp){
   //int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
   
   return (0 == in.compare(in.length()-comp.length(), comp.length(), comp));//比较in字符串从相减结果个字符开始的comp.length组成的字符串与comp的大小
     // onled onled; 5-5, 5, onled
     // offled onled; 6-5, 5, onled
     
}

int main(int argc, char* argv[]){
   if(argc!=2){
	cout << "Usage is makeLEDmulti is by calling one of: " << endl;
        cout << "   flashled, onled, offled, ledstatus" << endl;
	cout << " e.g. sudo ./flashled" << endl;
   }
   string cmd(argv[0]);
   cout << "Starting the makeLEDmulti program" << endl;
   cout << "The current LED Path is: " << LED3_PATH << endl;

   // select whether command is on, off, flash or status
   if(endsWith(cmd,"onled")){
        cout << "Turning the LED on" << endl;
	removeTrigger();
        writeLED("/brightness", "1");
   }
   else if (endsWith(cmd,"offled")){
        cout << "Turning the LED off" << endl;
	removeTrigger();
        writeLED("/brightness", "0");
   }
   else if (endsWith(cmd,"flashled")){
        cout << "Flashing the LED" << endl;
        writeLED("/trigger", "timer");
        writeLED("/delay_on", "50");
        writeLED("/delay_off", "50");
   }
   else if (endsWith(cmd,"ledstatus")){
	// display the current trigger details
        std::fstream fs;
	fs.open( LED3_PATH "/trigger", std::fstream::in);
	string line;
	while(getline(fs,line)) cout << line << endl;
	fs.close();
   }
   else{
	cout << "Invalid command!" << endl;
   }
   cout << "Finished the makeLEDmulti Program" << endl;
   return 0;
}

build文件

#!/bin/bash
echo "Building the makeLED application"
g++ makeLEDmulti.cpp -o makeLEDmulti
if ! [ -e "onled" ]; then 
    ln -s makeLEDmulti onled
    echo "Created onled symbolic link" 
fi
if ! [ -e "offled" ]; then
    ln -s makeLEDmulti offled
    echo "Created offled symbolic link"
fi
if ! [ -e "ledstatus" ]; then
    ln -s makeLEDmulti ledstatus
    echo "Created ledstatus symbolic link"
fi
if ! [ -e "flashled" ]; then
    ln -s makeLEDmulti flashled
    echo "Created flashled symbolic link"
fi
echo "Finished"

技术分享图片

 

3. 使用Device Tree来设置引脚初始状态

这里设置 P9_11 PINS$28 GPIO0_30 = 30 Output Mode7 pulldown

P9_13 PINS$29 GPIO0_31 = 31 Input Mode7 pullup

YW-GPIO-Example.dts内容如下

 

/dts-v1/;
/plugin/;

/{
       compatible = "ti,beaglebone", "ti,beaglebone-black";
       part-number = "YW-GPIO-Example";
       version = "00A0";

       [email protected] {
             target = <&am33xx_pinmux>;
            
             __overlay__ {
                  yw_example: YW_GPIO_Example {
			pinctrl-single,pins = <
                                0x070 0x07  // P9_11 PINS$28 GPIO0_30 = 30 Output Mode7 pulldown
                                0x074 0x37  // P9_13 PINS$29 GPIO0_31 = 31 Input Mode7 pullup 
  
                               /* OUTPUT  GPIO(mode7) 0x07 pulldown, 0x17 pullup, 0x?f no pullup/down */
			       /* INPUT   GPIO(mode7) 0x27 pulldown, 0x37 pullup, 0x?f no pullup/down */
			>;
		  };
             };
       };

       [email protected] {
		target = <&ocp>;
		__overlay__ {
			gpio_helper {
				compatible = "gpio-of-helper";
				status = "okay";
				pinctrl-names = "default";
				pinctrl-0 = <&yw_example>;
			};
		};
	};
};

 

3.1 编译dts

这个是device tree source文件,系统并不能直接用,使用如下命令编译

 

dtc -O dtb -o YW-GPIO-Example-00A0.dtbo -b 0 [email protected] YW-GPIO-Example.dts

技术分享图片
要真正起作用还需要拷贝到指定目录,使用命令

sudo cp YW-GPIO-Example-00A0.dtbo /lib/firmware

3.2 查看已加载的Slot

加载Device Tree Overlay之前,我们查看一下系统中已经存在哪些SLOTS

技术分享图片

对于SLOTS4 P-O-L 这里的L就是Load(加载)。

3.3 加载Slot

加载我们之前编译的dtbo文件(可以不需要版本号那些哦),即part-number

技术分享图片

查看引脚状态

技术分享图片

技术分享图片

3.4 卸载Slot

卸载SLOT

技术分享图片

有时候为了避免引脚冲突,我们需要移除HDMI或EMMC SLOT,它们的移除步骤复杂一点还需要改动/boot/uEnv.txt文件

 

/media$ sudo mkdir /mnt/vfat
/media$ sudo mount /dev/mmcblk0p1 /mnt/vfat
/media$ cd /mnt/vfat
/mnt/vfat$ sudo vim uEnv.txt

将这句取消注释

 

 

##Disable HDMI
optargs=capemgr.disable_partno=BB-BONELT-HDMI,BB-BONELT-HDMIN

然后

 

 

/mnt/vfat$ cd ..
/mnt$ sudo umount /mnt/vfat
/mnt$ sudo reboot

重启后查看一下slots,cat /sys/devices/bone_capemgr.9/slots

 

3.5 自启动Slot

还是需要修改uEnv.txt,先去看看/lib/firmware下的dtbo文件

技术分享图片

仿照上面的写

 

optargs=capemgr.enable_partno=ADAFRUIT-SPI0

也可以vim /etc/default/capemgr来添加自启动的slot。
 

 

参考资料:

  1. 《Exploring BeagleBone: Tools and Techniques for Building with Embedded Linux》by Derek Molloyhttp://exploringbeaglebone.com/chapter6/
  2. https://github.com/jadonk/validation-scripts/tree/master/test-capemgr
  3. https://github.com/jadonk/cape-firmware/tree/master/arch/arm/boot/dts cape参考
  4. http://blog.csdn.net/wyt2013/article/details/14175275 export 任意引脚

=================================2016.11=============================================

http://www.righto.com/2016/08/the-beaglebones-io-pins-inside-software.html

 




以上是关于三种方式控制GPIO的主要内容,如果未能解决你的问题,请参考以下文章

gpio IOCTL控制

GPIO编程1:用文件IO的方式操作GPIO

sysfs方式访问gpio

sysfs方式访问gpio

Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

Linux下用文件IO的方式操作GPIO(/sys/class/gpio)(转)