Skip to content

Commit 049289a

Browse files
author
Michael Stoops
committed
Numerous changes coming from code review
1 parent 9fbed81 commit 049289a

File tree

4 files changed

+133
-155
lines changed

4 files changed

+133
-155
lines changed

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,44 @@
22

33
Raspberry Pi Pico Setup provides a script for installing the Pico SDK and toolchain.
44

5-
# How-To
5+
## How-To
66

77
Download and run `pico_setup.sh`:
8+
89
```shell
910
wget https://raw.githubusercontent.com/raspberrypi/pico-setup/master/pico_setup.sh
10-
chmod +x pico_setup
11-
./pico_setup
11+
chmod +x pico_setup.sh
12+
./pico_setup.sh
1213
```
14+
1315
The script uses sudo, so you may need to enter your password.
1416

1517
After the script is complete, reboot to ensure that all changes take effect, such as the UART settings and environment variables.
1618

1719
If you want the testing script and documentation, you can clone the git repo too.
1820

19-
# Support
21+
## Support
2022

2123
This script works on most Debian-derived Linux distros and macOS, running on common Raspberry Pi, PC, and Mac hardware. This ***DOESN'T*** mean that all of the pico tools work properly on these platforms. It just means that this script runs and passes its own tests.
2224

2325
Operating systems:
26+
2427
* Raspberry Pi OS (32-bit)
2528
* Debian 10 (Buster)
26-
* Ubuntu 20.10 (Groovy)
29+
* Ubuntu 20.04 or later
2730
* macOS 11 (Big Sur)
28-
* Ubuntu 20.04 on Windows Subsystem for Linux on Windows Server 2019 Base
29-
* Debian GNU/Linux 1.3.0.0 on Windows Subsystem for Linux on Windows Server 2019 Base
30-
31-
This script does not support Windows natively, only Windows Subsystem for Linux.
31+
* Windows Subsystem for Linux
3232

3333
Hardware:
34-
* Raspberry Pi 2/3/4/400/CM3/CM4
34+
35+
* Any model of Raspberry Pi
3536
* PC (x86_64)
3637
* Mac (both Intel and Apple Silicon)
3738

38-
Other OSes and hardware may work, but haven't been tested. Use at your own risk.
39+
Visual Studio Code may not run well on Raspberry Pi 1 and Zero because of their smaller RAM capacity, but the rest of the toolkit works fine.
40+
41+
Other OSes and hardware _may_ work, but haven't been tested. Use at your own risk.
3942

40-
# Testing
43+
## Testing
4144

4245
See [test/README.md](test/README.md).

pico_setup.sh

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
# Phase 0: Preflight check
44
# Verify baseline dependencies
55

6-
# Phase 1: Setup minimum dev environment
7-
# Install the toolchain
6+
# Phase 1: Setup dev environment
7+
# Install the software packages from APT or Homebrew
88
# Create a directory called pico
99
# Download the pico-sdk repository and submodules
10-
# Define env variables for repo: PICO_SDK_PATH
11-
# Configure the Raspberry Pi UART for use with Raspberry Pi Pico
10+
# Define env variables: PICO_SDK_PATH
11+
# On Raspberry Pi only: configure the UART for use with Raspberry Pi Pico
1212

1313
# Phase 2: Setting up tutorial repos
1414
# Download pico-examples, pico-extras, pico-playground repositories, and submodules
@@ -24,13 +24,19 @@
2424
set -e
2525
# Show all commands
2626
set -x
27-
27+
# Trying to use an non-existent variable is an error
28+
set -u
2829

2930
# Number of cores when running make
3031
JNUM=4
3132

3233
# Where will the output go?
33-
WORKING_DIR="$(pwd)/pico"
34+
if printenv TARGET_DIR; then
35+
echo "Using target dir from \$TARGET_DIR: ${TARGET_DIR}"
36+
else
37+
TARGET_DIR="$(pwd)/pico"
38+
echo "Using target dir: ${TARGET_DIR}"
39+
fi
3440

3541

3642
linux() {
@@ -73,7 +79,7 @@ raspberry_pi() {
7379
}
7480

7581
phase_0() {
76-
# Preflight the check
82+
# Preflight check
7783
# Checks the baseline dependencies. If you don't have these, this script won't work.
7884
echo "Entering phase 0: Preflight check"
7985

@@ -108,12 +114,12 @@ phase_0() {
108114
fi
109115
}
110116

111-
install_toolchain_linux() {
112-
# Install toolchain for Linux
117+
install_dev_env_deps_linux() {
118+
# Install development environment dependencies for Linux
113119

114-
DEPS="python3 git cmake gcc-arm-none-eabi build-essential gdb-multiarch automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev"
120+
DEPS="python3 git cmake gcc-arm-none-eabi build-essential gdb-multiarch automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev minicom pkg-config"
115121
if debian || ubuntu; then
116-
DEPS="${DEPS} pkg-config libstdc++-arm-none-eabi-newlib"
122+
DEPS="${DEPS} libstdc++-arm-none-eabi-newlib"
117123
fi
118124
sudo apt install -y ${DEPS}
119125
}
@@ -124,51 +130,55 @@ brew_install_idempotent() {
124130
return ${?}
125131
}
126132

127-
install_toolchain_mac() {
128-
# Install dependencies for mac
133+
install_dev_env_deps_mac() {
134+
# Install development environment dependencies for mac
129135

130-
brew_install_idempotent git cmake pkg-config libtool automake libusb wget pkg-config gcc texinfo
131-
brew tap ArmMbed/homebrew-formulae
132-
brew_install_idempotent arm-none-eabi-gcc
136+
brew_install_idempotent git cmake pkg-config libtool automake libusb wget pkg-config gcc texinfo minicom ArmMbed/homebrew-formulae/arm-none-eabi-gcc
133137
}
134138

135139
create_working_dir() {
136140
# Creates ./pico directory if necessary
137141

138-
mkdir -p "${WORKING_DIR}"
142+
mkdir -p "${TARGET_DIR}"
139143
}
140144

141-
clone_repo() {
145+
clone_raspberrypi_repo() {
142146
# Clones the given repo name from GitHub and inits any submodules
143147
# $1 should be the full name of the repo, ex: pico-sdk
144148
# $2 should be the branch name. Defaults to master.
145149
# all other args are passed to git clone
146150
REPO_NAME="${1}"
147-
if shift && [ "${1}" ]; then
151+
if shift && [ ${#} -gt 0 ]; then
148152
BRANCH="${1}"
153+
# Can't just say `shift` because `set -e` will think it's an error and terminate the script.
154+
shift || true
149155
else
150156
BRANCH=master
151157
fi
152-
if shift; then
153-
# $* contains more args
154-
true
155-
fi
156158

157-
cd "${WORKING_DIR}"
159+
# Save the working directory
160+
pushd "${TARGET_DIR}" >> /dev/null
158161

159162
REPO_URL="https://github.com/raspberrypi/${REPO_NAME}.git"
160-
DEST="${WORKING_DIR}/${REPO_NAME}"
163+
DEST="${TARGET_DIR}/${REPO_NAME}"
161164

162165
if [ -d "${DEST}" ]; then
163166
echo "Not cloning ${DEST} because it already exists. If you really want to start over, delete it: rm -rf ${DEST}"
164167
else
165168
echo "Cloning ${REPO_URL}"
166-
git clone -b "${BRANCH}" "${REPO_URL}" ${*}
169+
if [ ${#} -gt 0 ]; then
170+
git clone -b "${BRANCH}" "${REPO_URL}" ${*}
171+
else
172+
git clone -b "${BRANCH}" "${REPO_URL}"
173+
fi
167174

168175
# Any submodules
169176
cd "${DEST}"
170177
git submodule update --init
171178
fi
179+
180+
# Restore working directory
181+
popd >> /dev/null
172182
}
173183

174184
set_env() {
@@ -187,10 +197,8 @@ set_env() {
187197

188198
# ensure that appends go to a new line
189199
if [ -f "${FILE}" ]; then
190-
if tail -n 1 "${FILE}" | grep -q "^$"; then
191-
echo "${FILE} exists and has trailing newline."
192-
else
193-
echo "${FILE} exists but has no trailing newline. Adding newline."
200+
if ! ( tail -n 1 "${FILE}" | grep -q "^$" ); then
201+
# FILE exists but has no trailing newline. Adding newline.
194202
echo >> "${FILE}"
195203
fi
196204
fi
@@ -206,20 +214,15 @@ set_env() {
206214
}
207215

208216
setup_sdk() {
209-
# Downloads and builds the SDK
210-
cd "${WORKING_DIR}"
211-
212-
clone_repo pico-sdk
217+
# Download the SDK
218+
clone_raspberrypi_repo pico-sdk
213219

214220
# Set env var PICO_SDK_PATH
215-
REPO_UPPER=$(echo ${REPO_NAME} | tr "[:lower:]" "[:upper:]")
216-
REPO_UPPER=$(echo ${REPO_UPPER} | tr "-" "_")
217-
set_env "${REPO_UPPER}_PATH=$DEST"
221+
set_env "PICO_SDK_PATH=${TARGET_DIR}/pico-sdk"
218222
}
219223

220224
enable_uart() {
221225
# Enable UART
222-
sudo apt install -y minicom
223226
echo "Disabling Linux serial console (UART) so we can use it for pico"
224227
sudo raspi-config nonint do_serial 2
225228
echo "You must run sudo reboot to finish UART setup"
@@ -230,26 +233,26 @@ phase_1() {
230233
echo "Entering phase 1: Setup minimum dev environment"
231234

232235
if mac; then
233-
install_toolchain_mac
236+
install_dev_env_deps_mac
234237
else
235-
install_toolchain_linux
238+
install_dev_env_deps_linux
236239
fi
237240

238241
create_working_dir
239242
setup_sdk
240243

241-
if raspbian && raspberry_pi; then
244+
if raspberry_pi && which raspi-config >> /dev/null; then
242245
enable_uart
243246
else
244-
echo "Not configuring UART because this is not running Raspberry Pi OS on a Raspberry Pi computer"
247+
echo "Not configuring UART because this is not a Raspberry Pi computer, or raspi-config is unavailable."
245248
fi
246249
}
247250

248251
build_examples() {
249252
# Build a couple of examples
250253
echo "Building selected examples"
251254

252-
cd "$WORKING_DIR/pico-examples"
255+
cd "$TARGET_DIR/pico-examples"
253256
mkdir -p build
254257
cd build
255258
cmake ../ -DCMAKE_BUILD_TYPE=Debug
@@ -267,7 +270,7 @@ phase_2() {
267270
echo "Entering phase 2: Setting up tutorial repos"
268271

269272
for REPO_NAME in pico-examples pico-extras pico-playground; do
270-
clone_repo "${REPO_NAME}"
273+
clone_raspberrypi_repo "${REPO_NAME}"
271274
done
272275

273276
build_examples
@@ -277,10 +280,10 @@ setup_picotool() {
277280
# Downloads, builds, and installs picotool
278281
echo "Setting up picotool"
279282

280-
cd "${WORKING_DIR}"
283+
cd "${TARGET_DIR}"
281284

282-
clone_repo picotool
283-
cd "${WORKING_DIR}/picotool"
285+
clone_raspberrypi_repo picotool
286+
cd "${TARGET_DIR}/picotool"
284287
mkdir -p build
285288
cd build
286289
cmake ../
@@ -294,10 +297,10 @@ setup_openocd() {
294297
# Download, build, and install OpenOCD for picoprobe and bit-banging without picoprobe
295298
echo "Setting up OpenOCD"
296299

297-
cd "${WORKING_DIR}"
300+
cd "${TARGET_DIR}"
298301

299-
clone_repo openocd picoprobe --depth=1
300-
cd "${WORKING_DIR}/openocd"
302+
clone_raspberrypi_repo openocd picoprobe --depth=1
303+
cd "${TARGET_DIR}/openocd"
301304
./bootstrap
302305
OPTS="--enable-ftdi --enable-bcm2835gpio --enable-picoprobe"
303306
if linux; then
@@ -313,10 +316,10 @@ setup_picoprobe() {
313316
# Download and build picoprobe. Requires that OpenOCD is already setup
314317
echo "Setting up picoprobe"
315318

316-
cd "${WORKING_DIR}"
319+
cd "${TARGET_DIR}"
317320

318-
clone_repo picoprobe
319-
cd "${WORKING_DIR}/picoprobe"
321+
clone_raspberrypi_repo picoprobe
322+
cd "${TARGET_DIR}/picoprobe"
320323
mkdir -p build
321324
cd build
322325
cmake ..
@@ -326,16 +329,14 @@ setup_picoprobe() {
326329
install_vscode_linux() {
327330
# Install Visual Studio Code
328331

329-
# VS Code is specially added to Raspberry Pi OS repos. Need to add the right repos to make it work on Debian/Ubuntu.
330-
if debian || ubuntu; then
331-
echo "Not yet implemented: testing Visual Studio Code on Debian/Ubuntu"
332+
# VS Code is specially added to Raspberry Pi OS repos, but might not be present on Debian/Ubuntu. So we check first.
333+
if ! apt list code; then
334+
echo "It appears that your APT repos do not offer Visual Studio Code. Skipping."
332335
return
333336
fi
334337

335338
echo "Installing Visual Studio Code"
336339

337-
cd "${WORKING_DIR}"
338-
339340
sudo apt install -y code
340341

341342
# Get extensions
@@ -374,7 +375,7 @@ main() {
374375
phase_2
375376
phase_3
376377

377-
echo "Congratulations, installation is complete. 😁"
378+
echo "Congratulations, installation is complete. :D"
378379
}
379380

380381
main

0 commit comments

Comments
 (0)