學 Go 玩創客,TinyGo 初體驗(5):使用其他開發板 — — DigiSpark、Arduino Nano 33 IoT、 Adafrut Metro M4 及 Bluepill

DigiSpark:ATTiny85 迷你開發板

安裝 bootloader(libusb-win32)

下載 Zadig 這個執行檔,並以系統管理員權限執行。把 DigiSpark 插上電腦,Zadig 內應該會看到一個 Unknown device。在右下選擇 libusb-win32 然後按 Install driver。稍待片刻,不要拔掉裝置(你會聽到裝置斷線的音效,那是正常的),直到 bootloader 裝好。

下載燒錄工具 micronucleus

接著,我們需要一個能燒錄程式到板子上的 command line 工具。在這裡下載 micronucleus.exe,丟到硬碟內某處,然後把它的路徑加入 PATH 系統變數(或者你能像我一樣,找個已經在 PATH 內的位置借放它)。

micronucleus -help
> Please plug in the device ... 
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 20% complete
connecting: 27% complete
connecting: 34% complete
connecting: 40% complete
> Device has firmware version 1.6
> Available space for user applications: 6012 bytes
> Suggested sleep time between sending pages: 8ms
> Whole page count: 94 page size: 64
> Erase function sleep duration: 752ms
> Error opening -help: No such file or directory
> Error loading or parsing hex file.
Photo by Vasily Koloda on Unsplash

燒錄 blinky 範例程式

下面是經典的 Blinky1 範例程式,其實完全就是之前的版本:

package mainimport (
"machine"
"time"
)
func main() {
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
for {
led.High()
time.Sleep(time.Millisecond * 500)
led.Low()
time.Sleep(time.Millisecond * 500)
}
}
tinygo flash -target digispark examples/blinky1
> Please plug in the device ... 
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.6
> Available space for user applications: 6012 bytes
> Suggested sleep time between sending pages: 8ms
> Whole page count: 94 page size: 64
> Erase function sleep duration: 752ms
parsing: 50% complete
> Erasing the memory ...
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
> Starting to upload ...
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app ...
running: 100% complete
>> Micronucleus done. Thank you!
Photo by Valerie Blanchett on Unsplash

Arduino Nano 33 IoT:SAMD21 + NINA-W102

第一篇文提過 2019 年春時,Arduino 推出四款新的 Nano 開發板:這四片板子的尺寸和腳位配置與舊的 Nano 一模一樣,但規格更加強大。最低階的 Nano Every 使用 ATmega4809(48 MHz,6 KB 記憶體),Nano 33 IoT 使用 SAMD21(48 MHz,32 KB 記憶體),Nano 33 BLE 則使用 nRF52840(64 MHz,1 MB 記憶體!)。Nano 33 BLE Sense 就是標準 BLE 加裝一堆感測器的豪華版,專門設計給 Tensorflow Lite 之類的套件使用。

安裝燒錄工具 BOSSA

下載並安裝 BOSSA,這是專門給 SAM 家族微處理器用的燒錄程式。不過,安裝時必須記得把安裝路徑改為 C:\Program Files\BOSSA\(不是預設的 C:\Program Files (x86)\BOSSA\)。然後,把 C:\Program Files\BOSSA\ 路徑加入 PATH 系統變數。

bossac --help
Usage: bossac.exe [OPTION...] [FILE]
Basic Open Source SAM-BA Application (BOSSA) Version 1.9.1
Flash programmer for Atmel SAM devices.
Copyright (c) 2011-2018 ShumaTech (http://www.shumatech.com)
...(下略)
Photo by Jon Tyson on Unsplash

燒錄 blinky 範例程式

在上傳程式之前,你必須按兩下 Nano 33 IoT 上的白色 reset 鈕,好讓它進入 bootloader 模式。(Arduino IDE 有能力在不需你動手的情況下讓它自動切換,而 TinyGo 就多了這個步驟。)

tinygo flash -target arduino-nano33 -port COM9 examples/blinky1
Device       : ATSAMD21x18
Version : v1.1 [Arduino:XYZ] May 17 2020 17:56:43
Address : 0x0
Pages : 4096
Page Size : 64 bytes
Total Size : 256KB
Planes : 1
Lock Regions : 16
Locked : none
Security : false
BOD : true
BOR : true
Erase flash
Done in 0.819 seconds
Write 8196 bytes to flash (129 pages)
[==============================] 100% (129/129 pages)
Done in 0.174 seconds
Verify 8196 bytes of flash
[==============================] 100% (129/129 pages)
Verify successful
Done in 0.781 seconds
tinygo flash -target arduino-nano33 -port COM9 tinygo.org/x/drivers/examples/lsm6ds3tinygo flash -target arduino-nano33 -port COM9 tinygo.org/x/drivers/examples/wifinina/webclient

Adafruit Metro M4:SAMD51

Photo by Steve Johnson on Unsplash

燒錄 blinky 範例程式

按兩下 reset 進入 bootloader 模式,然後使用以下指令(筆者的板子出現在 COM6):

tinygo flash -target metro-m4-airlift -port COM6 examples/blinky1

Bluepill:STM32F103C8T6

藍色藥丸典故來自《駭客任務》。當時確實也曾有過「紅色藥丸」板,但似乎已經絕跡了。

環境準備

首先,你需要買個 ST-LINK/V2 燒錄器:(接線方式可參考這篇

燒錄 blinky1 範例程式

若是在 Windows,你必須使用 TinyGo 0.15.0 版,這版修正了在 Windows 下傳遞路徑給 OpenOCD 的問題。不過,對 Linux 環境就沒有差別了。

tinygo flash -target bluepill examples/blinky1
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v34 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.231196
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08000714 msp: 0x20000800
** Programming Started **
auto erase enabled
Info : device id = 0x20036410
Info : flash size = 128kbytes
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000003a msp: 0x20000800
wrote 4096 bytes from file C:/Users/xxxx/AppData/Local/Temp/tinygo750737859/main.hex in 0.341090s (11.727 KiB/s)
** Programming Finished **
** Resetting Target **
shutdown command invoked

(緊急參戰!)Seeeduino XIAO

tinygo flash -target xiao -port COM25 examples/blinky1

ESP8266/ESP32 呢?

0.15.0 版終於迎來了令人期待已久的更新:新增對 ESP 家族開發板的支援。只要安裝一些工具後,就能直接從終端機上傳 TinyGo 程式到這些板子上了。

結語

本篇旨在展示 TinyGo 要如何在幾種不同的開發板上燒錄程式。依據板子規格的不同,有些需要比較複雜的準備作業,不過各位可以看到,燒錄上去的程式本身是一模一樣的,直接套官方範例照樣可以跑。

Photo by Nicholas Bartos on Unsplash

Former translator, after-hours Maker, sunny-day analog film shooter. Currently a junior tech-book editor based in Taiwan. https://krantasblog.blogspot.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store