/
WSPRbeacon.c
244 lines (213 loc) · 8.74 KB
/
WSPRbeacon.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
///////////////////////////////////////////////////////////////////////////////
//
// Roman Piksaykin [piksaykin@gmail.com], R2BDY
// https://www.qrz.com/db/r2bdy
//
///////////////////////////////////////////////////////////////////////////////
//
//
// WSPRbeacon.c - WSPR beacon - related functions.
//
// DESCRIPTION
// The pico-WSPR-tx project provides WSPR beacon function using only
// Pi Pico board. *NO* additional hardware such as freq.synth required.
//
// HOWTOSTART
// .
//
// PLATFORM
// Raspberry Pi pico.
//
// REVISION HISTORY
//
// Rev 0.1 18 Nov 2023
// Initial release.
//
// PROJECT PAGE
// https://github.com/RPiks/pico-WSPR-tx
//
// LICENCE
// MIT License (http://www.opensource.org/licenses/mit-license.php)
//
// Copyright (c) 2023 by Roman Piksaykin
//
// Permission is hereby granted, free of charge,to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction,including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY,WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////
#include "WSPRbeacon.h"
#include <WSPRutility.h>
#include <maidenhead.h>
/// @brief Initializes a new WSPR beacon context.
/// @param pcallsign HAM radio callsign, 12 chr max.
/// @param pgridsquare Maidenhead locator, 7 chr max.
/// @param txpow_dbm TX power, db`mW.
/// @param pdco Ptr to working DCO.
/// @param dial_freq_hz The begin of working WSPR passband.
/// @param shift_freq_hz The shift of tx freq. relative to dial_freq_hz.
/// @param gpio Pico's GPIO pin of RF output.
/// @return Ptr to the new context.
WSPRbeaconContext *WSPRbeaconInit(const char *pcallsign, const char *pgridsquare, int txpow_dbm,
PioDco *pdco, uint32_t dial_freq_hz, uint32_t shift_freq_hz,
int gpio)
{
assert_(pcallsign);
assert_(pgridsquare);
assert_(pdco);
WSPRbeaconContext *p = calloc(1, sizeof(WSPRbeaconContext));
assert_(p);
strncpy(p->_pu8_callsign, pcallsign, sizeof(p->_pu8_callsign));
strncpy(p->_pu8_locator, pgridsquare, sizeof(p->_pu8_locator));
p->_u8_txpower = txpow_dbm;
p->_pTX = TxChannelInit(682667, 0, pdco);
assert_(p->_pTX);
p->_pTX->_u32_dialfreqhz = dial_freq_hz + shift_freq_hz;
p->_pTX->_i_tx_gpio = gpio;
return p;
}
/// @brief Sets dial (baseband minima) freq.
/// @param pctx Context.
/// @param freq_hz the freq., Hz.
void WSPRbeaconSetDialFreq(WSPRbeaconContext *pctx, uint32_t freq_hz)
{
assert_(pctx);
pctx->_pTX->_u32_dialfreqhz = freq_hz;
}
/// @brief Constructs a new WSPR packet using the data available.
/// @param pctx Context
/// @return 0 if OK.
int WSPRbeaconCreatePacket(WSPRbeaconContext *pctx)
{
assert_(pctx);
wspr_encode(pctx->_pu8_callsign, pctx->_pu8_locator, pctx->_u8_txpower, pctx->_pu8_outbuf);
return 0;
}
/// @brief Sends a prepared WSPR packet using TxChannel.
/// @param pctx Context.
/// @return 0, if OK.
int WSPRbeaconSendPacket(const WSPRbeaconContext *pctx)
{
assert_(pctx);
assert_(pctx->_pTX);
assert_(pctx->_pTX->_u32_dialfreqhz > 500 * kHz);
TxChannelClear(pctx->_pTX);
memcpy(pctx->_pTX->_pbyte_buffer, pctx->_pu8_outbuf, WSPR_SYMBOL_COUNT);
pctx->_pTX->_ix_input = WSPR_SYMBOL_COUNT;
return 0;
}
/// @brief Arranges WSPR sending in accordance with pre-defined schedule.
/// @brief It works only if GPS receiver available (for now).
/// @param pctx Ptr to Context.
/// @param verbose Whether stdio output is needed.
/// @return 0 if OK, -1 if NO GPS received available
int WSPRbeaconTxScheduler(WSPRbeaconContext *pctx, int verbose)
{
assert_(pctx);
const uint64_t u64tmnow = GetUptime64();
const uint32_t is_GPS_available = pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u32_nmea_gprmc_count;
const uint32_t is_GPS_active = pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u8_is_solution_active;
const uint32_t is_GPS_override = pctx->_txSched._u8_tx_GPS_past_time == YES;
const uint64_t u64_GPS_last_age_sec
= (u64tmnow - pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u64_sysclk_nmea_last) / 1000000ULL;
if(!is_GPS_available)
{
if(verbose) StampPrintf("WSPR> Waiting for GPS receiver...");
return -1;
}
if(is_GPS_active || (pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u32_utime_nmea_last &&
is_GPS_override && u64_GPS_last_age_sec < WSPR_MAX_GPS_DISCONNECT_TM))
{
const uint32_t u32_unixtime_now
= pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u32_utime_nmea_last + u64_GPS_last_age_sec;
const int isec_of_hour = u32_unixtime_now % HOUR;
const int islot_number = isec_of_hour / (2 * MINUTE);
const int islot_modulo = islot_number % pctx->_txSched._u8_tx_slot_skip;
static int itx_trigger = 0;
if(ZERO == islot_modulo)
{
if(!itx_trigger)
{
itx_trigger = 1;
if(verbose) StampPrintf("WSPR> Start TX.");
PioDCOStart(pctx->_pTX->_p_oscillator);
WSPRbeaconCreatePacket(pctx);
sleep_ms(100);
WSPRbeaconSendPacket(pctx);
}
}
else
{
itx_trigger = 0;
if(verbose) StampPrintf("WSPR> Passive TX slot.");
PioDCOStop(pctx->_pTX->_p_oscillator);
}
}
return 0;
}
/// @brief Dumps the beacon context to stdio.
/// @param pctx Ptr to Context.
void WSPRbeaconDumpContext(const WSPRbeaconContext *pctx)
{
assert_(pctx);
assert_(pctx->_pTX);
const uint64_t u64tmnow = GetUptime64();
const uint64_t u64_GPS_last_age_sec
= (u64tmnow - pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u64_sysclk_nmea_last) / 1000000ULL;
StampPrintf("__________________");
StampPrintf("=TxChannelContext=");
StampPrintf("ftc:%llu", pctx->_pTX->_tm_future_call);
StampPrintf("ixi:%u", pctx->_pTX->_ix_input);
StampPrintf("dfq:%lu", pctx->_pTX->_u32_dialfreqhz);
StampPrintf("gpo:%u", pctx->_pTX->_i_tx_gpio);
GPStimeContext *pGPS = pctx->_pTX->_p_oscillator->_pGPStime;
const uint32_t u32_unixtime_now
= pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u32_utime_nmea_last + u64_GPS_last_age_sec;
assert_(pGPS);
StampPrintf("=GPStimeContext=");
StampPrintf("err:%ld", pGPS->_i32_error_count);
StampPrintf("ixw:%lu", pGPS->_u8_ixw);
StampPrintf("sol:%u", pGPS->_time_data._u8_is_solution_active);
StampPrintf("unl:%lu", pGPS->_time_data._u32_utime_nmea_last);
StampPrintf("snl:%llu", pGPS->_time_data._u64_sysclk_nmea_last);
StampPrintf("age:%llu", u64_GPS_last_age_sec);
StampPrintf("utm:%lu", u32_unixtime_now);
StampPrintf("rmc:%lu", pGPS->_time_data._u32_nmea_gprmc_count);
StampPrintf("pps:%llu", pGPS->_time_data._u64_sysclk_pps_last);
StampPrintf("ppb:%lld", pGPS->_time_data._i32_freq_shift_ppb);
}
/// @brief Extracts maidenhead type QTH locator (such as KO85) using GPS coords.
/// @param pctx Ptr to WSPR beacon context.
/// @return ptr to string of QTH locator (static duration object inside get_mh).
/// @remark It uses third-party project https://github.com/sp6q/maidenhead .
char *WSPRbeaconGetLastQTHLocator(const WSPRbeaconContext *pctx)
{
assert_(pctx);
assert_(pctx->_pTX);
assert_(pctx->_pTX->_p_oscillator);
assert_(pctx->_pTX->_p_oscillator->_pGPStime);
const double lat = 1e-5 * (double)pctx->_pTX->_p_oscillator->_pGPStime->_time_data._i64_lat_100k;
const double lon = 1e-5 * (double)pctx->_pTX->_p_oscillator->_pGPStime->_time_data._i64_lon_100k;
return get_mh(lat, lon, 8);
}
uint8_t WSPRbeaconIsGPSsolutionActive(const WSPRbeaconContext *pctx)
{
assert_(pctx);
assert_(pctx->_pTX);
assert_(pctx->_pTX->_p_oscillator);
assert_(pctx->_pTX->_p_oscillator->_pGPStime);
return YES == pctx->_pTX->_p_oscillator->_pGPStime->_time_data._u8_is_solution_active;
}