GPIO 描述及 FIND GPIO on board
http://wiki.openwrt.org/doc/hardware/port.gpio
Example
If know basics of GPIO on Arduino or other platforms then good news is that you can also use GPIO on OpenWrt. In this example we will use GPIO29 and use it as a switch.
First step is making GPIO available in Linux:
echo "29" > /sys/class/gpio/exportthen you need to decide if it will be input or output, as we will use it as a switch so we need output
echo "out" > /sys/class/gpio/gpio29/directionand last line turns GPIO on or off with 1 or 0:
echo "1" > /sys/class/gpio/gpio29/value
Hardware
GPIOs are commonly used in router devices for buttons or leds. They only safely supply or sink (pull to GND) a maximum of 4mA aprox., and the voltage is usually 3V when active. Only two states are posible: high or low. Depending on how a device is activated by a GPIO, active low or active high is defined.
-
active high: the device is activated when the GPIO is HIGH
-
active low: the device is activated when the GPIO is LOW
In this image you can see how a GPIO is wired to buttons or leds, to work as active low or high
You can connect 5V digital signal sensors if you use a voltage devider to get a 3.3V range signal.You need to get acces to GND and 5V-power from the router and connect the sensor to them. Sensor signal output is in the 5V range, connect it to the voltage devider!
GPIOs can be also used for complex tasks:
| kernel module | description | |
|---|---|---|
| 1-wire | kmod-w1-master-gpio | 1-wire bus master |
| PWM | kmod-pwm-gpio | pulse width modulator |
| SPI | kmod-spi-gpio | bitbanging Serial Peripheral Interface |
| kmod-mmc-over-gpio | MMC/SD card over GPIO | |
| I2C | kmod-i2c-gpio | bitbanging I2C |
| LIRC | no module yet | Linux Infrared Remote Control |
| Rotary encoder | kmod-input-gpio-encoder | GPIO rotary encoder |
| rcswitch-kmod | rcswitch-kmod (not yet packaged in OpenWrt) | 433 MHz RC power outlets (switches) |
GPIO Interrupts
GPIO interrupts are useful when a GPIO is used as input and you need to manage high signal frequencies. Without interrupts, GPIO inputs must be managed using the polling method. With polling you cannot manage signal inputs with high frequencies.
Not all boards have GPIO interrupts. For example bcm63xx SoCs haven't GPIO interrupts and that's because their buttons are polled. As a result of this, some input drivers listed above won't work in these boards.
Software
In linux GPIOs can be accesed through GPIO SYSFS interface: /sys/class/gpio/
To control GPIOs you can use gpioctl-sysfs. Also manually or with this simple script you can control GPIOs not used by buttons or leds.
#!/bin/sh
show_usage()
{
printf "\ngpio.sh <gpio pin number> [in|out [<value>]]\n"
}
if [ \( $# -eq 0 \) -o \( $# -gt 3 \) ] ; then
show_usage
printf "\n\nERROR: incorrect number of parameters\n"
exit 255
fi
#doesn't hurt to export a gpio more than once
(echo $1 > /sys/class/gpio/export) >& /dev/null
if [ $# -eq 1 ] ; then
cat /sys/class/gpio/gpio$1/value
exit 0
fi
if [ \( "$2" != "in" \) -a \( "$2" != "out" \) ] ; then
show_usage
printf "\n\nERROR: second parameter must be 'in' or 'out'\n"
exit 255
fi
echo $2 > /sys/class/gpio/gpio$1/direction
if [ $# -eq 2 ] ; then
cat /sys/class/gpio/gpio$1/value
exit 0
fi
VAL=$3
if [ $VAL -ne 0 ] ; then
VAL=1
fi
echo $VAL > /sys/class/gpio/gpio$1/value
Finding GPIO pins on the PCB
Sometimes you do not know where the physical GPIO pins are on your device's PCB. In that case, you can use this little script and a multimeter to find out.
#!/bin/sh
cd /sys/class/gpio
for i in `seq $1 $2`; do
echo $i > export; echo out >gpio$i/direction
done
nums=`seq $1 $2`
while true; do
for i in $nums; do
echo 0 > gpio$i/value
done
sleep 1
for i in $nums; do
echo 1 > gpio$i/value
done
sleep 1
done
-
Start with
./gpio 0 30, which means pin 0 to 30 -
Press ctrl-c to stop the script, then check which GPIOs have been created:
find /sys/class/gpio/gpio* -
Restart the script and measure with a multimeter which pins "blink".
-
When you find one, then cut the 0-30 range from above in half;
-
Repeat until you have identified the gpio number

浙公网安备 33010602011771号