forked from thasti/utrak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
389 lines (363 loc) · 10.1 KB
/
main.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* main tracker software
*
* Stefan Biereigel
*
*/
#include <msp430.h>
#include <inttypes.h>
#include "gps.h"
#include "main.h"
#include "si4060.h"
#include "spi.h"
#include "string.h"
#include "aprs.h"
#include "hw.h"
#include "tlm.h"
#include "rtty.h"
#include "geofence.h"
#include "backlog.h"
/*
* GLOBAL VARIABLES
*/
/*
* housekeeping and interrupt communication variables
*
* tick flags are set by ISRs and reset by the main software
*/
volatile uint16_t seconds = 0; /* timekeeping via timer */
volatile uint16_t timeout_seconds = 0; /* timeout counter */
volatile uint16_t tlm_tick = 0; /* flag for slow telemetry handling (ISR -> main) */
volatile uint16_t aprs_tick = 0; /* flag for APRS handling (ISR -> main) */
volatile uint16_t aprs_baud_tick = 0; /* flag for APRS baud rate (ISR -> main) */
volatile uint16_t aprs_bit = APRS_SPACE;/* the currently transmitted tone frequency (main -> ISR) */
volatile uint16_t servo_dir = 0; /* current servo direction */
/*
* the TX data buffer
* contains ASCII data, which is either transmitted as CW oder RTTY
*/
uint16_t tx_buf_rdy = 0; /* the read-flag (main -> main) */
uint16_t tx_buf_length = 0; /* how many chars to send */
char tx_buf[TX_BUF_MAX_LENGTH] = {SYNC_PREFIX "$$" PAYLOAD_NAME ","}; /* the actual buffer */
/* current (latest) GPS fix and measurements */
struct gps_fix current_fix;
void get_fix_and_measurements(void) {
gps_get_fix(¤t_fix);
current_fix.temperature_int = get_die_temperature();
current_fix.voltage_bat = get_battery_voltage();
current_fix.voltage_sol = timeout_seconds;//get_solar_voltage();
}
int main(void) {
uint16_t i;
uint16_t backlog_transmitted = 0;
struct gps_fix *backlog_fix = 0;
enum {up, beep, cut, down} rls_state = up;
enum {TX_RTTY, TX_APRS} tlm_state = TX_RTTY;
uint16_t beep_cnt = 0;
/* set watchdog timer interval to 11 seconds */
/* reset occurs if Si4060 does not respond or software locks up */
WDTCTL = WDTPW + WDTCNTCL + WDTIS1;
/* init all hardware components */
hw_init();
/* initialize the transmission buffer (for development only) */
init_tx_buffer();
/* reset the radio chip from shutdown */
si4060_reset();
/* check radio communication */
i = si4060_part_info();
if (i != 0x4060) {
while(1);
}
si4060_power_up();
si4060_setup(MOD_TYPE_OOK);
WDTCTL = WDTPW + WDTCNTCL + WDTIS1;
gps_startup_delay();
while(!(gps_disable_nmea_output()));
while(!(gps_set_gps_only()));
while(!(gps_set_airborne_model()));
while(!(gps_set_power_save()));
while(!(gps_power_save(0)));
while(!(gps_save_settings()));
/* APRS test code (for measurement of tone frequencies and spectra) */
/*
while(1) {
si4060_freq_aprs_eu();
tx_aprs();
if (aprs_bit == APRS_MARK) {
aprs_bit = APRS_SPACE;
} else {
aprs_bit = APRS_MARK;
}
}
*/
/* power up the Si4060 and set it to OOK, for transmission of blips */
si4060_setup(MOD_TYPE_OOK);
si4060_freq_rtty();
si4060_start_tx(0);
si4060_setup(MOD_TYPE_OOK);
/* TODO remove before flight */
/*backlog_invalidate_fixes(); */
servo_dir = 0;
P1OUT |= SERVO_EN;
/* the tracker outputs RF blips while waiting for a GPS fix */
while (current_fix.num_svs < 5 && current_fix.type < 3) {
WDTCTL = WDTPW + WDTCNTCL + WDTIS0;
if (seconds > BLIP_FIX_INTERVAL) {
seconds = 0;
gps_get_fix(¤t_fix);
tx_blips(1);
} else {
tx_blips(0);
}
}
P1OUT &= ~SERVO_EN;
timeout_seconds = 0;
si4060_stop_tx();
/* modulation from now on will be RTTY */
si4060_setup(MOD_TYPE_2FSK);
/* activate power save mode as fix is stable */
gps_power_save(1);
seconds = TLM_APRS_INTERVAL + 1;
/* entering operational state */
/* in fixed intervals, a new TX buffer is prepared and transmitted */
/* watchdog timer is active for resets, if somethings locks up */
si4060_freq_rtty();
while(1) {
WDTCTL = WDTPW + WDTCNTCL + WDTIS0;
#ifdef TLM_RTTY_APRS /* APRS and RTTY transmission */
switch (tlm_state) {
case TX_RTTY:
/* backlog transmission */
if (seconds > TLM_BACKLOG_OFFSET && backlog_transmitted == 0) {
backlog_fix = backlog_get_next_fix();
if (backlog_fix != 0) {
aprs_prepare_buffer(backlog_fix, 1);
geofence_aprs_frequency(¤t_fix);
tx_aprs();
}
backlog_transmitted = 1;
}
/* regular APRS transmission, start of RTTY transmission */
if ((!tx_buf_rdy) && (seconds > TLM_RTTY_INTERVAL)) {
get_fix_and_measurements();
backlog_add_fix(¤t_fix);
seconds = 0;
if (current_fix.type > 2) {
prepare_tx_buffer();
aprs_prepare_buffer(¤t_fix, 0);
}
geofence_aprs_frequency(¤t_fix);
tx_aprs();
si4060_freq_rtty();
/* possible switchover to APRS only */
if (!(geofence_slow_tlm_altitude(¤t_fix))) {
tlm_state = TX_APRS;
/* set the tx buffer to not ready to inhibit tx_rtty() from sending */
tx_buf_rdy = 0;
} else {
tx_buf_rdy = 1; /* starts the RTTY transmission */
}
backlog_transmitted = 0;
}
tx_rtty();
break;
case TX_APRS:
/* backlog transmission */
if (seconds > TLM_BACKLOG_OFFSET && backlog_transmitted == 0) {
backlog_fix = backlog_get_next_fix();
if (backlog_fix != 0) {
aprs_prepare_buffer(backlog_fix, 1);
geofence_aprs_frequency(¤t_fix);
tx_aprs();
}
backlog_transmitted = 1;
}
/* regular APRS transmission */
if (seconds > TLM_APRS_INTERVAL) {
get_fix_and_measurements();
backlog_add_fix(¤t_fix);
seconds = 0;
if (current_fix.type > 2) {
/* if no current fix is available, the old fix is transmitted again */
aprs_prepare_buffer(¤t_fix, 0);
}
geofence_aprs_frequency(¤t_fix);
tx_aprs();
/* possible switchover to RTTY + APRS transmission */
if (geofence_slow_tlm_altitude(¤t_fix)) {
tlm_state = TX_RTTY;
}
backlog_transmitted = 0;
}
break;
default:
tlm_state = TX_RTTY;
break;
} /* switch (tlm_state) */
#endif
#ifdef TLM_RTTY_ONLY
switch(rls_state) {
case up:
if ((!tx_buf_rdy) && (seconds > TLM_RTTY_INTERVAL)) {
get_fix_and_measurements();
seconds = 0;
if (current_fix.type > 2) {
prepare_tx_buffer();
}
si4060_freq_rtty();
tx_buf_rdy = 1; /* starts the RTTY transmission */
}
tx_rtty();
if ((!tx_buf_rdy) && (timeout_seconds > TIMEOUT)) {
rls_state = cut;
seconds = 0;
}
break;
case cut:
// enable servo
P1OUT |= SERVO_EN;
servo_dir = 1;
if (seconds > 3) {
rls_state = down;
P1OUT &= ~SERVO_EN;
//disable servo
}
break;
case down:
if ((!tx_buf_rdy) && (seconds > TLM_RTTY_INTERVAL)) {
get_fix_and_measurements();
seconds = 0;
if (current_fix.type > 2) {
prepare_tx_buffer();
}
si4060_freq_rtty();
tx_buf_rdy = 1; /* starts the RTTY transmission */
}
tx_rtty();
break;
}
#endif
#ifdef TLM_APRS_ONLY
/* backlog transmission */
if (seconds > TLM_BACKLOG_OFFSET && backlog_transmitted == 0) {
backlog_fix = backlog_get_next_fix();
if (backlog_fix != 0) {
aprs_prepare_buffer(backlog_fix, 1);
geofence_aprs_frequency(¤t_fix);
tx_aprs();
}
backlog_transmitted = 1;
}
/* regular APRS transmission */
if (seconds > TLM_APRS_INTERVAL) {
get_fix_and_measurements();
backlog_add_fix(¤t_fix);
seconds = 0;
if (current_fix.type > 2) {
/* if no current fix is available, the old fix is transmitted again */
aprs_prepare_buffer(¤t_fix, 0);
}
geofence_aprs_frequency(¤t_fix);
tx_aprs();
backlog_transmitted = 0;
}
#endif
} /* while(1) */
} /* main() */
/*
* Timer0 A0 ISR
* CCR0 compare interrupt (highest priority)
*
* generates the various baud rates and clocks needed in the design
*/
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR (void)
{
static uint16_t aprs_nco_count = 0;
static uint16_t aprs_bit_count = 0;
aprs_nco_count++;
aprs_bit_count++;
if (aprs_bit == APRS_SPACE && aprs_nco_count >= APRS_SPACE_TICKS) {
aprs_tick = 1;
aprs_nco_count = 0;
}
if (aprs_bit == APRS_MARK && aprs_nco_count >= APRS_MARK_TICKS) {
aprs_tick = 1;
aprs_nco_count = 0;
}
if (aprs_bit_count == APRS_BAUD_TICKS) {
aprs_baud_tick = 1;
aprs_bit_count = 0;
}
TA0CCR0 += N_APRS_NCO - 1;
}
/*
* Timer0 A1 ISR
* Realizes APRS baud rate, telemetry baud rate and second counter
* as msp430-gcc does not support the __even_in_range-intrinsic,
* we use this inline assembly routine for the vectoring.
*
* source: http://sourceforge.net/p/mspgcc/mailman/message/25525509/
*/
#pragma vector = TIMER0_A1_VECTOR
__interrupt void timera0x_handler(void)
{
__asm__ __volatile__("add %[src] ,r0 ":: [src] "m" (TA0IV));
__asm__ __volatile__("reti "::); // NO INT
__asm__ __volatile__("jmp timera0_cc1_handler "::); // CC1
__asm__ __volatile__("jmp timera0_cc2_handler "::); // CC2
__asm__ __volatile__("reti "::); // RESERVED
__asm__ __volatile__("reti "::); // RESERVED
__asm__ __volatile__("reti "::); // RESERVED
__asm__ __volatile__("reti "::); // RESERVED
__asm__ __volatile__("jmp timera0_ifg_handler "::); // IFG
}
/*
* CCR1, generates servo PWM
*/
__interrupt void timera0_cc1_handler(void)
{
static uint16_t pwm = 0;
if (pwm) {
if (servo_dir) {
TA0CCR1 += N_PWM_LO_L;
} else {
TA0CCR1 += N_PWM_LO_R;
}
P1OUT |= SERVO;
} else {
if (servo_dir) {
TA0CCR1 += N_PWM_HI_L;
} else {
TA0CCR1 += N_PWM_HI_R;
}
P1OUT &= ~SERVO;
}
pwm = ~pwm;
}
/*
* CCR2, generates RTTY baud rate
*/
__interrupt void timera0_cc2_handler(void)
{
static uint16_t sec_overflows = 0; /* overflow counter for second generation */
static uint16_t cc2_overflow = 0; /* CC2 has to overflow twice for one tlm period */
TA0CCR2 += N_TLM;
cc2_overflow = ~cc2_overflow;
if (cc2_overflow) {
tlm_tick = 1;
sec_overflows++;
if (sec_overflows >= TLM_HZ) {
seconds++;
timeout_seconds++;
sec_overflows = 0;
}
}
}
/*
* Overflow
*/
__interrupt void timera0_ifg_handler(void)
{
/* unused */
}