1763b9ebac6ad39e88a1b6592b0095d33ce80843
[fw/altos] / src / core / ao_log_mega.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include <ao_log.h>
20 #include <ao_data.h>
21 #include <ao_flight.h>
22
23 static __xdata uint8_t  ao_log_mutex;
24 static __xdata struct ao_log_mega log;
25
26 static uint8_t
27 ao_log_csum(__xdata uint8_t *b) __reentrant
28 {
29         uint8_t sum = 0x5a;
30         uint8_t i;
31
32         for (i = 0; i < sizeof (struct ao_log_mega); i++)
33                 sum += *b++;
34         return -sum;
35 }
36
37 uint8_t
38 ao_log_mega(__xdata struct ao_log_mega *log) __reentrant
39 {
40         uint8_t wrote = 0;
41         /* set checksum */
42         log->csum = 0;
43         log->csum = ao_log_csum((__xdata uint8_t *) log);
44         ao_mutex_get(&ao_log_mutex); {
45                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
46                         ao_log_stop();
47                 if (ao_log_running) {
48                         wrote = 1;
49                         ao_storage_write(ao_log_current_pos,
50                                          log,
51                                          sizeof (struct ao_log_mega));
52                         ao_log_current_pos += sizeof (struct ao_log_mega);
53                 }
54         } ao_mutex_put(&ao_log_mutex);
55         return wrote;
56 }
57
58 static uint8_t
59 ao_log_dump_check_data(void)
60 {
61         if (ao_log_csum((uint8_t *) &log) != 0)
62                 return 0;
63         return 1;
64 }
65
66 static __data uint8_t   ao_log_data_pos;
67
68 /* a hack to make sure that ao_log_megas fill the eeprom block in even units */
69 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_mega))] ;
70
71 #ifndef AO_SENSOR_INTERVAL_ASCENT
72 #define AO_SENSOR_INTERVAL_ASCENT       1
73 #define AO_SENSOR_INTERVAL_DESCENT      10
74 #define AO_OTHER_INTERVAL               32
75 #endif
76
77 void
78 ao_log(void)
79 {
80         __pdata uint16_t        next_sensor, next_other;
81         uint8_t                 i;
82
83         ao_storage_setup();
84
85         ao_log_scan();
86
87         while (!ao_log_running)
88                 ao_sleep(&ao_log_running);
89
90 #if HAS_FLIGHT
91         log.type = AO_LOG_FLIGHT;
92         log.tick = ao_sample_tick;
93 #if HAS_ACCEL
94         log.u.flight.ground_accel = ao_ground_accel;
95 #endif
96         log.u.flight.ground_pres = ao_ground_pres;
97         log.u.flight.flight = ao_flight_number;
98         ao_log_mega(&log);
99 #endif
100
101         /* Write the whole contents of the ring to the log
102          * when starting up.
103          */
104         ao_log_data_pos = ao_data_ring_next(ao_data_head);
105         next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
106         ao_log_state = ao_flight_startup;
107         for (;;) {
108                 /* Write samples to EEPROM */
109                 while (ao_log_data_pos != ao_data_head) {
110                         log.tick = ao_data_ring[ao_log_data_pos].tick;
111                         if ((int16_t) (log.tick - next_sensor) >= 0) {
112                                 log.type = AO_LOG_SENSOR;
113                                 log.u.sensor.pres = ao_data_ring[ao_log_data_pos].ms5607.pres;
114                                 log.u.sensor.temp = ao_data_ring[ao_log_data_pos].ms5607.temp;
115                                 log.u.sensor.accel_x = ao_data_ring[ao_log_data_pos].mpu6000.accel_x;
116                                 log.u.sensor.accel_y = ao_data_ring[ao_log_data_pos].mpu6000.accel_y;
117                                 log.u.sensor.accel_z = ao_data_ring[ao_log_data_pos].mpu6000.accel_z;
118                                 log.u.sensor.gyro_x = ao_data_ring[ao_log_data_pos].mpu6000.gyro_x;
119                                 log.u.sensor.gyro_y = ao_data_ring[ao_log_data_pos].mpu6000.gyro_y;
120                                 log.u.sensor.gyro_z = ao_data_ring[ao_log_data_pos].mpu6000.gyro_z;
121                                 ao_log_mega(&log);
122                                 if (ao_log_state <= ao_flight_coast)
123                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT;
124                                 else
125                                         next_sensor = log.tick + AO_SENSOR_INTERVAL_DESCENT;
126                         }
127                         if ((int16_t) (log.tick - next_other) >= 0) {
128                                 log.type = AO_LOG_TEMP_VOLT;
129                                 log.u.volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt;
130                                 log.u.volt.v_pbatt = ao_data_ring[ao_log_data_pos].adc.v_pbatt;
131                                 log.u.volt.n_sense = AO_ADC_NUM_SENSE;
132                                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
133                                         log.u.volt.sense[i] = ao_data_ring[ao_log_data_pos].adc.sense[i];
134                                 ao_log_mega(&log);
135                                 next_other = log.tick + AO_OTHER_INTERVAL;
136                         }
137                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
138                 }
139 #if HAS_FLIGHT
140                 /* Write state change to EEPROM */
141                 if (ao_flight_state != ao_log_state) {
142                         ao_log_state = ao_flight_state;
143                         log.type = AO_LOG_STATE;
144                         log.tick = ao_time();
145                         log.u.state.state = ao_log_state;
146                         log.u.state.reason = 0;
147                         ao_log_mega(&log);
148
149                         if (ao_log_state == ao_flight_landed)
150                                 ao_log_stop();
151                 }
152 #endif
153
154                 /* Wait for a while */
155                 ao_delay(AO_MS_TO_TICKS(100));
156
157                 /* Stop logging when told to */
158                 while (!ao_log_running)
159                         ao_sleep(&ao_log_running);
160         }
161 }
162
163 uint16_t
164 ao_log_flight(uint8_t slot)
165 {
166         if (!ao_storage_read(ao_log_pos(slot),
167                              &log,
168                              sizeof (struct ao_log_mega)))
169                 return 0;
170
171         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
172                 return log.u.flight.flight;
173         return 0;
174 }