This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ant-switch-backend-beagle-gpio
157 lines (145 loc) · 4.36 KB
/
ant-switch-backend-beagle-gpio
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
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# The MIT License (MIT)
#
# ant-switch-backend-beagle-gpio - beagle gpio backend to antenna switch
#
# This is part of KiwiSDR antenna switch extension
#
# Copyright (c) 2017-2023 Kari Karvonen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Safe GPIO pins
#
# Connector P8
#
# P8 pin 11 - Antenna 1
# P8 pin 12 - Antenna 2
# P8 pin 13 - Antenna 3
# P8 pin 14 - Antenna 4
# P8 pin 15 - Antenna 5
# P8 pin 16 - Antenna 6
# P8 pin 17 - Antenna 7
# P8 pin 18 - Antenna 8
# P8 pin 19 - Antenna 9
# P8 pin 26 - Antenna 10
#
VERSION=0.6
N_CH=10
AntSW_Platform() {
MODEL=`/usr/bin/tr < /proc/device-tree/model -d '\000'`
if [[ ${MODEL}x == *"BeagleBone AI-64x" ]]; then
PLATFORM="BBAI-64"
SAFEGPIOS=(360 359 389 375 361 362 303 304 388 351)
elif [[ ${MODEL}x == *"BeagleBone AIx" ]]; then
PLATFORM="BBAI"
SAFEGPIOS=(75 74 107 109 99 125 242 105 106 124)
elif [[ ${MODEL}x == *"AM335x"* ]]; then
PLATFORM="BBG/BBB"
SAFEGPIOS=(45 44 23 26 47 46 27 65 22 61)
else
echo "Error. Unknown platform: "${MODEL}
exit 1
fi
}
AntSW_ShowBackend() {
AntSW_Platform
echo "ant-switch-backend-beagle-gpio version ${VERSION} ${N_CH} ${PLATFORM}"
echo "More info: https://github.com/OH1KK/KiwiSDR-antenna-switch-extension/blob/master/docs/ant-switch-backend-beagle-gpio.txt"
}
AntSW_SetAddress() {
echo "not applicable"
}
AntSW_Initialize() {
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
AntSW_Platform
for GPIOPIN in "${SAFEGPIOS[@]}"; do
VAL=`cat /sys/class/gpio/gpio${GPIOPIN}/direction 2>/dev/null`
#echo ${GPIOPIN}=${VAL}
if [ "x$VAL" != "xout" ]; then
# Try automatically initialize
if [ "x$VAL" != "xin" ]; then
# don't export multiple times because that confuses BBAI
echo ${GPIOPIN} > /sys/class/gpio/export
fi
echo out > /sys/class/gpio/gpio${GPIOPIN}/direction
# Test again
VAL=`cat /sys/class/gpio/gpio${GPIOPIN}/value 2>/dev/null`
if [ "x$VAL" == "x" ]; then
echo "Error. GPIO pin $GPIOPIN failed to initialize."
exit 1
fi
fi
done
}
AntSW_GroundAll() {
for s in 1 2 3 4 5 6 7 8 9 10; do
AntSW_DeSelectAntenna $s
done
}
AntSW_SelectAntenna() {
IDX=$(($1-1))
ANT=${SAFEGPIOS[$IDX]}
echo 1 > /sys/class/gpio/gpio${ANT}/value
#echo W$1 ${ANT}=1
}
AntSW_DeSelectAntenna() {
IDX=$(($1-1))
ANT=${SAFEGPIOS[$IDX]}
echo 0 > /sys/class/gpio/gpio${ANT}/value
#echo W$1 ${ANT}=0
}
AntSW_ToggleAntenna() {
IDX=$(($1-1))
echo "IDX=$IDX"
ANT=${SAFEGPIOS[$IDX]}
# Read antenna status
VAL=`cat /sys/class/gpio/gpio${ANT}/value`
if [ "x$VAL" == "x0" ]; then
AntSW_SelectAntenna $1
else
AntSW_DeSelectAntenna $1
fi
}
AntSW_ShowSelected() {
# Read antenna status
selectedantennas=''
antennafound=0
antidx=0
for GPIOPIN in "${SAFEGPIOS[@]}"; do
antidx=$(($antidx+1))
VAL=`cat /sys/class/gpio/gpio${GPIOPIN}/value`
#echo R${antidx} ${GPIOPIN}=${VAL}
if [ "x${VAL}" == "x1" ]; then
selectedantennas="${selectedantennas}${antidx},"
antennafound=$(($antennafound+1))
fi
done
if [ "x$selectedantennas" == "x" ]; then
# All antenna inputs are grounded
antennafound=1
selectedantennas="g,"
fi
selectedantennas=${selectedantennas::-1}
echo -n "Selected antennas: "
echo $selectedantennas
}