Commit 507f927e authored by Tristan Gingold's avatar Tristan Gingold

libwr2rf: never cross 360 when updating iq dac phase

parent 21c010ec
......@@ -223,9 +223,9 @@ int libwr2rf_dac_set_phase_amp(struct libwr2rf_dev *dev, unsigned ch,
float amplitude, float phase, unsigned verbose)
{
if (!(amplitude >= 0.0 && amplitude <= 1.0))
return -1;
if (!(phase >= 0.0 && phase <= 360.0))
return -1;
return LIBWR2RF_ERROR_BAD_PARAM;
if (!(phase >= -360.0 && phase <= 360.0))
return LIBWR2RF_ERROR_BAD_PARAM;
float phase_rad = phase * M_PI / 180.0;
float igainf = amplitude * cosf (phase_rad);
......@@ -235,7 +235,7 @@ int libwr2rf_dac_set_phase_amp(struct libwr2rf_dev *dev, unsigned ch,
int qgain = dac_convert(qgainf);
if (!dac_check_range(igain) || !dac_check_range(qgain))
return -1;
return LIBWR2RF_ERROR_BAD_PARAM;
if (verbose)
fprintf(stderr,
......@@ -284,20 +284,13 @@ int libwr2rf_dac_phase_amp_ext(struct libwr2rf_dev *dev, unsigned ch,
float amplitude, float phase, unsigned verbose)
{
const int phase_step = 15;
int iphase;
int iphase = (int)phase;
unsigned phase_addr;
int last_phase;
int res;
/* According to fmod:
the returned value has the same sign as x and a magnitude
less than the magnitude of y. */
phase = fmodf(phase, 360.0);
if (phase < 0.0)
phase += 360.0;
/* IPHASE is in 0 .. 360. */
iphase = (int)phase;
if (!(phase >= -360.0 && phase <= 360.0))
return LIBWR2RF_ERROR_BAD_PARAM;
/* Current phase. */
switch (ch) {
......@@ -311,29 +304,20 @@ int libwr2rf_dac_phase_amp_ext(struct libwr2rf_dev *dev, unsigned ch,
return LIBWR2RF_ERROR_BAD_ID;
}
last_phase = libwr2rf_read16(dev, phase_addr);
last_phase = (short)libwr2rf_read16(dev, phase_addr);
while (1) {
int dph = iphase - last_phase;
if ((dph >= 0 && dph <= phase_step)
|| (dph < 0 && dph >= -phase_step)
|| (dph >= 0 && dph >= 360 - phase_step)
|| (dph <= 0 && dph <= -360 + phase_step)) {
if (dph >= -phase_step && dph <= phase_step) {
/* Smooth enough, apply directly. */
return libwr2rf_dac_set_phase_amp(dev, ch, amplitude, phase, verbose);
}
if ((dph >= 0 && dph <= 180) || (dph < 0 && dph < -180))
if (dph >= 0)
last_phase += phase_step;
else
last_phase -= phase_step;
/* Normalize the phase. */
if (last_phase < 0)
last_phase += 360;
else if (last_phase >= 360)
last_phase -= 360;
res = libwr2rf_dac_set_phase_amp(dev, ch, amplitude, last_phase, verbose);
if (res != 0)
return res;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment