gpio: Merge gpio_adc_sample_time() into gpio_adc_sample()
Return the number of clock ticks to wait directly from gpio_adc_sample(). This simplifies the ADC interface. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
@@ -282,27 +282,24 @@ gpio_adc_setup(uint8_t pin)
|
||||
shutdown("Not a valid ADC pin");
|
||||
}
|
||||
|
||||
uint32_t
|
||||
gpio_adc_sample_time(void)
|
||||
{
|
||||
return (13 + 1) * 128 + 200;
|
||||
}
|
||||
|
||||
enum { ADC_DUMMY=0xff };
|
||||
static uint8_t last_analog_read = ADC_DUMMY;
|
||||
|
||||
uint8_t
|
||||
// Try to sample a value. Returns zero if sample ready, otherwise
|
||||
// returns the number of clock ticks the caller should wait before
|
||||
// retrying this function.
|
||||
uint32_t
|
||||
gpio_adc_sample(struct gpio_adc g)
|
||||
{
|
||||
if (ADCSRA & (1<<ADSC))
|
||||
// Busy
|
||||
return 1;
|
||||
goto need_delay;
|
||||
if (last_analog_read == g.chan)
|
||||
// Sample now ready
|
||||
return 0;
|
||||
if (last_analog_read != ADC_DUMMY)
|
||||
// Sample on another channel in progress
|
||||
return 1;
|
||||
goto need_delay;
|
||||
last_analog_read = g.chan;
|
||||
|
||||
#if defined(ADCSRB) && defined(MUX5)
|
||||
@@ -315,16 +312,11 @@ gpio_adc_sample(struct gpio_adc g)
|
||||
|
||||
// start the conversion
|
||||
ADCSRA |= 1<<ADSC;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
gpio_adc_clear_sample(struct gpio_adc g)
|
||||
{
|
||||
if (last_analog_read == g.chan)
|
||||
last_analog_read = ADC_DUMMY;
|
||||
need_delay:
|
||||
return (13 + 1) * 128 + 200;
|
||||
}
|
||||
|
||||
// Read a value; use only after gpio_adc_sample() returns zero
|
||||
uint16_t
|
||||
gpio_adc_read(struct gpio_adc g)
|
||||
{
|
||||
@@ -332,6 +324,14 @@ gpio_adc_read(struct gpio_adc g)
|
||||
return ADC;
|
||||
}
|
||||
|
||||
// Cancel a sample that may have been started with gpio_adc_sample()
|
||||
void
|
||||
gpio_adc_cancel_sample(struct gpio_adc g)
|
||||
{
|
||||
if (last_analog_read == g.chan)
|
||||
last_analog_read = ADC_DUMMY;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
spi_config(void)
|
||||
|
||||
Reference in New Issue
Block a user