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