-
Notifications
You must be signed in to change notification settings - Fork 22
/
lgpio
executable file
·370 lines (333 loc) · 8.05 KB
/
lgpio
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2021 Da Xue <[email protected]>
# PURPOSE: GPIO mapping and testing
#set -x
set -e
if [ -z "$VENDOR" ]; then
if [ ! -e /sys/class/dmi/id/board_vendor ]; then
echo "No vendor found!" >&2
exit 1
fi
VENDOR=$(tr -d '\0' < /sys/class/dmi/id/board_vendor)
fi
if [ -z "$BOARD" ]; then
if [ ! -e /sys/class/dmi/id/board_name ]; then
echo "No boardname found!" >&2
exit 1
fi
BOARD=$(tr -d '\0' < /sys/class/dmi/id/board_name)
fi
cd $(dirname $(readlink -f "${BASH_SOURCE[0]}"))
if [ ! -f "$VENDOR/$BOARD/gpio.map" ]; then
echo "GPIO map not available for this board." >&2
exit 1
fi
MAP_FILE="$VENDOR/$BOARD/gpio.map"
TAB_CHAR=' '
LGPIO_getColumn(){
local cols=$(grep -iE "^#\s*header" "$MAP_FILE" | grep -Eio "^.*$1($TAB_CHAR|$)")
if [ -z "$cols" ]; then
echo "$FUNCNAME: invalid column name: $1" >&2
exit 1
fi
local col=$(echo "${cols//[^$TAB_CHAR]}" | wc -c)
echo $((col - 1))
}
LGPIO_headersHelp(){
echo "$0 headers" >&2
}
LGPIO_headers(){
if [ $# -gt 0 ]; then
LGPIO_headersHelp
return 1
fi
grep -v '^#' "$MAP_FILE" | cut -f 1 -d "$TAB_CHAR" | uniq
}
LGPIO_headerHelp(){
echo "$0 header [HEADER]" >&2
}
LGPIO_header(){
if [ -z "$1" ]; then
LGPIO_headerHelp
return 1
fi
grep -iE "^#\s*header" "$MAP_FILE" | cut -f 2- -d "$TAB_CHAR" >&2
grep -i "$1$TAB_CHAR" "$MAP_FILE" | cut -f 2- -d "$TAB_CHAR"
}
LGPIO_getDefaultHeader(){
echo $(grep -v '^#' "$MAP_FILE" | head -n 1 | cut -f 1 -d "$TAB_CHAR")
}
LGPIO_infoHelp(){
echo "$0 info [HEADER] PIN [type=all,gpiod,chip,line,sysfs,name,pad,ref,desc]" >&2
}
LGPIO_info(){
if [ $# -lt 1 ]; then
LGPIO_infoHelp
return 1
fi
local header
if [ "$1" -eq "$1" ] 2> /dev/null; then
header=$(LGPIO_getDefaultHeader)
else
header="$1"
shift
fi
if ! grep -i "^$header$TAB_CHAR" "$MAP_FILE" > /dev/null; then
echo "FUNCNAME: header does not exist." >&2
exit 1
fi
local pin
if [ "$1" -ne "$1" ] 2> /dev/null; then
echo "$FUNCNAME: pin must be a number" >&2
exit 1
fi
pin="$1"
shift
if ! grep -i "^$header$TAB_CHAR$pin$TAB_CHAR" "$MAP_FILE"> /dev/null; then
echo "FUNCNAME: pin does not exist." >&2
exit 1
fi
if [ -z "$1" -o "${1,,}" = "all" ]; then
grep -Ei "#\s*header" "$MAP_FILE" | cut -f 3- -d "$TAB_CHAR" >&2
grep -i "^$header$TAB_CHAR$pin$TAB_CHAR" "$MAP_FILE" | cut -f 3- -d "$TAB_CHAR"
elif [ "${1,,}" = "gpiod" ]; then
grep -i "^$header$TAB_CHAR$pin$TAB_CHAR" "$MAP_FILE" | cut -f 3,4 -d "$TAB_CHAR"
elif [ "$1" = "${1/,}" ]; then
grep -i "^$header$TAB_CHAR$pin$TAB_CHAR" "$MAP_FILE" | cut -f $(LGPIO_getColumn "$1") -d "$TAB_CHAR"
else
local colnames
for col in $@; do
colnames="$colnames ${col//,/ }"
done
local cols=""
for colname in $colnames; do
cols="$cols,$(LGPIO_getColumn "$colname")"
done
grep -Ei "#\s*header" "$MAP_FILE" | cut -f "${cols:1}" -d "$TAB_CHAR" >&2
grep -i "^$header$TAB_CHAR$pin$TAB_CHAR" "$MAP_FILE" | cut -f "${cols:1}" -d "$TAB_CHAR"
fi
}
LGPIO_getHelp(){
echo "$0 get [HEADER] PIN" >&2
}
LGPIO_get(){
if [ $# -lt 1 -o -z "$1" ]; then
LGPIO_getHelp
return 1
elif [ $# -gt 2 ]; then
echo "$FUNCNAME: only support one pin at a time." >&2
return 1
fi
if [ -z "$2" ]; then
local header=$(LGPIO_getDefaultHeader)
else
local header_match=0
for header in $(LGPIO_headers); do
if [ "${header^^}" = "${1^^}" ]; then
header_match=1
break
fi
done
if [ "$header_match" -eq 0 ]; then
echo "$FUNCNAME: header $1 not found." >&2
return 1
fi
shift
fi
local pin="$1"
if [ ! -z "${pin//[0-9]}" ]; then
echo "$FUNCNAME: pin must be numeric." >&2
return 1
fi
local gpiod=$(LGPIO_info $header $pin gpiod)
if [ -z "${gpiod//[0-9$TAB_CHAR]}" ]; then
gpioget $gpiod
else
echo "$FUNCNAME: header $header pin $pin not a GPIO." >&2
return 1
fi
}
LGPIO_setHelp(){
echo "$0 set [HEADER_][PIN]={0,1} [HEADER_][PIN]={0,1}" >&2
}
LGPIO_set(){
if [ $# -lt 1 -o -z "$1" ]; then
LGPIO_setHelp
return 1
fi
while [ ! -z "$1" ]; do
if [ -z "${1//*_*=*}" ]; then
local header="${1//_*}"
if [ -z "$header" ]; then
echo "$FUNCNAME: unable to determine header parameter." >&2
return 1
fi
local pin="${1//*_}"
local pin="${pin//=*}"
elif [ -z "${1//*=*}" ]; then
local header=$(LGPIO_getDefaultHeader)
local pin="${1//=*}"
else
echo "$FUNCNAME: unable to determine header or pin from parameter." >&2
return 1
fi
if [ -z "$pin" ]; then
echo "$FUNCNAME: unable to determine header parameter." >&2
return 1
elif [ ! -z "${pin//[0-9]}" ]; then
echo "$FUNCNAME: pin must be numeric." >&2
return 1
fi
local value="${1//*=}"
if [ ! -z "${value/[01]}" ]; then
echo "$FUNCNAME: invalid GPIO value $value." >&2
return 1
fi
if [ -z "$pin" ]; then
echo "$FUNCNAME: unable to determine pin value parameter." >&2
return 1
fi
local gpiod=$(LGPIO_info $header $pin gpiod)
if [ -z "${gpiod//[0-9$TAB_CHAR]}" ]; then
gpioset $gpiod=$value
else
echo "$FUNCNAME: header $header pin $pin is not a GPIO." >&2
return 1
fi
shift
done
}
LGPIO_bcmHelp(){
echo "$0 bcm [PIN] [TYPE={all,gpiod,chip,line,sysfs,name,pad,ref,desc}]" >&2
}
LGPIO_bcm(){
if [ $# -lt 1 ]; then
LGPIO_bcmHelp
return 1
fi
declare -A BCM_GPIO2PIN
BCM_GPIO2PIN[2]=3
BCM_GPIO2PIN[3]=5
BCM_GPIO2PIN[4]=7
BCM_GPIO2PIN[14]=8
BCM_GPIO2PIN[15]=10
BCM_GPIO2PIN[17]=11
BCM_GPIO2PIN[18]=12
BCM_GPIO2PIN[27]=13
BCM_GPIO2PIN[22]=15
BCM_GPIO2PIN[23]=16
BCM_GPIO2PIN[24]=18
BCM_GPIO2PIN[10]=19
BCM_GPIO2PIN[9]=21
BCM_GPIO2PIN[25]=22
BCM_GPIO2PIN[11]=23
BCM_GPIO2PIN[8]=24
BCM_GPIO2PIN[7]=26
BCM_GPIO2PIN[SDA0]=27
BCM_GPIO2PIN[SCL0]=28
BCM_GPIO2PIN[ID_SD]=27
BCM_GPIO2PIN[ID_SC]=28
BCM_GPIO2PIN[5]=29
BCM_GPIO2PIN[6]=31
BCM_GPIO2PIN[12]=32
BCM_GPIO2PIN[13]=33
BCM_GPIO2PIN[19]=35
BCM_GPIO2PIN[16]=36
BCM_GPIO2PIN[26]=37
BCM_GPIO2PIN[20]=38
BCM_GPIO2PIN[21]=40
local bcm_gpio=$1
shift
if [[ ! -v BCM_GPIO2PIN[$bcm_gpio] ]]; then
echo "$FUNCNAME: bcm gpio does not exist." >&2
exit 1
fi
LGPIO_info ${BCM_GPIO2PIN[$bcm_gpio]} $@
}
LGPIO_MD_getBit(){
local data_byte=$(memtool md -b 0x$1+1 | cut -f 2 -d ":" | cut -f 2 -d " ")
[ $((0x${data_byte} & (1 << $2))) -gt 0 ] && echo -n 1 || echo -n 0
}
LGPIO_debugHelp(){
echo "$0 debug [BANK][_NUM]" >&2
}
LGPIO_debug(){
if [ ! -f "$VENDOR/$BOARD/gpio.config" ]; then
echo "GPIO config not available for this board." >&2
exit 1
fi
. "$VENDOR/$BOARD/gpio.config"
if [ -z "$GPIO_DEBUG_PATH" ]; then
echo "GPIO debug not available for this board." >&2
exit 1
fi
if [ ! -f "$GPIO_DEBUG_PATH" ]; then
echo "$GPIO debug helper not found." >&2
exit 1
fi
if ! test -r /dev/mem; then
echo "$FUNCNAME: memory not readable! sudo?" >&2
return 1
fi
. "$GPIO_DEBUG_PATH"
if [ -z "$1" ]; then
local banks=$(GPIO_BANK_get)
else
local banks=${1%%_*}
if ! GPIO_BANK_get $banks; then
echo "$banks does not exist." >&2
exit 1
fi
if [ "$1" != "$banks" ]; then
local bank_io_num=${1##*_}
local bank_io_cnt=$(GPIO_BANK_IO_CNT_get $banks)
local bank_io_off=$(GPIO_BANK_IO_OFF_get $banks)
if [ "$bank_io_num" -lt $bank_io_off ]; then
echo "$1 does not exist." >&2
exit 1
elif [ "$bank_io_num" -ge $((bank_io_off + bank_io_cnt)) ]; then
echo "$1 does not exist." >&2
exit 1
fi
fi
fi
echo "IO_NAME OEN/DIR PULL_EN PULL_UP" >&2
for bank in $banks; do
local bank_io_off=$(GPIO_BANK_IO_OFF_get $bank)
if [ -z "$bank_io_num" ]; then
local bank_io=0
local bank_io_cnt=${GPIO_BANK_IO_CNT[$bank]}
else
local bank_io=$((bank_io_num - $bank_io_off))
local bank_io_cnt=$((bank_io + 1))
fi
while [ $bank_io -lt $bank_io_cnt ]; do
GPIO_BANK_IO_OFF_getName $bank $bank_io
echo -n " "
LGPIO_MD_getBit $(GPIO_BANK_OEN_get $bank $bank_io)
echo -n " "
LGPIO_MD_getBit $(GPIO_BANK_PULL_EN_get $bank $bank_io)
echo -n " "
LGPIO_MD_getBit $(GPIO_BANK_PULL_DIR_get $bank $bank_io)
echo
bank_io=$((bank_io + 1))
done
done
}
LGPIO_help(){
LGPIO_headersHelp
LGPIO_headerHelp
LGPIO_infoHelp
LGPIO_getHelp
LGPIO_setHelp
LGPIO_bcmHelp
LGPIO_debugHelp
exit 1
}
cmd=help
if [ ! -z "$1" ]; then
cmd=$1
shift
fi
LGPIO_$cmd $@