三種方式控制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燈控制舉例:

root@mdm9607-perf:/sys/class/leds# ls
net_status
root@mdm9607-perf:/sys/class/leds# cd net_status/
root@mdm9607-perf:/sys/devices/soc:leds_gpio/leds/net_status# ls
brightness      delay_on        max_brightness  subsystem       uevent
delay_off       device          power           trigger
root@mdm9607-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
root@mdm9607-perf:/sys/devices/soc:leds_gpio/leds/net_status# cat trigger
none usb-online rfkill0 [timer]
root@mdm9607-perf:/sys/devices/soc:leds_gpio/leds/net_status# echo none>trigger
root@mdm9607-perf:/sys/devices/soc:leds_gpio/leds/net_status# echo 1 > brightnes
s
root@mdm9607-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";

       fragment@0 {
             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 */
			>;
		  };
             };
       };

       fragment@1 {
		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 -@ 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM