refactor: move device types definition into flash

This commit is contained in:
Li Jie
2025-09-07 10:06:01 +08:00
parent 1c2aea10f0
commit c0afe199c2
6 changed files with 118 additions and 92 deletions

View File

@@ -59,7 +59,7 @@ func runMonitor(cmd *base.Command, args []string) {
fmt.Fprintf(os.Stderr, "llgo monitor: %v\n", err)
os.Exit(1)
}
serialPort = conf.Flash.SerialPort
serialPort = conf.Device.SerialPort
}
config := monitor.MonitorConfig{

View File

@@ -344,7 +344,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
case ModeInstall:
// Native already installed in linkMainPkg
if conf.Target != "" {
err = flash.Flash(ctx.crossCompile, finalApp, ctx.buildConf.Port, verbose)
err = flash.FlashDevice(ctx.crossCompile.Device, finalApp, ctx.buildConf.Port, verbose)
if err != nil {
return nil, err
}
@@ -356,7 +356,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
} else if conf.Emulator {
err = runInEmulator(ctx.crossCompile.Emulator, finalApp, pkg.Dir, pkg.PkgPath, conf, mode, verbose)
} else {
err = flash.Flash(ctx.crossCompile, finalApp, ctx.buildConf.Port, verbose)
err = flash.FlashDevice(ctx.crossCompile.Device, finalApp, ctx.buildConf.Port, verbose)
if err != nil {
return nil, err
}
@@ -365,9 +365,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
Target: conf.Target,
Executable: finalApp,
BaudRate: conf.BaudRate,
}
if ctx.crossCompile.Flash.Method != "openocd" {
monitorConfig.SerialPort = ctx.crossCompile.Flash.SerialPort
SerialPort: ctx.crossCompile.Device.SerialPort,
}
err = monitor.Monitor(monitorConfig, verbose)
}

View File

@@ -84,11 +84,11 @@ func determineFlashFormat(crossCompile *crosscompile.Export) string {
return ""
}
flashMethod := crossCompile.Flash.Method
flashMethod := crossCompile.Device.Flash.Method
switch flashMethod {
case "command", "":
// Extract format from flash command tokens
flashCommand := crossCompile.Flash.Command
flashCommand := crossCompile.Device.Flash.Command
switch {
case strings.Contains(flashCommand, "{hex}"):
return ".hex"
@@ -106,10 +106,10 @@ func determineFlashFormat(crossCompile *crosscompile.Export) string {
return ""
}
case "msd":
if crossCompile.MSD.FirmwareName == "" {
if crossCompile.Device.MSD.FirmwareName == "" {
return ""
}
return filepath.Ext(crossCompile.MSD.FirmwareName)
return filepath.Ext(crossCompile.Device.MSD.FirmwareName)
case "openocd":
return ".hex"
case "bmp":

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/goplus/llgo/internal/crosscompile"
"github.com/goplus/llgo/internal/flash"
)
func TestGenOutputs(t *testing.T) {
@@ -315,11 +316,13 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "esp32",
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {hex}",
},
},
},
wantOutPath: "", // temp file
wantOutExt: ".hex",
wantFileFmt: "hex",
@@ -336,11 +339,13 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "esp32",
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {bin}",
},
},
},
wantOutPath: "", // temp file
wantOutExt: ".bin",
wantFileFmt: "bin",
@@ -357,10 +362,12 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "arm",
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "openocd",
},
},
},
wantOutPath: "", // temp file
wantOutExt: ".hex",
wantFileFmt: "hex",
@@ -377,13 +384,15 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "uf2",
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "msd",
},
MSD: crosscompile.MSD{
MSD: flash.MSD{
FirmwareName: "firmware.uf2",
},
},
},
wantOutPath: "", // temp file
wantOutExt: ".uf2",
wantFileFmt: "uf2",
@@ -400,10 +409,12 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "arm",
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "bmp",
},
},
},
wantOutPath: "", // temp file
wantOutExt: ".elf",
wantFileFmt: "elf",
@@ -510,11 +521,13 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method command - extract hex",
conf: &Config{Mode: ModeRun, Target: "esp32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {hex}",
},
},
},
wantFmt: "hex",
wantExt: ".hex",
},
@@ -522,11 +535,13 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method command - extract bin",
conf: &Config{Mode: ModeRun, Target: "esp32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {bin}",
},
},
},
wantFmt: "bin",
wantExt: ".bin",
},
@@ -534,10 +549,12 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method openocd",
conf: &Config{Mode: ModeRun, Target: "stm32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "openocd",
},
},
},
wantFmt: "hex",
wantExt: ".hex",
},
@@ -545,13 +562,15 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method msd - extract from firmware name",
conf: &Config{Mode: ModeRun, Target: "rp2040"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "msd",
},
MSD: crosscompile.MSD{
MSD: flash.MSD{
FirmwareName: "firmware.uf2",
},
},
},
wantFmt: "uf2",
wantExt: ".uf2",
},
@@ -559,10 +578,12 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method bmp",
conf: &Config{Mode: ModeRun, Target: "stm32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Device: flash.Device{
Flash: flash.Flash{
Method: "bmp",
},
},
},
wantFmt: "elf",
wantExt: ".elf",
},

View File

@@ -12,32 +12,11 @@ import (
"github.com/goplus/llgo/internal/crosscompile/compile"
"github.com/goplus/llgo/internal/env"
"github.com/goplus/llgo/internal/flash"
"github.com/goplus/llgo/internal/targets"
"github.com/goplus/llgo/internal/xtool/llvm"
)
// Flash contains configuration for device flashing
type Flash struct {
Method string // Flash method: "command", "openocd", "msd", "bmp"
Command string // Flash command template
Serial string // Serial communication settings
SerialPort []string // Available serial ports
Flash1200BpsReset bool // Whether to use 1200bps reset
}
// MSD contains configuration for Mass Storage Device flashing
type MSD struct {
VolumeName []string // Names of the volumes
FirmwareName string // Firmware file name pattern
}
// OpenOCD contains configuration for OpenOCD debugging/flashing
type OpenOCD struct {
Interface string // Interface configuration (e.g., "stlink")
Transport string // Transport protocol (e.g., "swd", "jtag")
Target string // Target configuration (e.g., "stm32f4x")
}
type Export struct {
CC string // Compiler to use
CCFLAGS []string
@@ -59,9 +38,7 @@ type Export struct {
Emulator string // Emulator command template (e.g., "qemu-system-arm -M {} -kernel {}")
// Flashing/Debugging configuration
Flash Flash // Flash configuration for device programming
MSD MSD // Mass Storage Device configuration
OpenOCD OpenOCD // OpenOCD configuration for debugging/flashing
Device flash.Device // Device configuration for flashing/debugging
}
// URLs and configuration that can be overridden for testing
@@ -530,21 +507,23 @@ func UseTarget(targetName string) (export Export, err error) {
export.Emulator = config.Emulator
// Set flashing/debugging configuration
export.Flash = Flash{
Method: config.FlashMethod,
Command: config.FlashCommand,
export.Device = flash.Device{
Serial: config.Serial,
SerialPort: config.SerialPort,
Flash: flash.Flash{
Method: config.FlashMethod,
Command: config.FlashCommand,
Flash1200BpsReset: config.Flash1200BpsReset == "true",
}
export.MSD = MSD{
},
MSD: flash.MSD{
VolumeName: config.MSDVolumeName,
FirmwareName: config.MSDFirmwareName,
}
export.OpenOCD = OpenOCD{
},
OpenOCD: flash.OpenOCD{
Interface: config.OpenOCDInterface,
Transport: config.OpenOCDTransport,
Target: config.OpenOCDTarget,
},
}
// Build environment map for template variable expansion

View File

@@ -11,12 +11,40 @@ import (
"strings"
"time"
"github.com/goplus/llgo/internal/crosscompile"
"github.com/goplus/llgo/internal/env"
"go.bug.st/serial"
"go.bug.st/serial/enumerator"
)
// Device contains all flashing/debugging configuration for a device
type Device struct {
Serial string // Serial communication settings
SerialPort []string // Available serial ports
Flash Flash // Flash configuration for device programming
MSD MSD // Mass Storage Device configuration
OpenOCD OpenOCD // OpenOCD configuration for debugging/flashing
}
// Flash contains configuration for device flashing
type Flash struct {
Method string // Flash method: "command", "openocd", "msd", "bmp"
Command string // Flash command template
Flash1200BpsReset bool // Whether to use 1200bps reset
}
// MSD contains configuration for Mass Storage Device flashing
type MSD struct {
VolumeName []string // Names of the volumes
FirmwareName string // Firmware file name pattern
}
// OpenOCD contains configuration for OpenOCD debugging/flashing
type OpenOCD struct {
Interface string // Interface configuration (e.g., "stlink")
Transport string // Transport protocol (e.g., "swd", "jtag")
Target string // Target configuration (e.g., "stm32f4x")
}
// From tinygo/main.go getDefaultPort
// GetPort returns the default serial port depending on the operating system and USB interfaces.
func GetPort(portFlag string, usbInterfaces []string) (string, error) {
@@ -163,9 +191,9 @@ func touchSerialPortAt1200bps(port string) (err error) {
return fmt.Errorf("opening port: %s", err)
}
// Flash flashes firmware to a device based on the crosscompile configuration
func Flash(crossCompile crosscompile.Export, app string, port string, verbose bool) error {
method := crossCompile.Flash.Method
// FlashDevice flashes firmware to a device based on the device configuration
func FlashDevice(device Device, app string, port string, verbose bool) error {
method := device.Flash.Method
if method == "" {
method = "command"
}
@@ -173,7 +201,7 @@ func Flash(crossCompile crosscompile.Export, app string, port string, verbose bo
// Resolve port for methods that need it (all except openocd)
if method != "openocd" {
var err error
port, err = GetPort(port, crossCompile.Flash.SerialPort)
port, err = GetPort(port, device.SerialPort)
if err != nil {
return fmt.Errorf("failed to find port: %w", err)
}
@@ -185,7 +213,7 @@ func Flash(crossCompile crosscompile.Export, app string, port string, verbose bo
}
// Execute 1200bps reset before flashing if needed (except for openocd)
if method != "openocd" && crossCompile.Flash.Flash1200BpsReset {
if method != "openocd" && device.Flash.Flash1200BpsReset {
if verbose {
fmt.Fprintf(os.Stderr, "Triggering 1200bps reset on port: %s\n", port)
}
@@ -198,11 +226,11 @@ func Flash(crossCompile crosscompile.Export, app string, port string, verbose bo
switch method {
case "command":
return flashCommand(crossCompile.Flash, app, port, verbose)
return flashCommand(device.Flash, app, port, verbose)
case "openocd":
return flashOpenOCD(crossCompile.OpenOCD, app, verbose)
return flashOpenOCD(device.OpenOCD, app, verbose)
case "msd":
return flashMSD(crossCompile.MSD, app, verbose)
return flashMSD(device.MSD, app, verbose)
case "bmp":
return flashBMP(app, verbose)
default:
@@ -211,7 +239,7 @@ func Flash(crossCompile crosscompile.Export, app string, port string, verbose bo
}
// flashCommand handles command-based flashing
func flashCommand(flash crosscompile.Flash, app string, port string, verbose bool) error {
func flashCommand(flash Flash, app string, port string, verbose bool) error {
if flash.Command == "" {
return fmt.Errorf("flash command not specified")
}
@@ -241,7 +269,7 @@ func flashCommand(flash crosscompile.Flash, app string, port string, verbose boo
}
// flashOpenOCD handles OpenOCD-based flashing
func flashOpenOCD(openocd crosscompile.OpenOCD, app string, verbose bool) error {
func flashOpenOCD(openocd OpenOCD, app string, verbose bool) error {
if openocd.Interface == "" {
return fmt.Errorf("OpenOCD interface not specified")
}
@@ -279,7 +307,7 @@ func flashOpenOCD(openocd crosscompile.OpenOCD, app string, verbose bool) error
}
// flashMSD handles Mass Storage Device flashing
func flashMSD(msd crosscompile.MSD, app string, verbose bool) error {
func flashMSD(msd MSD, app string, verbose bool) error {
if len(msd.VolumeName) == 0 {
return fmt.Errorf("MSD volume names not specified")
}