id: limesdr_source
label: LimeSDR Source (RX)
category: '[LimeSDR]'
flags: throttle

parameters:
- id: serial
  label: Device serial
  dtype: string
  hide: part
  category: General

- id: filename
  label: File
  dtype: file_open
  hide: part
  category: General

- id: channel_mode
  label: Channel
  dtype: enum
  options:       ['0', '1', '2']
  option_labels: ['A', 'B', 'A+B']
  default: 0
  hide: part
  category: General

- id: rf_freq
  label: RF frequency
  dtype: float
  default: 100e6
  category: General

- id: samp_rate
  label: Sample rate
  dtype: float
  default: samp_rate
  category: General

- id: oversample
  label: Oversample
  dtype: enum
  options:       ['0', '1', '2', '4', '8', '16', '32']
  option_labels: ['Default', '1', '2', '4', '8', '16', '32']
  default: 0
  category: General

- id: nco_freq_ch0
  label: NCO frequency
  dtype: float
  default: 0
  hide: part
  category: Channel A

- id: calibr_bandw_ch0
  label: Calibration bandwidth
  dtype: float
  default: 2.5e6
  hide: part
  category: Channel A

- id: lna_path_ch0
  label: LNA path
  dtype: int
  options:       ['1', '2', '3', '255']
  option_labels: ['H', 'L', 'W', 'Auto']
  default: '255'
  hide: part
  category: Channel A

- id: analog_bandw_ch0
  label: Analog filter bandwidth
  dtype: float
  default: 1.5e6
  hide: part
  category: Channel A

- id: digital_bandw_ch0
  label: Digital filter bandwidth
  dtype: float
  default: samp_rate
  hide: part
  category: Channel A

- id: gain_dB_ch0
  label: Gain (dB)
  dtype: int
  default: 1
  hide: part
  category: Channel A

- id: nco_freq_ch1
  label: NCO frequency
  dtype: float
  default: 0
  hide: part
  category: Channel B

- id: calibr_bandw_ch1
  label: Calibration bandwidth
  dtype: float
  default: 2.5e6
  hide: part
  category: Channel B

- id: lna_path_ch1
  label: LNA path
  dtype: int
  options:       ['1', '2', '3']
  option_labels: ['H', 'L', 'W']
  default: 2
  hide: part
  category: Channel B

- id: analog_bandw_ch1
  label: Analog filter bandwidth
  dtype: float
  default: 1.5e6
  hide: part
  category: Channel B

- id: digital_bandw_ch1
  label: Digital filter bandwidth
  dtype: float
  default: samp_rate
  hide: part
  category: Channel B

- id: gain_dB_ch1
  label: Gain (dB)
  dtype: int
  default: 1
  hide: part
  category: Channel B

- id: allow_tcxo_dac
  label: Allow TCXO DAC control
  dtype: enum
  options:       ['0', '1']
  option_labels: ['No', 'Yes']
  default: 0
  hide: part
  category: Advanced

- id: dacVal
  label: TCX0:DAC val
  dtype: int
  default: 180
  hide: part
  category: Advanced

templates:
  imports: import limesdr
  make: |-
    limesdr.source(${serial}, ${channel_mode}, ${filename})

    % if filename() == "":

    self.${id}.set_sample_rate(${samp_rate})
 
    % if int(oversample()) > 0:
    self.${id}.set_oversampling(${oversample})
    % endif

    self.${id}.set_center_freq(${rf_freq}, 0)

    % if analog_bandw_ch0() > 0 and (int(channel_mode()) == 0 or int(channel_mode()) == 2):
    self.${id}.set_bandwidth(${analog_bandw_ch0}, 0)
    % endif

    % if analog_bandw_ch1() > 0 and int(channel_mode()) > 0:
    self.${id}.set_bandwidth(${analog_bandw_ch1}, 1)
    % endif

    % if digital_bandw_ch0() > 0 and (int(channel_mode()) == 0 or int(channel_mode()) == 2):
    self.${id}.set_digital_filter(${digital_bandw_ch0}, 0)
    % endif

    % if digital_bandw_ch1() > 0 and int(channel_mode()) > 0:
    self.${id}.set_digital_filter(${digital_bandw_ch1}, 1)
    % endif

    % if int(channel_mode()) == 0 or int(channel_mode()) == 2:
    self.${id}.set_gain(${gain_dB_ch0}, 0)
    % endif

    % if int(channel_mode()) > 0:
    self.${id}.set_gain(${gain_dB_ch1}, 1)
    % endif

    % if int(channel_mode()) == 0 or int(channel_mode()) == 2:
    self.${id}.set_antenna(${lna_path_ch0}, 0)
    % endif

    % if int(channel_mode()) > 0:
    self.${id}.set_antenna(${lna_path_ch1}, 1)
    % endif

    % if calibr_bandw_ch0() > 0 and (int(channel_mode()) == 0 or int(channel_mode()) == 2):
    self.${id}.calibrate(${calibr_bandw_ch0}, 0)
    % endif

    % if calibr_bandw_ch1() > 0 and int(channel_mode()) > 0:
    self.${id}.calibrate(${calibr_bandw_ch1}, 1)
    % endif

    % if nco_freq_ch0() != 0 and (int(channel_mode()) == 0 or int(channel_mode()) == 2):
    self.${id}.set_nco(${nco_freq_ch0},0)
    % endif

    % if nco_freq_ch1() != 0 and int(channel_mode()) > 0:
    self.${id}.set_nco(${nco_freq_ch1}, 1)
    % endif

    % endif # if filename() == ""
   
    % if int(allow_tcxo_dac) == 1:
    self.${id}.set_tcxo_dac(${dacVal})
    % endif

  callbacks:
  - set_center_freq(${rf_freq}, 0)
  - set_antenna(${lna_path_ch0}, 0)
  - set_antenna(${lna_path_ch1}, 1)
  - set_nco(${nco_freq_ch0}, 0)
  - set_nco(${nco_freq_ch1}, 1)
  - set_bandwidth(${analog_bandw_ch0}, 0)
  - set_bandwidth(${analog_bandw_ch1}, 1)
  - set_digital_filter(${digital_bandw_ch0}, 0)
  - set_digital_filter(${digital_bandw_ch1}, 1)
  - set_gain(${gain_dB_ch0}, 0)
  - set_gain(${gain_dB_ch1}, 1)
  - set_tcxo_dac(${dacVal})

asserts:
- ${ int(channel_mode) >= 0 }
- ${ 2 >= int(channel_mode) }

- ${ rf_freq > 0 }

- ${ calibr_bandw_ch0 >= 2.5e6 or calibr_bandw_ch0 == 0 }
- ${ 120e6 >= calibr_bandw_ch0 }

- ${ calibr_bandw_ch1 >= 2.5e6 or calibr_bandw_ch1 == 0 }
- ${ 120e6 >= calibr_bandw_ch1 }

- ${ analog_bandw_ch0 >= 1.5e6 or analog_bandw_ch0 == 0 }
- ${ 130e6 >= analog_bandw_ch0 }

- ${ analog_bandw_ch1 >= 1.5e6 or analog_bandw_ch1 == 0 }
- ${ 130e6 >= analog_bandw_ch1 }

- ${ digital_bandw_ch0 >= 0 }
- ${ samp_rate >= digital_bandw_ch0 or digital_bandw_ch0 == 0 }

- ${ digital_bandw_ch1 >= 0 }
- ${ samp_rate >= digital_bandw_ch1 or digital_bandw_ch0 == 0 }

- ${ gain_dB_ch0 >= 0 }
- ${ 70 >= gain_dB_ch0 }

- ${ gain_dB_ch1 >= 0 }
- ${ 70 >= gain_dB_ch1 }

- ${ samp_rate > 0 }
- ${ 61.44e6 >= samp_rate }

outputs:
- label: out
  domain: stream
  dtype: complex
  multiplicity: ${ (2 if int(channel_mode) == 2 else 1) }

file_format: 1

documentation: |-
  -------------------------------------------------------------------------------------------------------------------
  DEVICE SERIAL
  
  Device serial number obtained by running
  
  	LimeUtil --find
  
  If left blank, the first device in the list is used.
  -------------------------------------------------------------------------------------------------------------------
  CHANNEL
  
  Use this setting to choose between SISO channels or MIMO mode.
  
  Note: not all devices support MIMO mode and have more than one channel.
  -------------------------------------------------------------------------------------------------------------------
  FILE
  
  Generate .ini file with LimeSuite and select path.
  
  Note: setting must match in LimeSuite Source and Sink for the same device.
  -------------------------------------------------------------------------------------------------------------------
  RF FREQUENCY
  
  Set RF center frequency for RX (both channels).
  LimeSDR-USB supports 	  [100e3,3800e6] 	Hz.
  LimeSDR-PCIe supports 	[100e3,3800e6] 	Hz.
  LimeSDR-Mini supports 	[10e6,3500e6] 	Hz.
  LimeSDR-Micro supports 	[10e6,3500e6] 	Hz.
  -------------------------------------------------------------------------------------------------------------------
  SAMPLE RATE
  
  Here you can enter sample rate for RX.
  
  LimeSDR-USB sample rate must be no more than 61.44e6 S/s.
  LimeSDR-PCIe sample rate must be no more than 61.44e6 S/s.
  LimeSDR-Mini sample rate must be no more than 30.72e6 S/s.
  LimeSDR-Micro sample rate must be no more than 10e6 S/s.
  
  Note: LimeSDR-Mini and LimeSDR-Micro supports only the same sample rate for TX and RX.
  -------------------------------------------------------------------------------------------------------------------
  OVERSAMPLE
  
  Here you can select oversampling value for RX. Default value uses highest possible oversampling value.
  
  Note: LimeSDR-Mini and LimeSDR-Micro supports only the same oversampling value for TX and RX.
  -------------------------------------------------------------------------------------------------------------------
  NCO FREQUENCY
  
  Adjust numerically controlled oscillator for each channel. 0 means that NCO is OFF.
  -------------------------------------------------------------------------------------------------------------------
  CALIBRATION BANDW.
  
  This setting is used to set bandwidth for calibration for each channel. This value should be equal to your signal bandwidth.
  Calibration is off when bandwidth is set to 0.
  
  Calibration bandwidth range must be [2.5e6,120e6] Hz.
  -------------------------------------------------------------------------------------------------------------------
  LNA PATH
  
  Select active low-noise amplifier path of each channel.
  For LimeSDR-Mini and LimeNET-Micro Auto(Default) option sets preferred LNA path depending on RF frequency.
  For LimeSDR-USB and LimeSDR-PCIe Auto(Default) sets LNA path to LNAL.
  -------------------------------------------------------------------------------------------------------------------
  ANALOG FILTER BANDW.
  
  Enter analog filter bandwidth for each channel. Analog filter is off if bandwidth is set to 0.
  Analog filter bandwidth range must be [1.5e6,130e6] Hz.
  -------------------------------------------------------------------------------------------------------------------
  DIGITAL FILTER BANDW.
  
  Enter digital filter bandwidth for each channel. Digital filter if off if bandwidth is set to 0.
  Bandwidth should not be higher than sample rate.
  -------------------------------------------------------------------------------------------------------------------
  GAIN
  
  Controls combined RX gain settings. Gain range must be [0,70] dB.
  -------------------------------------------------------------------------------------------------------------------
  TCXO DAC
  
  Controls 40 MHz TCXO DAC settings.  To enable this parameter "Allow TCXO DAC control" in the "Advanced" tab must be set to "Yes"
  Care must be taken as this parameter is returned to default value only after power off.
  
  LimeSDR-Mini default value is 180 range is [0,255]
  LimeSDR-USB default value is 125 range is [0,255]
  LimeSDR-PCIe default value is 134 range is [0,255]
  LimeNET-Micro default value is 30714 range is [0,65535]
  -------------------------------------------------------------------------------------------------------------------
