host: add bladerf_adf4002_{get,set}_refclk and related functionality (#15)

* host: libbladeRF: add functions to get/set ADF4002 refclk frequency

* host: bladeRF-cli: add print/set adf_refclk

* host: python: add adf4002_refclk functions

This permits getting and setting the ADF4002 reference clock
frequency.

>>> import bladerf
>>> b = bladerf.BladeRF()
ad9361_init : AD936x Rev 2 successfully initialized
>>> b.adf4002_refclk
10000000
>>> b.adf4002_refclk = 9000000
>>> b.adf4002_refclk
9000000
>>> b.adf4002_refclk = 900000000
[ERROR @
"/home/rtucker/dev/privatebladeRF/host/libraries/libbladeRF/src/board/bladerf2/bladerf2.c":4360]
bladerf_adf4002_set_refclk: bladerf_adf4002_calculate_ratio failed:
Provided parameter was out of the allowable range
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/home/rtucker/dev/privatebladeRF/host/libraries/libbladeRF_bindings/python/bladerf.py",
line 1581, in set_adf4002_refclk
    _check_error(ret)
  File
"/home/rtucker/dev/privatebladeRF/host/libraries/libbladeRF_bindings/python/bladerf.py",
line 998, in _check_error
    raise globals()[errorval.name](err)
bladerf.RangeError: Provided parameter was out of the allowable range
>>> b.adf4002_refclk
9000000
>>> b.adf4002_refclk = 10000000
>>> b.adf4002_refclk
10000000

* host: libbladeRF: make ADF convenience functions static

bladerf_adf4002_set_refclk is much nicer (and safer) than calculating
the R/N ratio and then doing a bladerf_adf4002_configure.

* host: python: update cffi header block
This commit is contained in:
Rey Tucker 2018-03-23 11:10:14 -04:00 • committed by GitHub
commit 10d0f549b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 328 additions and 158 deletions

View file

@ -198,38 +198,41 @@ API_EXPORT
int CALL_CONV bladerf_adf4002_set_enable(struct bladerf *dev, bool enable);
/**
* Configure the ADF4002 Phase Detector/Frequency Synthesizer
*
* Use \ref bladerf_adf4002_calculate_ratio to compute the R and N from given
* reference and system clock frequencies.
* Get the valid range of frequencies for the ADF4002 reference clock input
*
* @param dev Device handle
* @param[in] R Reference counter divide ratio (1...16383)
* @param[in] N N counter divide ratio (1...8191)
* @param[out] range Reference clock frequency range
*
* @return 0 on success, value from \ref RETCODES list on failure
*/
API_EXPORT
int CALL_CONV bladerf_adf4002_configure(struct bladerf *dev,
uint16_t R,
uint16_t N);
int CALL_CONV bladerf_adf4002_get_refclk_range(struct bladerf *dev,
struct bladerf_range *range);
/**
* Calculate valid R and N values for given reference and system clock
* frequencies.
* Get the currently-configured frequency for the ADF4002 reference clock
* input.
*
* @param[in] ref_freq Reference clock frequency, in Hz
* @param[in] clock_freq System clock frequency, in Hz
* @param[out] R Reference counter divide ratio (1...16383)
* @param[out] N N counter divide ratio (1...8191)
* @param dev Device handle
* @param[out] frequency Reference clock frequency
*
* @return 0 on success, value from \ref RETCODES list on failure
*/
API_EXPORT
int CALL_CONV bladerf_adf4002_calculate_ratio(uint64_t ref_freq,
uint64_t clock_freq,
uint16_t *R,
uint16_t *N);
int CALL_CONV bladerf_adf4002_get_refclk(struct bladerf *dev,
uint64_t *frequency);
/**
* Set the expected frequency for the ADF4002 reference clock input.
*
* @param dev Device handle
* @param[in] frequency Reference clock frequency
*
* @return 0 on success, value from \ref RETCODES list on failure
*/
API_EXPORT
int CALL_CONV bladerf_adf4002_set_refclk(struct bladerf *dev,
uint64_t frequency);
/**
* Read value from ADF4002 Phase Detector/Frequency Synthesizer

View file

@ -100,7 +100,6 @@ struct bladerf2_board_data {
/* VCTCXO trim state */
enum bladerf2_vctcxo_trim_source trim_source;
uint16_t trimdac_value;
uint64_t refclk_frequency;
};
/* Macro for logging and returning an error status. This should be used for
@ -415,6 +414,13 @@ static const struct bladerf_range bladerf2_tx_frequency_range = {
FIELD_INIT(.scale, 1),
};
static const struct bladerf_range bladerf2_adf4002_refclk_range = {
FIELD_INIT(.min, 5e6),
FIELD_INIT(.max, 300e6),
FIELD_INIT(.step, 1),
FIELD_INIT(.scale, 1),
};
/* RF Ports */
static const struct bladerf_ad9361_port_name_map bladerf2_rx_port_map[] = {
{ FIELD_INIT(.name, "A_BALANCED"), FIELD_INIT(.id, A_BALANCED), },
@ -1129,19 +1135,9 @@ static int bladerf2_initialize(struct bladerf *dev)
board_data->trim_source = TRIM_SOURCE_TRIM_DAC;
/* Configure ADF4002 */
uint16_t R, N;
board_data->refclk_frequency = BLADERF_REFIN_DEFAULT;
status = bladerf_adf4002_calculate_ratio(board_data->refclk_frequency,
BLADERF_VCTCXO_FREQUENCY, &R, &N);
status = bladerf_adf4002_set_refclk(dev, BLADERF_REFIN_DEFAULT);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_calculate_ratio", status);
}
status = bladerf_adf4002_configure(dev, R, N);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_configure", status);
RETURN_ERROR_STATUS("bladerf_adf4002_set_refclk", status);
}
/* Update device state */
@ -4151,6 +4147,149 @@ int bladerf_ad9361_get_rssi(struct bladerf *dev,
/* Low level ADF4002 Accessors */
/******************************************************************************/
static int bladerf_adf4002_configure(struct bladerf *dev,
uint16_t R,
uint16_t N)
{
int status;
uint32_t init_array[3];
bool is_enabled;
if (NULL == dev) {
RETURN_INVAL("dev", "not initialized");
}
if (dev->board != &bladerf2_board_fns) {
return BLADERF_ERR_UNSUPPORTED;
}
if (R < 1 || R > 16383) {
RETURN_INVAL("R", "outside range [1,16383]");
}
if (N < 1 || N > 8191) {
RETURN_INVAL("N", "outside range [1,8191]");
}
CHECK_BOARD_STATE(STATE_FPGA_LOADED);
/* Get the present state... */
status = bladerf_adf4002_get_enable(dev, &is_enabled);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_get_enable", status);
}
/* Enable the chip if applicable */
if (!is_enabled) {
bladerf_adf4002_set_enable(dev, true);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_set_enable", status);
}
}
/* Register 0: Reference Counter Latch */
init_array[0] = 0;
/* R Counter: */
init_array[0] |= (R & ((1 << 14) - 1)) << 2;
/* Hardcoded values: */
/* Anti-backlash: 00 (2.9 ns) */
/* Lock detect precision: 0 (three cycles) */
/* Register 1: N Counter Latch */
init_array[1] = 1;
/* N Counter: */
init_array[1] |= (N & ((1 << 13) - 1)) << 8;
/* Hardcoded values: */
/* CP Gain: 0 (Setting 1) */
/* Register 2: Function Latch */
init_array[2] = 2;
/* Hardcoded values: */
/* Counter operation: 0 (Normal) */
/* Power down control: 00 (Normal) */
/* Muxout control: 0x1 (digital lock detect) */
init_array[2] |= (1 & ((1 << 3) - 1)) << 4;
/* PD Polarity: 1 (positive) */
init_array[2] |= 1 << 7;
/* CP three-state: 0 (normal) */
/* Fastlock Mode: 00 (disabled) */
/* Timer Counter Control: 0000 (3 PFD cycles) */
/* Current Setting 1: 111 (5 mA) */
init_array[2] |= 0x7 << 15;
/* Current Setting 2: 111 (5 mA) */
init_array[2] |= 0x7 << 18;
/* Write the values to the chip */
for (size_t i = 0; i < ARRAY_SIZE(init_array); ++i) {
log_debug("reg %x gets 0x%08x\n", i, init_array[i]);
status = bladerf_adf4002_write(dev, i, init_array[i]);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_write", status);
}
}
/* Re-disable the chip if applicable */
if (!is_enabled) {
bladerf_adf4002_set_enable(dev, false);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_set_enable", status);
}
}
return 0;
}
static int bladerf_adf4002_calculate_ratio(uint64_t ref_freq,
uint64_t clock_freq,
uint16_t *R,
uint16_t *N)
{
size_t const Rmax = 16383;
double const tol = 0.00001;
double target = (double)clock_freq / (double)ref_freq;
struct bladerf_range const clock_frequency_range = {
FIELD_INIT(.min, 5e6), FIELD_INIT(.max, 400e6), FIELD_INIT(.step, 1),
FIELD_INIT(.scale, 1),
};
if (NULL == R) {
RETURN_INVAL("R", "is null");
}
if (NULL == N) {
RETURN_INVAL("N", "is null");
}
if (!_is_within_range(&bladerf2_adf4002_refclk_range, ref_freq)) {
return BLADERF_ERR_RANGE;
}
if (!_is_within_range(&clock_frequency_range, clock_freq)) {
return BLADERF_ERR_RANGE;
}
for (uint16_t R_try = 1; R_try < Rmax; ++R_try) {
uint16_t N_try = (uint16_t)(target * R_try + 0.5);
if (N_try > 8191) {
continue;
}
double ratio = (double)N_try / (double)R_try;
double delta = (ratio > target) ? (ratio - target) : (target - ratio);
if (delta < tol) {
*R = R_try;
*N = N_try;
return 0;
}
}
RETURN_INVAL("requested ratio", "not achievable");
}
int bladerf_adf4002_get_locked(struct bladerf *dev, bool *locked)
{
int status;
@ -4277,11 +4416,31 @@ int bladerf_adf4002_set_enable(struct bladerf *dev, bool enable)
return 0;
}
int bladerf_adf4002_configure(struct bladerf *dev, uint16_t R, uint16_t N)
int bladerf_adf4002_get_refclk_range(struct bladerf *dev,
struct bladerf_range *range)
{
if (NULL == range) {
RETURN_INVAL("range", "is null");
}
*range = bladerf2_adf4002_refclk_range;
return 0;
}
int bladerf_adf4002_get_refclk(struct bladerf *dev, uint64_t *frequency)
{
int status;
uint32_t init_array[3];
bool is_enabled;
uint32_t reg;
uint16_t R, N;
uint8_t const R_LATCH_REG = 0;
size_t const R_LATCH_SHIFT = 2;
uint32_t const R_LATCH_MASK = 0x3fff;
uint8_t const N_LATCH_REG = 1;
size_t const N_LATCH_SHIFT = 8;
uint32_t const N_LATCH_MASK = 0x1fff;
if (NULL == dev) {
RETURN_INVAL("dev", "not initialized");
@ -4291,126 +4450,54 @@ int bladerf_adf4002_configure(struct bladerf *dev, uint16_t R, uint16_t N)
return BLADERF_ERR_UNSUPPORTED;
}
if (R < 1 || R > 16383) {
RETURN_INVAL("R", "outside range [1,16383]");
}
if (N < 1 || N > 8191) {
RETURN_INVAL("N", "outside range [1,8191]");
}
CHECK_BOARD_STATE(STATE_FPGA_LOADED);
/* Get the present state... */
status = bladerf_adf4002_get_enable(dev, &is_enabled);
// Get the current R value (latch 0, bits 2-15)
status = bladerf_adf4002_read(dev, R_LATCH_REG, &reg);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_get_enable", status);
RETURN_ERROR_STATUS("bladerf_adf4002_read", status);
}
R = (reg >> R_LATCH_SHIFT) & R_LATCH_MASK;
/* Enable the chip if applicable */
if (!is_enabled) {
bladerf_adf4002_set_enable(dev, true);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_set_enable", status);
}
// Get the current N value (latch 1, bits 8-20)
status = bladerf_adf4002_read(dev, N_LATCH_REG, &reg);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_read", status);
}
N = (reg >> N_LATCH_SHIFT) & N_LATCH_MASK;
/* Register 0: Reference Counter Latch */
init_array[0] = 0;
/* R Counter: */
init_array[0] |= (R & ((1 << 14) - 1)) << 2;
/* Hardcoded values: */
/* Anti-backlash: 00 (2.9 ns) */
/* Lock detect precision: 0 (three cycles) */
/* Register 1: N Counter Latch */
init_array[1] = 1;
/* N Counter: */
init_array[1] |= (N & ((1 << 13) - 1)) << 8;
/* Hardcoded values: */
/* CP Gain: 0 (Setting 1) */
/* Register 2: Function Latch */
init_array[2] = 2;
/* Hardcoded values: */
/* Counter operation: 0 (Normal) */
/* Power down control: 00 (Normal) */
/* Muxout control: 0x1 (digital lock detect) */
init_array[2] |= (1 & ((1 << 3) - 1)) << 4;
/* PD Polarity: 1 (positive) */
init_array[2] |= 1 << 7;
/* CP three-state: 0 (normal) */
/* Fastlock Mode: 00 (disabled) */
/* Timer Counter Control: 0000 (3 PFD cycles) */
/* Current Setting 1: 111 (5 mA) */
init_array[2] |= 0x7 << 15;
/* Current Setting 2: 111 (5 mA) */
init_array[2] |= 0x7 << 18;
/* Write the values to the chip */
for (size_t i = 0; i < ARRAY_SIZE(init_array); ++i) {
log_debug("reg %x gets 0x%08x\n", i, init_array[i]);
status = bladerf_adf4002_write(dev, i, init_array[i]);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_write", status);
}
}
/* Re-disable the chip if applicable */
if (!is_enabled) {
bladerf_adf4002_set_enable(dev, false);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_set_enable", status);
}
}
// We assume the system clock frequency is BLADERF_VCTCXO_FREQUENCY.
// If it isn't, do your own math
*frequency = R * BLADERF_VCTCXO_FREQUENCY / N;
return 0;
}
int bladerf_adf4002_calculate_ratio(uint64_t ref_freq,
uint64_t clock_freq,
uint16_t *R,
uint16_t *N)
int bladerf_adf4002_set_refclk(struct bladerf *dev, uint64_t frequency)
{
size_t const Rmax = 16383;
double const tol = 0.00001;
double target = (double)clock_freq / (double)ref_freq;
int status;
uint16_t R, N;
if (NULL == R) {
RETURN_INVAL("R", "is null");
if (NULL == dev) {
RETURN_INVAL("dev", "not initialized");
}
if (NULL == N) {
RETURN_INVAL("N", "is null");
if (dev->board != &bladerf2_board_fns) {
return BLADERF_ERR_UNSUPPORTED;
}
if (ref_freq < 5e6 || ref_freq > 300e6) {
RETURN_INVAL("ref_freq", "is out of range [5e6,300e6]");
// We assume the system clock frequency is BLADERF_VCTCXO_FREQUENCY.
// If it isn't, do your own math
status = bladerf_adf4002_calculate_ratio(frequency,
BLADERF_VCTCXO_FREQUENCY, &R, &N);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_calculate_ratio", status);
}
if (clock_freq < 5e6 || clock_freq > 400e6) {
RETURN_INVAL("clock_freq", "is out of range [5e6,400e6]");
status = bladerf_adf4002_configure(dev, R, N);
if (status < 0) {
RETURN_ERROR_STATUS("bladerf_adf4002_configure", status);
}
for (uint16_t R_try = 1; R_try < Rmax; ++R_try) {
uint16_t N_try = (uint16_t)(target * R_try + 0.5);
if (N_try > 8191) {
continue;
}
double ratio = (double)N_try / (double)R_try;
double delta = (ratio > target) ? (ratio - target) : (target - ratio);
if (delta < tol) {
*R = R_try;
*N = N_try;
return 0;
}
}
RETURN_INVAL("requested ratio", "not achievable");
return 0;
}
int bladerf_adf4002_read(struct bladerf *dev, uint8_t address, uint32_t *val)

View file

@ -87,6 +87,11 @@ def _print_cmd_info(device=None, devinfo=None, verbose=False):
print(" Modes ", _strify_list(b.loopback_modes))
print("RX Mux ", b.rx_mux)
print("ADF4002 Mode ", _bool_to_onoff(b.adf4002_enable))
if b.adf4002_enable:
print("ADF4002 Refclk ", b.adf4002_refclk)
print("ADF4002 Locked ", _bool_to_onoff(b.adf4002_locked))
print("RX Channel Count ", b.rx_channel_count)
for c in range(b.rx_channel_count):
ch = b.Channel(bladerf.CHANNEL_RX(c))

View file

@ -585,10 +585,12 @@ ffi.cdef("""
int bladerf_adf4002_get_locked(struct bladerf *dev, bool *locked);
int bladerf_adf4002_get_enable(struct bladerf *dev, bool *enabled);
int bladerf_adf4002_set_enable(struct bladerf *dev, bool enable);
int bladerf_adf4002_configure(struct bladerf *dev, uint16_t R,
uint16_t N);
int bladerf_adf4002_calculate_ratio(uint64_t ref_freq, uint64_t
clock_freq, uint16_t *R, uint16_t *N);
int bladerf_adf4002_get_refclk_range(struct bladerf *dev, struct
bladerf_range *range);
int bladerf_adf4002_get_refclk(struct bladerf *dev, uint64_t
*frequency);
int bladerf_adf4002_set_refclk(struct bladerf *dev, uint64_t
frequency);
int bladerf_adf4002_read(struct bladerf *dev, uint8_t address,
uint32_t *val);
int bladerf_adf4002_write(struct bladerf *dev, uint8_t address,
@ -1534,19 +1536,25 @@ class BladeRF:
# ADF4002 Phase Detector/Frequency Synthesizer
def get_adf4002_locked(self):
locked = ffi.new("bool *")
ret = libbladeRF.bladerf_adf4002_get_locked(self.dev[0], locked)
_check_error(ret)
return bool(locked[0])
try:
locked = ffi.new("bool *")
ret = libbladeRF.bladerf_adf4002_get_locked(self.dev[0], locked)
_check_error(ret)
return bool(locked[0])
except UnsupportedError:
return None
adf4002_locked = property(get_adf4002_locked,
doc="ADF4002 PLL lock indication")
def get_adf4002_enable(self):
enable = ffi.new("bool *")
ret = libbladeRF.bladerf_adf4002_get_enable(self.dev[0], enable)
_check_error(ret)
return bool(enable[0])
try:
enable = ffi.new("bool *")
ret = libbladeRF.bladerf_adf4002_get_enable(self.dev[0], enable)
_check_error(ret)
return bool(enable[0])
except UnsupportedError:
return None
def set_adf4002_enable(self, enable):
ret = libbladeRF.bladerf_adf4002_set_enable(self.dev[0], enable)
@ -1555,20 +1563,21 @@ class BladeRF:
adf4002_enable = property(get_adf4002_enable, set_adf4002_enable,
doc="ADF4002 Enable")
def set_adf4002_frequency(self, refclk, sysclk=BLADERF_VCTCXO_FREQUENCY):
r = ffi.new("uint16_t *")
n = ffi.new("uint16_t *")
# Calculate R and N values for clock ratio
ret = libbladeRF.bladerf_adf4002_calculate_ratio(int(refclk),
int(sysclk), r, n)
def get_adf4002_refclk(self):
try:
freq = ffi.new("uint64_t *")
ret = libbladeRF.bladerf_adf4002_get_refclk(self.dev[0], freq)
_check_error(ret)
return int(freq[0])
except UnsupportedError:
return None
def set_adf4002_refclk(self, freq):
ret = libbladeRF.bladerf_adf4002_set_refclk(self.dev[0], freq)
_check_error(ret)
# Configure w/ the ratios
ret = libbladeRF.bladerf_adf4002_configure(self.dev[0], r[0], n[0])
_check_error(ret)
adf4002_frequency = property(fset=set_adf4002_frequency,
doc="ADF4002 Reference Clock frequency")
adf4002_refclk = property(get_adf4002_refclk, set_adf4002_refclk,
doc="ADF4002 Reference Clock frequency")
# Power source

View file

@ -87,6 +87,7 @@ struct printset_entry {
/* Declarations */
PRINTSET_DECL(ad9361);
PRINTSET_DECL(adf_enable);
PRINTSET_DECL(adf_refclk);
PRINTSET_DECL(agc);
PRINTSET_DECL(power_source);
PRINTSET_DECL(clock_sel);
@ -140,6 +141,7 @@ struct printset_entry printset_table[] = {
PRINTSET_ENTRY(samplerate, PRINTALL_OPTION_APPEND_NEWLINE, BOARD_ANY),
PRINTSET_ENTRY(smb_mode, PRINTALL_OPTION_APPEND_NEWLINE, BOARD_BLADERF1),
PRINTSET_ENTRY(adf_enable, PRINTALL_OPTION_APPEND_NEWLINE, BOARD_BLADERF2),
PRINTSET_ENTRY(adf_refclk, PRINTALL_OPTION_APPEND_NEWLINE, BOARD_BLADERF2),
PRINTSET_ENTRY(biastee, PRINTALL_OPTION_APPEND_NEWLINE, BOARD_BLADERF2),
PRINTSET_ENTRY(trimdac, PRINTALL_OPTION_APPEND_NEWLINE, BOARD_ANY),
PRINTSET_ENTRY(vctcxo_tamer, PRINTALL_OPTION_APPEND_NEWLINE, BOARD_BLADERF1),
@ -262,6 +264,70 @@ int set_adf_enable(struct cli_state *state, int argc, char **argv)
return rv;
}
int print_adf_refclk(struct cli_state *state, int argc, char **argv)
{
int rv = CLI_RET_OK;
int *err = &state->last_lib_error;
int status;
uint64_t val;
status = bladerf_adf4002_get_refclk(state->dev, &val);
if (status < 0) {
*err = status;
rv = CLI_RET_LIBBLADERF;
}
if (rv == CLI_RET_OK) {
printf(" ADF Ref Clock Frequency: %10" PRIu64 " Hz\n", val);
}
return rv;
}
int set_adf_refclk(struct cli_state *state, int argc, char **argv)
{
/* set adf_refclk {frequency} */
int rv = CLI_RET_OK;
int *err = &state->last_lib_error;
int status;
struct bladerf_range range;
uint64_t freq;
if (rv == CLI_RET_OK) {
/* Get valid frequency range */
status = bladerf_adf4002_get_refclk_range(state->dev, &range);
if (status < 0) {
*err = status;
rv = CLI_RET_LIBBLADERF;
}
}
if (argc == 3) {
bool ok;
/* Parse frequency */
freq = str2uint64_suffix(argv[argc - 1], range.min, range.max,
freq_suffixes, NUM_FREQ_SUFFIXES, &ok);
if (!ok) {
cli_err_nnl(state, argv[0], "Invalid adf_refclk value (%s)\n",
argv[2]);
rv = CLI_RET_INVPARAM;
} else {
status = bladerf_adf4002_set_refclk(state->dev, freq);
if (status < 0) {
*err = status;
rv = CLI_RET_LIBBLADERF;
} else {
rv = print_adf_refclk(state, argc, argv);
}
}
} else {
rv = CLI_RET_NARGS;
}
return rv;
}
int print_agc(struct cli_state *state, int argc, char **argv)
{
int rv = CLI_RET_OK;