libbladeRF: Decoupling correction calls from USB code

The backend interface now presents functions for IQ gain/phase
access, instead of lumping it all under "FPGA corrections."

There is no need for the USB code to know anything about LMS
corrections, so this has been moved back up the top level,
with bladerf.c calling into the lms.c functions that now
provide this functionality.

The USB backend now hooks up calls to nios_legacy_* routines.
In the future we can maintain a USB backend that uses these, while
another backend structure proivdes the use of new NIOS packet handlers.
This commit is contained in:
Jon Szymaniak 2015-06-15 00:09:53 -04:00
commit a219666893
9 changed files with 462 additions and 223 deletions

View file

@ -729,4 +729,64 @@ int lms_select_sampling(struct bladerf *dev, bladerf_sampling sampling);
*/
int lms_get_sampling(struct bladerf *dev, bladerf_sampling *sampling);
/**
* Set the DC offset value on the I channel
*
* For consistency with other bladeRF correction values,
* this value is scaled to [-2048, 2048].
*
* @param[in] dev Device handle
* @param[in] module Module to adjust
* @param[in] value DC offset adjustment value to write
*
* @return 0 on succes, BLADERF_ERR_* value on failure
*/
int lms_set_dc_offset_i(struct bladerf *dev,
bladerf_module module, uint16_t value);
/**
* Get the DC offset value on the I channel
*
* For consistency with other bladeRF correction values,
* this value is scaled to [-2048, 2048].
*
* @param[in] dev Device handle
* @param[in] module Module to adjust
* @param[out] value On success, the DC offset value on the I channel
*
* @return 0 on succes, BLADERF_ERR_* value on failure
*/
int lms_get_dc_offset_i(struct bladerf *dev,
bladerf_module module, int16_t *value);
/**
* Set the DC offset value on the Q channel.
*
* For consistency with other bladeRF correction values,
* this value is scaled to [-2048, 2048].
*
* @param[in] dev Device handle
* @param[in] module Module to adjust
* @param[in] value DC offset adjustment value to write
*
* @return 0 on succes, BLADERF_ERR_* value on failure
*/
int lms_set_dc_offset_q(struct bladerf *dev,
bladerf_module module, int16_t value);
/**
* Get the DC offset value on the Q channel
*
* For consistency with other bladeRF correction values,
* this value is scaled to [-2048, 2048].
*
* @param[in] dev Device handle
* @param[in] module Module to adjust
* @param[out] value On success, the DC offset value on the I channel
*
* @return 0 on succes, BLADERF_ERR_* value on failure
*/
int lms_get_dc_offset_q(struct bladerf *dev,
bladerf_module module, int16_t *value);
#endif /* LMS_H_ */

View file

@ -3566,3 +3566,172 @@ out:
return status;
}
#endif
#ifndef BLADERF_NIOS_BUILD
static inline uint8_t scale_dc_offset(bladerf_module module, int16_t value)
{
uint8_t ret;
switch (module) {
case BLADERF_MODULE_RX:
/* RX only has 6 bits of scale to work with, remove normalization */
value >>= 5;
if (value < 0) {
if (value <= -64) {
/* Clamp */
value = 0x3f;
} else {
value = (-value) & 0x3f;
}
/* This register uses bit 6 to denote a negative value */
value |= (1 << 6);
} else {
if (value >= 64) {
/* Clamp */
value = 0x3f;
} else {
value = value & 0x3f;
}
}
ret = (uint8_t) value;
break;
case BLADERF_MODULE_TX:
/* TX only has 7 bits of scale to work with, remove normalization */
value >>= 4;
/* LMS6002D 0x00 = -16, 0x80 = 0, 0xff = 15.9375 */
if (value >= 0) {
ret = (uint8_t) (value >= 128) ? 0x7f : (value & 0x7f);
/* Assert bit 7 for positive numbers */
ret = (1 << 7) | ret;
} else {
ret = (uint8_t) (value <= -128) ? 0x00 : (value & 0x7f);
}
break;
default:
assert(!"Invalid module provided");
ret = 0x00;
}
return ret;
}
#endif
#ifndef BLADERF_NIOS_BUILD
static int set_dc_offset_reg(struct bladerf *dev, bladerf_module module,
uint8_t addr, int16_t value)
{
int status;
uint8_t regval, tmp;
switch (module) {
case BLADERF_MODULE_RX:
status = LMS_READ(dev, addr, &tmp);
if (status != 0) {
return status;
}
/* Bit 7 is unrelated to lms dc correction, save its state */
tmp = tmp & (1 << 7);
regval = scale_dc_offset(module, value) | tmp;
break;
case BLADERF_MODULE_TX:
regval = scale_dc_offset(module, value);
break;
default:
return BLADERF_ERR_INVAL;
}
status = LMS_WRITE(dev, addr, regval);
return status;
}
#endif
#ifndef BLADERF_NIOS_BUILD
int lms_set_dc_offset_i(struct bladerf *dev,
bladerf_module module, uint16_t value)
{
const uint8_t addr = (module == BLADERF_MODULE_TX) ? 0x42 : 0x71;
return set_dc_offset_reg(dev, module, addr, value);
}
#endif
#ifndef BLADERF_NIOS_BUILD
int lms_set_dc_offset_q(struct bladerf *dev,
bladerf_module module, int16_t value)
{
const uint8_t addr = (module == BLADERF_MODULE_TX) ? 0x43 : 0x72;
return set_dc_offset_reg(dev, module, addr, value);
}
#endif
#ifndef BLADERF_NIOS_BUILD
int get_dc_offset(struct bladerf *dev, bladerf_module module,
uint8_t addr, int16_t *value)
{
int status;
uint8_t tmp;
status = LMS_READ(dev, addr, &tmp);
if (status != 0) {
return status;
}
switch (module) {
case BLADERF_MODULE_RX:
/* Mask out an unrelated control bit */
tmp = tmp & 0x7f;
/* Determine sign */
if (tmp & (1 << 6)) {
*value = -(int16_t)(tmp & 0x3f);
} else {
*value = (int16_t)(tmp & 0x3f);
}
/* Renormalize to 2048 */
*value <<= 5;
break;
case BLADERF_MODULE_TX:
*value = (int16_t) tmp;
/* Renormalize to 2048 */
*value <<= 4;
break;
default:
return BLADERF_ERR_INVAL;
}
return 0;
}
#endif
#ifndef BLADERF_NIOS_BUILD
int lms_get_dc_offset_i(struct bladerf *dev,
bladerf_module module, int16_t *value)
{
const uint8_t addr = (module == BLADERF_MODULE_TX) ? 0x42 : 0x71;
return get_dc_offset(dev, module, addr, value);
}
#endif
#ifndef BLADERF_NIOS_BUILD
int lms_get_dc_offset_q(struct bladerf *dev,
bladerf_module module, int16_t *value)
{
const uint8_t addr = (module == BLADERF_MODULE_TX) ? 0x43 : 0x72;
return get_dc_offset(dev, module, addr, value);
}
#endif

View file

@ -104,11 +104,18 @@ struct backend_fns {
int (*expansion_gpio_dir_write)(struct bladerf *dev, uint32_t val);
int (*expansion_gpio_dir_read)(struct bladerf *dev, uint32_t *val);
/* IQ Calibration Settings */
int (*set_correction)(struct bladerf *dev, bladerf_module,
bladerf_correction corr, int16_t value);
int (*get_correction)(struct bladerf *dev, bladerf_module module,
bladerf_correction corr, int16_t *value);
/* IQ Correction Settings */
int (*set_iq_gain_correction)(struct bladerf *dev, bladerf_module module,
int16_t value);
int (*set_iq_phase_correction)(struct bladerf *dev, bladerf_module module,
int16_t value);
int (*get_iq_gain_correction)(struct bladerf *dev, bladerf_module module,
int16_t *value);
int (*get_iq_phase_correction)(struct bladerf *dev, bladerf_module module,
int16_t *value);
/* Get current timestamp counter values */
int (*get_timestamp)(struct bladerf *dev, bladerf_module mod, uint64_t *value);

View file

@ -132,18 +132,36 @@ static int dummy_expansion_gpio_dir_read(struct bladerf *dev, uint32_t *val)
return 0;
}
static int dummy_set_correction(struct bladerf *dev, bladerf_module module,
bladerf_correction corr, int16_t value)
static int dummy_set_iq_gain_correction(struct bladerf *dev,
bladerf_module module,
int16_t value)
{
return 0;
}
static int dummy_get_correction(struct bladerf *dev, bladerf_module module,
bladerf_correction corr, int16_t *value)
static int dummy_set_iq_phase_correction(struct bladerf *dev,
bladerf_module module,
int16_t value)
{
return 0;
}
static int dummy_get_iq_gain_correction(struct bladerf *dev,
bladerf_module module,
int16_t *value)
{
*value = 0;
return 0;
}
static int dummy_get_iq_phase_correction(struct bladerf *dev,
bladerf_module module,
int16_t *value)
{
*value = 0;
return 0;
}
static int dummy_get_timestamp(struct bladerf *dev, bladerf_module mod,
uint64_t *val)
{
@ -265,8 +283,10 @@ const struct backend_fns backend_fns_dummy = {
FIELD_INIT(.expansion_gpio_dir_write, dummy_expansion_gpio_dir_write),
FIELD_INIT(.expansion_gpio_dir_read, dummy_expansion_gpio_dir_read),
FIELD_INIT(.set_correction, dummy_set_correction),
FIELD_INIT(.get_correction, dummy_get_correction),
FIELD_INIT(.set_iq_gain_correction, dummy_set_iq_gain_correction),
FIELD_INIT(.set_iq_phase_correction, dummy_set_iq_phase_correction),
FIELD_INIT(.get_iq_gain_correction, dummy_get_iq_gain_correction),
FIELD_INIT(.get_iq_phase_correction, dummy_get_iq_phase_correction),
FIELD_INIT(.get_timestamp, dummy_get_timestamp),

View file

@ -392,10 +392,7 @@ int nios_legacy_vctcxo_trim_dac_write(struct bladerf *dev, uint16_t value)
return status;
}
int nios_legacy_get_fpga_correction(struct bladerf *dev,
bladerf_correction corr,
uint8_t addr, int16_t *value)
static int get_iq_correction(struct bladerf *dev, uint8_t addr, int16_t *value)
{
int i;
int status;
@ -411,28 +408,15 @@ int nios_legacy_get_fpga_correction(struct bladerf *dev,
*value |= (cmd.data << (i * 8));
}
/* Gain corrections have an offset that needs to be accounted for */
if (corr == BLADERF_CORR_FPGA_GAIN) {
*value -= 4096;
}
return status;
}
int nios_legacy_set_fpga_correction(struct bladerf *dev,
bladerf_correction corr,
uint8_t addr, int16_t value)
static int set_iq_correction(struct bladerf *dev, uint8_t addr, int16_t value)
{
int i;
int status;
struct uart_cmd cmd;
/* If this is a gain correction add in the 1.0 value so 0 correction yields
* an unscaled gain */
if (corr == BLADERF_CORR_FPGA_GAIN) {
value += (int16_t)4096;
}
for (i = status = 0; status == 0 && i < 2; i++) {
cmd.addr = i + addr;
@ -444,6 +428,117 @@ int nios_legacy_set_fpga_correction(struct bladerf *dev,
return status;
}
int nios_legacy_get_iq_gain_correction(struct bladerf *dev,
bladerf_module module, int16_t *value)
{
int status;
uint8_t addr;
switch (module) {
case BLADERF_MODULE_RX:
addr = NIOS_PKT_LEGACY_DEV_RX_GAIN_ADDR;
break;
case BLADERF_MODULE_TX:
addr = NIOS_PKT_LEGACY_DEV_TX_GAIN_ADDR;
break;
default:
log_debug("%s: invalid module provided (%d)\n",
__FUNCTION__, module);
return BLADERF_ERR_INVAL;
}
status = get_iq_correction(dev, addr, value);
/* Gain corrections have an offset that needs to be accounted for */
if (status == 0) {
*value -= 4096;
}
return status;
}
int nios_legacy_get_iq_phase_correction(struct bladerf *dev,
bladerf_module module, int16_t *value)
{
uint8_t addr;
switch (module) {
case BLADERF_MODULE_RX:
addr = NIOS_PKT_LEGACY_DEV_RX_PHASE_ADDR;
break;
case BLADERF_MODULE_TX:
addr = NIOS_PKT_LEGACY_DEV_TX_PHASE_ADDR;
break;
default:
log_debug("%s: invalid module provided (%d)\n",
__FUNCTION__, module);
return BLADERF_ERR_INVAL;
}
return get_iq_correction(dev, addr, value);
}
int nios_legacy_set_iq_gain_correction(struct bladerf *dev,
bladerf_module module, int16_t value)
{
uint8_t addr;
switch (module) {
case BLADERF_MODULE_RX:
addr = NIOS_PKT_LEGACY_DEV_RX_GAIN_ADDR;
break;
case BLADERF_MODULE_TX:
addr = NIOS_PKT_LEGACY_DEV_TX_GAIN_ADDR;
break;
default:
log_debug("%s: invalid module provided (%d)\n",
__FUNCTION__, module);
return BLADERF_ERR_INVAL;
}
log_verbose("%s: %s, %d\n", __FUNCTION__, module2str(module), value);
/* Gain correction requires than an offset be applied */
value += (int16_t) 4096;
return set_iq_correction(dev, addr, value);
}
int nios_legacy_set_iq_phase_correction(struct bladerf *dev,
bladerf_module module, int16_t value)
{
uint8_t addr;
switch (module) {
case BLADERF_MODULE_RX:
addr = NIOS_PKT_LEGACY_DEV_RX_PHASE_ADDR;
break;
case BLADERF_MODULE_TX:
addr = NIOS_PKT_LEGACY_DEV_TX_PHASE_ADDR;
break;
default:
log_debug("%s: invalid module provided (%d)\n",
__FUNCTION__, module);
return BLADERF_ERR_INVAL;
}
log_verbose("%s: %s, %d\n", __FUNCTION__, module2str(module), value);
return set_iq_correction(dev, addr, value);
}
int nios_legacy_xb200_synth_write(struct bladerf *dev, uint32_t value)
{
log_verbose("%s: 0x%08x\n", __FUNCTION__, value);

View file

@ -163,32 +163,52 @@ int nios_legacy_lms6_write(struct bladerf *dev, uint8_t addr, uint8_t data);
int nios_legacy_vctcxo_trim_dac_write(struct bladerf *dev, uint16_t value);
/**
* Read a value from an FPGA correction block
* Read a IQ gain correction value
*
* @param[in] dev Device handle
* @param[in] corr Correction type
* @param[in] addr Address to write to target correction module
* @param[out] value On success, updated with read value
* @param[in] module Module to read from
* @param[out] value On success, updated with phase correction value
*
* @return 0 on success, BLADERF_ERR_* code on error.
*/
int nios_legacy_get_fpga_correction(struct bladerf *dev,
bladerf_correction corr,
uint8_t addr, int16_t *value);
int nios_legacy_get_iq_gain_correction(struct bladerf *dev,
bladerf_module module, int16_t *value);
/**
* Write a value to an FPGA correction block
* Read a IQ phase correction value
*
* @param[in] dev Device handle
* @param[in] corr Correction type
* @param[in] addr Address to write to target correction module
* @param[in] module Module to read from
* @param[out] value On success, updated with phase correction value
*
* @return 0 on success, BLADERF_ERR_* code on error.
*/
int nios_legacy_get_iq_phase_correction(struct bladerf *dev,
bladerf_module module, int16_t *value);
/**
* Write an IQ gain correction value
*
* @param[in] dev Device handle
* @param[in] module Module to write to
* @param[in] value Correction value to write
*
* @return 0 on success, BLADERF_ERR_* code on error.
*/
int nios_legacy_set_fpga_correction(struct bladerf *dev,
bladerf_correction corr,
uint8_t addr, int16_t value);
int nios_legacy_set_iq_gain_correction(struct bladerf *dev,
bladerf_module module, int16_t value);
/**
* Write an IQ phase correction value
*
* @param[in] dev Device handle
* @param[in] module Module to write to
* @param[in] value Correction value to write
*
* @return 0 on success, BLADERF_ERR_* code on error.
*/
int nios_legacy_set_iq_phase_correction(struct bladerf *dev,
bladerf_module module, int16_t value);
/**
* Write a value to the XB-200's ADF4351 synthesizer

View file

@ -764,179 +764,6 @@ static int usb_get_device_speed(struct bladerf *dev, bladerf_dev_speed *speed)
return usb->fn->get_speed(driver, speed);
}
static inline void get_correction_addr_type(bladerf_module module,
bladerf_correction corr,
uint8_t *addr, corr_type *type)
{
switch (corr) {
/* These items are controlled in the FPGA */
case BLADERF_CORR_FPGA_PHASE:
*type = CORR_FPGA;
*addr = module == BLADERF_MODULE_TX ?
NIOS_PKT_LEGACY_DEV_TX_PHASE_ADDR : NIOS_PKT_LEGACY_DEV_RX_PHASE_ADDR;
break;
case BLADERF_CORR_FPGA_GAIN:
*type = CORR_FPGA;
*addr = module == BLADERF_MODULE_TX ?
NIOS_PKT_LEGACY_DEV_TX_GAIN_ADDR : NIOS_PKT_LEGACY_DEV_RX_GAIN_ADDR;
break;
/* These items are controlled by the LMS6002D */
case BLADERF_CORR_LMS_DCOFF_I:
*type = CORR_LMS;
*addr = module == BLADERF_MODULE_TX ? 0x42 : 0x71;
break;
case BLADERF_CORR_LMS_DCOFF_Q:
*type = CORR_LMS;
*addr = module == BLADERF_MODULE_TX ? 0x43 : 0x72;
break;
default:
*type = CORR_INVALID;
*addr = 0xff;
}
}
static int set_lms_correction(struct bladerf *dev, bladerf_module module,
uint8_t addr, int16_t value)
{
int status;
uint8_t tmp;
status = nios_legacy_lms6_read(dev, addr, &tmp);
if (status != 0) {
return status;
}
/* Mask out any control bits in the RX DC correction area */
if (module == BLADERF_MODULE_RX) {
/* Bit 7 is unrelated to lms dc correction, save its state */
tmp = tmp & (1 << 7);
/* RX only has 6 bits of scale to work with, remove normalization */
value >>= 5;
if (value < 0) {
value = (value <= -64) ? 0x3f : (abs(value) & 0x3f);
/*This register uses bit 6 to denote a negative gain */
value |= (1 << 6);
} else {
value = (value >= 64) ? 0x3f : (value & 0x3f);
}
value |= tmp;
} else {
/* TX only has 7 bits of scale to work with, remove normalization */
value >>= 4;
/* LMS6002D 0x00 = -16, 0x80 = 0, 0xff = 15.9375 */
if (value >= 0) {
tmp = (value >= 128) ? 0x7f : (value & 0x7f);
/* Assert bit 7 for positive numbers */
value = (1 << 7) + tmp;
} else {
value = (value <= -128) ? 0x00 : (value & 0x7f);
}
}
status = nios_legacy_lms6_write(dev, addr, (uint8_t)value);
if (status < 0) {
return status;
}
return status;
}
static int usb_set_correction(struct bladerf *dev, bladerf_module module,
bladerf_correction corr, int16_t value)
{
int status;
uint8_t addr;
corr_type type;
get_correction_addr_type(module, corr, &addr, &type);
switch (type) {
case CORR_FPGA:
status = nios_legacy_set_fpga_correction(dev, corr, addr, value);
break;
case CORR_LMS:
status = set_lms_correction(dev, module, addr, value);
break;
default:
status = BLADERF_ERR_INVAL;
}
return status;
return 0;
}
static int get_lms_correction(struct bladerf *dev,
bladerf_module module,
uint8_t addr, int16_t *value)
{
uint8_t tmp;
int status;
status = nios_legacy_lms6_read(dev, addr, &tmp);
if (status == 0) {
/* Mask out any control bits in the RX DC correction area */
if (module == BLADERF_MODULE_RX) {
tmp = tmp & 0x7f;
if (tmp & (1 << 6)) {
*value = -(int16_t)(tmp & 0x3f);
} else {
*value = (int16_t)(tmp & 0x3f);
}
/* Renormalize to 2048 */
*value <<= 5;
} else {
*value = (int16_t)tmp;
/* Renormalize to 2048 */
*value <<= 4;
}
}
return status;
}
static int usb_get_correction(struct bladerf *dev, bladerf_module module,
bladerf_correction corr, int16_t *value)
{
int status;
uint8_t addr;
corr_type type;
get_correction_addr_type(module, corr, &addr, &type);
switch (type) {
case CORR_LMS:
status = get_lms_correction(dev, module, addr, value);
break;
case CORR_FPGA:
status = nios_legacy_get_fpga_correction(dev, corr, addr, value);
break;
default:
status = BLADERF_ERR_INVAL;
}
return status;
}
static int usb_set_firmware_loopback(struct bladerf *dev, bool enable) {
int result;
int status;
@ -1296,8 +1123,10 @@ const struct backend_fns backend_fns_usb = {
FIELD_INIT(.expansion_gpio_dir_write, nios_legacy_expansion_gpio_dir_write),
FIELD_INIT(.expansion_gpio_dir_read, nios_legacy_expansion_gpio_dir_read),
FIELD_INIT(.set_correction, usb_set_correction),
FIELD_INIT(.get_correction, usb_get_correction),
FIELD_INIT(.set_iq_gain_correction, nios_legacy_set_iq_gain_correction),
FIELD_INIT(.set_iq_phase_correction, nios_legacy_set_iq_phase_correction),
FIELD_INIT(.get_iq_gain_correction, nios_legacy_get_iq_gain_correction),
FIELD_INIT(.get_iq_phase_correction, nios_legacy_get_iq_phase_correction),
FIELD_INIT(.get_timestamp, nios_legacy_get_timestamp),

View file

@ -1583,7 +1583,28 @@ int bladerf_set_correction(struct bladerf *dev, bladerf_module module,
int status;
MUTEX_LOCK(&dev->ctrl_lock);
status = dev->fn->set_correction(dev, module, corr, value);
switch (corr) {
case BLADERF_CORR_FPGA_PHASE:
status = dev->fn->set_iq_phase_correction(dev, module, value);
break;
case BLADERF_CORR_FPGA_GAIN:
status = dev->fn->set_iq_gain_correction(dev, module, value);
break;
case BLADERF_CORR_LMS_DCOFF_I:
status = lms_set_dc_offset_i(dev, module, value);
break;
case BLADERF_CORR_LMS_DCOFF_Q:
status = lms_set_dc_offset_q(dev, module, value);
break;
default:
status = BLADERF_ERR_INVAL;
log_debug("Invalid correction type: %d\n", corr);
break;
}
MUTEX_UNLOCK(&dev->ctrl_lock);
return status;
@ -1595,8 +1616,28 @@ int bladerf_get_correction(struct bladerf *dev, bladerf_module module,
int status;
MUTEX_LOCK(&dev->ctrl_lock);
status = dev->fn->get_correction(dev, module, corr, value);
switch (corr) {
case BLADERF_CORR_FPGA_PHASE:
status = dev->fn->get_iq_phase_correction(dev, module, value);
break;
case BLADERF_CORR_FPGA_GAIN:
status = dev->fn->get_iq_gain_correction(dev, module, value);
break;
case BLADERF_CORR_LMS_DCOFF_I:
status = lms_get_dc_offset_i(dev, module, value);
break;
case BLADERF_CORR_LMS_DCOFF_Q:
status = lms_get_dc_offset_q(dev, module, value);
break;
default:
status = BLADERF_ERR_INVAL;
log_debug("Invalid correction type: %d\n", corr);
break;
}
MUTEX_UNLOCK(&dev->ctrl_lock);
return status;
}

View file

@ -187,14 +187,12 @@ int tuning_set_freq(struct bladerf *dev, bladerf_module module,
if (dc_cal != NULL) {
dc_cal_tbl_vals(dc_cal, frequency, &dc_i, &dc_q);
status = dev->fn->set_correction(dev, module,
BLADERF_CORR_LMS_DCOFF_I, dc_i);
status = lms_set_dc_offset_i(dev, module, dc_i);
if (status != 0) {
return status;
}
status = dev->fn->set_correction(dev, module,
BLADERF_CORR_LMS_DCOFF_Q, dc_q);
status = lms_set_dc_offset_q(dev, module, dc_q);
if (status != 0) {
return status;
}