From 7c8addc5c5f811eefbed901fcd182a3012e2e75b Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 2 Nov 2016 17:30:34 -0400 Subject: [PATCH] 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 --- src/adccmds.c | 9 +++++---- src/avr/gpio.c | 34 +++++++++++++++++----------------- src/avr/gpio.h | 6 ++---- src/generic/gpio.h | 5 ++--- src/simulator/gpio.c | 9 +++------ 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/adccmds.c b/src/adccmds.c index 9c70ab88..907e05d4 100644 --- a/src/adccmds.c +++ b/src/adccmds.c @@ -22,8 +22,9 @@ static uint8_t analog_in_event(struct timer *timer) { struct analog_in *a = container_of(timer, struct analog_in, timer); - if (gpio_adc_sample(a->pin)) { - a->timer.waketime += gpio_adc_sample_time(); + uint32_t sample_delay = gpio_adc_sample(a->pin); + if (sample_delay) { + a->timer.waketime += sample_delay; return SF_RESCHEDULE; } uint16_t value = gpio_adc_read(a->pin); @@ -62,7 +63,7 @@ command_query_analog_in(uint32_t *args) { struct analog_in *a = lookup_oid(args[0], command_config_analog_in); sched_del_timer(&a->timer); - gpio_adc_clear_sample(a->pin); + gpio_adc_cancel_sample(a->pin); a->next_begin_time = args[1]; a->timer.waketime = a->next_begin_time; a->sample_time = args[2]; @@ -111,7 +112,7 @@ analog_in_shutdown(void) uint8_t i; struct analog_in *a; foreach_oid(i, a, command_config_analog_in) { - gpio_adc_clear_sample(a->pin); + gpio_adc_cancel_sample(a->pin); } } DECL_SHUTDOWN(analog_in_shutdown); diff --git a/src/avr/gpio.c b/src/avr/gpio.c index 9919cdd1..3df6e9e8 100644 --- a/src/avr/gpio.c +++ b/src/avr/gpio.c @@ -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< -#include "compiler.h" // __always_inline struct gpio_out { struct gpio_digital_regs *regs; @@ -31,10 +30,9 @@ struct gpio_adc { uint8_t chan; }; struct gpio_adc gpio_adc_setup(uint8_t pin); -uint32_t gpio_adc_sample_time(void); -uint8_t gpio_adc_sample(struct gpio_adc g); -void gpio_adc_clear_sample(struct gpio_adc g); +uint32_t gpio_adc_sample(struct gpio_adc g); uint16_t gpio_adc_read(struct gpio_adc g); +void gpio_adc_cancel_sample(struct gpio_adc g); void spi_config(void); void spi_transfer(char *data, uint8_t len); diff --git a/src/generic/gpio.h b/src/generic/gpio.h index 07307b9e..4b5dc812 100644 --- a/src/generic/gpio.h +++ b/src/generic/gpio.h @@ -26,10 +26,9 @@ struct gpio_adc { uint8_t pin; }; struct gpio_adc gpio_adc_setup(uint8_t pin); -uint32_t gpio_adc_sample_time(void); -uint8_t gpio_adc_sample(struct gpio_adc g); -void gpio_adc_clear_sample(struct gpio_adc g); +uint32_t gpio_adc_sample(struct gpio_adc g); uint16_t gpio_adc_read(struct gpio_adc g); +void gpio_adc_cancel_sample(struct gpio_adc g); void spi_config(void); void spi_transfer(char *data, uint8_t len); diff --git a/src/simulator/gpio.c b/src/simulator/gpio.c index 6e89f946..569f4b78 100644 --- a/src/simulator/gpio.c +++ b/src/simulator/gpio.c @@ -27,17 +27,14 @@ void gpio_pwm_write(struct gpio_pwm g, uint8_t val) { struct gpio_adc gpio_adc_setup(uint8_t pin) { return (struct gpio_adc){.pin=pin}; } -uint32_t gpio_adc_sample_time(void) { +uint32_t gpio_adc_sample(struct gpio_adc g) { return 0; } -uint8_t gpio_adc_sample(struct gpio_adc g) { - return 0; -} -void gpio_adc_clear_sample(struct gpio_adc g) { -} uint16_t gpio_adc_read(struct gpio_adc g) { return 0; } +void gpio_adc_cancel_sample(struct gpio_adc g) { +} void spi_config(void) { }