summaryrefslogtreecommitdiff
path: root/src/kernel/ao_log_telescience.c
blob: 002a10bd9fd8dab64f5f407b60cf76787140ae04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright © 2011 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "ao.h"
#include "ao_product.h"
#include "ao_log.h"
#include "ao_companion.h"

static uint8_t	ao_log_data_pos;

__code uint8_t ao_log_format = AO_LOG_FORMAT_TELESCIENCE;

static void
ao_log_telescience_csum(void) __reentrant
{
	__xdata uint8_t *b = ao_log_single_write_data.bytes;
	uint8_t	sum = 0x5a;
	uint8_t	i;

	ao_log_single_write_data.telescience.csum = 0;
	for (i = 0; i < sizeof (struct ao_log_telescience); i++)
		sum += *b++;
	ao_log_single_write_data.telescience.csum = -sum;
}

void
ao_log_single(void)
{
	ao_storage_setup();

	/* This can take a while, so let the rest
	 * of the system finish booting before we start
	 */
	ao_delay(AO_SEC_TO_TICKS(10));

	ao_log_single_restart();
	for (;;) {
		while (!ao_log_running)
			ao_sleep(&ao_log_running);

		ao_log_start_pos = ao_log_current_pos;
		ao_log_single_write_data.telescience.type = AO_LOG_TELESCIENCE_START;
		ao_log_single_write_data.telescience.tick = ao_time();
		ao_log_single_write_data.telescience.adc[0] = ao_companion_command.serial;
		ao_log_single_write_data.telescience.adc[1] = ao_companion_command.flight;
		ao_log_telescience_csum();
		ao_log_single_write();
		/* Write the whole contents of the ring to the log
		 * when starting up.
		 */
		ao_log_data_pos = ao_data_ring_next(ao_data_head);
		ao_log_single_write_data.telescience.type = AO_LOG_TELESCIENCE_DATA;
		while (ao_log_running) {
			/* Write samples to EEPROM */
			while (ao_log_data_pos != ao_data_head) {
				ao_log_single_write_data.telescience.tick = ao_data_ring[ao_log_data_pos].tick;
				memcpy(&ao_log_single_write_data.telescience.adc, (void *) ao_data_ring[ao_log_data_pos].adc.adc,
				       AO_LOG_TELESCIENCE_NUM_ADC * sizeof (uint16_t));
				ao_log_telescience_csum();
				ao_log_single_write();
				ao_log_data_pos = ao_data_ring_next(ao_log_data_pos);
			}
			/* Wait for more ADC data to arrive */
			ao_sleep((void *) &ao_data_head);
		}
		memset(&ao_log_single_write_data.telescience.adc, '\0', sizeof (ao_log_single_write_data.telescience.adc));
	}
}

void
ao_log_single_list(void)
{
	uint32_t	pos;
	uint32_t	start = 0;
	uint8_t		flight = 0;

	for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
		if (pos >= ao_storage_config ||
		    !ao_log_single_read(pos) ||
		    ao_log_single_read_data.telescience.type == AO_LOG_TELESCIENCE_START)
		{
			if (pos != start) {
				printf("flight %d start %x end %x\n",
				       flight,
				       (uint16_t) (start >> 8),
				       (uint16_t) ((pos + 0xff) >> 8)); flush();
			}
			if (ao_log_single_read_data.telescience.type != AO_LOG_TELESCIENCE_START)
				break;
			start = pos;
			flight++;
		}
	}
	printf ("done\n");
}

void
ao_log_single_extra_query(void)
{
	printf("log data tick: %04x\n", ao_log_single_write_data.telescience.tick);
	printf("TM data tick: %04x\n", ao_log_single_write_data.telescience.tm_tick);
	printf("TM state: %d\n", ao_log_single_write_data.telescience.tm_state);
	printf("TM serial: %d\n", ao_companion_command.serial);
	printf("TM flight: %d\n", ao_companion_command.flight);
}