altos: Support staging by going back to boost as needed
[fw/altos] / src / core / ao_log_telescience.c
1 /*
2  * Copyright © 2011 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_product.h"
20
21 static uint8_t  ao_log_adc_pos;
22
23 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELESCIENCE;
24
25 static void
26 ao_log_telescience_csum(void) __reentrant
27 {
28         __xdata uint8_t *b = ao_log_single_write_data.bytes;
29         uint8_t sum = 0x5a;
30         uint8_t i;
31
32         ao_log_single_write_data.telescience.csum = 0;
33         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
34                 sum += *b++;
35         ao_log_single_write_data.telescience.csum = -sum;
36 }
37
38 void
39 ao_log_single(void)
40 {
41         ao_storage_setup();
42
43         /* This can take a while, so let the rest
44          * of the system finish booting before we start
45          */
46         ao_delay(AO_SEC_TO_TICKS(10));
47
48         ao_log_single_restart();
49         for (;;) {
50                 while (!ao_log_running)
51                         ao_sleep(&ao_log_running);
52
53                 ao_log_start_pos = ao_log_current_pos;
54                 ao_log_single_write_data.telescience.type = AO_LOG_TELESCIENCE_START;
55                 ao_log_single_write_data.telescience.tick = ao_time();
56                 ao_log_single_write_data.telescience.adc[0] = ao_companion_command.serial;
57                 ao_log_single_write_data.telescience.adc[1] = ao_companion_command.flight;
58                 ao_log_telescience_csum();
59                 ao_log_single_write();
60                 /* Write the whole contents of the ring to the log
61                  * when starting up.
62                  */
63                 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
64                 ao_log_single_write_data.telescience.type = AO_LOG_TELESCIENCE_DATA;
65                 while (ao_log_running) {
66                         /* Write samples to EEPROM */
67                         while (ao_log_adc_pos != ao_adc_head) {
68                                 ao_log_single_write_data.telescience.tick = ao_adc_ring[ao_log_adc_pos].tick;
69                                 memcpy(&ao_log_single_write_data.telescience.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
70                                        AO_LOG_TELESCIENCE_NUM_ADC * sizeof (uint16_t));
71                                 ao_log_telescience_csum();
72                                 ao_log_single_write();
73                                 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
74                         }
75                         /* Wait for more ADC data to arrive */
76                         ao_sleep((void *) &ao_adc_head);
77                 }
78                 memset(&ao_log_single_write_data.telescience.adc, '\0', sizeof (ao_log_single_write_data.telescience.adc));
79         }
80 }
81
82 void
83 ao_log_single_list(void)
84 {
85         uint32_t        pos;
86         uint32_t        start = 0;
87         uint8_t         flight = 0;
88
89         for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
90                 if (pos >= ao_storage_config ||
91                     !ao_log_single_read(pos) ||
92                     ao_log_single_read_data.telescience.type == AO_LOG_TELESCIENCE_START)
93                 {
94                         if (pos != start) {
95                                 printf("flight %d start %x end %x\n",
96                                        flight,
97                                        (uint16_t) (start >> 8),
98                                        (uint16_t) ((pos + 0xff) >> 8)); flush();
99                         }
100                         if (ao_log_single_read_data.telescience.type != AO_LOG_TELESCIENCE_START)
101                                 break;
102                         start = pos;
103                         flight++;
104                 }
105         }
106         printf ("done\n");
107 }
108
109 void
110 ao_log_single_extra_query(void)
111 {
112         printf("log data tick: %04x\n", ao_log_single_write_data.telescience.tick);
113         printf("TM data tick: %04x\n", ao_log_single_write_data.telescience.tm_tick);
114         printf("TM state: %d\n", ao_log_single_write_data.telescience.tm_state);
115         printf("TM serial: %d\n", ao_companion_command.serial);
116         printf("TM flight: %d\n", ao_companion_command.flight);
117 }