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