altos: Perform time comparisons using 16-bit arithmetic to handle wrap
[fw/altos] / src / kernel / ao_flight.c
index 2b433ee9b9bd998ba7325be82e62e21643299255..b0d911ed7f2663ac5b820cc0146d4944da9d2d85 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -60,10 +61,10 @@ __xdata uint8_t                     ao_sensor_errors;
  * resting
  */
 static __data uint16_t         ao_interval_end;
-static __data int16_t          ao_interval_min_height;
-static __data int16_t          ao_interval_max_height;
+static __data ao_v_t           ao_interval_min_height;
+static __data ao_v_t           ao_interval_max_height;
 #if HAS_ACCEL
-static __data int16_t          ao_coast_avg_accel;
+static __data ao_v_t           ao_coast_avg_accel;
 #endif
 
 __pdata uint8_t                        ao_flight_force_idle;
@@ -130,7 +131,7 @@ ao_flight(void)
                        {
                                /* Set pad mode - we can fly! */
                                ao_flight_state = ao_flight_pad;
-#if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE
+#if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE && !DEBUG
                                /* Disable the USB controller in flight mode
                                 * to save power
                                 */
@@ -268,7 +269,7 @@ ao_flight(void)
                         * number of seconds.
                         */
                        if (ao_config.apogee_lockout) {
-                               if ((ao_sample_tick - ao_boost_tick) <
+                               if ((int16_t) (ao_sample_tick - ao_boost_tick) <
                                    AO_SEC_TO_TICKS(ao_config.apogee_lockout))
                                        break;
                        }
@@ -370,8 +371,10 @@ ao_flight(void)
                                {
                                        ao_flight_state = ao_flight_landed;
 
+#if HAS_ADC
                                        /* turn off the ADC capture */
                                        ao_timer_set_adc_interval(0);
+#endif
 
                                        ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
                                }
@@ -398,14 +401,14 @@ ao_flight(void)
 }
 
 #if HAS_FLIGHT_DEBUG
-static inline int int_part(int16_t i)  { return i >> 4; }
-static inline int frac_part(int16_t i) { return ((i & 0xf) * 100 + 8) / 16; }
+static inline int int_part(ao_v_t i)   { return i >> 4; }
+static inline int frac_part(ao_v_t i)  { return ((i & 0xf) * 100 + 8) / 16; }
 
 static void
 ao_flight_dump(void)
 {
 #if HAS_ACCEL
-       int16_t accel;
+       ao_v_t  accel;
 
        accel = ((ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale) >> 16;
 #endif