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

@@ -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,9 +316,11 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "esp32",
Flash: crosscompile.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {hex}",
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {hex}",
},
},
},
wantOutPath: "", // temp file
@@ -336,9 +339,11 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "esp32",
Flash: crosscompile.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {bin}",
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {bin}",
},
},
},
wantOutPath: "", // temp file
@@ -357,8 +362,10 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "arm",
Flash: crosscompile.Flash{
Method: "openocd",
Device: flash.Device{
Flash: flash.Flash{
Method: "openocd",
},
},
},
wantOutPath: "", // temp file
@@ -377,11 +384,13 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "uf2",
Flash: crosscompile.Flash{
Method: "msd",
},
MSD: crosscompile.MSD{
FirmwareName: "firmware.uf2",
Device: flash.Device{
Flash: flash.Flash{
Method: "msd",
},
MSD: flash.MSD{
FirmwareName: "firmware.uf2",
},
},
},
wantOutPath: "", // temp file
@@ -400,8 +409,10 @@ func TestGenOutputs(t *testing.T) {
pkgName: "hello",
crossCompile: &crosscompile.Export{
BinaryFormat: "arm",
Flash: crosscompile.Flash{
Method: "bmp",
Device: flash.Device{
Flash: flash.Flash{
Method: "bmp",
},
},
},
wantOutPath: "", // temp file
@@ -510,9 +521,11 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method command - extract hex",
conf: &Config{Mode: ModeRun, Target: "esp32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {hex}",
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {hex}",
},
},
},
wantFmt: "hex",
@@ -522,9 +535,11 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method command - extract bin",
conf: &Config{Mode: ModeRun, Target: "esp32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {bin}",
Device: flash.Device{
Flash: flash.Flash{
Method: "command",
Command: "esptool.py --chip esp32 write_flash 0x10000 {bin}",
},
},
},
wantFmt: "bin",
@@ -534,8 +549,10 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method openocd",
conf: &Config{Mode: ModeRun, Target: "stm32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Method: "openocd",
Device: flash.Device{
Flash: flash.Flash{
Method: "openocd",
},
},
},
wantFmt: "hex",
@@ -545,11 +562,13 @@ 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{
Method: "msd",
},
MSD: crosscompile.MSD{
FirmwareName: "firmware.uf2",
Device: flash.Device{
Flash: flash.Flash{
Method: "msd",
},
MSD: flash.MSD{
FirmwareName: "firmware.uf2",
},
},
},
wantFmt: "uf2",
@@ -559,8 +578,10 @@ func TestDetermineFormat(t *testing.T) {
name: "flash method bmp",
conf: &Config{Mode: ModeRun, Target: "stm32"},
crossCompile: &crosscompile.Export{
Flash: crosscompile.Flash{
Method: "bmp",
Device: flash.Device{
Flash: flash.Flash{
Method: "bmp",
},
},
},
wantFmt: "elf",