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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* 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"
static struct ao_task ao_log_task;
uint8_t ao_log_running;
uint8_t ao_log_mutex;
uint32_t ao_log_start_pos;
uint32_t ao_log_end_pos;
uint32_t ao_log_current_pos;
#define AO_LOG_TELESCIENCE_START ((uint8_t) 's')
#define AO_LOG_TELESCIENCE_DATA ((uint8_t) 'd')
struct ao_log_telescience {
uint8_t type;
uint8_t csum;
uint16_t tick;
union {
uint8_t bytes[28];
uint16_t adc[NUM_ADC];
} u;
};
static struct ao_log_telescience log;
static uint8_t ao_log_adc_pos;
static uint8_t
ao_log_csum(__xdata uint8_t *b) __reentrant
{
uint8_t sum = 0x5a;
uint8_t i;
for (i = 0; i < sizeof (struct ao_log_telescience); i++)
sum += *b++;
return -sum;
}
uint8_t
ao_log_telescience_data(struct ao_log_telescience *log)
{
uint8_t wrote = 0;
log->csum = 0;
log->csum = ao_log_csum((__xdata uint8_t *) log);
ao_mutex_get(&ao_log_mutex); {
if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
ao_log_stop();
if (ao_log_running) {
wrote = 1;
ao_storage_write(ao_log_current_pos,
log,
sizeof (struct ao_log_telescience));
ao_log_current_pos += sizeof (struct ao_log_telescience);
}
} ao_mutex_put(&ao_log_mutex);
return wrote;
}
static uint8_t
ao_log_valid(struct ao_log_telescience *log)
{
uint8_t *d;
uint8_t i;
d = (uint8_t *) log;
for (i = 0; i < sizeof (struct ao_log_telescience); i++)
if (d[i] != 0xff)
return 1;
return 0;
}
void
ao_log_telescience(void)
{
ao_storage_setup();
/* Find end of data */
while (ao_log_start_pos < ao_log_end_pos) {
if (!(ao_storage_read(ao_log_start_pos, &log, sizeof (struct ao_log_telescience))))
break;
if (!ao_log_valid(&log))
break;
}
ao_log_current_pos = ao_log_start_pos;
ao_log_end_pos = ao_storage_config;
for (;;) {
while (!ao_log_running)
ao_sleep(&ao_log_running);
memset(&log, '\0', sizeof (struct ao_log_telescience));
log.type = AO_LOG_TELESCIENCE_START;
log.tick = ao_time();
ao_log_telescience_data(&log);
/* Write the whole contents of the ring to the log
* when starting up.
*/
ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
log.type = AO_LOG_TELESCIENCE_DATA;
while (ao_log_running) {
/* Write samples to EEPROM */
while (ao_log_adc_pos != ao_adc_head) {
log.tick = ao_adc_ring[ao_log_adc_pos].tick;
memcpy(&log.u.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
NUM_ADC * sizeof (uint16_t));
ao_log_telescience_data(&log);
ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
}
/* Wait for more ADC data to arrive */
ao_sleep((void *) &ao_adc_head);
}
}
}
void
ao_log_start(void)
{
printf("Log goes from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
ao_log_running = 1;
ao_wakeup(&ao_log_running);
}
void
ao_log_stop(void)
{
printf ("Log stopped at %ld\n", ao_log_current_pos);
ao_log_running = 0;
ao_wakeup((void *) &ao_adc_head);
}
void
ao_log_set(void)
{
ao_cmd_hex();
if (ao_cmd_status == ao_cmd_success) {
if (ao_cmd_lex_i)
ao_log_start();
else
ao_log_stop();
}
}
void
ao_log_list(void)
{
uint32_t pos;
uint32_t start = 0;
uint8_t flight = 0;
for (pos = 0; pos < ao_storage_config; pos += sizeof (struct ao_log_telescience)) {
if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
break;
if (!ao_log_valid(&log) || log.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));
}
if (!ao_log_valid(&log))
break;
start = pos;
flight++;
}
}
printf ("done\n");
}
void
ao_log_delete(void)
{
uint32_t pos;
ao_cmd_hex();
if (ao_cmd_status != ao_cmd_success)
return;
if (ao_cmd_lex_i != 1) {
ao_cmd_status = ao_cmd_syntax_error;
printf("No such flight: %d\n", ao_cmd_lex_i);
return;
}
for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
if (!ao_storage_read(pos, &log, sizeof (struct ao_log_telescience)))
break;
if (!ao_log_valid(&log))
break;
ao_storage_erase(pos);
}
if (pos == 0)
printf("No such flight: %d\n", ao_cmd_lex_i);
else
printf ("Erased\n");
}
const struct ao_cmds ao_log_cmds[] = {
{ ao_log_set, "L <0 off, 1 on>\0Set logging mode" },
{ ao_log_list, "l\0List stored flight logs" },
{ ao_log_delete, "d 1\0Delete all stored flights" },
{ 0, NULL },
};
void
ao_log_init(void)
{
ao_log_running = 0;
ao_cmd_register(&ao_log_cmds[0]);
ao_add_task(&ao_log_task, ao_log_telescience, "log");
}
|