forked from espressif/esp-hosted
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpi_init.sh
More file actions
executable file
·533 lines (467 loc) · 13.2 KB
/
rpi_init.sh
File metadata and controls
executable file
·533 lines (467 loc) · 13.2 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only
#
# Espressif Systems Wireless LAN device driver
#
# SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
#
# SPDX-License-Identifier: GPL-2.0-only
#
################## USER config (porting) ######################
# Module param, resetpin: Mandatory for SPI
resetpin="518"
# Module param, clockspeed : clock freq of SPI or SDIO - Optional
# Defaults: SPI: 10MHz SDIO: As per device tree, typically 25MHz or 50MHz
clockspeed="10"
# Module param, spi_bus: spi bus instance to use
spi_bus="0"
# Module param, spi_cs: spi cs instance to use
spi_cs="0"
# Module param, spi_mode: spi mode to use from (1/2/3)
spi_mode="3"
# Module param, spi_handshake: spi handshake GPIO to use
spi_handshake="534"
# Module param, spi_dataready: spi dataready GPIO to use
spi_dataready="539"
################## Local params for host ######################
cpu_perf="on"
# Old Raspberry Pi config
#resetpin="6"
#spi_handshake="22"
#spi_dataready="27"
XTRA_MODULE_PARAMS=""
################## Bluetooth Config ###########################
# 1) Bluetooth over HCI : Bluetooth over spi/sdio
# No extra pins or config needed. Reuse SPI/SDIO pins for Wi-Fi+Bluetooth
# Use: BT_CONFIG="bt_using_hci"
# 2) Bluetooth over UART : Wi-Fi over spi/sdio. Bluetooth over dedicated UART
# Use: BT_CONFIG="bt_using_uart_2pins" OR
# BT_CONFIG="bt_using_uart_4pins"
# 2 pins UART: Tx and Rx only
# 4 pins UART: Tx, Rx, RTS, CTS.
# Extra configuration needed!
# - Connect extra pins
# - Configure these 2/4 UART pins in your device tree as UART pins
# 3) Disable Bluetooth (Only Wi-Fi)
# Use: BT_CONFIG=""
BT_CONFIG=""
#BT_CONFIG="bt_using_hci"
#BT_CONFIG="bt_using_uart_2pins"
#BT_CONFIG="bt_using_uart_4pins"
################## Wi-Fi config ##############################
# 1) wifi enabled : 'wifi_on' (default)
# 2) wifi disabled : 'wifi_off'
WIFI_CONFIG="wifi_on"
#WIFI_CONFIG="wifi_off"
############ Cross compilation options ########################
CROSS_COMPILE="/bin/arm-linux-gnueabihf-"
KERNEL_BUILD_DIR="/lib/modules/$(uname -r)/build"
ARCH=""
TEST_RAW_TP="0"
IF_TYPE=""
MODULE_NAME="esp32_${IF_TYPE}.ko"
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
warn() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] **Warn** $1"
}
log_enter() {
log "Entering ${FUNCNAME[1]}"
}
log_exit() {
log "Exiting ${FUNCNAME[1]}"
}
device_tree_dependency_spi() {
log_enter
cd $SCRIPT_DIR
log "Current dir: $PWD"
rm spidev_disabler.dtbo > /dev/null
dtc spidev_disabler.dts -O dtb > spidev_disabler.dtbo
sudo dtoverlay -d . spidev_disabler
cd $MAKE_DIR
log_exit
}
device_tree_dependency_uart_2pins() {
log_enter
gpio_pin_ctl=pinctrl
$gpio_pin_ctl 2> /dev/null
if [ $? -ne 0 ]; then
gpio_pin_ctl=raspi-gpio
fi
sudo $gpio_pin_ctl set 15 a0 pu
sudo $gpio_pin_ctl set 14 a0 pu
log_exit
}
device_tree_dependency_uart_4pins() {
log_enter
gpio_pin_ctl=pinctrl
$gpio_pin_ctl 2> /dev/null
if [ $? -ne 0 ]; then
gpio_pin_ctl=raspi-gpio
fi
sudo $gpio_pin_ctl set 15 a0 pu
sudo $gpio_pin_ctl set 14 a0 pu
sudo $gpio_pin_ctl set 16 a3 pu
sudo $gpio_pin_ctl set 17 a3 pu
log_exit
}
build_esp_hosted_rpc_lib() {
log_enter
cd $RPC_LIB_DIR
make -j8
LIB_PATH="$RPC_LIB_DIR/libesp_hosted_rpc.a"
if [ ! -f "$LIB_PATH" ]; then
log "ERROR: Failed to build libesp_hosted_rpc.a, exiting"
exit 1
fi
cd $SCRIPT_DIR
log_exit
}
build_c_demo_app() {
log_enter
# Check if replxx library is available by looking for the header file
if [ ! -f /usr/include/replxx.h ] && [ ! -f /usr/local/include/replxx.h ]; then
warn "replxx library not found. Skipping hosted_shell build."
warn "Install replxx from https://github.com/AmokHuginnsson/replxx"
warn "After installing, run 'make -j8' in 'host/linux/host_control/c_support' directory"
fi
cd $SCRIPT_DIR/c_support/
make -j8
if [ $? -ne 0 ]; then
log "Failed to build C demo apps"
exit 1
fi
cd ..
log_exit
}
build_python_demo_app() {
log_enter
cd $SCRIPT_DIR/python_support/
make -j8
if [ $? -ne 0 ]; then
log "Failed to build python demo app"
exit 1
fi
cd ..
log_exit
}
build_user_space_apps() {
log_enter
build_esp_hosted_rpc_lib
build_c_demo_app
build_python_demo_app
log_exit
}
remove_module() {
log_enter
if [ "$(lsmod | grep esp32 | wc -l)" != "0" ]; then
if [ "$(lsmod | grep esp32_sdio | wc -l)" != "0" ]; then
sudo rmmod esp32_sdio &> /dev/null
else
sudo rmmod esp32_spi &> /dev/null
fi
if [ $? -ne 0 ]; then
log "Failed to remove esp kernel module"
exit 1
fi
log "esp module removed using script"
fi
log_exit
}
build_module()
{
if [ "$TEST_RAW_TP" = "0" ] ; then
VAL_CONFIG_TEST_RAW_TP=n
else
VAL_CONFIG_TEST_RAW_TP=y
fi
if [ "$BT_CONFIG" != "" ] ; then
VAL_BT_ENABLED=y
else
VAL_BT_ENABLED=n
fi
# For Linux other than Raspberry Pi, Please uncomment below 'make' line and provide:
# <CROSS_COMPILE> -> <Toolchain-Path>/bin/arm-linux-gnueabihf-
# <KERNEL> -> Place where kernel is checked out and built. For Example, "/lib/modules/$(uname -r)/build"
# <ARCH> -> Architecture. for example, arm64
#make -j8 target=$IF_TYPE CROSS_COMPILE=<CROSS_COMPILE> KERNEL=<KERNEL> ARCH=<ARCH> CONFIG_TEST_RAW_TP="$VAL_CONFIG_TEST_RAW_TP"
# Also, Check detailed doc, esp_hosted_fg/docs/Linux_based_host/porting_guide.md for more details.
# Populate your arch if not populated correctly.
if [ "$ARCH" = "" ]; then
arch_num_bits=$(getconf LONG_BIT)
if [ "$arch_num_bits" = "32" ] ; then arch_found="arm"; else arch_found="arm64"; fi
ARCH="$arch_found"
fi
cd $MAKE_DIR
log "Current dir: $PWD"
log "Building for target $IF_TYPE"
log "Using ARCH as : $ARCH"
log "Using KERNEL as $KERNEL_BUILD_DIR"
log "Using CONFIG_TEST_RAW_TP as $CONFIG_TEST_RAW_TP"
make -j8 target="$IF_TYPE" KERNEL="$KERNEL_BUILD_DIR" ARCH="$ARCH" CONFIG_TEST_RAW_TP="$VAL_CONFIG_TEST_RAW_TP" CONFIG_BT_ENABLED="$VAL_BT_ENABLED"
# Check the exit status of make
if [ $? -ne 0 ] ; then
log "Failed to build the esp kernel module"
exit -1
fi
}
load_bluetooth_module()
{
sudo modprobe bluetooth
if [ `lsmod | grep bluetooth | wc -l` = "0" ]; then
log "Failed to insert bluetooth module, comment if bluetooth is configured directly in kernel"
exit -1
fi
}
insert_module() {
log_enter
# Check and apply bluetooth config
if [ "$BT_CONFIG" != "" ]; then
load_bluetooth_module
if [ "$BT_CONFIG" = "bt_using_uart_2pins" ]; then
log "Wi-Fi ($IF_TYPE) + Bluetooth (UART-2pins)"
device_tree_dependency_uart_2pins
elif [ "$BT_CONFIG" = "bt_using_uart_4pins" ]; then
log "Wi-Fi ($IF_TYPE) + Bluetooth (UART-4pins)"
device_tree_dependency_uart_4pins
elif [ "$BT_CONFIG" = "bt_using_hci" ]; then
log "$IF_TYPE only setup: Wi-Fi+Bluetooth both over $IF_TYPE"
else
log "Incorrect bluetooth config"
exit 1
fi
fi
# Additional dependency for SPI
if [ "$IF_TYPE" = "spi" ]; then
device_tree_dependency_spi
fi
# Insert module with parameters
sudo insmod $MODULE_NAME $XTRA_MODULE_PARAMS
if [ $? -ne 0 ]; then
log "Failed to insert module"
exit 1
fi
if [ "$(lsmod | grep esp32 | wc -l)" != "0" ]; then
log "esp32 module inserted using script"
else
log "Failed to insert esp32 module"
exit 1
fi
log_exit
}
parse_arguments() {
log_enter
while [ "$1" != "" ]; do
case $1 in
--help | -h )
usage
exit 0
;;
wifi=*)
WIFI_TP=${1#*=}
if [ "$WIFI_TP" = "spi" ]; then
log "Wi-Fi on SPI"
elif [ "$WIFI_TP" = "sdio" ]; then
log "Wi-Fi on SDIO"
elif [ "$WIFI_TP" = "-" ]; then
log "****** Disable Wi-Fi ******"
WIFI_TP=""
else
log "****** Unsupported Wi-Fi transport[$WIFI_TP] ******"
log "Possible transport values: 'spi' / 'sdio' / '-'"
exit 1
fi
;;
resetpin=*)
log "Recvd Option: $1"
resetpin=${1#*=}
;;
bt=*)
log "Recvd Option: $1"
BT_TP=${1#*=}
if [ "$BT_TP" = "spi" ]; then
BT_CONFIG="bt_using_hci"
log "BT on SPI"
elif [ "$BT_TP" = "sdio" ]; then
BT_CONFIG="bt_using_hci"
log "BT on SDIO"
elif [ "$BT_TP" = "uart_2pins" ]; then
BT_CONFIG="bt_using_uart_2pins"
log "BT on UART (Tx, RX)"
elif [ "$BT_TP" = "uart_4pins" ]; then
BT_CONFIG="bt_using_uart_4pins"
log "BT on UART (Tx, RX, CTS, RTS)"
elif [ "$BT_TP" = "-" ]; then
BT_CONFIG=""
log "****** Disable BT ******"
BT_TP=""
else
log "****** Unsupported BT transport[$BT_TP] ******"
log "Possible transport values: 'spi' / 'sdio' / 'uart_2pins' / 'uart_4pins' / '-'"
exit 1
fi
;;
rawtp)
log "Test RAW TP"
TEST_RAW_TP="1"
;;
clockspeed=*)
clockspeed=${1#*=}
log "Clock freq: $clockspeed MHz"
;;
spi_bus=*)
spi_bus=${1#*=}
log "SPI bus: $spi_bus"
;;
spi_cs=*)
spi_cs=${1#*=}
log "SPI CS: $spi_cs"
;;
spi_mode=*)
spi_mode=${1#*=}
log "SPI Mode: $spi_mode"
;;
spi_handshake=*)
spi_handshake=${1#*=}
log "SPI handshake gpio: $spi_handshake"
;;
spi_dataready=*)
spi_dataready=${1#*=}
log "SPI dataready gpio: $spi_dataready"
;;
cpu_perf=*)
cpu_perf=${1#*=}
log "Set CPU performance: $cpu_perf"
;;
*)
log "$1 : unknown option"
usage
exit 1
;;
esac
shift
done
verify_transport_combination
verify_clock_freq
log_exit
}
verify_transport_combination()
{
if [ "$WIFI_TP" != "" ]; then
IF_TYPE=$WIFI_TP
fi
if [ "$BT_TP" != "" ]; then
if [ "$BT_TP" != "uart_2pins" ] && [ "$BT_TP" != "uart_4pins" ]; then
if [ "$WIFI_TP" != "" ] && [ "$BT_TP" != "$WIFI_TP" ]; then
log "Transport combination unsupported: Wifi[$WIFI_TP] BT[$BT_TP]"
exit 1
fi
IF_TYPE=$BT_TP
fi
fi
if [ "$IF_TYPE" = "" ] ; then
echo ""
log "transport combination unsupported. At least, Wi-Fi or BT need to run over SPI/SDIO"
echo ""
usage
exit 1
fi
}
verify_clock_freq()
{
if [ "$clockspeed" != "" ]; then
if [ "$IF_TYPE" = "spi" ] && [ $clockspeed -gt 40 ]; then
log "SPI slave clock freq [$clockspeed] not supported"
exit 1
elif [ "$IF_TYPE" = "sdio" ] && [ $clockspeed -gt 50 ]; then
log "SDIO slave clock [$clockspeed] not supported"
exit 1
fi
fi
}
add_module_param()
{
local param_name=$1
if [ "${!param_name}" = "" ]; then
warn "Param, $param_name not configured, ignoring"
else
XTRA_MODULE_PARAMS="$XTRA_MODULE_PARAMS $param_name=${!param_name}"
log "Adding module_param '$param_name=${!param_name}'"
fi
}
# Example usage function
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " --help, -h Show this help message"
echo " wifi=<value> Set bluetooth transport"
echo " > 'sdio' <Use Wi-Fi over SDIO>"
echo " > 'spi' <Use Wi-Fi over SPI>"
echo " > '-' <Disable Wi-Fi>"
echo " resetpin=<gpio> Set the reset pin GPIO"
echo " bt=<value> Set bluetooth transport"
echo " > 'spi' <Use bluetooth over SPI>"
echo " > 'sdio' <Use bluetooth over SDIO>"
echo " > 'uart_2pins' <Use bluetooth over UART Tx,Rx>"
echo " > 'uart_4pins' <Use bluetooth over UART Tx,Rx,CTS,RTS>"
echo " > '-' <Disable bluetooth>"
echo " clockspeed=<freq_in_mhz> Set SPI/SDIO clock frequency to be used"
echo " SPI Default: 10MHz"
echo " SDIO Default: As per Device Tree (25 or 50MHz)"
echo " spi_bus=<num> Use this SPI bus instance"
echo " spi_cs=<num> Use this ChipSelect instance"
echo " spi_mode=<num> Use this SPI mode"
echo " spi_handshake=<gpio_num> SPI Handshake GPIO"
echo " spi_dataready=<gpio_num> SPI DataReady GPIO"
echo " rawtp Test RAW TP"
echo " cpu_perf=<on/off> Change cpu performance level(may need porting)"
echo ""
}
populate_module_params()
{
# Populate module params
add_module_param "resetpin"
add_module_param "clockspeed"
if [ "$IF_TYPE" = "spi" ]; then
add_module_param "spi_bus"
add_module_param "spi_cs"
add_module_param "spi_mode"
add_module_param "spi_handshake"
add_module_param "spi_dataready"
fi
}
port_populate_local_params()
{
if [ "$cpu_perf" = "on" ] ; then
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
sudo sh -c "echo performance > $cpu"
done
elif [ "$cpu_perf" = "off" ] ; then
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
sudo sh -c "echo ondemand > $cpu"
done
fi
echo "Current CPU governor settings:"
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
echo "$cpu: $(cat $cpu)"
done
}
######## Script ##########
SCRIPT_DIR=$PWD
MAKE_DIR=$SCRIPT_DIR/../host_driver/esp32/
RPC_LIB_DIR=$SCRIPT_DIR/../../control_lib
cd $MAKE_DIR
parse_arguments "$@"
log "Building for $IF_TYPE protocol"
MODULE_NAME=esp32_${IF_TYPE}.ko
populate_module_params
port_populate_local_params
build_user_space_apps
remove_module
build_module
insert_module
log "--- finished ---"
log ""
log "Please proceed to choose desired 'C' or 'Python' demo app"
log "Refer for detailed setup: https://github.com/espressif/esp-hosted/blob/master/esp_hosted_fg/docs/Linux_based_host/Getting_started.md"