/* See libbladeRF's dc_cal_table.c for the packed table data format */ int calibrate_dc_gen_tbl(struct cli_state *s, bladerf_module module, const char *filename, unsigned int f_low, unsigned f_inc, unsigned int f_high) { int retval, status; size_t off; struct bladerf_lms_dc_cals lms_dc_cals; unsigned int f; struct settings settings; bladerf_loopback loopback_backup; struct bladerf_image *image = NULL; const uint16_t magic = HOST_TO_LE16(0x1ab1); const uint32_t reserved = HOST_TO_LE32(0x00000000); const uint32_t tbl_version = HOST_TO_LE32(0x00000001); const size_t lms_data_size = 10; /* 10 uint8_t register values */ const uint32_t n_frequencies = (f_high - f_low) / f_inc + 1; const uint32_t n_frequencies_le = HOST_TO_LE32(n_frequencies); const size_t entry_size = sizeof(uint32_t) + /* Frequency */ 2 * sizeof(int16_t); /* DC I and Q valus */ const size_t table_size = n_frequencies * entry_size; const size_t data_size = sizeof(magic) + sizeof(reserved) + sizeof(tbl_version) + sizeof(n_frequencies_le) + lms_data_size + table_size; assert(data_size <= UINT_MAX); status = backup_and_update_settings(s->dev, module, &settings); if (status != 0) { return status; } status = bladerf_get_loopback(s->dev, &loopback_backup); if (status != 0) { return status; } status = bladerf_lms_get_dc_cals(s->dev, &lms_dc_cals); if (status != 0) { goto out; } if (module == BLADERF_MODULE_RX) { image = bladerf_alloc_image(BLADERF_IMAGE_TYPE_RX_DC_CAL, 0xffffffff, (unsigned int) data_size); } else { image = bladerf_alloc_image(BLADERF_IMAGE_TYPE_TX_DC_CAL, 0xffffffff, (unsigned int) data_size); } if (image == NULL) { status = BLADERF_ERR_MEM; goto out; } status = bladerf_get_serial(s->dev, image->serial); if (status != 0) { goto out; } if (module == BLADERF_MODULE_RX) { status = bladerf_set_loopback(s->dev, BLADERF_LB_NONE); if (status != 0) { goto out; } } off = 0; memcpy(&image->data[off], &magic, sizeof(magic)); off += sizeof(magic); memcpy(&image->data[off], &reserved, sizeof(reserved)); off += sizeof(reserved); memcpy(&image->data[off], &tbl_version, sizeof(tbl_version)); off += sizeof(tbl_version); memcpy(&image->data[off], &n_frequencies_le, sizeof(n_frequencies_le)); off += sizeof(n_frequencies_le); image->data[off++] = (uint8_t)lms_dc_cals.lpf_tuning; image->data[off++] = (uint8_t)lms_dc_cals.tx_lpf_i; image->data[off++] = (uint8_t)lms_dc_cals.tx_lpf_q; image->data[off++] = (uint8_t)lms_dc_cals.rx_lpf_i; image->data[off++] = (uint8_t)lms_dc_cals.rx_lpf_q; image->data[off++] = (uint8_t)lms_dc_cals.dc_ref; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2a_i; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2a_q; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2b_i; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2b_q; putchar('\n'); for (f = f_low; f <= f_high; f += f_inc) { const uint32_t frequency = HOST_TO_LE32((uint32_t)f); int16_t dc_i, dc_q; printf(" Calibrating @ %u Hz...", f); status = bladerf_set_frequency(s->dev, module, f); if (status != 0) { goto out; } if (module == BLADERF_MODULE_RX) { int16_t error_i, error_q; status = calibrate_dc_rx(s, &dc_i, &dc_q, &error_i, &error_q); printf(" I=%-4d (avg: %-4d), Q=%-4d (avg: %-4d)\r", dc_i, error_i, dc_q, error_q); } else { float error_i, error_q; status = calibrate_dc_tx(s, &dc_i, &dc_q, &error_i, &error_q); printf(" I=%-4d (avg: %3.3f), Q=%-4d (avg: %3.3f)\r", dc_i, error_i, dc_q, error_q); } if (status != 0) { goto out; } fflush(stdout); dc_i = HOST_TO_LE16(dc_i); dc_q = HOST_TO_LE16(dc_q); memcpy(&image->data[off], &frequency, sizeof(frequency)); off += sizeof(frequency); memcpy(&image->data[off], &dc_i, sizeof(dc_i)); off += sizeof(dc_i); memcpy(&image->data[off], &dc_q, sizeof(dc_q)); off += sizeof(dc_q); } status = bladerf_image_write(image, filename); printf("\n Done.\n\n"); out: retval = status; if (module == BLADERF_MODULE_RX) { status = bladerf_set_loopback(s->dev, loopback_backup); retval = first_error(retval, status); } status = bladerf_enable_module(s->dev, BLADERF_MODULE_RX, false); retval = first_error(retval, status); status = restore_settings(s->dev, module, &settings); retval = first_error(retval, status); bladerf_free_image(image); return retval; }
int calibrate_dc(struct cli_state *s, unsigned int ops) { int retval = 0; int status = BLADERF_ERR_UNEXPECTED; struct settings rx_settings, tx_settings; bladerf_loopback loopback; int16_t dc_i, dc_q; if (IS_RX_CAL(ops)) { status = backup_and_update_settings(s->dev, BLADERF_MODULE_RX, &rx_settings); if (status != 0) { s->last_lib_error = status; return CLI_RET_LIBBLADERF; } } if (IS_TX_CAL(ops)) { status = backup_and_update_settings(s->dev, BLADERF_MODULE_TX, &tx_settings); if (status != 0) { s->last_lib_error = status; return CLI_RET_LIBBLADERF; } } status = bladerf_get_loopback(s->dev, &loopback); if (status != 0) { s->last_lib_error = status; return CLI_RET_LIBBLADERF; } if (IS_RX_CAL(ops)) { status = set_rx_dc(s->dev, 0, 0); if (status != 0) { goto error; } status = bladerf_enable_module(s->dev, BLADERF_MODULE_RX, true); if (status != 0) { goto error; } } if (IS_TX_CAL(ops)) { status = bladerf_enable_module(s->dev, BLADERF_MODULE_RX, true); if (status != 0) { goto error; } status = bladerf_set_loopback(s->dev, BLADERF_LB_BB_TXVGA1_RXVGA2); if (status != 0) { goto error; } status = bladerf_enable_module(s->dev, BLADERF_MODULE_TX, true); if (status != 0) { goto error; } status = dummy_tx(s->dev); if (status != 0) { goto error; } status = bladerf_enable_module(s->dev, BLADERF_MODULE_TX, false); if (status != 0) { goto error; } } status = bladerf_set_loopback(s->dev, BLADERF_LB_NONE); if (status != 0) { goto error; } putchar('\n'); if (IS_CAL(CAL_DC_LMS_TUNING, ops)) { printf(" Calibrating LMS LPF tuning module...\n"); status = bladerf_calibrate_dc(s->dev, BLADERF_DC_CAL_LPF_TUNING); if (status != 0) { goto error; } else { struct bladerf_lms_dc_cals dc_cals; status = bladerf_lms_get_dc_cals(s->dev, &dc_cals); if (status != 0) { goto error; } printf(" LPF tuning module: %d\n\n", dc_cals.lpf_tuning); } } if (IS_CAL(CAL_DC_LMS_TXLPF, ops)) { printf(" Calibrating LMS TX LPF modules...\n"); status = bladerf_calibrate_dc(s->dev, BLADERF_DC_CAL_TX_LPF); if (status != 0) { goto error; } else { struct bladerf_lms_dc_cals dc_cals; status = bladerf_lms_get_dc_cals(s->dev, &dc_cals); if (status != 0) { goto error; } printf(" TX LPF I filter: %d\n", dc_cals.tx_lpf_i); printf(" TX LPF Q filter: %d\n\n", dc_cals.tx_lpf_q); } } if (IS_CAL(CAL_DC_LMS_RXLPF, ops)) { printf(" Calibrating LMS RX LPF modules...\n"); status = bladerf_calibrate_dc(s->dev, BLADERF_DC_CAL_RX_LPF); if (status != 0) { goto error; } else { struct bladerf_lms_dc_cals dc_cals; status = bladerf_lms_get_dc_cals(s->dev, &dc_cals); if (status != 0) { goto error; } printf(" RX LPF I filter: %d\n", dc_cals.rx_lpf_i); printf(" RX LPF Q filter: %d\n\n", dc_cals.rx_lpf_q); } } if (IS_CAL(CAL_DC_LMS_RXVGA2, ops)) { printf(" Calibrating LMS RXVGA2 modules...\n"); status = bladerf_calibrate_dc(s->dev, BLADERF_DC_CAL_RXVGA2); if (status != 0) { goto error; } else { struct bladerf_lms_dc_cals dc_cals; status = bladerf_lms_get_dc_cals(s->dev, &dc_cals); if (status != 0) { goto error; } printf(" RX VGA2 DC reference module: %d\n", dc_cals.dc_ref); printf(" RX VGA2 stage 1, I channel: %d\n", dc_cals.rxvga2a_i); printf(" RX VGA2 stage 1, Q channel: %d\n", dc_cals.rxvga2a_q); printf(" RX VGA2 stage 2, I channel: %d\n", dc_cals.rxvga2b_i); printf(" RX VGA2 stage 2, Q channel: %d\n\n", dc_cals.rxvga2b_q); } } if (IS_CAL(CAL_DC_AUTO_RX, ops)) { int16_t avg_i, avg_q; status = calibrate_dc_rx(s, &dc_i, &dc_q, &avg_i, &avg_q); if (status != 0) { goto error; } else { printf(" RX DC I Setting = %d, error ~= %d\n", dc_i, avg_i); printf(" RX DC Q Setting = %d, error ~= %d\n\n", dc_q, avg_q); } } if (IS_CAL(CAL_DC_AUTO_TX, ops)) { float error_i, error_q; status = calibrate_dc_tx(s, &dc_i, &dc_q, &error_i, &error_q); if (status != 0) { goto error; } else { printf(" TX DC I Setting = %d, error ~= %f\n", dc_i, error_i); printf(" TX DC Q Setting = %d, error ~= %f\n\n", dc_q, error_q); } } error: retval = status; if (IS_RX_CAL(ops)) { status = restore_settings(s->dev, BLADERF_MODULE_RX, &rx_settings); retval = first_error(retval, status); } if (IS_TX_CAL(ops)) { status = restore_settings(s->dev, BLADERF_MODULE_TX, &tx_settings); retval = first_error(retval, status); } status = bladerf_enable_module(s->dev, BLADERF_MODULE_RX, false); retval = first_error(retval, status); status = bladerf_enable_module(s->dev, BLADERF_MODULE_TX, false); retval = first_error(retval, status); status = bladerf_set_loopback(s->dev, loopback); retval = first_error(retval, status); if (retval != 0) { s->last_lib_error = retval; retval = CLI_RET_LIBBLADERF; } return retval; }
/* See libbladeRF's dc_cal_table.c for the packed table data format */ int calibrate_dc_gen_tbl(struct cli_state *s, bladerf_module module, const char *filename, unsigned int f_low, unsigned f_inc, unsigned int f_high) { int retval, status; size_t off; struct bladerf_lms_dc_cals lms_dc_cals; unsigned int f; struct settings settings; bladerf_loopback loopback_backup; struct bladerf_image *image = NULL; FILE *write_check; const uint16_t magic = HOST_TO_LE16(0x1ab1); const uint32_t reserved = HOST_TO_LE32(0x00000000); const uint32_t tbl_version = HOST_TO_LE32(0x00000001); const size_t lms_data_size = 10; /* 10 uint8_t register values */ const uint32_t n_frequencies = (f_high - f_low) / f_inc + 1; const uint32_t n_frequencies_le = HOST_TO_LE32(n_frequencies); const size_t entry_size = sizeof(uint32_t) + /* Frequency */ 2 * sizeof(int16_t); /* DC I and Q valus */ const size_t table_size = n_frequencies * entry_size; const size_t data_size = sizeof(magic) + sizeof(reserved) + sizeof(tbl_version) + sizeof(n_frequencies_le) + lms_data_size + table_size; assert(data_size <= UINT_MAX); /* This operation may take a bit of time, so let's make sure we * actually have write access before kicking things off. Note that * access is checked later when the file is actually written. */ write_check = fopen(filename, "wb"); if (write_check == NULL) { if (errno == EACCES) { return BLADERF_ERR_PERMISSION; } else { return BLADERF_ERR_IO; } } else { fclose(write_check); /* Not much we care to do if this fails. Throw away the return value * to make this explicit to our static analysis tools */ (void) remove(filename); } status = backup_and_update_settings(s->dev, module, &settings); if (status != 0) { return status; } status = bladerf_get_loopback(s->dev, &loopback_backup); if (status != 0) { return status; } status = bladerf_lms_get_dc_cals(s->dev, &lms_dc_cals); if (status != 0) { goto out; } if (module == BLADERF_MODULE_RX) { image = bladerf_alloc_image(BLADERF_IMAGE_TYPE_RX_DC_CAL, 0xffffffff, (unsigned int) data_size); } else { image = bladerf_alloc_image(BLADERF_IMAGE_TYPE_TX_DC_CAL, 0xffffffff, (unsigned int) data_size); } if (image == NULL) { status = BLADERF_ERR_MEM; goto out; } status = bladerf_get_serial(s->dev, image->serial); if (status != 0) { goto out; } if (module == BLADERF_MODULE_RX) { status = bladerf_set_loopback(s->dev, BLADERF_LB_NONE); if (status != 0) { goto out; } } off = 0; memcpy(&image->data[off], &magic, sizeof(magic)); off += sizeof(magic); memcpy(&image->data[off], &reserved, sizeof(reserved)); off += sizeof(reserved); memcpy(&image->data[off], &tbl_version, sizeof(tbl_version)); off += sizeof(tbl_version); memcpy(&image->data[off], &n_frequencies_le, sizeof(n_frequencies_le)); off += sizeof(n_frequencies_le); image->data[off++] = (uint8_t)lms_dc_cals.lpf_tuning; image->data[off++] = (uint8_t)lms_dc_cals.tx_lpf_i; image->data[off++] = (uint8_t)lms_dc_cals.tx_lpf_q; image->data[off++] = (uint8_t)lms_dc_cals.rx_lpf_i; image->data[off++] = (uint8_t)lms_dc_cals.rx_lpf_q; image->data[off++] = (uint8_t)lms_dc_cals.dc_ref; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2a_i; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2a_q; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2b_i; image->data[off++] = (uint8_t)lms_dc_cals.rxvga2b_q; putchar('\n'); for (f = f_low; f <= f_high; f += f_inc) { const uint32_t frequency = HOST_TO_LE32((uint32_t)f); int16_t dc_i, dc_q; printf(" Calibrating @ %u Hz...", f); status = bladerf_set_frequency(s->dev, module, f); if (status != 0) { goto out; } if (module == BLADERF_MODULE_RX) { int16_t error_i, error_q; status = calibrate_dc_rx(s, &dc_i, &dc_q, &error_i, &error_q); printf(" I=%-4d (avg: %-4d), Q=%-4d (avg: %-4d)\r", dc_i, error_i, dc_q, error_q); } else { float error_i, error_q; status = calibrate_dc_tx(s, &dc_i, &dc_q, &error_i, &error_q); printf(" I=%-4d (avg: %3.3f), Q=%-4d (avg: %3.3f)\r", dc_i, error_i, dc_q, error_q); } if (status != 0) { goto out; } fflush(stdout); dc_i = HOST_TO_LE16(dc_i); dc_q = HOST_TO_LE16(dc_q); memcpy(&image->data[off], &frequency, sizeof(frequency)); off += sizeof(frequency); memcpy(&image->data[off], &dc_i, sizeof(dc_i)); off += sizeof(dc_i); memcpy(&image->data[off], &dc_q, sizeof(dc_q)); off += sizeof(dc_q); } status = bladerf_image_write(image, filename); printf("\n Done.\n\n"); out: retval = status; if (module == BLADERF_MODULE_RX) { status = bladerf_set_loopback(s->dev, loopback_backup); retval = first_error(retval, status); } status = bladerf_enable_module(s->dev, BLADERF_MODULE_RX, false); retval = first_error(retval, status); status = restore_settings(s->dev, module, &settings); retval = first_error(retval, status); bladerf_free_image(image); return retval; }