From da9865104fc44d3ca52bf2beeff1fa5b037b7020 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sat, 6 Sep 2025 16:21:15 +0800 Subject: [PATCH] export flash, openocd, msd --- doc/Embedded_Cmd.md | 12 ++++---- internal/crosscompile/crosscompile.go | 43 +++++++++++++++++++++++++++ internal/targets/config.go | 8 +++-- internal/targets/loader.go | 6 ++++ 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/doc/Embedded_Cmd.md b/doc/Embedded_Cmd.md index d4616484..58d9386d 100644 --- a/doc/Embedded_Cmd.md +++ b/doc/Embedded_Cmd.md @@ -42,11 +42,11 @@ llgo run hello.go # run locally llgo install hello.go # install to bin # Cross-compilation -llgo build -target esp32 hello.go # -> hello (ELF) -llgo build -target esp32 -file-format bin hello.go # -> hello.bin -llgo run -target esp32 hello.go # run on ESP32 -llgo run -target esp32 -emulator hello.go # run in emulator -llgo test -target esp32 -port /dev/ttyUSB0 . # run tests on device +llgo build -target esp32 . # -> hello (ELF) +llgo build -target esp32 -file-format bin , # -> hello.bin +llgo run -target esp32 . # run on ESP32 (guess a port) +llgo run -target esp32 -emulator . # run in emulator +llgo test -target esp32 -port /dev/ttyUSB0 . # run tests on device llgo test -target esp32 -emulator . # run tests in emulator -llgo install -target esp32 -port /dev/ttyUSB0 hello.go # flash to specific port +llgo install -target esp32 -port /dev/ttyUSB0 . # flash to specific port ``` diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index 6a4ee4a9..6ce60529 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -16,6 +16,27 @@ import ( "github.com/goplus/llgo/internal/xtool/llvm" ) +// Flash contains configuration for device flashing +type Flash struct { + 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 @@ -35,6 +56,11 @@ type Export struct { BinaryFormat string // Binary format (e.g., "elf", "esp", "uf2") FormatDetail string // For uf2, it's uf2FamilyID 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 } // URLs and configuration that can be overridden for testing @@ -502,6 +528,23 @@ func useTarget(targetName string) (export Export, err error) { export.FormatDetail = config.FormatDetail() export.Emulator = config.Emulator + // Set flashing/debugging configuration + export.Flash = Flash{ + Command: config.FlashCommand, + Serial: config.Serial, + SerialPort: config.SerialPort, + Flash1200BpsReset: config.Flash1200BpsReset == "true", + } + export.MSD = MSD{ + VolumeName: config.MSDVolumeName, + FirmwareName: config.MSDFirmwareName, + } + export.OpenOCD = OpenOCD{ + Interface: config.OpenOCDInterface, + Transport: config.OpenOCDTransport, + Target: config.OpenOCDTarget, + } + // Build environment map for template variable expansion envs := buildEnvMap(env.LLGoROOT()) diff --git a/internal/targets/config.go b/internal/targets/config.go index d75d1f45..1d56e7d6 100644 --- a/internal/targets/config.go +++ b/internal/targets/config.go @@ -35,10 +35,14 @@ type Config struct { UF2FamilyID string `json:"uf2-family-id"` // Flash and deployment configuration - FlashCommand string `json:"flash-command"` - FlashMethod string `json:"flash-method"` + FlashMethod string `json:"flash-method"` // values: command, openocd, msd + FlashCommand string `json:"flash-command"` // used when FlashMethod == "command" Flash1200BpsReset string `json:"flash-1200-bps-reset"` + // Serial configuration + Serial string `json:"serial"` // Serial communication type (e.g., "usb") + SerialPort []string `json:"serial-port"` // Serial port identifiers (e.g., vendor:product IDs) + // Mass storage device configuration MSDVolumeName []string `json:"msd-volume-name"` MSDFirmwareName string `json:"msd-firmware-name"` diff --git a/internal/targets/loader.go b/internal/targets/loader.go index 95d1e73a..5603ddcd 100644 --- a/internal/targets/loader.go +++ b/internal/targets/loader.go @@ -167,6 +167,9 @@ func (l *Loader) mergeConfig(dst, src *Config) { if src.Flash1200BpsReset != "" { dst.Flash1200BpsReset = src.Flash1200BpsReset } + if src.Serial != "" { + dst.Serial = src.Serial + } if src.MSDFirmwareName != "" { dst.MSDFirmwareName = src.MSDFirmwareName } @@ -202,6 +205,9 @@ func (l *Loader) mergeConfig(dst, src *Config) { if len(src.ExtraFiles) > 0 { dst.ExtraFiles = append(dst.ExtraFiles, src.ExtraFiles...) } + if len(src.SerialPort) > 0 { + dst.SerialPort = append(dst.SerialPort, src.SerialPort...) + } if len(src.MSDVolumeName) > 0 { dst.MSDVolumeName = append(dst.MSDVolumeName, src.MSDVolumeName...) }