Skip to content

Commit

Permalink
bladeRF-cli: Cleaned up correct command
Browse files Browse the repository at this point in the history
Added return value and parameter checks. This command now ensures that the FPGA
is loaded. Performed misc. cleanup.
  • Loading branch information
jynik authored and bpadalino committed Jan 16, 2014
1 parent 971c585 commit 2959205
Showing 1 changed file with 185 additions and 85 deletions.
270 changes: 185 additions & 85 deletions host/utilities/bladeRF-cli/src/cmd/correct.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,109 +29,209 @@
#define MAX_PHASE (2048)
#define MAX_GAIN (2048)

static int print_correction(struct cli_state *state, bladerf_module module)
{
int status;
int16_t dc_i, dc_q, phase,gain;
struct bladerf *dev = state->dev;

status = bladerf_get_correction(dev, module, BLADERF_IQ_CORR_DC_I, &dc_i);
if (status != 0) {
goto print_correction_out;
}

status = bladerf_get_correction(dev, module, BLADERF_IQ_CORR_DC_Q, &dc_q);
if (status != 0) {
goto print_correction_out;
}

status = bladerf_get_correction(dev, module, BLADERF_IQ_CORR_PHASE, &phase);
if (status != 0) {
goto print_correction_out;
}

status = bladerf_get_correction(dev, module, BLADERF_IQ_CORR_GAIN, &gain);

print_correction_out:

if (status != 0) {
state->last_lib_error = status;
status = CMD_RET_LIBBLADERF;
} else {
printf("\nCurrent Settings: DC Offset I=%d Q=%d, Phase=%d, Gain=%d\n\n",
dc_i, dc_q, phase, gain);
}

return status;
}

static inline int set_phase_correction(struct cli_state *state,
bladerf_module module, int16_t value)
{
int status;
struct bladerf *dev = state->dev;

status = bladerf_set_correction(dev, module, BLADERF_IQ_CORR_PHASE, value);

if (status != 0) {
state->last_lib_error = status;
status = CMD_RET_LIBBLADERF;
}

return status;
}

static inline int set_gain_correction(struct cli_state *state,
bladerf_module module, int16_t value)
{
int status;
struct bladerf *dev = state->dev;

status = bladerf_set_correction(dev, module, BLADERF_IQ_CORR_GAIN, value);

if (status != 0) {
state->last_lib_error = status;
status = CMD_RET_LIBBLADERF;
}

return status;
}

static inline int set_dc_correction(struct cli_state *state,
bladerf_module module,
int16_t val_i, int16_t val_q)
{
int status;
struct bladerf *dev = state->dev;

status = bladerf_set_correction(dev, module, BLADERF_IQ_CORR_DC_I, val_i);
if (status == 0) {
status = bladerf_set_correction(dev, module, BLADERF_IQ_CORR_DC_Q, val_q);
}

if (status != 0) {
state->last_lib_error = status;
status = CMD_RET_LIBBLADERF;
}

return status;
}

/* Usage:
*
* Print the current correction state
* corect <rx|tx>
*
* Set phase or gain:
* correct <rx|tx> <phase|gain> <value>
*
* Set DC offset:
* correct <rx|tx> <dc> <i_off> <q_off>
*/
int cmd_correct(struct cli_state *state, int argc, char **argv)
{
int rv = CMD_RET_OK;
int status=0;
bool ok = true;
bool isTx = false;
int fpga_status;
bool ok;
bladerf_module module;

if (!cli_device_is_opened(state))
{
if (!cli_device_is_opened(state)) {
return CMD_RET_NODEV;
}


//get the direction to print
if (argc >= 2)
{
if (!strcasecmp(argv[1],"tx"))
isTx = true;
else if(!strcasecmp(argv[1],"rx"))
isTx = false;
else
return CMD_RET_INVPARAM;
/* The FPGA needs to be loaded */
fpga_status = bladerf_is_fpga_configured(state->dev);
if (fpga_status < 0) {
state->last_lib_error = fpga_status;
return CMD_RET_LIBBLADERF;
} else if (fpga_status != 1) {
return CMD_RET_NOFPGA;
}

if( argc == 2 )
{
int16_t dc_i, dc_q, phase,gain;
//argv[1]
if(isTx)
{
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_TX_DC_I, &dc_i);
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_TX_DC_Q, &dc_q);
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_TX_PHASE, &phase);
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_TX_GAIN, &gain);
}
else
{
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_RX_DC_I, &dc_i);
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_RX_DC_Q, &dc_q);
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_RX_PHASE, &phase);
status = bladerf_print_correction(state->dev,BLADERF_IQ_CORR_RX_GAIN, &gain);
/* Get the direction to print */
if (argc >= 2) {
if (!strcasecmp(argv[1], "tx")) {
module = BLADERF_MODULE_TX;
} else if(!strcasecmp(argv[1], "rx")) {
module = BLADERF_MODULE_RX;
} else {
cli_err(state, argv[0],
"Invalid module provided. Valid options are: 'rx' or 'tx'");
return CMD_RET_INVPARAM;
}
printf("Current Settings: DC OFFSET I=%d Q=%d, Phase=%d, Gain=%d\n",dc_i,dc_q,phase,gain);
} else {
return CMD_RET_NARGS;
}
/* Parse the value */
else if( argc == 4 )
{

if (argc == 2) {
rv = print_correction(state, module);
} else if (argc == 4) {
int16_t value = 0;

if (!strcasecmp(argv[2],"phase"))
{
value = str2int( argv[3], -MAX_PHASE ,MAX_PHASE, &ok );
if(isTx)
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_TX_PHASE, value);
else
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_RX_PHASE, value);

}
else if (!strcasecmp(argv[2],"gain"))
{
value = str2int( argv[3], -MAX_GAIN ,MAX_GAIN, &ok );
if(isTx)
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_TX_GAIN, value);
else
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_RX_GAIN, value);
}
else
{
if (!strcasecmp(argv[2], "phase")) {

value = str2int(argv[3], -MAX_PHASE, MAX_PHASE, &ok);
if (!ok) {
cli_err(state, argv[0], "Phase value must be in [%d, %d]",
-MAX_PHASE, MAX_PHASE);
rv = CMD_RET_INVPARAM;
} else {
rv = set_phase_correction(state, module, value);
}

} else if (!strcasecmp(argv[2], "gain")){

value = str2int( argv[3], -MAX_GAIN, MAX_GAIN, &ok);
if (!ok) {
cli_err(state, argv[0], "Gain value must be in [%d, %d]",
-MAX_GAIN, MAX_GAIN);
rv = CMD_RET_INVPARAM;
} else {
rv = set_gain_correction(state, module, value);
}

} else {
cli_err(state, argv[0],
"Invalid correction module for %d arguments: %s",
argc - 2, argv[2]);
rv = CMD_RET_INVPARAM;
}
}
else if (argc == 5) //dc correction requires 2 parameters
{

if (!strcasecmp(argv[2],"dc"))
{
int16_t val_i,val_q;
int16_t max = isTx ? MAX_TX_DC_OFFSET : MAX_RX_DC_OFFSET;
int16_t min = isTx ? -(MAX_TX_DC_OFFSET +1) : -MAX_RX_DC_OFFSET;

val_i = str2int( argv[3], min, max, &ok );
val_q = str2int( argv[4], min, max, &ok );
if(isTx)
{
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_TX_DC_I, val_i);
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_TX_DC_Q, val_q);
}
else
{
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_RX_DC_I, val_i);
status = bladerf_set_correction(state->dev,BLADERF_IQ_CORR_RX_DC_Q, val_q);
}
} else if (argc == 5) {

}
if (status < 0) {
cli_err(state, argv[0], "Error setting correction");
}
if (!strcasecmp(argv[2], "dc")) {
int16_t val_i, val_q;
const int16_t max = module == BLADERF_MODULE_TX ?
MAX_TX_DC_OFFSET : MAX_RX_DC_OFFSET;

}
else {
cli_err(state, argv[0], "Invalid number of arguments (%d)\n", argc);
rv = CMD_RET_INVPARAM;
const int16_t min = module == BLADERF_MODULE_TX ?
-(MAX_TX_DC_OFFSET + 1) : -MAX_RX_DC_OFFSET;

val_i = str2int(argv[3], min, max, &ok);
if (!ok) {
cli_err(state, argv[0], "I offset must be in [%d, %d]", min, max);
rv = CMD_RET_INVPARAM;
return rv;
}

val_q = str2int(argv[4], min, max, &ok);
if (!ok) {
cli_err(state, argv[0], "Q offset must be in [%d, %d]", min, max);
rv = CMD_RET_INVPARAM;
return rv;
}

rv = set_dc_correction(state, module, val_i, val_q);

} else {
cli_err(state, argv[0],
"Invalid correction module for %d arguments: %s",
argc - 2, argv[2]);
rv = CMD_RET_INVPARAM;
}
} else {
rv = CMD_RET_NARGS;
}

return rv;
}

0 comments on commit 2959205

Please sign in to comment.