-
Notifications
You must be signed in to change notification settings - Fork 4
/
SerialPort.h
398 lines (350 loc) · 11.6 KB
/
SerialPort.h
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
389
390
391
392
393
394
395
396
397
398
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Hotspot Firmware
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2015,2016,2018,2020,2021 Jonathan Naylor, G4KLX
* Copyright (C) 2018 Andy Uribe, CA6JAU
* Copyright (C) 2018,2021-2024 Bryan Biedenkapp, N2PLL
*
*/
/**
* @file SerialPort.h
* @ingroup hotspot_fw
* @file SerialPort.cpp
* @ingroup hotspot_fw
* @file SerialSTM.cpp
* @ingroup hotspot_fw
*/
#if !defined(__SERIAL_PORT_H__)
#define __SERIAL_PORT_H__
#include "Defines.h"
#include "Globals.h"
#include "SerialBuffer.h"
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/**
* @addtogroup modem
* @{
*/
/**
* @brief Modem operation states.
*/
enum DVM_STATE {
STATE_IDLE = 0U, //! Idle
// DMR
STATE_DMR = 1U, //! DMR
// Project 25
STATE_P25 = 2U, //! Project 25
// NXDN
STATE_NXDN = 3U, //! NXDN
// CW
STATE_CW = 10U, //! Continuous Wave
// Calibration States
STATE_INT_CAL = 90U, //!
STATE_P25_CAL_1K = 92U, //! Project 25 Calibration 1K
STATE_DMR_DMO_CAL_1K = 93U, //! DMR DMO Calibration 1K
STATE_DMR_CAL_1K = 94U, //! DMR Calibration 1K
STATE_DMR_LF_CAL = 95U, //! DMR Low Frequency Calibration
STATE_RSSI_CAL = 96U, //! RSSI Calibration
STATE_P25_CAL = 97U, //! Project 25 Calibration
STATE_DMR_CAL = 98U, //! DMR Calibration
STATE_NXDN_CAL = 99U //! NXDN Calibration
};
/**
* @brief Modem commands.
*/
enum DVM_COMMANDS {
CMD_GET_VERSION = 0x00U, //! Get Modem Version
CMD_GET_STATUS = 0x01U, //! Get Modem Status
CMD_SET_CONFIG = 0x02U, //! Set Modem Configuration
CMD_SET_MODE = 0x03U, //! Set Modem Mode
CMD_SET_SYMLVLADJ = 0x04U, //! Set Symbol Level Adjustments
CMD_SET_RXLEVEL = 0x05U, //! Set Rx Level
CMD_SET_RFPARAMS = 0x06U, //! (Hotspot) Set RF Parameters
CMD_CAL_DATA = 0x08U, //! Calibration Data
CMD_RSSI_DATA = 0x09U, //! RSSI Data
CMD_SEND_CWID = 0x0AU, //! Send Continous Wave ID (Morse)
CMD_SET_BUFFERS = 0x0FU, //! Set FIFO Buffer Lengths
CMD_DMR_DATA1 = 0x18U, //! DMR Data Slot 1
CMD_DMR_LOST1 = 0x19U, //! DMR Data Lost Slot 1
CMD_DMR_DATA2 = 0x1AU, //! DMR Data Slot 2
CMD_DMR_LOST2 = 0x1BU, //! DMR Data Lost Slot 2
CMD_DMR_SHORTLC = 0x1CU, //! DMR Short Link Control
CMD_DMR_START = 0x1DU, //! DMR Start Transmit
CMD_DMR_ABORT = 0x1EU, //! DMR Abort
CMD_DMR_CACH_AT_CTRL = 0x1FU, //! DMR Set CACH AT Control
CMD_DMR_CLEAR1 = 0x20U, //! DMR Clear Slot 1 Buffer
CMD_DMR_CLEAR2 = 0x21U, //! DMR Clear Slot 2 Buffer
CMD_P25_DATA = 0x31U, //! Project 25 Data
CMD_P25_LOST = 0x32U, //! Project 25 Data Lost
CMD_P25_CLEAR = 0x33U, //! Project 25 Clear Buffer
CMD_NXDN_DATA = 0x41U, //! NXDN Data
CMD_NXDN_LOST = 0x42U, //! NXDN Data Lost
CMD_NXDN_CLEAR = 0x43U, //! NXDN Clear Buffer
CMD_ACK = 0x70U, //! Command ACK
CMD_NAK = 0x7FU, //! Command NACK
CMD_FLSH_READ = 0xE0U, //! Read Flash Partition
CMD_FLSH_WRITE = 0xE1U, //! Write Flash Partition
CMD_RESET_MCU = 0xEAU, //! Soft Reboot MCU
CMD_DEBUG1 = 0xF1U, //!
CMD_DEBUG2 = 0xF2U, //!
CMD_DEBUG3 = 0xF3U, //!
CMD_DEBUG4 = 0xF4U, //!
CMD_DEBUG5 = 0xF5U, //!
CMD_DEBUG_DUMP = 0xFAU, //!
};
/**
* @brief Modem response reason codes.
*/
enum CMD_REASON_CODE {
RSN_OK = 0U, //! OK
RSN_NAK = 1U, //! Negative Acknowledge
RSN_ILLEGAL_LENGTH = 2U, //! Illegal Length
RSN_INVALID_REQUEST = 4U, //! Invalid Request
RSN_RINGBUFF_FULL = 8U, //! Ring Buffer Full
RSN_INVALID_FDMA_PREAMBLE = 10U, //! Invalid FDMA Preamble Length
RSN_INVALID_MODE = 11U, //! Invalid Mode
RSN_INVALID_DMR_CC = 12U, //! Invalid DMR CC
RSN_INVALID_DMR_SLOT = 13U, //! Invalid DMR Slot
RSN_INVALID_DMR_START = 14U, //! Invaild DMR Start Transmit
RSN_INVALID_DMR_RX_DELAY = 15U, //! Invalid DMR Rx Delay
RSN_INVALID_P25_CORR_COUNT = 16U, //! Invalid P25 Correlation Count
RSN_NO_INTERNAL_FLASH = 20U, //! No Internal Flash
RSN_FAILED_ERASE_FLASH = 21U, //! Failed to erase flash partition
RSN_FAILED_WRITE_FLASH = 22U, //! Failed to write flash partition
RSN_FLASH_WRITE_TOO_BIG = 23U, //! Data to large for flash partition
RSN_HS_NO_DUAL_MODE = 32U, //! (Hotspot) No Dual Mode Operation
RSN_DMR_DISABLED = 63U, //! DMR Disabled
RSN_P25_DISABLED = 64U, //! Project 25 Disabled
RSN_NXDN_DISABLED = 65U //! NXDN Disabled
};
const uint8_t DVM_SHORT_FRAME_START = 0xFEU;
const uint8_t DVM_LONG_FRAME_START = 0xFDU;
#define SERIAL_SPEED 115200
/** @} */
// ---------------------------------------------------------------------------
// Class Declaration
// ---------------------------------------------------------------------------
/**
* @brief Implements the RS232 serial bus to communicate with the HOST S/W.
* @ingroup hotspot_fw
*/
class DSP_FW_API SerialPort {
public:
/**
* @brief Initializes a new instance of the SerialPort class.
*/
SerialPort();
/**
* @brief Starts serial port communications.
*/
void start();
/**
* @brief Process data from serial port.
*/
void process();
/**
* @brief Helper to check if the modem is in a calibration state.
* @param state
* @returns bool
*/
bool isCalState(DVM_STATE state);
/**
* @brief Helper to determine digital mode if the modem is in a calibration state.
* @param state
* @returns DVM_STATE
*/
DVM_STATE calRelativeState(DVM_STATE state);
/**
* @brief Write DMR frame data to serial port.
* @param slot DMR slot number.
* @param[in] data Data to write.
* @param length Length of data to write.
*/
void writeDMRData(bool slot, const uint8_t* data, uint8_t length);
/**
* @brief Write lost DMR frame data to serial port.
* @param slot DMR slot number.
*/
void writeDMRLost(bool slot);
/**
* @brief Write P25 frame data to serial port.
* @param[in] data Data to write.
* @param length Length of data to write.
*/
void writeP25Data(const uint8_t* data, uint16_t length);
/**
* @brief Write lost P25 frame data to serial port.
*/
void writeP25Lost();
/**
* @brief Write NXDN frame data to serial port.
* @param[in] data Data to write.
* @param length Length of data to write.
*/
void writeNXDNData(const uint8_t* data, uint8_t length);
/**
* @brief Write lost NXDN frame data to serial port.
*/
void writeNXDNLost();
/**
* @brief Write calibration frame data to serial port.
* @param[in] data Data to write.
* @param length Length of data to write.
*/
void writeCalData(const uint8_t* data, uint8_t length);
/**
* @brief Write RSSI frame data to serial port.
* @param[in] data Data to write.
* @param length Length of data to write.
*/
void writeRSSIData(const uint8_t* data, uint8_t length);
/**
* @brief
* @param[in] text
*/
void writeDebug(const char* text);
/**
* @brief
* @param[in] text
* @param n1
*/
void writeDebug(const char* text, int16_t n1);
/**
* @brief
* @param[in] text
* @param n1
* @param n2
*/
void writeDebug(const char* text, int16_t n1, int16_t n2);
/**
* @brief
* @param[in] text
* @param n1
* @param n2
* @param n3
*/
void writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3);
/**
* @brief
* @param[in] text
* @param n1
* @param n2
* @param n3
* @param n4
*/
void writeDebug(const char* text, int16_t n1, int16_t n2, int16_t n3, int16_t n4);
/**
* @brief
* @param[in] data
* @param length
*/
void writeDump(const uint8_t* data, uint16_t length);
private:
uint8_t m_buffer[518U];
uint8_t m_ptr;
uint16_t m_len;
bool m_dblFrame;
bool m_debug;
/**
* @brief Write acknowlegement.
*/
void sendACK();
/**
* @brief Write negative acknowlegement.
* @param err
*/
void sendNAK(uint8_t err);
/**
* @brief Write modem DSP status.
*/
void getStatus();
/**
* @brief Write modem DSP version.
*/
void getVersion();
/**
* @brief Helper to validate the passed modem state is valid.
* @param state
* @returns uint8_t Reason code.
*/
uint8_t modemStateCheck(DVM_STATE state);
/**
* @brief Set modem DSP configuration from serial port data.
* @param[in] data Buffer containing configuration frame.
* @param length Length of buffer.
* @returns uint8_t Reason code.
*/
uint8_t setConfig(const uint8_t* data, uint8_t length);
/**
* @brief Set modem DSP mode from serial port data.
* @param[in] data Buffer containing mode frame.
* @param length Length of buffer.
* @returns uint8_t Reason code.
*/
uint8_t setMode(const uint8_t* data, uint8_t length);
/**
* @brief Sets the modem state.
* @param modemState
*/
void setMode(DVM_STATE modemState);
/**
* @brief Sets the RF parameters.
* @param[in] data Buffer containing RF parameters frame.
* @param length Length of buffer.
* @returns uint8_t Reason code.
*/
uint8_t setRFParams(const uint8_t* data, uint8_t length);
/**
* @brief Sets the protocol ring buffer sizes.
* @param[in] data Buffer containing set buffers frame.
* @param length Length of buffer.
* @returns uint8_t Reason code.
*/
uint8_t setBuffers(const uint8_t* data, uint8_t length);
/**
* @brief Reads data from the modem flash parititon.
*/
void flashRead();
/**
* @brief Writes data to the modem flash partition.
* @param[in] data Buffer containing data to write to flash partition.
* @param length Length of buffer.
* @returns uint8_t Reason code.
*/
uint8_t flashWrite(const uint8_t* data, uint8_t length);
// Hardware specific routines
/**
* @brief
* @param n
* @param speed
*/
void beginInt(uint8_t n, int speed);
/**
* @brief
* @param n
*/
int availableInt(uint8_t n);
/**
* @brief
* @param n
*/
int availableForWriteInt(uint8_t n);
/**
* @brief
* @param n
*/
uint8_t readInt(uint8_t n);
/**
* @brief
* @param n
* @param[in] data
* @param length
* @param flush
*/
void writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool flush = false);
};
#endif // __SERIAL_PORT_H__