forked from RPiks/pico-hf-oscillator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conswrapper.c
245 lines (223 loc) · 7.61 KB
/
conswrapper.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
///////////////////////////////////////////////////////////////////////////////
//
// Roman Piksaykin [[email protected]], R2BDY, PhD
// https://www.qrz.com/db/r2bdy
//
///////////////////////////////////////////////////////////////////////////////
//
//
// conswrapper.c - Serial console commands processing manager.
//
// DESCRIPTION
// -
//
// PLATFORM
// Raspberry Pi pico.
//
// REVISION HISTORY
// Rev 0.1 23 Dec 2023 Initial revision.
//
// PROJECT PAGE
// https://github.com/RPiks/pico-hf-oscillator
//
// 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hardware/uart.h"
#include "./lib/assert.h"
#include "piodco/piodco.h"
#include "protos.h"
extern PioDco DCO;
/// @brief Console commands manager. Currently available:
/// @brief HELP - Usage.
/// @brief SETFREQ f - Set oscillator output frequency in Hz.
/// @brief SWITCH ON/OFF - Switch output to ON or OFF state.
/// @param cmd Ptr to command.
/// @param narg Argument count.
/// @param params Command params, full string.
void ConsoleCommandsWrapper(char *cmd, int narg, char *params)
{
assert_(cmd);
if(strstr(cmd, "HELP"))
{
printf("\n");
printf("Pico-hf-oscillator project HELP page\n");
printf("Copyright (c) 2023 by Roman Piksaykin\n");
printf("Build date: %s %s\n",__DATE__, __TIME__);
printf("Project official page: github.com/RPiks/pico-hf-oscillator\n");
printf("----------------------------------------------------------\n");
printf("-\n");
printf(" HELP - this page.\n");
printf("-\n");
printf(" STATUS - print system status.\n");
printf("-\n");
printf(" SETFREQ f - set output frequency f in Hz.\n");
printf(" example: SETFREQ 14074010 - set output frequency to 14.074145 MHz.\n");
printf("-\n");
printf(" SWITCH s - enable/disable generation.\n");
printf(" example: SWITCH ON - enable generation.\n");
printf("-\n");
printf(" GPSREC OFF/uart_id,pps_pin,baud - enable/disable GPS receiver connection.\n");
printf(" example: GPSREC 0,3,9600 - enable GPS receiver connection with UART0 & PPS on gpio3, 9600 baud port speed.\n");
printf(" example: GPSREC OFF - disable GPS receiver connection.\n");
return;
} else if(strstr(cmd, "SETFREQ"))
{
if(2 != narg)
{
PushErrorMessage(-1);
return;
}
const uint32_t ui32frq = atol(params);
if(ui32frq < 1000000L || ui32frq > 32333333)
{
PushErrorMessage(-11);
return;
}
PioDCOSetFreq(&DCO, ui32frq, 0U);
printf("\nFrequency is set to %lu+.000 Hz", ui32frq);
return;
} else if(strstr(cmd, "STATUS"))
{
PushStatusMessage();
return;
} else if(strstr(cmd, "SWITCH"))
{
if(2 != narg)
{
PushErrorMessage(-1);
return;
}
if(strstr(params, "ON"))
{
PioDCOStart(&DCO);
printf("\nOutput is enabled");
return;
} else if(strstr(params, "OFF"))
{
PioDCOStop(&DCO);
printf("\nOutput is disabled");
return;
}
} else if(strstr(cmd, "GPSREC"))
{
if(4 == narg)
{
char *p = params;
const uint32_t ui32uart = atol(p);
if(0 != ui32uart && 1 != ui32uart)
{
PushErrorMessage(-12);
return;
}
p += strlen(p) + 1;
if(p)
{
if(strlen(p))
{
const uint32_t ui32pps = atol(p);
p += strlen(p) + 1;
const uint32_t ui32baud = atol(p);
//printf("\nbaud=%s\n", p);
sleep_ms(5);
if(DCO._pGPStime)
{
GPStimeDestroy(&DCO._pGPStime);
printf("\nGPS subsystem is disabled.");
}
GPStimeContext *pGPS = GPStimeInit(ui32uart, ui32baud, ui32pps);
assert_(pGPS);
DCO._pGPStime = pGPS;
printf("\nGPS subsystem is set to UART%lu (%lu baud) & PPS pin%lu", ui32uart, ui32baud, ui32pps);
return;
}
}
PushErrorMessage(-1);
return;
} else if(2 == narg)
{
if(DCO._pGPStime)
{
GPStimeDestroy(&DCO._pGPStime);
printf("\nGPS subsystem is disabled.");
}
return;
}
}
PushErrorMessage(-13);
}
void PushErrorMessage(int id)
{
switch(id)
{
case -1:
printf("\nInvalid argument");
break;
case -11:
printf("\nInvalid frequency");
break;
case -12:
printf("\nInvalid UART id, should be 0 OR 1");
break;
case -13:
printf("\nInvalid command");
break;
default:
printf("\nUnknown error");
break;
}
}
void PushStatusMessage(void)
{
printf("\nPico-hf-oscillator system status\n");
printf("Working freq: %lu Hz + %ld milliHz\n", DCO._ui32_frq_hz, DCO._ui32_frq_millihz);
printf("Output is ");
if(DCO._is_enabled)
{
printf("ENABLED");
}
else
{
printf("DISABLED");
}
if(DCO._pGPStime)
{
printf("\nGPS UART id %d", DCO._pGPStime->_uart_id);
printf("\nGPS UART baud %d", DCO._pGPStime->_uart_baudrate);
printf("\nGPS PPS GPIO pin %d", DCO._pGPStime->_pps_gpio);
printf("\nGPS error count %ld", DCO._pGPStime->_i32_error_count);
printf("\nGPS NAV solution flag %u", DCO._pGPStime->_time_data._u8_is_solution_active);
printf("\nGPS GPRMC receive count %u", DCO._pGPStime->_time_data._u32_nmea_gprmc_count);
printf("\nGPS PPS period %llu", DCO._pGPStime->_time_data._u64_pps_period_1M);
printf("\nGPS frequency correction %lld ppb", DCO._pGPStime->_time_data._i32_freq_shift_ppb);
printf("\nGPS lat %lld deg1e5", DCO._pGPStime->_time_data._i64_lat_100k);
printf("\nGPS lon %lld deg1e5", DCO._pGPStime->_time_data._i64_lon_100k);
}
else
{
printf("\nGPS subsystem hasn't been initialized.");
}
}