fleshing out logging for telefiretwo
[fw/altos] / src / kernel / ao_log_firetwo.c
1 /*
2  * Copyright © 2017 Bdale Garbee <bdale@gag.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include <ao_log.h>
21 #include <ao_data.h>
22 #include <ao_flight.h>
23
24 static __xdata struct ao_log_firetwo log;
25
26 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELEFIRETWO;
27
28 static uint8_t
29 ao_log_csum(__xdata uint8_t *b) __reentrant
30 {
31         uint8_t sum = 0x5a;
32         uint8_t i;
33
34         for (i = 0; i < sizeof (struct ao_log_firetwo); i++)
35                 sum += *b++;
36         return -sum;
37 }
38
39 uint8_t
40 ao_log_firetwo(__xdata struct ao_log_firetwo *log) __reentrant
41 {
42         uint8_t wrote = 0;
43         /* set checksum */
44         log->csum = 0;
45         log->csum = ao_log_csum((__xdata uint8_t *) log);
46         ao_mutex_get(&ao_log_mutex); {
47                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
48                         ao_log_stop();
49                 if (ao_log_running) {
50                         wrote = 1;
51                         ao_storage_write(ao_log_current_pos,
52                                          log,
53                                          sizeof (struct ao_log_firetwo));
54                         ao_log_current_pos += sizeof (struct ao_log_firetwo);
55                 }
56         } ao_mutex_put(&ao_log_mutex);
57         return wrote;
58 }
59
60 static uint8_t
61 ao_log_dump_check_data(void)
62 {
63         if (ao_log_csum((uint8_t *) &log) != 0)
64                 return 0;
65         return 1;
66 }
67
68 #if HAS_ADC
69 static __data uint8_t   ao_log_data_pos;
70
71 /* a hack to make sure that ao_log_metrums fill the eeprom block in even units */
72 typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_firetwo))] ;
73 #endif
74
75 void
76 ao_log(void)
77 {
78         __pdata uint16_t        next_sensor, next_other;
79
80         ao_storage_setup();
81
82         ao_log_scan();
83
84         while (!ao_log_running)
85                 ao_sleep(&ao_log_running);
86
87 #if HAS_FLIGHT
88         log.type = AO_LOG_FLIGHT;
89         log.tick = ao_sample_tick;
90         log.u.flight.idle_pressure = ao_idle_pressure;
91         log.u.flight.idle_thrust = ao_idle_thrust;
92         log.u.flight.flight = ao_flight_number;
93         ao_log_firetwo(&log);
94 #endif
95
96         /* Write the whole contents of the ring to the log
97          * when starting up.
98          */
99         ao_log_data_pos = ao_data_ring_next(ao_data_head);
100         next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick;
101         ao_log_state = ao_flight_startup;
102         for (;;) {
103                 /* Write samples to EEPROM */
104                 while (ao_log_data_pos != ao_data_head) {
105                         log.tick = ao_data_ring[ao_log_data_pos].tick;
106                         if ((int16_t) (log.tick - next_sensor) >= 0) {
107                                 log.type = AO_LOG_SENSOR;
108                                 log.u.sensor.pressure = ao_data_ring[ao_log_data_pos].sensor.pressure;
109                                 log.u.sensor.thrust = ao_data_ring[ao_log_data_pos].sensor.thrust;
110                                 for (i = 0; i < 4; i++) {
111                                         log.u.sensor.thermistor[i] = ao_data_ring[ao_log_data_pos].sensor.thermistor[i];
112                                 }
113                                 ao_log_firetwo(&log);
114                         }
115                         ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
116                 }
117 #if HAS_FLIGHT
118                 /* Write state change to EEPROM */
119                 if (ao_flight_state != ao_log_state) {
120                         ao_log_state = ao_flight_state;
121                         log.type = AO_LOG_STATE;
122                         log.tick = ao_time();
123                         log.u.state.state = ao_log_state;
124                         log.u.state.reason = 0;
125                         ao_log_firetwo(&log);
126
127                         if (ao_log_state == ao_flight_landed)
128                                 ao_log_stop();
129                 }
130 #endif
131
132                 ao_log_flush();
133
134                 /* Wait for a while */
135                 ao_delay(AO_MS_TO_TICKS(100));
136
137                 /* Stop logging when told to */
138                 while (!ao_log_running)
139                         ao_sleep(&ao_log_running);
140         }
141 }
142 #endif
143
144 uint16_t
145 ao_log_flight(uint8_t slot)
146 {
147         if (!ao_storage_read(ao_log_pos(slot),
148                              &log,
149                              sizeof (struct ao_log_firetwo)))
150                 return 0;
151
152         if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT)
153                 return log.u.flight.flight;
154         return 0;
155 }