altos: Make ao_xmem funcs require __xdata void * instead of casting
[fw/altos] / src / core / ao_telemetry.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
21 static __pdata uint16_t ao_telemetry_interval;
22 static __pdata int8_t ao_telemetry_config_max;
23 static __pdata int8_t ao_telemetry_config_cur;
24 #if HAS_GPS
25 static __pdata int8_t ao_telemetry_loc_cur;
26 static __pdata int8_t ao_telemetry_sat_cur;
27 #endif
28 #if HAS_COMPANION
29 static __pdata int8_t ao_telemetry_companion_max;
30 static __pdata int8_t ao_telemetry_companion_cur;
31 #endif
32 static __pdata uint8_t ao_rdf = 0;
33 static __pdata uint16_t ao_rdf_time;
34
35 #define AO_RDF_INTERVAL_TICKS   AO_SEC_TO_TICKS(5)
36 #define AO_RDF_LENGTH_MS        500
37
38 #if defined(TELEMETRUM_V_0_1) || defined(TELEMETRUM_V_0_2) || defined(TELEMETRUM_V_1_0) || defined(TELEMETRUM_V_1_1) || defined(TELEBALLOON_V_1_1)
39 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMETRUM
40 #endif
41
42 #if defined(TELEMINI_V_1_0)
43 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
44 #endif
45
46 #if defined(TELENANO_V_0_1)
47 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
48 #endif
49
50 static __xdata union ao_telemetry_all   telemetry;
51
52 /* Send sensor packet */
53 static void
54 ao_send_sensor(void)
55 {
56         uint8_t         sample;
57         sample = ao_sample_adc;
58                         
59         telemetry.generic.tick = ao_adc_ring[sample].tick;
60         telemetry.generic.type = AO_TELEMETRY_SENSOR;
61
62         telemetry.sensor.state = ao_flight_state;
63 #if HAS_ACCEL
64         telemetry.sensor.accel = ao_adc_ring[sample].accel;
65 #else
66         telemetry.sensor.accel = 0;
67 #endif
68         telemetry.sensor.pres = ao_adc_ring[sample].pres;
69         telemetry.sensor.temp = ao_adc_ring[sample].temp;
70         telemetry.sensor.v_batt = ao_adc_ring[sample].v_batt;
71 #if HAS_IGNITE
72         telemetry.sensor.sense_d = ao_adc_ring[sample].sense_d;
73         telemetry.sensor.sense_m = ao_adc_ring[sample].sense_m;
74 #else
75         telemetry.sensor.sense_d = 0;
76         telemetry.sensor.sense_m = 0;
77 #endif
78
79         telemetry.sensor.acceleration = ao_accel;
80         telemetry.sensor.speed = ao_speed;
81         telemetry.sensor.height = ao_height;
82
83         telemetry.sensor.ground_pres = ao_ground_pres;
84 #if HAS_ACCEL
85         telemetry.sensor.ground_accel = ao_ground_accel;
86         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
87         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
88 #else
89         telemetry.sensor.ground_accel = 0;
90         telemetry.sensor.accel_plus_g = 0;
91         telemetry.sensor.accel_minus_g = 0;
92 #endif
93
94         ao_radio_send(&telemetry, sizeof (telemetry));
95 }
96
97 static uint8_t          ao_baro_sample;
98
99 #ifdef AO_SEND_ALL_BARO
100 static void
101 ao_send_baro(void)
102 {
103         uint8_t         sample = ao_sample_adc;
104         uint8_t         samples = (sample - ao_baro_sample) & (AO_ADC_RING - 1);
105
106         if (samples > 12) {
107                 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_ADC_RING - 1);
108                 samples = 12;
109         }
110         telemetry.generic.tick = ao_adc_ring[sample].tick;
111         telemetry.generic.type = AO_TELEMETRY_BARO;
112         telemetry.baro.samples = samples;
113         for (sample = 0; sample < samples; sample++) {
114                 telemetry.baro.baro[sample] = ao_adc_ring[ao_baro_sample].pres;
115                 ao_baro_sample = ao_adc_ring_next(ao_baro_sample);
116         }
117         ao_radio_send(&telemetry, sizeof (telemetry));
118 }
119 #endif
120
121 static void
122 ao_send_configuration(void)
123 {
124         if (--ao_telemetry_config_cur <= 0)
125         {
126                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
127                 telemetry.configuration.device = AO_idProduct_NUMBER;
128                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
129                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
130                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
131                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
132                 telemetry.configuration.main_deploy = ao_config.main_deploy;
133                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
134                 ao_xmemcpy (telemetry.configuration.callsign,
135                             ao_config.callsign,
136                             AO_MAX_CALLSIGN);
137                 ao_xmemcpy (telemetry.configuration.version,
138                             CODE_TO_XDATA(ao_version),
139                             AO_MAX_VERSION);
140                 ao_radio_send(&telemetry, sizeof (telemetry));
141                 ao_telemetry_config_cur = ao_telemetry_config_max;
142         }
143 }
144
145 #if HAS_GPS
146 static void
147 ao_send_location(void)
148 {
149         if (--ao_telemetry_loc_cur <= 0)
150         {
151                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
152                 ao_mutex_get(&ao_gps_mutex);
153                 ao_xmemcpy(&telemetry.location.flags,
154                        &ao_gps_data.flags,
155                        26);
156                 ao_mutex_put(&ao_gps_mutex);
157                 ao_radio_send(&telemetry, sizeof (telemetry));
158                 ao_telemetry_loc_cur = ao_telemetry_config_max;
159         }
160 }
161
162 static void
163 ao_send_satellite(void)
164 {
165         if (--ao_telemetry_sat_cur <= 0)
166         {
167                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
168                 ao_mutex_get(&ao_gps_mutex);
169                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
170                 ao_xmemcpy(&telemetry.satellite.sats,
171                        &ao_gps_tracking_data.sats,
172                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
173                 ao_mutex_put(&ao_gps_mutex);
174                 ao_radio_send(&telemetry, sizeof (telemetry));
175                 ao_telemetry_sat_cur = ao_telemetry_config_max;
176         }
177 }
178 #endif
179
180 #if HAS_COMPANION
181 static void
182 ao_send_companion(void)
183 {
184         if (--ao_telemetry_companion_cur <= 0) {
185                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
186                 telemetry.companion.board_id = ao_companion_setup.board_id;
187                 telemetry.companion.update_period = ao_companion_setup.update_period;
188                 telemetry.companion.channels = ao_companion_setup.channels;
189                 ao_mutex_get(&ao_companion_mutex);
190                 ao_xmemcpy(&telemetry.companion.companion_data,
191                        ao_companion_data,
192                        ao_companion_setup.channels * 2);
193                 ao_mutex_put(&ao_companion_mutex);
194                 ao_radio_send(&telemetry, sizeof (telemetry));
195                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
196         }
197 }
198 #endif
199
200 void
201 ao_telemetry(void)
202 {
203         uint16_t        time;
204         int16_t         delay;
205
206         ao_config_get();
207         if (!ao_config.radio_enable)
208                 ao_exit();
209         while (!ao_flight_number)
210                 ao_sleep(&ao_flight_number);
211
212         telemetry.generic.serial = ao_serial_number;
213         for (;;) {
214                 while (ao_telemetry_interval == 0)
215                         ao_sleep(&telemetry);
216                 time = ao_rdf_time = ao_time();
217                 while (ao_telemetry_interval) {
218
219
220 #ifdef AO_SEND_ALL_BARO
221                         ao_send_baro();
222 #endif
223                         ao_send_sensor();
224 #if HAS_COMPANION
225                         if (ao_companion_running)
226                                 ao_send_companion();
227 #endif
228                         ao_send_configuration();
229 #if HAS_GPS
230                         ao_send_location();
231                         ao_send_satellite();
232 #endif
233 #ifndef AO_SEND_ALL_BARO
234                         if (ao_rdf &&
235                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
236                         {
237                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
238                                 ao_radio_rdf(AO_MS_TO_RDF_LEN(AO_RDF_LENGTH_MS));
239                         }
240 #endif
241                         time += ao_telemetry_interval;
242                         delay = time - ao_time();
243                         if (delay > 0) {
244                                 ao_alarm(delay);
245                                 ao_sleep(&telemetry);
246                                 ao_clear_alarm();
247                         }
248                         else
249                                 time = ao_time();
250                 }
251         }
252 }
253
254 void
255 ao_telemetry_set_interval(uint16_t interval)
256 {
257         ao_telemetry_interval = interval;
258
259 #if HAS_COMPANION
260         if (!ao_companion_setup.update_period)
261                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
262         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
263         ao_telemetry_companion_cur = 1;
264 #endif
265
266         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
267 #if HAS_COMPANION
268         ao_telemetry_config_cur = ao_telemetry_companion_cur;
269         if (ao_telemetry_config_max > ao_telemetry_config_cur)
270                 ao_telemetry_config_cur++;
271 #else
272         ao_telemetry_config_cur = 1;
273 #endif
274
275 #if HAS_GPS
276         ao_telemetry_loc_cur = ao_telemetry_config_cur;
277         if (ao_telemetry_config_max > ao_telemetry_loc_cur)
278                 ao_telemetry_loc_cur++;
279         ao_telemetry_sat_cur = ao_telemetry_loc_cur;
280         if (ao_telemetry_config_max > ao_telemetry_sat_cur)
281                 ao_telemetry_sat_cur++;
282 #endif
283         ao_wakeup(&telemetry);
284 }
285
286 void
287 ao_rdf_set(uint8_t rdf)
288 {
289         ao_rdf = rdf;
290         if (rdf == 0)
291                 ao_radio_rdf_abort();
292         else
293                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
294 }
295
296 __xdata struct ao_task  ao_telemetry_task;
297
298 void
299 ao_telemetry_init()
300 {
301         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
302 }