-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaprs.c
289 lines (259 loc) · 6.38 KB
/
aprs.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
#ifndef TEST
#include <msp430.h>
#include "hw.h"
#include "si4060.h"
#include "main.h"
#endif
#include <inttypes.h>
#include <math.h>
#include "aprs.h"
#include "fix.h"
#include "string.h"
/*
* the APRS data buffer
* contains ASCII data
*/
char aprs_buf[APRS_BUF_LEN] = "/ddhhmmz/xxxxyyyyOaa1|ss001122|";
extern volatile uint16_t aprs_bit;
extern volatile uint16_t aprs_tick;
extern volatile uint16_t aprs_baud_tick;
const unsigned char aprs_header[APRS_HEADER_LEN] = {
'A'*2, 'P'*2, 'R'*2, 'S'*2, ' '*2, ' '*2, SSID_RESC + (DST_SSID << 1),
'D'*2, 'K'*2, '3'*2, 'S'*2, 'B'*2, ' '*2, SSID_RESC + (SRC_SSID << 1),
'W'*2, 'I'*2, 'D'*2, 'E'*2, '1'*2, ' '*2, SSID_RESC + (WIDE_SSID << 1) + HEADER_END,
CONTROL_UI, PID_NONE};
enum aprs_states {SM_INIT, SFLAG, AX25_HEADER, AX25_DATA, AX25_FCS1, AX25_FCS2, EFLAG};
volatile uint16_t aprs_state = SM_INIT;
volatile uint16_t fcs = 0;
volatile uint8_t bitcnt = 8;
volatile uint8_t onecnt = 0;
volatile uint8_t finished = 0;
volatile uint8_t stuffing = 0;
#ifndef TEST
inline void calculate_fcs(void) {
int i;
CRCINIRES = 0xffff;
for (i = 0; i < APRS_HEADER_LEN; i++) {
CRCDI_L = aprs_header[i];
}
for (i = 0; i < APRS_BUF_LEN; i++) {
CRCDI_L = aprs_buf[i];
}
fcs = CRCRESR ^ 0xffff;
}
#else
void calculate_fcs(void) {}
#endif
/*
* base91_encode
*
* encodes one short value for the telemetry extension to base91
* does not work for positions etc. this way yet!
*
*/
void base91_encode_tlm(char *buf, uint16_t value) {
value = value % 8281;
buf[0] = 33 + (value / 91);
buf[1] = 33 + (value % 91);
}
void base91_encode_latlon(char *buf, uint32_t value) {
buf[0] = 33 + (value / ((uint32_t)91*91*91));
value = value % ((uint32_t)91*91*91);
buf[1] = 33 + (value / (91*91));
value = value % (91*91);
buf[2] = 33 + (value / 91);
buf[3] = 33 + (value % 91);
}
/*
* aprs_prepare_buffer
*
* prepares the buffer for APRS transmission with the fix given as a reference.
* checks for validity of the fix, does not change the buffer if the fix is unsuitable for transmission.
*
* backlog_fix contains a flag marking the fix as a backlog fix, containing a zero TLM sequence ID
*
* always transmits the latest temperature / battery voltage, no historical values!
*
*/
inline void aprs_prepare_buffer(struct gps_fix* fix, uint8_t backlog_fix) {
int16_t temp_aprs = 0;
uint16_t seq_tmp;
static uint16_t aprs_seqnum = 0;
if (fix->type < 3)
return;
i16toa(fix->day, 2, &aprs_buf[APRS_TIME_START]);
i16toa(fix->hour, 2, &aprs_buf[APRS_TIME_START + 2]);
i16toa(fix->min, 2, &aprs_buf[APRS_TIME_START + 4]);
base91_encode_latlon(&aprs_buf[APRS_LAT_START], 380926.0f * (90.0f - (float)fix->lat/10000000.0f));
base91_encode_latlon(&aprs_buf[APRS_LON_START], 190463.0f * (180.0f + (float)fix->lon/10000000.0f));
base91_encode_tlm(&aprs_buf[APRS_ALT_START], logf((float)fix->alt * 3.28f)/logf(1.002f));
if (backlog_fix) {
seq_tmp = 0;
} else {
aprs_seqnum = (aprs_seqnum + 1) % 8281;
seq_tmp = aprs_seqnum;
}
temp_aprs = fix->temperature_int + APRS_TLM_TEMP_OFFSET;
base91_encode_tlm(&aprs_buf[APRS_SEQ_START], seq_tmp);
base91_encode_tlm(&aprs_buf[APRS_TEMP_START], (uint16_t)temp_aprs);
base91_encode_tlm(&aprs_buf[APRS_VOLT_START], fix->voltage_bat);
base91_encode_tlm(&aprs_buf[APRS_VSOL_START], fix->voltage_sol);
calculate_fcs();
}
/*
* aprs_init
*
* prepares the state machine for transmission of the next APRS frame
*/
void aprs_init(void) {
aprs_state = SM_INIT;
finished = 0;
bitcnt = 8;
onecnt = 0;
stuffing = 0;
}
/*
* get_next_byte
*
* fetches the next byte to be transmitted, until there is none left. it advances the
* internal AX.25 state machine. first, a configurable number of flags are returned,
* which are followed by the header, the payload, and end flags. an infinite number of
* end flags are returned, until the state machine is reset by init_aprs()
*
* returns:
* byte to be transmitted
*/
uint8_t get_next_byte(void) {
static int i = 0;
uint8_t retval;
switch (aprs_state) {
case SM_INIT:
stuffing = 0;
aprs_state = SFLAG;
i = 0;
case SFLAG:
retval = AX25_FLAG;
if (++i >= AX25_SFLAGS) {
aprs_state = AX25_HEADER;
i = 0;
}
break;
case AX25_HEADER:
stuffing = 1;
retval = aprs_header[i];
if (++i >= APRS_HEADER_LEN) {
aprs_state = AX25_DATA;
i = 0;
}
break;
case AX25_DATA:
retval = aprs_buf[i];
if (++i >= APRS_BUF_LEN) {
aprs_state = AX25_FCS1;
i = 0;
}
break;
case AX25_FCS1:
retval = (uint8_t)fcs;
aprs_state = AX25_FCS2;
break;
case AX25_FCS2:
retval = (uint8_t)(fcs >> 8);
aprs_state = EFLAG;
break;
case EFLAG:
stuffing = 0;
retval = AX25_FLAG;
if (++i > AX25_EFLAGS) {
finished = 1;
i = 0;
}
break;
default:
retval = AX25_FLAG;
}
return retval;
}
/*
* get_next_bit
*
* fetches the next bit for the data stream.
*
* returns: the next bit to be transmitted, already NRZI coded and bit stuffed
*/
uint8_t get_next_bit(void) {
static uint8_t byte = 0;
static uint8_t bit = 1;
static uint8_t bit_d = 0;
if (bitcnt >= 8) {
byte = get_next_byte();
bitcnt = 0;
}
/* all bytes LSB first */
bit = byte & 0x01;
if (bit) {
/* bit stuffing */
onecnt++;
if ((stuffing == 1) && (onecnt >= 5)) {
/* next time the same bit will be a zero -> stuffed bit */
byte &= ~0x01;
onecnt = 0;
} else {
byte >>= 1;
bitcnt = bitcnt + 1;
}
} else {
/* grab next bit only if not stuffed */
onecnt = 0;
byte >>= 1;
bitcnt = bitcnt + 1;
}
/* NRZI encoding */
if (bit == 0) {
bit_d ^= 0x01;
}
return bit_d;
}
#ifndef TEST
/*
* tx_aprs
*
* transmits an APRS packet.
*
*/
void tx_aprs(void) {
aprs_init();
aprs_timer_enable();
/* use 2FSK mode so we can adjust the OFFSET register */
si4060_setup(MOD_TYPE_2GFSK);
si4060_start_tx(0);
/* add some TX delay */
__delay_cycles(300000);
aprs_tick = 0;
do {
if (aprs_tick) {
/* running with APRS sample clock */
aprs_tick = 0;
P1OUT ^= SI_DATA;
if (aprs_baud_tick) {
/* running with bit clock (1200 / sec) */
WDTCTL = WDTPW + WDTCNTCL + WDTIS1;
aprs_baud_tick = 0;
if (get_next_bit()) {
aprs_bit = APRS_SPACE;
} else {
aprs_bit = APRS_MARK;
}
}
/* tell us when we fail to meet the timing */
if (aprs_tick) {
while(1) {
WDTCTL = WDTPW + WDTCNTCL + WDTIS1;
}
}
}
} while(!finished);
si4060_stop_tx();
aprs_timer_disable();
}
#endif