src-avr: Log ADC data to eeprom
[fw/altos] / src-avr / 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
20 static struct ao_task ao_log_task;
21
22 uint8_t ao_log_running;
23 uint8_t ao_log_mutex;
24 uint32_t        ao_log_begin_pos, ao_log_end_pos;
25 uint32_t        ao_log_current_pos;
26
27 #define AO_LOG_TELESCIENCE_START        'b'
28 #define AO_LOG_TELESCIENCE_STOP         'e'
29 #define AO_LOG_TELESCIENCE_DATA         'd'
30
31 struct ao_log_telescience {
32         uint8_t         type;
33         uint8_t         csum;
34         uint16_t        tick;
35         union {
36                 uint8_t         bytes[28];
37                 uint16_t        adc[NUM_ADC];
38         } u;
39 };
40
41 static struct ao_log_telescience log;
42 static uint8_t  ao_log_adc_pos;
43
44 static uint8_t
45 ao_log_csum(__xdata uint8_t *b) __reentrant
46 {
47         uint8_t sum = 0x5a;
48         uint8_t i;
49
50         for (i = 0; i < sizeof (struct ao_log_telescience); i++)
51                 sum += *b++;
52         return -sum;
53 }
54
55 uint8_t
56 ao_log_telescience_data(struct ao_log_telescience *log)
57 {
58         uint8_t wrote = 0;
59
60         log->csum = 0;
61         log->csum = ao_log_csum((__xdata uint8_t *) log);
62         ao_mutex_get(&ao_log_mutex); {
63                 if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
64                         ao_log_stop();
65                 if (ao_log_running) {
66                         wrote = 1;
67                         ao_storage_write(ao_log_current_pos,
68                                          log,
69                                          sizeof (struct ao_log_telescience));
70                         ao_log_current_pos += sizeof (struct ao_log_telescience);
71                 }
72         } ao_mutex_put(&ao_log_mutex);
73         return wrote;
74 }
75
76 void
77 ao_log_telescience(void)
78 {
79         ao_storage_setup();
80
81         ao_log_current_pos = 0;
82         ao_log_end_pos = ao_storage_config;
83         for (;;) {
84                 while (!ao_log_running)
85                         ao_sleep(&ao_log_running);
86
87                 memset(&log, '\0', sizeof (struct ao_log_telescience));
88                 log.type = AO_LOG_TELESCIENCE_START;
89                 log.tick = ao_time();
90                 ao_log_telescience_data(&log);
91                 /* Write the whole contents of the ring to the log
92                  * when starting up.
93                  */
94                 ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
95                 log.type = AO_LOG_TELESCIENCE_DATA;
96                 while (ao_log_running) {
97                         /* Write samples to EEPROM */
98                         while (ao_log_adc_pos != ao_adc_head) {
99                                 log.tick = ao_adc_ring[ao_log_adc_pos].tick;
100                                 memcpy(&log.u.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
101                                        NUM_ADC * sizeof (uint16_t));
102                                 ao_log_telescience_data(&log);
103                                 ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
104                         }
105                         /* Wait for more ADC data to arrive */
106                         ao_sleep((void *) &ao_adc_head);
107                 }
108                 memset(&log, '\0', sizeof (struct ao_log_telescience));
109                 log.type = AO_LOG_TELESCIENCE_STOP;
110                 log.tick = ao_time();
111                 ao_log_telescience_data(&log);
112                 ao_storage_flush();
113         }
114 }
115
116 void
117 ao_log_start(void)
118 {
119         ao_log_running = 1;
120         ao_wakeup(&ao_log_running);
121 }
122
123 void
124 ao_log_stop(void)
125 {
126         ao_log_running = 0;
127         ao_wakeup((void *) &ao_adc_head);
128 }
129
130 void
131 ao_log_set(void)
132 {
133         ao_cmd_hex();
134         if (ao_cmd_status == ao_cmd_success) {
135                 if (ao_cmd_lex_i)
136                         ao_log_start();
137                 else
138                         ao_log_stop();
139         }
140 }
141
142 const struct ao_cmds ao_log_cmds[] = {
143         { ao_log_set,   "L <0 off, 1 on>\0Set logging mode" },
144         { 0,    NULL },
145 };
146
147 void
148 ao_log_init(void)
149 {
150         ao_log_running = 0;
151
152         ao_cmd_register(&ao_log_cmds[0]);
153
154         ao_add_task(&ao_log_task, ao_log_telescience, "log");
155 }