diff options
Diffstat (limited to 'src/avr')
-rw-r--r-- | src/avr/gpio.c | 34 | ||||
-rw-r--r-- | src/avr/gpio.h | 6 |
2 files changed, 19 insertions, 21 deletions
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<<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) diff --git a/src/avr/gpio.h b/src/avr/gpio.h index e6cbbd32..4eb37204 100644 --- a/src/avr/gpio.h +++ b/src/avr/gpio.h @@ -2,7 +2,6 @@ #define __AVR_GPIO_H #include <stdint.h> -#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); |