LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2022-04-14 f100b562fdb496adce766d80fdc9d99b1fbf8b54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# This shell script used to control GPIO port on RzBoard
 
direction=out
sysdir=/sys/class/gpio/
pinbase=120
groupin=8
 
pinnum=
pindir=
action=
 
set -u
set -e
 
# RedLed: P8_1  GreenLed: P17_2 BlueLed: P19_1
# UserKey: P39_0
function usage()
{
   echo "Show pinmap Usage: $0 [-v]"
   echo "Output set  Usage: $0 P9_1 [1/0]"
   echo "Input read  Usage: $0 [-i] P9_1"
   echo "Unexport    Usage: $0 [-u] P9_1"
   exit;
}
 
function show_pinmap()
{
   echo "
 +--------------------+--RzBoard--+--------------------+
 |      Name/GPIO     |  Physical |      Name/GPIO     |
 +--------------------+-----++----+--------------------+
 |        3.3v        |   1 ||  2 |         5v         |
 |      SDA2(P3_0)    |   3 ||  4 |         5v         |
 |      SCL2(P3_1)    |   5 ||  6 |        GND         |
 |     GPIO(P12_0)    |   7 ||  8 |     TXD2(P48_0)    |
 |        GND         |   9 || 10 |     RXD2(P48_1)    |
 |     SCL3(P48_3)    |  11 || 12 |     GPIO(P17_1)    |
 |     SDA3(P48_2)    |  13 || 14 |        GND         |
 |     GPIO(P17_0)    |  15 || 16 |     GPIO(P13_2)    |
 |        3.3v        |  17 || 18 |     GPIO(P14_0)    |
 |  SPI1_MOSI(P44_1)  |  19 || 20 |        GND         |
 |  SPI1_MISO(P44_2)  |  21 || 22 |     GPIO(P39_1)    |
 |  SPI1_CLK(P44_0)   |  23 || 24 |    SPI1_SS(P44_3)  |
 |        GND         |  25 || 26 |     GPIO(P0_1)     |
 |     GPIO(P14_1)    |  27 || 28 |     GPIO(P46_3)    |
 |     GPIO(P42_3)    |  29 || 30 |        GND         |
 |     GPIO(P42_4)    |  31 || 32 |     GPIO(P15_1)    |
 |     GPIO(P10_0)    |  33 || 34 |        GND         |
 |     GPIO(P9_1)     |  35 || 36 |     RTS2(P48_4)    |
 |     GPIO(P13_1)    |  37 || 38 |   CAN0_RX(P11_0)   |
 |        GND         |  39 || 40 |   CAN0_TX(P10_1)   |
 +--------------------+-----++----+--------------------+
 |      Name/GPIO     |  Physical |      Name/GPIO     |
 +--------------------+--RzBoard--+--------------------+
 "
   exit 0;
}
 
function calc_pinum()
{
   pinstr=$1
 
   group=`echo $pinstr | cut -d_ -f1 | tr -cd "[0-9]"`
   pin=`echo $pinstr | cut -d_ -f2 | tr -cd "[0-9]"`
   
   pinum=`expr $group \* $groupin + $pin + $pinbase`
   pindir=$sysdir/gpio$pinum
 
   #echo "INFO: GPIO $pinstr map to pinum[$pinum]"
}
 
function export_gpio()
{
    if [ -e $pindir ] ; then
       return ;
    fi
 
    echo $pinum > $sysdir/export
}
 
function unexport_gpio()
{
    if [ ! -e $pindir ] ; then
       return ;
    fi
 
    echo $pinum > $sysdir/unexport
}
 
function set_gpio()
{
    echo out > $pindir/direction
    echo $1 > $pindir/value
}
 
function read_gpio()
{
    echo in > $pindir/direction
    cat $pindir/value
}
 
if [ $# -lt 1 ] ; then
   usage;
fi
 
while getopts "iuvh" OPTNAME
do
    case "${OPTNAME}" in
        "i")
           direction=in
           shift
           ;;
 
        "u")
           action=unexport;
           shift
           ;;
 
        "v")
           show_pinmap;
           shift
           ;;
 
        "h")
           usage;
           ;;
    esac
done
 
calc_pinum $1
 
if [[ $action == unexport ]] ; then
   unexport_gpio
   exit;
fi
 
export_gpio
 
if [ $direction == in ] ; then
   read_gpio
else
   set_gpio $2
fi