attenuator: split out 'execution' from 'validation' part

This commit is contained in:
Harald Welte 2021-05-06 08:16:18 +02:00
parent 73b514d793
commit 6284afc23d
1 changed files with 30 additions and 25 deletions

View File

@ -43,36 +43,15 @@ static void gpio_pulse_clk(void)
gpio_pulse(g_att_cfg->gpio_clock.bank, g_att_cfg->gpio_clock.gpio_nr);
}
/* set a given attenuator to a given value
* \param channel The RF channel (0..7)
* \param stage Attenuator stage (0..1)
* \param value in quarter-dB (0..124)
*/
int attenuator_set(uint8_t channel, uint8_t stage, uint8_t val_qdb)
static void _attenuator_stage_set(uint8_t channel, uint8_t stage, uint8_t val_qdb)
{
uint8_t val = val_qdb/4; /* whole dB for this attenuator */
const struct attenuator_def *ad;
const struct attenuator_def *ad = &g_att_cfg->channels[channel].stage[stage];
uint8_t val;
int i;
if (!g_att_cfg)
return -ENODEV;
if (channel >= g_att_cfg->num_channels)
return -ENODEV;
if (stage >= g_att_cfg->channels[channel].num_stages)
return -ENODEV;
ad = &g_att_cfg->channels[channel].stage[stage];
if (val > 0x1f)
return -ERANGE;
printf("Setting CH%u-ST%u to %u dB\r\n", channel, stage, val);
/* the DAT-31A-SP actually has a B0 which works in half-dB steps,
* but according to the manual it must alsays be 0 */
val = val * 2;
val = val_qdb / 2;
/* The shift register should be loaded while LE is held low to
* prevent the attenuator value from changing as data is
@ -89,6 +68,32 @@ int attenuator_set(uint8_t channel, uint8_t stage, uint8_t val_qdb)
/* actual value we have set may not be exactly what was requested */
g_att_state[channel][stage].value_qdB.current = val_qdb;
}
/* set a given attenuator to a given value
* \param channel The RF channel (0..7)
* \param stage Attenuator stage (0..1)
* \param value in quarter-dB (0..124)
*/
int attenuator_set(uint8_t channel, uint8_t stage, uint8_t val_qdb)
{
uint8_t val = val_qdb/4; /* whole dB for this attenuator */
if (!g_att_cfg)
return -ENODEV;
if (channel >= g_att_cfg->num_channels)
return -ENODEV;
if (stage >= g_att_cfg->channels[channel].num_stages)
return -ENODEV;
if (val > 0x1f)
return -ERANGE;
printf("Setting CH%u-ST%u to %u dB\r\n", channel, stage, val);
_attenuator_stage_set(channel, stage, val_qdb);
return 0;
}