joonas-fi / joonas.fi

My personal blog
https://joonas.fi/
Apache License 2.0
3 stars 1 forks source link

RP2040, tinygo #111

Open joonas-fi opened 7 months ago

joonas-fi commented 7 months ago

RP2040

Programming language / compiler

Tinygo is variant of Go intended for microcontrollers.

The microcontroller

Build

Have this main.go:

package main

import (
    "machine"
    "time"
)

func main() {
    led := machine.LED
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})
    for {
        led.Low()
        time.Sleep(time.Millisecond * 250)

        led.High()
        time.Sleep(time.Millisecond * 250)
    }
}

Run the build in a container:

docker run --rm -it -v "$(pwd):/src" --entrypoint=bash --workdir=/src tinygo/tinygo:0.30.0
tinygo build -target=pico -o blink.uf2

This will produce blink.uf2 which is your file-to-flash.

Flash

Enter bootloader mode by:

  1. holding boot (B) button and
  2. pressing reset (R) once

The UF2 bootloader of the RP2040 presents the flash memory to your computer as a USB mass storage device. Mount it so its virtual filesystem is visible to you.

Then:

cp blink.uf2 /media/joonas/RPI-RP2/

This will boot the device with the content that you flashed.

arduino-nano

The base instructions are about same as RP2040, but other details were like this:

$ docker run --rm -it -v "$(pwd):/src" --entrypoint=bash --workdir=/src --device=/dev/ttyUSB1:/dev/ttyUSB0 --group-add dialout tinygo/tinygo:0.30.0
$ sudo apt update && sudo apt install -y avrdude
$ tinygo flash -target=arduino-nano