diff --git a/host/libraries/libbladeRF/include/libbladeRF.h b/host/libraries/libbladeRF/include/libbladeRF.h index bf45a138..f8a3bb15 100644 --- a/host/libraries/libbladeRF/include/libbladeRF.h +++ b/host/libraries/libbladeRF/include/libbladeRF.h @@ -2227,6 +2227,26 @@ int CALL_CONV bladerf_si5338_set_tx_freq(struct bladerf *dev, unsigned freq); API_EXPORT int CALL_CONV bladerf_si5338_set_rx_freq(struct bladerf *dev, unsigned freq); +/** + * This structure is used to directly apply DC calibration register values to + * the LMS, rather than use the values resulting from an auto-calibration. + * + * A value < 0 is used to denote that the specified value should not + * be written. If a value is to be written, it will be truncated to 8-bits. + */ +struct bladerf_lms_dc_cals +{ + int16_t lpf_tuning; /**< LPF tuning module */ + int16_t tx_lpf_i; /**< TX LPF I filter */ + int16_t tx_lpf_q; /**< TX LPF Q filter */ + int16_t rx_lpf_i; /**< RX LPF I filter */ + int16_t rx_lpf_q; /**< RX LPF Q filter */ + int16_t dc_ref; /**< RX VGA2 DC reference module */ + int16_t rxvga2a_i; /**< RX VGA2, I channel of first gain stage */ + int16_t rxvga2a_q; /**< RX VGA2, Q channel of first gain stage */ + int16_t rxvga2b_i; /**< RX VGA2, I channel of second gain stage */ + int16_t rxvga2b_q; /**< RX VGA2, Q channel of second gain stage */ +}; /** * Read a LMS register @@ -2254,6 +2274,34 @@ API_EXPORT int CALL_CONV bladerf_lms_write(struct bladerf *dev, uint8_t address, uint8_t val); +/** + * Manually load values into LMS6002 DC calibration registers. + * + * This is generally intended for applying a set of known values resulting from + * a previous run of the LMS autocalibrations. + * + * @param dev Device handle + * @param dc_cals Calibration values to load. Values set to <0 will + * not be applied. + * + * @return 0 on success, value from \ref RETCODES list on failure + */ +API_EXPORT +int CALL_CONV bladerf_lms_set_dc_cals(struct bladerf *dev, + const struct bladerf_lms_dc_cals *dc_cals); + +/** + * Retrieve the current DC calibration values from the LMS6002 + * + * @param[in] dev Device handle + * @param[out] dc_cals Populated with current values + * + * @return 0 on success, value from \ref RETCODES list on failure + */ +API_EXPORT +int CALL_CONV bladerf_lms_get_dc_cals(struct bladerf *dev, + struct bladerf_lms_dc_cals *dc_cals); + /** * Enable LMS receive * diff --git a/host/libraries/libbladeRF/src/bladerf.c b/host/libraries/libbladeRF/src/bladerf.c index b87863c7..1629f0a5 100644 --- a/host/libraries/libbladeRF/src/bladerf.c +++ b/host/libraries/libbladeRF/src/bladerf.c @@ -925,7 +925,7 @@ int bladerf_si5338_write(struct bladerf *dev, uint8_t address, uint8_t val) } /*------------------------------------------------------------------------------ - * LMS register read / write functions + * LMS register access and low-level functions *----------------------------------------------------------------------------*/ int bladerf_lms_read(struct bladerf *dev, uint8_t address, uint8_t *val) @@ -938,6 +938,18 @@ int bladerf_lms_write(struct bladerf *dev, uint8_t address, uint8_t val) return dev->fn->lms_write(dev,address,val); } +int bladerf_lms_set_dc_cals(struct bladerf *dev, + const struct bladerf_lms_dc_cals *dc_cals) +{ + return lms_set_dc_cals(dev, dc_cals); +} + +int bladerf_lms_get_dc_cals(struct bladerf *dev, + struct bladerf_lms_dc_cals *dc_cals) +{ + return lms_get_dc_cals(dev, dc_cals); +} + /*------------------------------------------------------------------------------ * GPIO register read / write functions *----------------------------------------------------------------------------*/ diff --git a/host/libraries/libbladeRF/src/lms.c b/host/libraries/libbladeRF/src/lms.c index 159104b8..46663cfa 100644 --- a/host/libraries/libbladeRF/src/lms.c +++ b/host/libraries/libbladeRF/src/lms.c @@ -205,6 +205,38 @@ static const uint8_t lms_reg_dumpset[] = { #define LOOPBBEN_ENVPK (3 << 2) #define LOOBBBEN_MASK (3 << 2) +/* TODO migrate some RMW calls to use these set/clr functions */ +static inline int lms_set(struct bladerf *dev, uint8_t addr, uint8_t mask) +{ + int status; + uint8_t regval; + + status = bladerf_lms_read(dev, addr, ®val); + if (status != 0) { + return status; + } + + regval |= mask; + + return bladerf_lms_write(dev, addr, regval); +} + +static inline int lms_clr(struct bladerf *dev, uint8_t addr, uint8_t mask) +{ + int status; + uint8_t regval; + + status = bladerf_lms_read(dev, addr, ®val); + if (status != 0) { + return status; + } + + regval &= ~mask; + + return bladerf_lms_write(dev, addr, regval); +} + + static inline int is_loopback_enabled(struct bladerf *dev) { bladerf_loopback loopback; @@ -2184,6 +2216,351 @@ int lms_calibrate_dc(struct bladerf *dev, bladerf_cal_module module) return status; } +static inline int enable_lpf_cal_clock(struct bladerf *dev, bool enable) +{ + const uint8_t mask = (1 << 5); + + if (enable) { + return lms_set(dev, 0x09, mask); + } else { + return lms_clr(dev, 0x09, mask); + } +} + +static inline int enable_rxvga2_dccal_clock(struct bladerf *dev, bool enable) +{ + const uint8_t mask = (1 << 4); + + if (enable) { + return lms_set(dev, 0x09, mask); + } else { + return lms_clr(dev, 0x09, mask); + } +} + +static inline int enable_rxlpf_dccal_clock(struct bladerf *dev, bool enable) +{ + const uint8_t mask = (1 << 3); + + if (enable) { + return lms_set(dev, 0x09, mask); + } else { + return lms_clr(dev, 0x09, mask); + } +} + +static inline int enable_txlpf_dccal_clock(struct bladerf *dev, bool enable) +{ + const uint8_t mask = (1 << 1); + + if (enable) { + return lms_set(dev, 0x09, mask); + } else { + return lms_clr(dev, 0x09, mask); + } +} + +static int set_dc_cal_value(struct bladerf *dev, uint8_t base, + uint8_t dc_addr, int16_t value) +{ + int status; + const uint8_t new_value = (uint8_t)value; + uint8_t regval = (0x08 | dc_addr); + + /* Keep reset inactive, cal disable, load addr */ + status = bladerf_lms_write(dev, base + 3, regval); + if (status != 0) { + return status; + } + + /* Update DC_CNTVAL */ + status = bladerf_lms_write(dev, base + 2, new_value); + if (status != 0) { + return status; + } + + /* Strobe DC_LOAD */ + regval |= (1 << 4); + status = bladerf_lms_write(dev, base + 3, regval); + if (status != 0) { + return status; + } + + regval &= ~(1 << 4); + status = bladerf_lms_write(dev, base + 3, regval); + if (status != 0) { + return status; + } + + status = bladerf_lms_read(dev, base, ®val); + if (status != 0) { + return status; + } + + return 0; +} + +static int get_dc_cal_value(struct bladerf *dev, uint8_t base, + uint8_t dc_addr, int16_t *value) +{ + int status; + uint8_t regval; + + /* Keep reset inactive, cal disable, load addr */ + status = bladerf_lms_write(dev, base + 3, (0x08 | dc_addr)); + if (status != 0) { + return status; + } + + /* Fetch value from DC_REGVAL */ + status = bladerf_lms_read(dev, base, ®val); + if (status != 0) { + *value = -1; + return status; + } + + *value = regval; + return 0; +} + +int lms_set_dc_cals(struct bladerf *dev, + const struct bladerf_lms_dc_cals *dc_cals) +{ + int status; + const bool cal_tx_lpf = + (dc_cals->tx_lpf_i >= 0) || (dc_cals->tx_lpf_q >= 0); + + const bool cal_rx_lpf = + (dc_cals->rx_lpf_i >= 0) || (dc_cals->rx_lpf_q >= 0); + + const bool cal_rxvga2 = + (dc_cals->dc_ref >= 0) || + (dc_cals->rxvga2a_i >= 0) || (dc_cals->rxvga2a_q >= 0) || + (dc_cals->rxvga2b_i >= 0) || (dc_cals->rxvga2b_q >= 0); + + if (dc_cals->lpf_tuning >= 0) { + status = enable_lpf_cal_clock(dev, true); + if (status != 0) { + return status; + } + + status = set_dc_cal_value(dev, 0x00, 0, dc_cals->lpf_tuning); + if (status != 0) { + return status; + } + + status = enable_lpf_cal_clock(dev, false); + if (status != 0) { + return status; + } + } + + if (cal_tx_lpf) { + status = enable_txlpf_dccal_clock(dev, true); + if (status != 0) { + return status; + } + + if (dc_cals->tx_lpf_i >= 0) { + status = set_dc_cal_value(dev, 0x30, 0, dc_cals->tx_lpf_i); + if (status != 0) { + return status; + } + } + + if (dc_cals->tx_lpf_q >= 0) { + status = set_dc_cal_value(dev, 0x30, 1, dc_cals->tx_lpf_q); + if (status != 0) { + return status; + } + } + + status = enable_txlpf_dccal_clock(dev, false); + if (status != 0) { + return status; + } + } + + if (cal_rx_lpf) { + status = enable_rxlpf_dccal_clock(dev, true); + if (status != 0) { + return status; + } + + if (dc_cals->rx_lpf_i >= 0) { + status = set_dc_cal_value(dev, 0x50, 0, dc_cals->rx_lpf_i); + if (status != 0) { + return status; + } + } + + if (dc_cals->rx_lpf_q >= 0) { + status = set_dc_cal_value(dev, 0x50, 1, dc_cals->rx_lpf_q); + if (status != 0) { + return status; + } + } + + status = enable_rxlpf_dccal_clock(dev, false); + if (status != 0) { + return status; + } + } + + if (cal_rxvga2) { + status = enable_rxvga2_dccal_clock(dev, true); + if (status != 0) { + return status; + } + + if (dc_cals->dc_ref >= 0) { + status = set_dc_cal_value(dev, 0x60, 0, dc_cals->dc_ref); + if (status != 0) { + return status; + } + } + + if (dc_cals->rxvga2a_i >= 0) { + status = set_dc_cal_value(dev, 0x60, 1, dc_cals->rxvga2a_i); + if (status != 0) { + return status; + } + } + + if (dc_cals->rxvga2a_q >= 0) { + status = set_dc_cal_value(dev, 0x60, 2, dc_cals->rxvga2a_q); + if (status != 0) { + return status; + } + } + + if (dc_cals->rxvga2b_i >= 0) { + status = set_dc_cal_value(dev, 0x60, 3, dc_cals->rxvga2b_i); + if (status != 0) { + return status; + } + } + + if (dc_cals->rxvga2b_q >= 0) { + status = set_dc_cal_value(dev, 0x60, 4, dc_cals->rxvga2b_q); + if (status != 0) { + return status; + } + } + + status = enable_rxvga2_dccal_clock(dev, false); + if (status != 0) { + return status; + } + } + + return 0; +} + +int lms_get_dc_cals(struct bladerf *dev, struct bladerf_lms_dc_cals *dc_cals) +{ + int status; + + + status = enable_lpf_cal_clock(dev, true); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x00, 0, &dc_cals->lpf_tuning); + if (status != 0) { + return status; + } + + status = enable_lpf_cal_clock(dev, false); + if (status != 0) { + return status; + } + + + + status = enable_txlpf_dccal_clock(dev, true); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x30, 0, &dc_cals->tx_lpf_i); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x30, 1, &dc_cals->tx_lpf_q); + if (status != 0) { + return status; + } + + status = enable_txlpf_dccal_clock(dev, false); + if (status != 0) { + return status; + } + + + + status = enable_rxlpf_dccal_clock(dev, true); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x50, 0, &dc_cals->rx_lpf_i); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x50, 1, &dc_cals->rx_lpf_q); + if (status != 0) { + return status; + } + + status = enable_rxlpf_dccal_clock(dev, false); + if (status != 0) { + return status; + } + + + + status = enable_rxvga2_dccal_clock(dev, true); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x60, 0, &dc_cals->dc_ref); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x60, 1, &dc_cals->rxvga2a_i); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x60, 2, &dc_cals->rxvga2a_q); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x60, 3, &dc_cals->rxvga2b_i); + if (status != 0) { + return status; + } + + status = get_dc_cal_value(dev, 0x60, 4, &dc_cals->rxvga2b_q); + if (status != 0) { + return status; + } + + status = enable_rxvga2_dccal_clock(dev, false); + if (status != 0) { + return status; + } + + return 0; +} + int lms_select_band(struct bladerf *dev, bladerf_module module, unsigned int freq) { diff --git a/host/libraries/libbladeRF/src/lms.h b/host/libraries/libbladeRF/src/lms.h index c26eecbb..52997fd4 100644 --- a/host/libraries/libbladeRF/src/lms.h +++ b/host/libraries/libbladeRF/src/lms.h @@ -531,6 +531,24 @@ int lms_dump_registers(struct bladerf *dev); */ int lms_calibrate_dc(struct bladerf *dev, bladerf_cal_module module); +/** + * Load DC calibration values directly via device registers instead of + * running autocalibration routines. + * + * @param[in] dev Device handle + * @param[in] dc_cals Calibration values to load + */ +int lms_set_dc_cals(struct bladerf *dev, + const struct bladerf_lms_dc_cals *dc_cals); + +/** + * Retrieve the DC calibration values currently in use + * + * @param[in] dev Device handle + * @param[out] dc_cals Calibration values to load + */ +int lms_get_dc_cals(struct bladerf *dev, struct bladerf_lms_dc_cals *dc_cals); + /** * Initialize and configure the LMS6002D given the transceiver * configuration passed in.