bladeRF icon indicating copy to clipboard operation
bladeRF copied to clipboard

Sample Rate Set Fails After Disabling Oversample at Max Rate

Open vadimart92 opened this issue 8 months ago • 2 comments

When the Oversample feature is disabled after setting the sample rate to the maximum supported value, a subsequent call to set the sample rate again to that same maximum value results in a failure during RX (receiving) with a timeout.

However, introducing a workaround — by first setting the sample rate to a different value (e.g., a minimum supported rate) before setting it back to the desired maximum rate — resolves the issue. This suggests that an internal state may not be correctly refreshed unless the sample rate value is explicitly changed.

Repro Steps:

  • Enable Oversample and set sample rate to max (e.g., 122.88 MHz)
  • Disable Oversample
  • [Problematic] Set sample rate again to 61.44 MHz directly → results in RX failure
  • [Working] Set to a lower rate first (e.g., 520.834 kHz), then to 61.44 MHz → RX works

Pseudo code with repro in c# below

[Test]
public async Task Bug([Values]bool useFix, Tuner tuner) {
  var device = Connect(GetFirstDeviceId());
  var channelId = GetRxChannel(0);

  bladeRF.bladerf_enable_feature(device, BladeRFFeature.Oversample, true);
  var rate122 = BladeRFRRationalRate.FromDouble(122880000);
  bladeRF.bladerf_set_rational_sample_rate(device, channelId, ref rate122, out var actual122);
  Assert.That(rate122,Is.EqualTo(actual122));

  bladeRF.bladerf_enable_feature(device, BladeRFFeature.Oversample, false);

  if (useFix) {
	  var minRate = BladeRFRRationalRate.FromDouble(520834);
	  bladeRF.bladerf_set_rational_sample_rate(device, channelId, ref minRate, out _);
  }

  var rate61 = BladeRFRRationalRate.FromDouble(61440000);
  bladeRF.bladerf_set_rational_sample_rate(device, channelId, ref rate61, out var actual61);
  Assert.That(rate61,Is.EqualTo(actual61));

  bladeRF.bladerf_sync_config(device, BladeRFChannelLayout.RxX1, BladeRFFormat.Sc16Q11Meta, 512,
	  122880, 32, 3500);
  bladeRF.bladerf_enable_module(device, channelId, true);

  var buffer = new short[122880];
  var startTime = Stopwatch.GetTimestamp();
  var iterationNumber = 0;
  while (Stopwatch.GetElapsedTime(startTime) < TimeSpan.FromSeconds(10)) {
	  var metadata = new BladeRFMetadata{ flags = BladerfMetaFlag.RxNow };
	  var status = bladeRF.bladerf_sync_rx(device, buffer, 61_440, ref metadata, 3500);
	  //will fail on iteration #128136
	  Assert.That(status, Is.EqualTo(BladeRFStatus.Success), $"Failed on iteration #{iterationNumber}");
	  iterationNumber++;
  }

  bladeRF.bladerf_enable_module(device, channelId, false);
}

vadimart92 avatar May 07 '25 14:05 vadimart92