altos: Use other TeleDongle LED for CRC-invalid packet reporting
[fw/altos] / src / kernel / ao_monitor.c
1 /*
2  * Copyright © 2009 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_telem.h"
20 #include "ao_flight.h"
21
22 #if !HAS_MONITOR
23 #error Must define HAS_MONITOR to 1
24 #endif
25
26 #ifndef LEGACY_MONITOR
27 #error Must define LEGACY_MONITOR
28 #endif
29
30 #ifndef HAS_MONITOR_PUT
31 #define HAS_MONITOR_PUT 1
32 #endif
33
34 #ifndef AO_MONITOR_LED
35 #error Must define AO_MONITOR_LED
36 #endif
37
38 __data uint8_t ao_monitoring;
39 static __data uint8_t ao_monitor_disabled;
40 static __data uint8_t ao_internal_monitoring;
41 static __data uint8_t ao_external_monitoring;
42
43 __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
44
45 __data uint8_t  ao_monitor_head;
46
47 static void
48 _ao_monitor_adjust(void)
49 {
50         if (ao_monitoring)
51                 ao_radio_recv_abort();
52         if (ao_monitor_disabled)
53                 ao_monitoring = 0;
54         else {
55                 if (ao_external_monitoring)
56                         ao_monitoring = ao_external_monitoring;
57                 else
58                         ao_monitoring = ao_internal_monitoring;
59         }
60         ao_wakeup(DATA_TO_XDATA(&ao_monitoring));
61 }
62
63 void
64 ao_monitor_get(void)
65 {
66         uint8_t size;
67
68         for (;;) {
69                 switch (ao_monitoring) {
70                 case 0:
71                         ao_sleep(DATA_TO_XDATA(&ao_monitoring));
72                         continue;
73 #if LEGACY_MONITOR
74                 case AO_MONITORING_ORIG:
75                         size = sizeof (struct ao_telemetry_orig_recv);
76                         break;
77 #endif
78                 default:
79                         if (ao_monitoring > AO_MAX_TELEMETRY)
80                                 ao_monitoring = AO_MAX_TELEMETRY;
81                         size = ao_monitoring;
82                         break;
83                 }
84                 if (!ao_radio_recv(&ao_monitor_ring[ao_monitor_head], size + 2, 0))
85                         continue;
86                 ao_monitor_head = ao_monitor_ring_next(ao_monitor_head);
87                 ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
88         }
89 }
90
91 #if AO_MONITOR_LED
92 __xdata struct ao_task ao_monitor_blink_task;
93
94 void
95 ao_monitor_blink(void)
96 {
97 #ifdef AO_MONITOR_BAD
98         uint8_t         *recv;
99 #endif
100         for (;;) {
101                 ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
102 #ifdef AO_MONITOR_BAD
103                 recv = (uint8_t *) &ao_monitor_ring[ao_monitor_ring_prev(ao_monitor_head)];
104                 if (ao_monitoring && !(recv[ao_monitoring + 1] & AO_RADIO_STATUS_CRC_OK))
105                         ao_led_for(AO_MONITOR_BAD, AO_MS_TO_TICKS(100));
106                 else
107 #endif
108                         ao_led_for(AO_MONITOR_LED, AO_MS_TO_TICKS(100));
109         }
110 }
111 #endif
112
113 #if HAS_MONITOR_PUT
114
115 static const char xdigit[16] = {
116         '0', '1', '2', '3', '4', '5', '6', '7',
117         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
118 };
119
120 #define hex(c) do { putchar(xdigit[(c) >> 4]); putchar(xdigit[(c)&0xf]); } while (0)
121
122 void
123 ao_monitor_put(void)
124 {
125 #if LEGACY_MONITOR
126         __xdata char callsign[AO_MAX_CALLSIGN+1];
127 #endif
128 #if LEGACY_MONITOR || HAS_RSSI
129         int16_t rssi;
130 #endif
131         uint8_t ao_monitor_tail;
132         uint8_t state;
133         uint8_t sum, byte;
134         __xdata union ao_monitor        *m;
135
136 #define recv_raw        ((m->raw))
137 #define recv_orig       ((m->orig))
138 #define recv_tiny       ((m->tiny))
139
140         ao_monitor_tail = ao_monitor_head;
141         for (;;) {
142                 while (!ao_external_monitoring)
143                         ao_sleep(DATA_TO_XDATA(&ao_external_monitoring));
144                 while (ao_monitor_tail == ao_monitor_head && ao_external_monitoring)
145                         ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
146                 if (!ao_external_monitoring)
147                         continue;
148                 m = &ao_monitor_ring[ao_monitor_tail];
149                 ao_monitor_tail = ao_monitor_ring_next(ao_monitor_tail);
150                 switch (ao_monitoring) {
151                 case 0:
152                         break;
153 #if LEGACY_MONITOR
154                 case AO_MONITORING_ORIG:
155                         state = recv_orig.telemetry_orig.flight_state;
156
157                         rssi = (int16_t) AO_RSSI_FROM_RADIO(recv_orig.rssi);
158                         ao_xmemcpy(callsign, recv_orig.telemetry_orig.callsign, AO_MAX_CALLSIGN);
159                         if (state > ao_flight_invalid)
160                                 state = ao_flight_invalid;
161                         if (recv_orig.status & PKT_APPEND_STATUS_1_CRC_OK) {
162
163                                 /* General header fields */
164                                 printf(AO_TELEM_VERSION " %d "
165                                        AO_TELEM_CALL " %s "
166                                        AO_TELEM_SERIAL " %d "
167                                        AO_TELEM_FLIGHT " %d "
168                                        AO_TELEM_RSSI " %d "
169                                        AO_TELEM_STATE " %s "
170                                        AO_TELEM_TICK " %d ",
171                                        AO_TELEMETRY_VERSION,
172                                        callsign,
173                                        recv_orig.telemetry_orig.serial,
174                                        recv_orig.telemetry_orig.flight,
175                                        rssi,
176                                        ao_state_names[state],
177                                        recv_orig.telemetry_orig.adc.tick);
178
179                                 /* Raw sensor values */
180                                 printf(AO_TELEM_RAW_ACCEL " %d "
181                                        AO_TELEM_RAW_BARO " %d "
182                                        AO_TELEM_RAW_THERMO " %d "
183                                        AO_TELEM_RAW_BATT " %d "
184                                        AO_TELEM_RAW_DROGUE " %d "
185                                        AO_TELEM_RAW_MAIN " %d ",
186                                        recv_orig.telemetry_orig.adc.accel,
187                                        recv_orig.telemetry_orig.adc.pres,
188                                        recv_orig.telemetry_orig.adc.temp,
189                                        recv_orig.telemetry_orig.adc.v_batt,
190                                        recv_orig.telemetry_orig.adc.sense_d,
191                                        recv_orig.telemetry_orig.adc.sense_m);
192
193                                 /* Sensor calibration values */
194                                 printf(AO_TELEM_CAL_ACCEL_GROUND " %d "
195                                        AO_TELEM_CAL_BARO_GROUND " %d "
196                                        AO_TELEM_CAL_ACCEL_PLUS " %d "
197                                        AO_TELEM_CAL_ACCEL_MINUS " %d ",
198                                        recv_orig.telemetry_orig.ground_accel,
199                                        recv_orig.telemetry_orig.ground_pres,
200                                        recv_orig.telemetry_orig.accel_plus_g,
201                                        recv_orig.telemetry_orig.accel_minus_g);
202
203                                 if (recv_orig.telemetry_orig.u.k.unused == 0x8000) {
204                                         /* Kalman state values */
205                                         printf(AO_TELEM_KALMAN_HEIGHT " %d "
206                                                AO_TELEM_KALMAN_SPEED " %d "
207                                                AO_TELEM_KALMAN_ACCEL " %d ",
208                                                recv_orig.telemetry_orig.height,
209                                                recv_orig.telemetry_orig.u.k.speed,
210                                                recv_orig.telemetry_orig.accel);
211                                 } else {
212                                         /* Ad-hoc flight values */
213                                         printf(AO_TELEM_ADHOC_ACCEL " %d "
214                                                AO_TELEM_ADHOC_SPEED " %ld "
215                                                AO_TELEM_ADHOC_BARO " %d ",
216                                                recv_orig.telemetry_orig.accel,
217                                                recv_orig.telemetry_orig.u.flight_vel,
218                                                recv_orig.telemetry_orig.height);
219                                 }
220                                 ao_gps_print(&recv_orig.telemetry_orig.gps);
221                                 ao_gps_tracking_print(&recv_orig.telemetry_orig.gps_tracking);
222                                 putchar('\n');
223 #if HAS_RSSI
224                                 ao_rssi_set(rssi);
225 #endif
226                         } else {
227                                 printf("CRC INVALID RSSI %3d\n", rssi);
228                         }
229                         break;
230 #endif /* LEGACY_MONITOR */
231                 default:
232 #if AO_PROFILE
233                 {
234                         extern uint32_t ao_rx_start_tick, ao_rx_packet_tick, ao_rx_done_tick, ao_rx_last_done_tick;
235                         extern uint32_t ao_fec_decode_start, ao_fec_decode_end;
236
237                         printf ("between packet: %d\n", ao_rx_start_tick - ao_rx_last_done_tick);
238                         printf ("receive start delay: %d\n", ao_rx_packet_tick - ao_rx_start_tick);
239                         printf ("decode time: %d\n", ao_fec_decode_end - ao_fec_decode_start);
240                         printf ("rx cleanup: %d\n", ao_rx_done_tick - ao_fec_decode_end);
241                 }
242 #endif
243                         printf("TELEM ");
244                         hex((uint8_t) (ao_monitoring + 2));
245                         sum = 0x5a;
246                         for (state = 0; state < ao_monitoring + 2; state++) {
247                                 byte = recv_raw.packet[state];
248                                 sum += byte;
249                                 hex(byte);
250                         }
251                         hex(sum);
252                         putchar ('\n');
253 #if HAS_RSSI
254                         if (recv_raw.packet[ao_monitoring + 1] & AO_RADIO_STATUS_CRC_OK) {
255                                 rssi = AO_RSSI_FROM_RADIO(recv_raw.packet[ao_monitoring]);
256                                 ao_rssi_set(rssi);
257                         }
258 #endif
259                         break;
260                 }
261                 ao_usb_flush();
262         }
263 }
264
265 __xdata struct ao_task ao_monitor_put_task;
266 #endif
267
268 __xdata struct ao_task ao_monitor_get_task;
269
270 void
271 ao_monitor_set(uint8_t monitoring)
272 {
273         ao_internal_monitoring = monitoring;
274         _ao_monitor_adjust();
275 }
276
277 void
278 ao_monitor_disable(void)
279 {
280         ++ao_monitor_disabled;
281         _ao_monitor_adjust();
282 }
283
284 void
285 ao_monitor_enable(void)
286 {
287         --ao_monitor_disabled;
288         _ao_monitor_adjust();
289 }
290
291 #if HAS_MONITOR_PUT
292 static void
293 set_monitor(void)
294 {
295         ao_cmd_hex();
296         ao_external_monitoring = ao_cmd_lex_i;
297         ao_wakeup(DATA_TO_XDATA(&ao_external_monitoring));
298         ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
299         _ao_monitor_adjust();
300 }
301
302 __code struct ao_cmds ao_monitor_cmds[] = {
303         { set_monitor,  "m <0 off, 1 old, 20 std>\0Set radio monitoring" },
304         { 0,    NULL },
305 };
306 #endif
307
308 void
309 ao_monitor_init(void) __reentrant
310 {
311 #if HAS_MONITOR_PUT
312         ao_cmd_register(&ao_monitor_cmds[0]);
313         ao_add_task(&ao_monitor_put_task, ao_monitor_put, "monitor_put");
314 #endif
315         ao_add_task(&ao_monitor_get_task, ao_monitor_get, "monitor_get");
316 #if AO_MONITOR_LED
317         ao_add_task(&ao_monitor_blink_task, ao_monitor_blink, "monitor_blink");
318 #endif
319 }