Documentation:NGW/NGW100 Switched Input
From AVRFreaks Wiki
Contents |
[edit] This is a Demo of how to read a switched input from a GPIO pin from the NGW100
[edit] How to determine the Pin
- gpio_id: Which PIO controller instance to use. 0 for PIOA, 1 for PIOB and so on.
| Port | A | B | C | D | E |
|---|---|---|---|---|---|
| gpio_id | 0 | 1 | 2 | 3 | 4 |
- pin_mask: Which pins to use on the selected PIO controller
- oe_mask: Which pins to set up as outputs (oe = output enable)
| Pin Function | Input | Ouput |
|---|---|---|
| oe_mask | 0 | 1 |
pin_mask and oe_mask are both 32-bit bitmaps, with one bit corresponding to each pin on the PIO controller. Pin 0 is the rightmost (least significant) bit.
You can't set any bits in pin_mask that are already reserved for other purposes, and you can't set any bits in oe_mask that aren't set in pin_mask.
[edit] For example on the NGW100
if we connect a switch to PE03 (pin 1 of J7) high
This will create a entry in /dev/gpioX
mkdir /config/gpio/switch1 cd /config/gpio/switch1 echo 4 > gpio_id echo 0x00000008 > pin_mask echo 0x00000000 > oe_mask echo 1 > enabled
You Now should have a extra device /dev/gpio3
cat /dev/gpio3
Hint: use putty in windows as it will show you difference between "0" and "Not 0" (cursor moves from the left of screen to right)
Left Cursor = Low or 0V
Right Cursor = High or 3.3V
New Info: This is better than 'cat'
less /dev/gpio3
| Pin | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Bit | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 |
| pin_mask | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| Hex | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8 | ||||||||||||||||||||||||
pin_mask = 0x00000008
| Pin | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Bit | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 |
| oe_mask | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| Hex | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||||||||||||||||||||||||
oe_mask = 0x00000000
echo 1 > enabled
Creates the /dev/gpioX
if there is a problem with it will not be created
cat /config/gpio/switch1/enable
if this is 0 it has not been enable and /dev/gpioX has NOT been created.
[edit] Read LED example
This is a quick demo of how to read the status of the LED form the shell
echo -ne "\x00\x00\x00\x00" > /dev/gpio2
Turns LED B ON
cat /dev/gpio2
The cursor should be NEWLINE on Left (ctrl-c to stop)
echo -ne "\x00\x08\x00\x00" > /dev/gpio2
Turns LED B OFF
cat /dev/gpio2
The cursor should be on the Right (No Newline) (ctrl-c to stop)
'Hint:' LED is connected sink
A better method to read LED status would be:
Grab four bytes from /dev/gpio2
hexdump -n 4 /dev/gpio2
or if you want to remove the address added by hexdump:
hexdump -n 4 /dev/gpio2 | cut -d ' ' -f 2,3 -s
[edit] For Reference
This is how the startup Script creates the device for the 3 LED's (A B and SYS)
~ # cat /etc/init.d/S99gpio
#!/bin/sh
echo -n " * get board type for GPIO ... "
if [ ! -e "/etc/release" ]; then
echo "[ MISSING ]";
fi
BOARD=`cat /etc/release | cut -d ' ' -f1`
if [ "${BOARD}" == "" ]; then
echo "[ FAILED ]";
elif [ "${BOARD}" == "NGW" ]; then
echo "'${BOARD}'"
echo -n " * setup GPIO boot LED ... "
if mkdir /config/gpio/bootled > /dev/null 2> /dev/null; then
if ! echo 0 > /config/gpio/bootled/gpio_id; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0x10000 > /config/gpio/bootled/pin_mask; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0x10000 > /config/gpio/bootled/oe_mask; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 1 > /config/gpio/bootled/enabled; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0x10000 > /dev/gpio0; then
echo "[ FAILED ]"
exit 1
fi
echo "[ OK ]"
else
echo "[ FAILED ]"
exit 1
fi
echo -n " * setup GPIO LED A ... "
if mkdir /config/gpio/leda > /dev/null 2> /dev/null; then
if ! echo 0 > /config/gpio/leda/gpio_id; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0x80000 > /config/gpio/leda/pin_mask; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0x80000 > /config/gpio/leda/oe_mask; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 1 > /config/gpio/leda/enabled; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0 > /dev/gpio1; then
echo "[ FAILED ]"
exit 1
fi
echo "[ OK ]"
else
echo "[ FAILED ]"
exit 1
fi
echo -n " * setup GPIO LED B ... "
if mkdir /config/gpio/ledb > /dev/null 2> /dev/null; then
if ! echo 4 > /config/gpio/ledb/gpio_id; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0x80000 > /config/gpio/ledb/pin_mask; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0x80000 > /config/gpio/ledb/oe_mask; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 1 > /config/gpio/ledb/enabled; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0 > /dev/gpio2; then
echo "[ FAILED ]"
exit 1
fi
echo "[ OK ]"
else
echo "[ FAILED ]"
exit 1
fi
elif [ "${BOARD}" == "STK1000" ]; then # end NGW
echo "'${BOARD}'"
echo -n " * setup GPIO switches ... "
if mkdir /config/gpio/switches > /dev/null 2> /dev/null; then
if ! echo 1 > /config/gpio/switches/gpio_id; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 0xff > /config/gpio/switches/pin_mask; then
echo "[ FAILED ]"
exit 1
fi
if ! echo 1 > /config/gpio/switches/enabled; then
echo "[ FAILED ]"
exit 1
fi
echo "[ OK ]"
else
echo "[ FAILED ]"
exit 1
fi
else # end STK1000
echo ${BOARD}
echo " * no GPIO for this board"
fi
exit 0
[edit] i2c stuff
mkdir /config/gpio/gpio0" echo 0 > /config/gpio/gpio0/gpio_id echo 0x000000C0 > /config/gpio/gpio0/pin_mask /* mask PA 6, 7 */ echo 0x000000C0 > /config/gpio/gpio0/oe_mask /* mask PA 6, 7 as output */ echo 1 > /config/gpio/gpio0/enabled echo -ne '\x2e' > /dev/i2c-0
or
cat /dev/i2c-0
NOTE: I don't know who put this here or why, but you should not have to use gpiodev to configure directionality of i2c pins. Unless someone can clarify why and when this will be needed, it will be removed. --Squidgit.
