Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2. Getting Started

This chapter puts gogo on a board with one command, then builds the tools from source, runs a first program on your computer, and shows the manual way to flash a board.

2.1 The quick way: gogo setup

gogo setup takes a new M5StickS3 or Cardputer ADV to a running program: it identifies the board, downloads the matching firmware, flashes it, sets up Wi-Fi and runs a first program. You need:

  • the board and a USB-C data cable (some cables only charge);
  • the gogo command, prebuilt for your system;
  • esptool 5 or newer, the ESP32 flashing tool: pip install esptool or brew install esptool.

You do not need Go, a C compiler or ESP-IDF.

Install gogo

On macOS and Linux, one command installs it:

curl -fsSL https://raw.githubusercontent.com/c4pt0r/gogo/main/install.sh | sh
gogo version

The script (install.sh in the repository) picks the file for your system and CPU (amd64 or arm64), checks it against the release’s SHA256SUMS, and installs gogo in /usr/local/bin, or in ~/.local/bin when /usr/local/bin is not writable (it tells you if that directory is not on your PATH). It also installs gogovm when the release has one. GOGO_VERSION=v0.1.0 installs a given release and GOGO_INSTALL_DIR=dir chooses the directory, for example curl -fsSL .../install.sh | GOGO_INSTALL_DIR=~/bin sh.

Each release on GitHub has the command for macOS, Linux and Windows, on amd64 and arm64: gogo-darwin-arm64, gogo-darwin-amd64, gogo-linux-amd64, gogo-linux-arm64, gogo-windows-amd64.exe and gogo-windows-arm64.exe. On Windows, or to install by hand, download the file for your system from the release page and put it on your PATH as gogo (gogo.exe on Windows). By hand on an Apple silicon Mac:

curl -L -o gogo https://github.com/c4pt0r/gogo/releases/latest/download/gogo-darwin-arm64
chmod +x gogo
sudo mv gogo /usr/local/bin/

A file downloaded with a browser on macOS may need xattr -d com.apple.quarantine gogo before it runs.

Run it

Connect the board and run:

gogo setup

It works in six steps, says what each one does, and asks before it writes to the board:

[1/6] Checking the environment
  esptool 5.6.0 (/usr/local/bin/esptool)
  port /dev/cu.usbmodem5101

[2/6] Identifying the board
  $ esptool --port /dev/cu.usbmodem5101 --after watchdog-reset read-flash 0x8000 3072 ...
  chip: ESP32-S3-PICO-1 (LGA56) (revision v0.2), embedded PSRAM
  This looks like the M5StickS3. Right? [Y/n]

[3/6] Getting the firmware
  release: https://github.com/c4pt0r/gogo/releases/latest/download
  sticks3-flasher_args.json (899 bytes, sha256 ok)
  sticks3-bootloader.bin (18880 bytes, sha256 ok)
  sticks3-partition-table.bin (3072 bytes, sha256 ok)
  sticks3-gogo.bin (1093072 bytes, sha256 ok)

[4/6] Flashing the firmware
  board firmware unknown (no answer on its console); flashing it
  0x00000000  bootloader.bin (18880 bytes)
  0x00008000  partition-table.bin (3072 bytes)
  0x00010000  gogo.bin (1093072 bytes)
  not written: NVS 0x9000..0xf000 (settings such as Wi-Fi stay)
  Flash this firmware to the M5StickS3 on /dev/cu.usbmodem5101? [Y/n]

[5/6] Setting up Wi-Fi
  Set up Wi-Fi now? [Y/n]
  scanning for networks on the board...
  1) Home Net                         ch 6 -50 dBm
  2) another network (type its name)
  Which network? [1]
  Wi-Fi password (not shown):
  saved on the board: wifi.ssid=Home Net, wifi.password=(set)
  waiting for the board to join...
  | gogo: wifi connected (Home Net, ch 6, -50 dBm)
  Wi-Fi works

[6/6] Running a first program
  hello program: 1552 bytes, to the image partition at 0x210000
  ...
  | gogo <version>: image 1552 bytes, arena 163840 bytes, free RAM <F> (largest <L>)
  | hello from gogo on the M5StickS3!
  1. Environment. It finds esptool and the board’s serial port (/dev/cu.usbmodem* on macOS, /dev/ttyACM* on Linux). With several ports it asks which one. On Windows, pass -port COM5; there setup flashes the firmware and the first program but skips the Wi-Fi step and does not watch the board’s console (gogo cannot use the serial console on Windows).
  2. Board. esptool reports the chip. Both boards are ESP32-S3s; the StickS3 has embedded PSRAM and the Cardputer ADV has none, so that is the guess, which you confirm (if esptool does not say, setup asks). Any other chip is refused.
  3. Firmware. It downloads the board’s firmware from the gogo release that matches your gogo (or the latest one), checks every file against the release’s SHA256SUMS, and keeps them in your user cache directory (~/Library/Caches/gogo on macOS, ~/.cache/gogo on Linux), so a second run downloads only SHA256SUMS again.
  4. Flash. First it asks the firmware already on the board which version it is (gogo device info, chapter 12). If that is exactly the version about to be flashed, built for the same board, and the board’s app is that very build (its SHA256, read from the board’s flash, the same as the firmware’s) with the same partition table, it says the board already runs gogo <version> (<board>, app <sha>); not reflashing and moves on; -reflash flashes anyway. Otherwise (another version, a new board, a firmware too old to answer, a development build) it says why in one line and writes the bootloader, the partition table and the firmware at the offsets the firmware build lists. It never erases the chip and never writes the NVS partition, where the board keeps its settings, so a Wi-Fi setup from before survives. If the firmware now on the board keeps its settings somewhere else (a board straight from the factory may), or its partition table cannot be read, setup stops and says so; -new-layout flashes anyway, still without writing either settings area. The old settings are then not used, and later firmware or programs may reuse the space where they were.
  5. Wi-Fi. The board scans for networks and you pick one. The password is read without echo and sent only to the board; gogo never prints or stores it. You can skip this step and use gogo config later (chapter 11).
  6. First program. A small program is flashed to the program partition; the board shows its greeting on the screen and on the console, where gogo setup waits for it.

When something fails, gogo setup prints what went wrong and a line starting fix: that says what to do, then stops. Running it again is always safe.

Options

OptionEffect
-port Pthe board’s serial port
-board sticks3|cardputerskip the board question
-version vX.Y.Zthat release instead of the one matching gogo
-release-url URL|DIRthe files of one release from another place: an http(s) URL or a local directory (also GOGO_RELEASE_URL)
-offlinebuild the firmware locally instead of downloading it
-skip-wifileave the Wi-Fi settings as they are
-reflashflash the firmware even when the board already runs the same version
-new-layoutflash even though the board keeps its settings elsewhere than the new firmware, or its partition table cannot be read (the old settings are then not used, and their space may be reused later)
-yesaccept every default without asking (Wi-Fi is then skipped)
-dry-runread the board and download, but only print the commands that would write to it (and whether the firmware would be flashed at all)
-esptool paththe esptool to run
-no-usb-resetdo not reset the board’s USB device when it stops answering (section 2.5, Troubleshooting)

Without a release to download (no network, or no such release), gogo setup offers to build the firmware with ESP-IDF instead, if it is installed (idf.py on PATH, $IDF_PATH or ~/esp/esp-idf) and you run it inside the gogo source tree. Section 2.6 has the manual commands.

The prebuilt gogo also compiles and flashes your own programs (section 2.5). To run programs on your computer, as most of this book does, you also need the host VM, gogovm. Each release has it for macOS and Linux: gogovm-darwin-arm64, gogovm-darwin-amd64, gogovm-linux-amd64 and gogovm-linux-arm64. Put it next to gogo, named gogovm (the asset name works too):

curl -L -o gogovm https://github.com/c4pt0r/gogo/releases/latest/download/gogovm-darwin-arm64
chmod +x gogovm
sudo mv gogovm /usr/local/bin/   # the directory where gogo is
gogo env                         # shows the gogovm it found

The release VM has no tls port (HTTPS on the host, chapter 17); for that, build gogo from source (section 2.2).

Set up Wi-Fi from your phone

A board can also get its Wi-Fi network from a phone, over Bluetooth, with no computer attached. The board’s firmware does it, before any program runs, so the program never sees the password. It uses Espressif’s standard provisioning protocol, so the phone app is Espressif’s own ESP BLE Provisioning (free, for Android and iOS); gogo has no app of its own.

The firmware does this with its bleprov feature, which is on in the default firmware of both boards, beside the program’s own Bluetooth port (ble, chapter 23): both use the one radio, never at once. A release older than the first with Bluetooth lacks it; build and flash the firmware from the gogo source tree as in section 2.6. Like gogo setup, this leaves the board’s settings and program alone. gogo firmware (chapter 12) keeps bleprov in a trimmed firmware whenever it has ble and net (the setup key is read without the keyboard driver, so the Cardputer’s works without kbd), and -without bleprov leaves the setup mode out.

Then:

  1. Hold the setup key and start the board. On the M5StickS3 that is button A (KEY1, the big front button); on the Cardputer ADV it is W (for Wi-Fi). Hold it while you switch the board on or press its reset button; the screen says gogo: keep holding button A for Wi-Fi setup; keep holding for about a second, until it shows the setup. Nothing else opens provisioning: an unconfigured board does not start it on its own, and a board that restarts by itself (after a crash, a watchdog, a low battery, or the end of a setup session) does not even look at the key, so a program that uses button A is never taken over.

  2. The screen shows the session:

    gogo: Wi-Fi setup over Bluetooth, 120 s
    gogo: app: ESP BLE Provisioning
    gogo: device PROV_gogo_9B56
    gogo: PIN 639615
    gogo: press button A again to cancel
    

    The device name ends in four hex digits of the board’s Bluetooth address. The PIN is new at every session. A line repeats the PIN with the time left every 15 seconds. The same lines go to the serial console.

  3. In the app, choose to provision a new device over BLE. When it offers to scan a QR code, choose to continue without one (the board shows none), then pick PROV_gogo_... from the list. The app asks for the proof of possession (on some versions the “PIN” or “password”): type the PIN from the screen. If the app asks for a username, it is wifiprov, the app’s default.

  4. Type the network name and password. The board does not scan for networks, so the app asks for the name instead of listing them. The password is the network’s passphrase, up to 63 characters (as for gogo setup); a 64-digit hexadecimal key is not accepted.

  5. The board joins that network to check the password, and the app shows the result.

What happens next:

  • Success. The app says the device is connected. The board writes the network into wifi.ssid and wifi.password, exactly as gogo setup and gogo config do (chapter 11), says gogo: Wi-Fi settings saved (...) and restarts. It boots normally and joins the new network: gogo monitor -reset shows gogo: wifi connected (<name>, ...), and gogo config get wifi.ssid the name.
  • Wrong password or no such network. After three tries the app shows the failure (could not join or network not found on the board’s screen). Nothing is saved; send the settings again from the app within the same session.
  • Wrong PIN. The app cannot open a session (session refused on the screen). Try again with the right PIN; after five refused sessions the board closes the setup.
  • Cancel. Let go of the key and press it again: the board says gogo: Wi-Fi setup cancelled, nothing changed and restarts as before.
  • Timeout. After 120 seconds without success the board says gogo: Wi-Fi setup timed out, nothing changed, restarts and runs as before with its old settings. A join already under way at the end gets up to 30 seconds more, and if it succeeds the settings are saved as above, even past the 120 seconds. A key still held at that restart does not start another session.
  • A phone that connects and does not open a session (the PIN typed and checked) within 60 seconds is dropped, so a stranger cannot hold the setup for the whole window. Once the PIN is accepted, the app may stay connected until the end of the 120 seconds while you type the network name and password.

Every session ends with a restart, so the program starts afresh with the radio and its memory in the normal state.

The security: the session is Espressif’s security 2, a password authenticated key exchange (SRP6a) with the PIN, then AES-GCM for every message, so someone nearby who does not see the screen cannot join or read the password, and a recording of the radio traffic does not reveal the PIN. The window is open only while you hold the key at boot and for two minutes at most. Section 5.4.11 of the design has the details.

2.2 Building the tools from source

To change gogo itself, run its tests, or have a host VM with HTTPS, build the tools from source.

What you need

On your computer (macOS or Linux):

  • Go 1.27 or newer, to build the gogo command (the compiler and board tools). It uses only Go’s standard library.
  • A C compiler and make, to build gogovm, the VM that runs programs on your computer. Any C99 compiler works (cc, clang, gcc).
  • The gogo source tree.

To flash firmware by hand you also need ESP-IDF, covered in section 2.6. You do not need it to follow most of this book: every example runs on your computer.

Building

From the root of the gogo source tree:

make vm                          # builds build/gogovm, the host VM
go build -o build/gogo ./cmd/gogo

build/gogo is the only command you use directly. When it runs a program it looks for the VM in this order: $GOGO_VM; gogovm in the directory of the gogo command itself (build/gogovm here, or wherever a release put it); build/gogovm in the gogo tree around gogo or your current directory. gogo env prints the one it would use.

Run it without arguments to see what it can do:

$ build/gogo
usage: gogo build [-path dirs] [-o out.ggb] <dir|file.gg>
       gogo run [--key c=pin]... [--fs-root DIR] [--actor-memory SIZE] [--memory SIZE] [--max-actors N] [--max-ports N] [--max-links N] [--bytes-handles N[/M]] [-path dirs] <dir|file.gg|file.ggb> [args...]
       gogo env [-path dirs]
       gogo dis <file.ggb>
       gogo info [-path dirs] <file.ggb|dir|file.gg>
       gogo flash [-port P] [-esptool path] [-path dirs] [-no-usb-reset] <app.ggb|dir|file.gg>
       gogo push [-port P] [-timeout seconds] [-no-usb-reset] <file.ggb> [/flash/path]
       gogo monitor [-port P] [-timeout seconds] [-until text] [-reset] [-no-usb-reset]
       gogo firmware (-board sticks3|cardputer | -port P [-esptool path] [-no-usb-reset]) [-o dir] [-build] [-with list] [-without list] [-component dir]... files...
       gogo config [-port P | -local] [-no-usb-reset] set <key> <value|-> | get <key> | list | rm <key> | import [-n] <file|-> | export | keys | scan | stats
       gogo device [-port P] [-yes] [-no-usb-reset] info | scan | stats | format-flash | usb-reset
       gogo setup [-port P] [-board sticks3|cardputer] [-version vX.Y.Z] [-release-url URL|DIR] [-esptool path] [-offline] [-skip-wifi] [-new-layout] [-reflash] [-yes] [-dry-run] [-no-usb-reset]
       gogo version

The rest of this book writes gogo for build/gogo. Put it on your PATH if you like.

2.3 Your first program

A gogo program is a directory holding one package main (in one or more .gg files: gogo source, Go syntax) with a func main():

// The smallest gogo program: package main, func main, println.
package main

func main() {
	println("hello, gogo", 42, true)
}

Run it with gogo run and the directory:

$ gogo run docs/book/examples/02-hello
hello, gogo 42 true

gogo run compiles the directory to a temporary image and runs it on the host VM. println is the only printing function: it prints its arguments separated by spaces, followed by a newline, on standard output. (There is no fmt package.)

When the program has nothing left to do (no actor can run, no timer is pending, no input can arrive) the VM exits. The exit status says how main ended: 0 when it returned, 4 when it crashed, or the code it passed to actor.Exit (chapter 12 has the table). Crashes of other actors are reported on standard error, as you will see in chapter 5, and do not change it.

Scripts

gogo run also takes a single .gg file, and compiles that file alone:

$ gogo run hello.gg

A file whose first line starts with #! is an executable script: the compiler reads that line as an empty one (line numbers in errors stay right), so with

#!/usr/bin/env -S gogo run
package main

func main() {
	println("hello from a script")
}

and chmod +x hello.gg, ./hello.gg runs it (gogo must be on your PATH). env -S, which splits gogo run into two words, is in macOS and in GNU coreutils 8.30 and newer, but not in BusyBox (Alpine) or older coreutils. There, name gogo by its absolute path instead, which needs no env: #!/usr/local/bin/gogo run (the kernel passes run as one argument). The name need not end in .gg: gogo run reads any file starting with #! as source. Arguments after the script are the program’s: it reads them with sys.Args() (chapter 12).

A single file and a directory differ: gogo run dir compiles every .gg file in dir as one package main, so a directory of scripts, each with its own func main, only runs file by file. A script can still import packages from its directory (import "scripts/calc" for scripts/calc/ next to a script in scripts/) and from the search path, as below.

Packages and GOGOPATH

A program can be split into packages of its own (section 3.1.1): import "app/shapes" in a program in app/ is the directory app/shapes/. Packages that several programs share live in a directory on the search path, by default $HOME/gogo:

mkdir -p ~/gogo/lib/ring                 # import "lib/ring"
export GOGOPATH=$HOME/gogo:$HOME/work/gogo-pkgs   # optional: several directories
gogo env                                 # prints the search path

gogo build -path dirs and gogo run -path dirs use dirs instead of GOGOPATH for one build.

2.4 A first actor

The main function runs in the first actor. It can start more with actor.Spawn, which takes a function to run, and talk to them with actor.Send and actor.Receive:

// A first actor: main spawns a greeter, sends it a name, and waits for the
// greeting to come back.
package main

import "gogo/actor"

// Greet asks the greeter to greet Name and answer to From.
type Greet struct {
	From actor.PID
	Name string
}

func greeter() {
	for {
		switch m := actor.Receive().(type) {
		case Greet:
			actor.Send(m.From, "hello, "+m.Name)
		}
	}
}

func main() {
	g := actor.Spawn(greeter)
	actor.Send(g, Greet{From: actor.Self(), Name: "world"})
	println(actor.Receive())
}
hello, world

Read it in order:

  1. actor.Spawn(greeter) starts a new actor running greeter and returns its PID (process identifier).
  2. actor.Send(g, Greet{...}) puts a Greet message in the greeter’s mailbox. The message carries actor.Self(), main’s own PID, so the greeter knows where to answer.
  3. The greeter’s actor.Receive() returns the message as an any; the type switch finds out it is a Greet.
  4. The greeter sends a string back, and main’s actor.Receive() returns it.

Chapter 4 covers all of this in detail. Notice already that greeter is still waiting in Receive when main returns. That is fine: nothing can ever reach it again, so the VM exits.

Editor support

gogo source files end in .gg, and compiled images in .ggb. A .gg file is Go syntax. The repository’s editors/ directory has ready-made support that gives .gg a gogo file type (Go highlighting and indentation, plus the //gogo: directives and #! scripts) and a black and white color scheme, gogo mono:

  • VS Code: link the extension and restart VS Code,

    ln -s "$PWD/editors/vscode" ~/.vscode/extensions/gogo-lang
    
  • Vim / Neovim: add editors/vim to the runtime path,

    set runtimepath^=/path/to/gogo/editors/vim
    

    and optionally colorscheme gogo-mono.

  • Emacs: with go-mode installed, load editors/emacs/gogo-mode.el ((require 'gogo-mode) after adding the directory to load-path).

In any other editor, map the extension to Go: in VS Code without the extension that is "files.associations": { "*.gg": "go" }, in Vim autocmd BufRead,BufNewFile *.gg setfiletype go.

That is syntax only. gopls and go vet do not apply: the Go toolchain does not read .gg files, and gogo is a subset of Go with its own packages (gogo/actor, gogo/net, …), which exist only inside the gogo compiler. Programs are only ever compiled and run by gogo, never by go run; gogo build is the judge of what is valid. Their sources are in lib/ of the gogo tree (one directory per package) if you want to read an API.

2.5 Running on a board

Programs and firmware are separate on the board. The firmware (the VM, drivers, Wi-Fi, file systems and the screen console) is flashed once, by gogo setup (section 2.1) or by hand (section 2.6). Your program image goes into its own flash partition and can be replaced in a second, without touching the firmware.

Build, flash and watch a program

gogo build docs/book/examples/02-hello -o hello.ggb
gogo flash hello.ggb
gogo monitor -reset -timeout 5 -until "gogo: vm exited"
  • gogo build compiles the directory to an image file (.ggb; without -o it writes <directory name>.ggb in the current directory).
  • gogo flash writes the image to the board’s program partition. It needs esptool on your PATH (the one you installed for gogo setup, or ESP-IDF’s, which its export.sh puts there) and uses the only board port it finds (/dev/cu.usbmodem* on macOS, /dev/ttyACM* on Linux); pass -port if there are several. It also takes a directory, and builds it first: gogo flash docs/book/examples/02-hello does both steps.
  • gogo monitor copies the board’s console to your terminal. -reset restarts the board first, so you see the program from the beginning; -timeout 5 stops after 5 seconds; -until stops at the first line containing that text.

The board prints a boot line, runs the program, and prints a line when the VM exits:

gogo <version>: image <N> bytes, arena <cap> bytes, free RAM <F> (largest <L>)
hello, gogo 42 true
gogo: vm exited (<code>), arena used <bytes>

Everything the program prints also appears on the board’s screen; crash reports (standard error) are shown in red.

Note. Only one program can use the serial port at a time. Close gogo monitor (or any other serial terminal) before gogo flash or gogo config.

Note. The image format has a version. When you update gogo and the version changes, reflash the firmware too (run gogo setup again, or section 2.6): an older firmware refuses a newer image with gogo: bad image (unsupported version).

Troubleshooting

  • no answer from the board, or esptool’s No serial data received. The board’s USB serial link sometimes goes silent while the program keeps running. gogo recovers it on its own on macOS and Linux: when the board’s USB device fails a descriptor request too (a board that runs other firmware does not), it bus-resets the device (the chip is not restarted), retries once and prints gogo: the board's USB link was silent; reset it (USB bus reset). gogo device usb-reset does the same by hand. On Linux the reset needs a udev rule (gogo prints it when it is missing; docs/known-issues.md has it); on Windows, or if the board still does not answer, unplug it and plug it in again.
  • The port is busy. Another program has it open: close gogo monitor or the serial terminal. gogo does not reset a board’s USB device while another of your programs has the port open; it cannot see programs run as root or by other users, so close those yourself.
  • gogo: bad image (unsupported version). The firmware is older than the image format: update it (note above).

You now have the whole loop: edit, gogo run on the computer, then gogo build and gogo flash for the board. The next chapter looks at the language itself.

2.6 Flashing the firmware by hand

gogo setup is the easy way to flash the firmware. To build it yourself, for example after changing the firmware, install ESP-IDF and use idf.py from the gogo tree. gogo uses ESP-IDF v6.1.1:

mkdir -p ~/esp && cd ~/esp
git clone -b v6.1.1 --recursive --shallow-submodules --depth 1 https://github.com/espressif/esp-idf.git
cd esp-idf && ./install.sh esp32s3

Then, from the gogo tree, for the M5StickS3:

. ~/esp/esp-idf/export.sh
idf.py -C platform/esp32s3 build
idf.py -C platform/esp32s3 -p /dev/cu.usbmodemXXXX flash

The Cardputer ADV firmware is built in its own directory with its own settings:

idf.py -C platform/esp32s3 -B platform/esp32s3/build-cardputer -DGOGO_BOARD=cardputer \
    -DSDKCONFIG=build-cardputer/sdkconfig \
    -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ble;sdkconfig.cardputer" build
idf.py -C platform/esp32s3 -B platform/esp32s3/build-cardputer -p /dev/cu.usbmodemXXXX flash

Make sure you flash the right firmware to the right board (esptool chip-id reports embedded PSRAM on the StickS3 and none on the Cardputer ADV). Chapter 11 says more about the two boards.

idf.py flash writes the same three files as gogo setup and, like it, leaves the NVS partition (and so the Wi-Fi settings) alone. gogo firmware (chapter 12) builds a firmware with only the parts your programs need.