From c93607625df8a0ce684cd4bc36544159f8083d6a Mon Sep 17 00:00:00 2001 From: Jan Svabenik Date: Fri, 3 Apr 2026 17:28:51 +0200 Subject: [PATCH] fix: make Pluto live LO retune fail loudly and avoid redundant channel lookups Cache altvoltage1 during Configure(), clear it in cleanup(), and use the cached channel for Tune() instead of calling findChannel on every retune. Tune() now checks the direct iio_channel_attr_write_longlong return code and reports LO write failures back to the caller with the failing frequency and libiio error code. --- internal/platform/plutosdr/pluto_windows.go | 22 +++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/platform/plutosdr/pluto_windows.go b/internal/platform/plutosdr/pluto_windows.go index fa57e3f..eb51e24 100644 --- a/internal/platform/plutosdr/pluto_windows.go +++ b/internal/platform/plutosdr/pluto_windows.go @@ -110,6 +110,7 @@ type PlutoDriver struct { phyDev uintptr // iio_device* (ad9361-phy) chanI uintptr // iio_channel* TX I chanQ uintptr // iio_channel* TX Q + chanLO uintptr // iio_channel* TX LO (altvoltage1), cached for Tune() buf uintptr // iio_buffer* bufSize int // samples per buffer push @@ -204,6 +205,7 @@ func (d *PlutoDriver) Configure(_ context.Context, cfg platform.SoapyConfig) err // TX LO frequency phyChanLO := d.findChannel(phyDev, "altvoltage1", true) // TX LO + d.chanLO = phyChanLO // cache for Tune() if phyChanLO != 0 { freqHz := int64(cfg.CenterFreqHz) if freqHz <= 0 { @@ -371,14 +373,21 @@ func (d *PlutoDriver) Flush(_ context.Context) error { return nil } func (d *PlutoDriver) Tune(_ context.Context, freqHz float64) error { d.mu.Lock() defer d.mu.Unlock() - if d.phyDev == 0 { - return fmt.Errorf("pluto: not configured") + if !d.configured || d.chanLO == 0 { + return fmt.Errorf("pluto: not configured or LO channel not available") + } + if d.lib.pChannelAttrWriteLongLong == nil { + return fmt.Errorf("pluto: iio_channel_attr_write_longlong not loaded") } - phyChanLO := d.findChannel(d.phyDev, "altvoltage1", true) - if phyChanLO == 0 { - return fmt.Errorf("pluto: TX LO channel not found") + cAttr, _ := syscall.BytePtrFromString("frequency") + ret, _, _ := d.lib.pChannelAttrWriteLongLong.Call( + d.chanLO, + uintptr(unsafe.Pointer(cAttr)), + uintptr(int64(freqHz)), + ) + if int32(ret) < 0 { + return fmt.Errorf("pluto: LO tune to %.0f Hz failed (iio rc=%d)", freqHz, int32(ret)) } - d.writeChanAttrLL(phyChanLO, "frequency", int64(freqHz)) return nil } @@ -420,6 +429,7 @@ func (d *PlutoDriver) cleanup() { d.disableChannel(d.chanQ) d.chanQ = 0 } + d.chanLO = 0 // config-only channel, no disable needed if d.ctx != 0 && d.lib.pDestroyCtx != nil { d.lib.pDestroyCtx.Call(d.ctx) d.ctx = 0