Allow ADC to be disabled
[fw/altos] / ao_timer.c
index cb6255d2f4c83f72719b1a30f959a42231c3ea69..a6a7646f5a22c0b48aeb7ff0cbb0245ddea30467 100644 (file)
 
 #include "ao.h"
 
-volatile __data uint16_t ao_time;
+static volatile __data uint16_t ao_tick_count;
 
-void ao_timer_isr(void) interrupt 9
+uint16_t ao_time(void) __critical
 {
-       ++ao_time;
-       ao_adc_poll();
-       ao_wakeup(DATA_TO_XDATA(&ao_time));
+       return ao_tick_count;
 }
 
-uint16_t ao_time_atomic(void)
+void
+ao_delay(uint16_t ticks)
 {
-       uint8_t ret1;
-       uint16_t ret2;
-       
-       for (;;) {
-               ret1 = ao_time >> 8;
-               ret2 = ao_time;
-               if (ret1 == ((__xdata uint8_t *)(&ret2))[1])
-                       break;
-       }
-       return ret2;
+       uint16_t until = ao_time() + ticks;
+
+       while ((int16_t) (until - ao_time()) > 0)
+               ao_sleep(DATA_TO_XDATA(&ao_tick_count));
 }
 
 #define T1_CLOCK_DIVISOR       8       /* 24e6/8 = 3e6 */
 #define T1_SAMPLE_TIME         30000   /* 3e6/30000 = 100 */
 
+volatile __data uint8_t        ao_adc_interval = 1;
+volatile __data uint8_t        ao_adc_count;
+
+void ao_timer_isr(void) interrupt 9
+{
+       ++ao_tick_count;
+       if (++ao_adc_count == ao_adc_interval) {
+               ao_adc_count = 0;
+               ao_adc_poll();
+       }
+       ao_wakeup(DATA_TO_XDATA(&ao_tick_count));
+}
+
+void
+ao_timer_set_adc_interval(uint8_t interval) __critical
+{
+       ao_adc_interval = interval;
+       ao_adc_count = 0;
+}
+
 void
 ao_timer_init(void)
 {
@@ -53,7 +66,7 @@ ao_timer_init(void)
 
        /* set the sample rate */
        T1CC0H = T1_SAMPLE_TIME >> 8;
-       T1CC0L = T1_SAMPLE_TIME;
+       T1CC0L = (uint8_t) T1_SAMPLE_TIME;
 
        T1CCTL0 = T1CCTL_MODE_COMPARE;
        T1CCTL1 = 0;
@@ -71,11 +84,3 @@ ao_timer_init(void)
        T1CTL = T1CTL_MODE_MODULO | T1CTL_DIV_8;
 }
 
-void
-ao_delay(uint16_t ticks)
-{
-       uint16_t until = ao_time_atomic() + ticks;
-
-       while ((int16_t) (until - ao_time_atomic()) > 0)
-               ao_sleep(DATA_TO_XDATA(&ao_time));
-}