Add support to decode DUIDs in during processHdr; Add support to process non-LDU packets during processHdr.

This commit is contained in:
Bryan Biedenkapp 2018-05-14 09:09:43 -04:00
parent 91a15a5782
commit 260dca9664
3 changed files with 683 additions and 607 deletions

View File

@ -1,69 +1,84 @@
/* /*
* Copyright (C) 2016,2017 by Jonathan Naylor G4KLX * Copyright (C) 2016,2017 by Jonathan Naylor G4KLX
* * Copyright (C) 2018 by Bryan Biedenkapp <gatekeep@gmail.com>
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * This program is free software; you can redistribute it and/or modify
* the Free Software Foundation; either version 2 of the License, or * it under the terms of the GNU General Public License as published by
* (at your option) any later version. * the Free Software Foundation; either version 2 of the License, or
* * (at your option) any later version.
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * This program is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * GNU General Public License for more details.
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software * You should have received a copy of the GNU General Public License
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * along with this program; if not, write to the Free Software
*/ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined(P25DEFINES_H)
#define P25DEFINES_H #if !defined(P25DEFINES_H)
#define P25DEFINES_H
const unsigned int P25_RADIO_SYMBOL_LENGTH = 5U; // At 24 kHz sample rate
const unsigned int P25_RADIO_SYMBOL_LENGTH = 5U; // At 24 kHz sample rate
const unsigned int P25_HDR_FRAME_LENGTH_BYTES = 99U;
const unsigned int P25_HDR_FRAME_LENGTH_BITS = P25_HDR_FRAME_LENGTH_BYTES * 8U; const unsigned int P25_HDR_FRAME_LENGTH_BYTES = 99U;
const unsigned int P25_HDR_FRAME_LENGTH_SYMBOLS = P25_HDR_FRAME_LENGTH_BYTES * 4U; const unsigned int P25_HDR_FRAME_LENGTH_BITS = P25_HDR_FRAME_LENGTH_BYTES * 8U;
const unsigned int P25_HDR_FRAME_LENGTH_SAMPLES = P25_HDR_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH; const unsigned int P25_HDR_FRAME_LENGTH_SYMBOLS = P25_HDR_FRAME_LENGTH_BYTES * 4U;
const unsigned int P25_HDR_FRAME_LENGTH_SAMPLES = P25_HDR_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH;
const unsigned int P25_LDU_FRAME_LENGTH_BYTES = 216U;
const unsigned int P25_LDU_FRAME_LENGTH_BITS = P25_LDU_FRAME_LENGTH_BYTES * 8U; const unsigned int P25_LDU_FRAME_LENGTH_BYTES = 216U;
const unsigned int P25_LDU_FRAME_LENGTH_SYMBOLS = P25_LDU_FRAME_LENGTH_BYTES * 4U; const unsigned int P25_LDU_FRAME_LENGTH_BITS = P25_LDU_FRAME_LENGTH_BYTES * 8U;
const unsigned int P25_LDU_FRAME_LENGTH_SAMPLES = P25_LDU_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH; const unsigned int P25_LDU_FRAME_LENGTH_SYMBOLS = P25_LDU_FRAME_LENGTH_BYTES * 4U;
const unsigned int P25_LDU_FRAME_LENGTH_SAMPLES = P25_LDU_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH;
const unsigned int P25_TERMLC_FRAME_LENGTH_BYTES = 54U;
const unsigned int P25_TERMLC_FRAME_LENGTH_BITS = P25_TERMLC_FRAME_LENGTH_BYTES * 8U; const unsigned int P25_TERMLC_FRAME_LENGTH_BYTES = 54U;
const unsigned int P25_TERMLC_FRAME_LENGTH_SYMBOLS = P25_TERMLC_FRAME_LENGTH_BYTES * 4U; const unsigned int P25_TERMLC_FRAME_LENGTH_BITS = P25_TERMLC_FRAME_LENGTH_BYTES * 8U;
const unsigned int P25_TERMLC_FRAME_LENGTH_SAMPLES = P25_TERMLC_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH; const unsigned int P25_TERMLC_FRAME_LENGTH_SYMBOLS = P25_TERMLC_FRAME_LENGTH_BYTES * 4U;
const unsigned int P25_TERMLC_FRAME_LENGTH_SAMPLES = P25_TERMLC_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH;
const unsigned int P25_TERM_FRAME_LENGTH_BYTES = 18U;
const unsigned int P25_TERM_FRAME_LENGTH_BITS = P25_TERM_FRAME_LENGTH_BYTES * 8U; const unsigned int P25_TERM_FRAME_LENGTH_BYTES = 18U;
const unsigned int P25_TERM_FRAME_LENGTH_SYMBOLS = P25_TERM_FRAME_LENGTH_BYTES * 4U; const unsigned int P25_TERM_FRAME_LENGTH_BITS = P25_TERM_FRAME_LENGTH_BYTES * 8U;
const unsigned int P25_TERM_FRAME_LENGTH_SAMPLES = P25_TERM_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH; const unsigned int P25_TERM_FRAME_LENGTH_SYMBOLS = P25_TERM_FRAME_LENGTH_BYTES * 4U;
const unsigned int P25_TERM_FRAME_LENGTH_SAMPLES = P25_TERM_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH;
const unsigned int P25_SYNC_LENGTH_BYTES = 6U;
const unsigned int P25_SYNC_LENGTH_BITS = P25_SYNC_LENGTH_BYTES * 8U; const unsigned int P25_TSDU_FRAME_LENGTH_BYTES = 45U;
const unsigned int P25_SYNC_LENGTH_SYMBOLS = P25_SYNC_LENGTH_BYTES * 4U; const unsigned int P25_TSDU_FRAME_LENGTH_BITS = P25_TSDU_FRAME_LENGTH_BYTES * 8U;
const unsigned int P25_SYNC_LENGTH_SAMPLES = P25_SYNC_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH; const unsigned int P25_TSDU_FRAME_LENGTH_SYMBOLS = P25_TSDU_FRAME_LENGTH_BYTES * 4U;
const unsigned int P25_TSDU_FRAME_LENGTH_SAMPLES = P25_TSDU_FRAME_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH;
const unsigned int P25_NID_LENGTH_BITS = 64U;
const unsigned int P25_NID_LENGTH_SYMBOLS = 32U; const unsigned int P25_SYNC_LENGTH_BYTES = 6U;
const unsigned int P25_NID_LENGTH_SAMPLESS = P25_NID_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH; const unsigned int P25_SYNC_LENGTH_BITS = P25_SYNC_LENGTH_BYTES * 8U;
const unsigned int P25_SYNC_LENGTH_SYMBOLS = P25_SYNC_LENGTH_BYTES * 4U;
const uint8_t P25_SYNC_BYTES[] = {0x55U, 0x75U, 0xF5U, 0xFFU, 0x77U, 0xFFU}; const unsigned int P25_SYNC_LENGTH_SAMPLES = P25_SYNC_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH;
const uint8_t P25_SYNC_BYTES_LENGTH = 6U;
const unsigned int P25_NID_LENGTH_BYTES = 8U;
const uint64_t P25_SYNC_BITS = 0x00005575F5FF77FFU; const unsigned int P25_NID_LENGTH_BITS = P25_NID_LENGTH_BYTES * 8U;
const uint64_t P25_SYNC_BITS_MASK = 0x0000FFFFFFFFFFFFU; const unsigned int P25_NID_LENGTH_SYMBOLS = P25_NID_LENGTH_BYTES * 4U;
const unsigned int P25_NID_LENGTH_SAMPLES = P25_NID_LENGTH_SYMBOLS * P25_RADIO_SYMBOL_LENGTH;
// 5 5 7 5 F 5 F F 7 7 F F
// 01 01 01 01 01 11 01 01 11 11 01 01 11 11 11 11 01 11 01 11 11 11 11 11 const uint8_t P25_SYNC_BYTES[] = {0x55U, 0x75U, 0xF5U, 0xFFU, 0x77U, 0xFFU};
// +3 +3 +3 +3 +3 -3 +3 +3 -3 -3 +3 +3 -3 -3 -3 -3 +3 -3 +3 -3 -3 -3 -3 -3 const uint8_t P25_SYNC_BYTES_LENGTH = 6U;
const int8_t P25_SYNC_SYMBOLS_VALUES[] = {+3, +3, +3, +3, +3, -3, +3, +3, -3, -3, +3, +3, -3, -3, -3, -3, +3, -3, +3, -3, -3, -3, -3, -3}; const uint64_t P25_SYNC_BITS = 0x00005575F5FF77FFU;
const uint64_t P25_SYNC_BITS_MASK = 0x0000FFFFFFFFFFFFU;
const uint32_t P25_SYNC_SYMBOLS = 0x00FB30A0U;
const uint32_t P25_SYNC_SYMBOLS_MASK = 0x00FFFFFFU; // 5 5 7 5 F 5 F F 7 7 F F
// 01 01 01 01 01 11 01 01 11 11 01 01 11 11 11 11 01 11 01 11 11 11 11 11
#endif // +3 +3 +3 +3 +3 -3 +3 +3 -3 -3 +3 +3 -3 -3 -3 -3 +3 -3 +3 -3 -3 -3 -3 -3
const int8_t P25_SYNC_SYMBOLS_VALUES[] = {+3, +3, +3, +3, +3, -3, +3, +3, -3, -3, +3, +3, -3, -3, -3, -3, +3, -3, +3, -3, -3, -3, -3, -3};
const uint32_t P25_SYNC_SYMBOLS = 0x00FB30A0U;
const uint32_t P25_SYNC_SYMBOLS_MASK = 0x00FFFFFFU;
const uint8_t P25_DUID_HDU = 0x00U; // Header Data Unit
const uint8_t P25_DUID_TDU = 0x03U; // Simple Terminator Data Unit
const uint8_t P25_DUID_LDU1 = 0x05U; // Logical Link Data Unit 1
const uint8_t P25_DUID_TSDU = 0x07U; // Trunking System Data Unit
const uint8_t P25_DUID_LDU2 = 0x0AU; // Logical Link Data Unit 2
const uint8_t P25_DUID_PDU = 0x0CU; // Packet Data Unit
const uint8_t P25_DUID_TDULC = 0x0FU; // Terminator Data Unit with Link Control
#endif

991
P25RX.cpp
View File

@ -1,466 +1,525 @@
/* /*
* Copyright (C) 2009-2017 by Jonathan Naylor G4KLX * Copyright (C) 2009-2017 by Jonathan Naylor G4KLX
* * Copyright (C) 2018 by Bryan Biedenkapp <gatekeep@gmail.com>
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * This program is free software; you can redistribute it and/or modify
* the Free Software Foundation; either version 2 of the License, or * it under the terms of the GNU General Public License as published by
* (at your option) any later version. * the Free Software Foundation; either version 2 of the License, or
* * (at your option) any later version.
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * This program is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * GNU General Public License for more details.
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software * You should have received a copy of the GNU General Public License
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * along with this program; if not, write to the Free Software
*/ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Config.h"
#include "Globals.h" #include "Config.h"
#include "P25RX.h" #include "Globals.h"
#include "Utils.h" #include "P25RX.h"
#include "Utils.h"
const q15_t SCALING_FACTOR = 18750; // Q15(0.57)
const q15_t SCALING_FACTOR = 18750; // Q15(0.57)
const uint8_t MAX_SYNC_BIT_START_ERRS = 2U;
const uint8_t MAX_SYNC_BIT_RUN_ERRS = 4U; const uint8_t CORRELATION_COUNTDOWN = 10U;//5U;
const uint8_t MAX_SYNC_SYMBOLS_ERRS = 2U; const uint8_t MAX_SYNC_BIT_START_ERRS = 2U;
const uint8_t MAX_SYNC_BIT_RUN_ERRS = 4U;
const uint8_t BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
const uint8_t MAX_SYNC_SYMBOLS_ERRS = 2U;
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
const uint8_t BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
const uint8_t NOAVEPTR = 99U;
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
const uint16_t NOENDPTR = 9999U;
const uint8_t NOAVEPTR = 99U;
const unsigned int MAX_SYNC_FRAMES = 4U + 1U;
const uint16_t NOENDPTR = 9999U;
CP25RX::CP25RX() :
m_state(P25RXS_NONE), const unsigned int MAX_SYNC_FRAMES = 4U + 1U;
m_bitBuffer(),
m_buffer(), CP25RX::CP25RX() :
m_bitPtr(0U), m_state(P25RXS_NONE),
m_dataPtr(0U), m_bitBuffer(),
m_hdrStartPtr(NOENDPTR), m_buffer(),
m_lduStartPtr(NOENDPTR), m_bitPtr(0U),
m_lduEndPtr(NOENDPTR), m_dataPtr(0U),
m_minSyncPtr(NOENDPTR), m_hdrStartPtr(NOENDPTR),
m_maxSyncPtr(NOENDPTR), m_lduStartPtr(NOENDPTR),
m_hdrSyncPtr(NOENDPTR), m_lduEndPtr(NOENDPTR),
m_lduSyncPtr(NOENDPTR), m_minSyncPtr(NOENDPTR),
m_maxCorr(0), m_maxSyncPtr(NOENDPTR),
m_lostCount(0U), m_hdrSyncPtr(NOENDPTR),
m_countdown(0U), m_lduSyncPtr(NOENDPTR),
m_centre(), m_maxCorr(0),
m_centreVal(0), m_lostCount(0U),
m_threshold(), m_countdown(0U),
m_thresholdVal(0), m_centre(),
m_averagePtr(NOAVEPTR), m_centreVal(0),
m_rssiAccum(0U), m_threshold(),
m_rssiCount(0U) m_thresholdVal(0),
{ m_averagePtr(NOAVEPTR),
} m_rssiAccum(0U),
m_rssiCount(0U),
void CP25RX::reset() m_duid(0U)
{ {
m_state = P25RXS_NONE; }
m_dataPtr = 0U;
m_bitPtr = 0U; void CP25RX::reset()
m_maxCorr = 0; {
m_averagePtr = NOAVEPTR; m_state = P25RXS_NONE;
m_hdrStartPtr = NOENDPTR; m_dataPtr = 0U;
m_lduStartPtr = NOENDPTR; m_bitPtr = 0U;
m_lduEndPtr = NOENDPTR; m_maxCorr = 0;
m_hdrSyncPtr = NOENDPTR; m_averagePtr = NOAVEPTR;
m_lduSyncPtr = NOENDPTR; m_hdrStartPtr = NOENDPTR;
m_minSyncPtr = NOENDPTR; m_lduStartPtr = NOENDPTR;
m_maxSyncPtr = NOENDPTR; m_lduEndPtr = NOENDPTR;
m_centreVal = 0; m_hdrSyncPtr = NOENDPTR;
m_thresholdVal = 0; m_lduSyncPtr = NOENDPTR;
m_lostCount = 0U; m_minSyncPtr = NOENDPTR;
m_countdown = 0U; m_maxSyncPtr = NOENDPTR;
m_rssiAccum = 0U; m_centreVal = 0;
m_rssiCount = 0U; m_thresholdVal = 0;
} m_lostCount = 0U;
m_countdown = 0U;
void CP25RX::samples(const q15_t* samples, uint16_t* rssi, uint8_t length) m_rssiAccum = 0U;
{ m_rssiCount = 0U;
for (uint8_t i = 0U; i < length; i++) { m_duid = 0U;
q15_t sample = samples[i]; }
m_rssiAccum += rssi[i]; void CP25RX::samples(const q15_t* samples, uint16_t* rssi, uint8_t length)
m_rssiCount++; {
for (uint8_t i = 0U; i < length; i++) {
m_bitBuffer[m_bitPtr] <<= 1; q15_t sample = samples[i];
if (sample < 0)
m_bitBuffer[m_bitPtr] |= 0x01U; m_rssiAccum += rssi[i];
m_rssiCount++;
m_buffer[m_dataPtr] = sample;
m_bitBuffer[m_bitPtr] <<= 1;
switch (m_state) { if (sample < 0)
case P25RXS_HDR: m_bitBuffer[m_bitPtr] |= 0x01U;
processHdr(sample);
break; m_buffer[m_dataPtr] = sample;
case P25RXS_LDU:
processLdu(sample); switch (m_state) {
break; case P25RXS_HDR:
default: processHdr(sample);
processNone(sample); break;
break; case P25RXS_LDU:
} processLdu(sample);
break;
m_dataPtr++; default:
if (m_dataPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) processNone(sample);
m_dataPtr = 0U; break;
}
m_bitPtr++;
if (m_bitPtr >= P25_RADIO_SYMBOL_LENGTH) m_dataPtr++;
m_bitPtr = 0U; if (m_dataPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) {
} m_dataPtr = 0U;
} m_duid = 0U;
}
void CP25RX::processNone(q15_t sample)
{ m_bitPtr++;
bool ret = correlateSync(); if (m_bitPtr >= P25_RADIO_SYMBOL_LENGTH)
if (ret) { m_bitPtr = 0U;
// On the first sync, start the countdown to the state change }
if (m_countdown == 0U) { }
m_rssiAccum = 0U;
m_rssiCount = 0U; void CP25RX::processNone(q15_t sample)
{
io.setDecode(true); bool ret = correlateSync();
io.setADCDetection(true); if (ret) {
// On the first sync, start the countdown to the state change
m_averagePtr = NOAVEPTR; if (m_countdown == 0U) {
m_rssiAccum = 0U;
m_countdown = 5U; m_rssiCount = 0U;
}
} io.setDecode(true);
io.setADCDetection(true);
if (m_countdown > 0U)
m_countdown--; m_averagePtr = NOAVEPTR;
if (m_countdown == 1U) { m_countdown = CORRELATION_COUNTDOWN;
// These are the sync positions for the following LDU after a HDR }
m_minSyncPtr = m_hdrSyncPtr + P25_HDR_FRAME_LENGTH_SAMPLES - 1U; }
if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; if (m_countdown > 0U)
m_countdown--;
m_maxSyncPtr = m_hdrSyncPtr + P25_HDR_FRAME_LENGTH_SAMPLES + 1U;
if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) if (m_countdown == 1U) {
m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; // These are the sync positions for the following LDU after a HDR
m_minSyncPtr = m_hdrSyncPtr + P25_HDR_FRAME_LENGTH_SAMPLES - 1U;
m_state = P25RXS_HDR; if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
m_countdown = 0U; m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
}
} m_maxSyncPtr = m_hdrSyncPtr + P25_HDR_FRAME_LENGTH_SAMPLES + 1U;
if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
void CP25RX::processHdr(q15_t sample) m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
{
if (m_minSyncPtr < m_maxSyncPtr) { m_state = P25RXS_HDR;
if (m_dataPtr >= m_minSyncPtr && m_dataPtr <= m_maxSyncPtr) m_countdown = 0U;
correlateSync(); }
} else { }
if (m_dataPtr >= m_minSyncPtr || m_dataPtr <= m_maxSyncPtr)
correlateSync(); void CP25RX::processHdr(q15_t sample)
} {
if (m_minSyncPtr < m_maxSyncPtr) {
if (m_dataPtr == m_maxSyncPtr) { if (m_dataPtr >= m_minSyncPtr && m_dataPtr <= m_maxSyncPtr)
if (m_hdrSyncPtr != m_lduSyncPtr) { correlateSync();
calculateLevels(m_hdrStartPtr, P25_HDR_FRAME_LENGTH_SYMBOLS); } else {
if (m_dataPtr >= m_minSyncPtr || m_dataPtr <= m_maxSyncPtr)
DEBUG4("P25RX: sync found in Hdr pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal); correlateSync();
}
uint8_t frame[P25_HDR_FRAME_LENGTH_BYTES + 1U];
samplesToBits(m_hdrStartPtr, P25_HDR_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal); if (m_dataPtr == m_maxSyncPtr) {
uint16_t nidStartPtr = m_hdrStartPtr + P25_SYNC_LENGTH_SAMPLES;
frame[0U] = 0x01U; if (nidStartPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
serial.writeP25Hdr(frame, P25_HDR_FRAME_LENGTH_BYTES + 1U); nidStartPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
}
uint8_t nid[2U];
m_minSyncPtr = m_lduSyncPtr + P25_LDU_FRAME_LENGTH_SAMPLES - 1U; samplesToBits(nidStartPtr, (2U * 4U), nid, 0U, m_centreVal, m_thresholdVal);
if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) // DEBUG3("P25RX: nid (b0 - b1)", nid[0U], nid[1U]);
m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
m_duid = nid[1U] & 0x0F;
m_maxSyncPtr = m_lduSyncPtr + 1U;
if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) switch (m_duid) {
m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; case P25_DUID_HDU: {
calculateLevels(m_hdrStartPtr, P25_HDR_FRAME_LENGTH_SYMBOLS);
m_state = P25RXS_LDU;
m_maxCorr = 0; DEBUG4("P25RX: sync found in Hdr pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal);
}
} uint8_t frame[P25_HDR_FRAME_LENGTH_BYTES + 1U];
samplesToBits(m_hdrStartPtr, P25_HDR_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
void CP25RX::processLdu(q15_t sample)
{ frame[0U] = 0x01U;
if (m_minSyncPtr < m_maxSyncPtr) { serial.writeP25Hdr(frame, P25_HDR_FRAME_LENGTH_BYTES + 1U);
if (m_dataPtr >= m_minSyncPtr && m_dataPtr <= m_maxSyncPtr) }
correlateSync(); break;
} else { case P25_DUID_TSDU: {
if (m_dataPtr >= m_minSyncPtr || m_dataPtr <= m_maxSyncPtr) calculateLevels(m_hdrStartPtr, P25_TSDU_FRAME_LENGTH_SYMBOLS);
correlateSync();
} DEBUG4("P25RX: sync found in TSDU pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal);
if (m_dataPtr == m_lduEndPtr) { uint8_t frame[P25_TSDU_FRAME_LENGTH_BYTES + 1U];
// Only update the centre and threshold if they are from a good sync samplesToBits(m_hdrStartPtr, P25_TSDU_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
if (m_lostCount == MAX_SYNC_FRAMES) {
m_minSyncPtr = m_lduSyncPtr + P25_LDU_FRAME_LENGTH_SAMPLES - 1U; frame[0U] = 0x01U;
if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) serial.writeP25Hdr(frame, P25_TSDU_FRAME_LENGTH_BYTES + 1U);
m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; }
break;
m_maxSyncPtr = m_lduSyncPtr + 1U; case P25_DUID_TDU: {
if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) calculateLevels(m_hdrStartPtr, P25_TERM_FRAME_LENGTH_SYMBOLS);
m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
} DEBUG4("P25RX: sync found in TDU pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal);
calculateLevels(m_lduStartPtr, P25_LDU_FRAME_LENGTH_SYMBOLS); uint8_t frame[P25_TERM_FRAME_LENGTH_BYTES + 1U];
samplesToBits(m_hdrStartPtr, P25_TERM_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
DEBUG4("P25RX: sync found in Ldu pos/centre/threshold", m_lduSyncPtr, m_centreVal, m_thresholdVal);
frame[0U] = 0x01U;
uint8_t frame[P25_LDU_FRAME_LENGTH_BYTES + 3U]; serial.writeP25Hdr(frame, P25_TERM_FRAME_LENGTH_BYTES + 1U);
samplesToBits(m_lduStartPtr, P25_LDU_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal); }
break;
// We've not seen a data sync for too long, signal RXLOST and change to RX_NONE case P25_DUID_TDULC: {
m_lostCount--; calculateLevels(m_hdrStartPtr, P25_TERMLC_FRAME_LENGTH_SYMBOLS);
if (m_lostCount == 0U) {
DEBUG1("P25RX: sync timed out, lost lock"); DEBUG4("P25RX: sync found in TDULC pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal);
io.setDecode(false); uint8_t frame[P25_TERMLC_FRAME_LENGTH_BYTES + 1U];
io.setADCDetection(false); samplesToBits(m_hdrStartPtr, P25_TERMLC_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
serial.writeP25Lost(); frame[0U] = 0x01U;
serial.writeP25Hdr(frame, P25_TERMLC_FRAME_LENGTH_BYTES + 1U);
m_state = P25RXS_NONE; }
m_lduEndPtr = NOENDPTR; break;
m_averagePtr = NOAVEPTR; default:
m_countdown = 0U; break;
m_maxCorr = 0; }
} else {
frame[0U] = m_lostCount == (MAX_SYNC_FRAMES - 1U) ? 0x01U : 0x00U; m_minSyncPtr = m_lduSyncPtr + P25_LDU_FRAME_LENGTH_SAMPLES - 1U;
writeRSSILdu(frame); if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
m_maxCorr = 0; m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
}
} m_maxSyncPtr = m_lduSyncPtr + 1U;
} if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
bool CP25RX::correlateSync()
{ m_state = P25RXS_LDU;
if (countBits32((m_bitBuffer[m_bitPtr] & P25_SYNC_SYMBOLS_MASK) ^ P25_SYNC_SYMBOLS) <= MAX_SYNC_SYMBOLS_ERRS) { m_maxCorr = 0;
uint16_t ptr = m_dataPtr + P25_LDU_FRAME_LENGTH_SAMPLES - P25_SYNC_LENGTH_SAMPLES + P25_RADIO_SYMBOL_LENGTH; }
if (ptr >= P25_LDU_FRAME_LENGTH_SAMPLES) }
ptr -= P25_LDU_FRAME_LENGTH_SAMPLES;
void CP25RX::processLdu(q15_t sample)
q31_t corr = 0; {
q15_t min = 16000; if (m_minSyncPtr < m_maxSyncPtr) {
q15_t max = -16000; if (m_dataPtr >= m_minSyncPtr && m_dataPtr <= m_maxSyncPtr)
correlateSync();
for (uint8_t i = 0U; i < P25_SYNC_LENGTH_SYMBOLS; i++) { } else {
q15_t val = m_buffer[ptr]; if (m_dataPtr >= m_minSyncPtr || m_dataPtr <= m_maxSyncPtr)
correlateSync();
if (val > max) }
max = val;
if (val < min) if (m_dataPtr == m_lduEndPtr) {
min = val; // Only update the centre and threshold if they are from a good sync
if (m_lostCount == MAX_SYNC_FRAMES) {
switch (P25_SYNC_SYMBOLS_VALUES[i]) { m_minSyncPtr = m_lduSyncPtr + P25_LDU_FRAME_LENGTH_SAMPLES - 1U;
case +3: if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
corr -= (val + val + val); m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
break;
case +1: m_maxSyncPtr = m_lduSyncPtr + 1U;
corr -= val; if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
break; m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
case -1: }
corr += val;
break; calculateLevels(m_lduStartPtr, P25_LDU_FRAME_LENGTH_SYMBOLS);
default: // -3
corr += (val + val + val); DEBUG4("P25RX: sync found in Ldu pos/centre/threshold", m_lduSyncPtr, m_centreVal, m_thresholdVal);
break;
} uint8_t frame[P25_LDU_FRAME_LENGTH_BYTES + 3U];
samplesToBits(m_lduStartPtr, P25_LDU_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
ptr += P25_RADIO_SYMBOL_LENGTH;
if (ptr >= P25_LDU_FRAME_LENGTH_SAMPLES) // We've not seen a data sync for too long, signal RXLOST and change to RX_NONE
ptr -= P25_LDU_FRAME_LENGTH_SAMPLES; m_lostCount--;
} if (m_lostCount == 0U) {
DEBUG1("P25RX: sync timed out, lost lock");
if (corr > m_maxCorr) {
if (m_averagePtr == NOAVEPTR) { io.setDecode(false);
m_centreVal = (max + min) >> 1; io.setADCDetection(false);
q31_t v1 = (max - m_centreVal) * SCALING_FACTOR; serial.writeP25Lost();
m_thresholdVal = q15_t(v1 >> 15);
} m_state = P25RXS_NONE;
m_lduEndPtr = NOENDPTR;
uint16_t startPtr = m_dataPtr + P25_LDU_FRAME_LENGTH_SAMPLES - P25_SYNC_LENGTH_SAMPLES + P25_RADIO_SYMBOL_LENGTH; m_averagePtr = NOAVEPTR;
if (startPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) m_countdown = 0U;
startPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; m_maxCorr = 0;
m_duid = 0U;
uint8_t sync[P25_SYNC_BYTES_LENGTH]; } else {
samplesToBits(startPtr, P25_SYNC_LENGTH_SYMBOLS, sync, 0U, m_centreVal, m_thresholdVal); frame[0U] = m_lostCount == (MAX_SYNC_FRAMES - 1U) ? 0x01U : 0x00U;
writeRSSILdu(frame);
uint8_t maxErrs; m_maxCorr = 0;
if (m_state == P25RXS_NONE) }
maxErrs = MAX_SYNC_BIT_START_ERRS; }
else }
maxErrs = MAX_SYNC_BIT_RUN_ERRS;
bool CP25RX::correlateSync()
uint8_t errs = 0U; {
for (uint8_t i = 0U; i < P25_SYNC_BYTES_LENGTH; i++) if (countBits32((m_bitBuffer[m_bitPtr] & P25_SYNC_SYMBOLS_MASK) ^ P25_SYNC_SYMBOLS) <= MAX_SYNC_SYMBOLS_ERRS) {
errs += countBits8(sync[i] ^ P25_SYNC_BYTES[i]); uint16_t ptr = m_dataPtr + P25_LDU_FRAME_LENGTH_SAMPLES - P25_SYNC_LENGTH_SAMPLES + P25_RADIO_SYMBOL_LENGTH;
if (ptr >= P25_LDU_FRAME_LENGTH_SAMPLES)
if (errs <= maxErrs) { ptr -= P25_LDU_FRAME_LENGTH_SAMPLES;
m_maxCorr = corr;
m_lostCount = MAX_SYNC_FRAMES; q31_t corr = 0;
q15_t min = 16000;
m_lduSyncPtr = m_dataPtr; q15_t max = -16000;
// These are the positions of the start and end of an LDU for (uint8_t i = 0U; i < P25_SYNC_LENGTH_SYMBOLS; i++) {
m_lduStartPtr = startPtr; q15_t val = m_buffer[ptr];
m_lduEndPtr = m_dataPtr + P25_LDU_FRAME_LENGTH_SAMPLES - P25_SYNC_LENGTH_SAMPLES - 1U; if (val > max)
if (m_lduEndPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) max = val;
m_lduEndPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; if (val < min)
min = val;
if (m_state == P25RXS_NONE) {
m_hdrSyncPtr = m_dataPtr; switch (P25_SYNC_SYMBOLS_VALUES[i]) {
case +3:
// This is the position of the start of a HDR corr -= (val + val + val);
m_hdrStartPtr = startPtr; break;
case +1:
// These are the range of positions for a sync for an LDU following a HDR corr -= val;
m_minSyncPtr = m_dataPtr + P25_HDR_FRAME_LENGTH_SAMPLES - 1U; break;
if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) case -1:
m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; corr += val;
break;
m_maxSyncPtr = m_dataPtr + P25_HDR_FRAME_LENGTH_SAMPLES + 1U; default: // -3
if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) corr += (val + val + val);
m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES; break;
} }
return true; ptr += P25_RADIO_SYMBOL_LENGTH;
} if (ptr >= P25_LDU_FRAME_LENGTH_SAMPLES)
} ptr -= P25_LDU_FRAME_LENGTH_SAMPLES;
} }
return false; if (corr > m_maxCorr) {
} if (m_averagePtr == NOAVEPTR) {
m_centreVal = (max + min) >> 1;
void CP25RX::calculateLevels(uint16_t start, uint16_t count)
{ q31_t v1 = (max - m_centreVal) * SCALING_FACTOR;
q15_t maxPos = -16000; m_thresholdVal = q15_t(v1 >> 15);
q15_t minPos = 16000; }
q15_t maxNeg = 16000;
q15_t minNeg = -16000; uint16_t startPtr = m_dataPtr + P25_LDU_FRAME_LENGTH_SAMPLES - P25_SYNC_LENGTH_SAMPLES + P25_RADIO_SYMBOL_LENGTH;
if (startPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
for (uint16_t i = 0U; i < count; i++) { startPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
q15_t sample = m_buffer[start];
uint8_t sync[P25_SYNC_BYTES_LENGTH];
if (sample > 0) { samplesToBits(startPtr, P25_SYNC_LENGTH_SYMBOLS, sync, 0U, m_centreVal, m_thresholdVal);
if (sample > maxPos)
maxPos = sample; uint8_t maxErrs;
if (sample < minPos) if (m_state == P25RXS_NONE)
minPos = sample; maxErrs = MAX_SYNC_BIT_START_ERRS;
} else { else
if (sample < maxNeg) maxErrs = MAX_SYNC_BIT_RUN_ERRS;
maxNeg = sample;
if (sample > minNeg) uint8_t errs = 0U;
minNeg = sample; for (uint8_t i = 0U; i < P25_SYNC_BYTES_LENGTH; i++)
} errs += countBits8(sync[i] ^ P25_SYNC_BYTES[i]);
start += P25_RADIO_SYMBOL_LENGTH; if (errs <= maxErrs) {
if (start >= P25_LDU_FRAME_LENGTH_SAMPLES) m_maxCorr = corr;
start -= P25_LDU_FRAME_LENGTH_SAMPLES; m_lostCount = MAX_SYNC_FRAMES;
}
m_lduSyncPtr = m_dataPtr;
q15_t posThresh = (maxPos + minPos) >> 1;
q15_t negThresh = (maxNeg + minNeg) >> 1; // These are the positions of the start and end of an LDU
m_lduStartPtr = startPtr;
q15_t centre = (posThresh + negThresh) >> 1;
m_lduEndPtr = m_dataPtr + P25_LDU_FRAME_LENGTH_SAMPLES - P25_SYNC_LENGTH_SAMPLES - 1U;
q15_t threshold = posThresh - centre; if (m_lduEndPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
m_lduEndPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
DEBUG5("P25RX: pos/neg/centre/threshold", posThresh, negThresh, centre, threshold);
if (m_state == P25RXS_NONE) {
if (m_averagePtr == NOAVEPTR) { m_hdrSyncPtr = m_dataPtr;
for (uint8_t i = 0U; i < 16U; i++) {
m_centre[i] = centre; // This is the position of the start of a HDR
m_threshold[i] = threshold; m_hdrStartPtr = startPtr;
}
// These are the range of positions for a sync for an LDU following a HDR
m_averagePtr = 0U; m_minSyncPtr = m_dataPtr + P25_HDR_FRAME_LENGTH_SAMPLES - 1U;
} else { if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
m_centre[m_averagePtr] = centre; m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
m_threshold[m_averagePtr] = threshold;
m_maxSyncPtr = m_dataPtr + P25_HDR_FRAME_LENGTH_SAMPLES + 1U;
m_averagePtr++; if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
if (m_averagePtr >= 16U) m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
m_averagePtr = 0U; }
}
return true;
m_centreVal = 0; }
m_thresholdVal = 0; }
}
for (uint8_t i = 0U; i < 16U; i++) {
m_centreVal += m_centre[i]; return false;
m_thresholdVal += m_threshold[i]; }
}
void CP25RX::calculateLevels(uint16_t start, uint16_t count)
m_centreVal >>= 4; {
m_thresholdVal >>= 4; q15_t maxPos = -16000;
} q15_t minPos = 16000;
q15_t maxNeg = 16000;
void CP25RX::samplesToBits(uint16_t start, uint16_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold) q15_t minNeg = -16000;
{
for (uint16_t i = 0U; i < count; i++) { for (uint16_t i = 0U; i < count; i++) {
q15_t sample = m_buffer[start] - centre; q15_t sample = m_buffer[start];
if (sample < -threshold) { if (sample > 0) {
WRITE_BIT1(buffer, offset, false); if (sample > maxPos)
offset++; maxPos = sample;
WRITE_BIT1(buffer, offset, true); if (sample < minPos)
offset++; minPos = sample;
} else if (sample < 0) { } else {
WRITE_BIT1(buffer, offset, false); if (sample < maxNeg)
offset++; maxNeg = sample;
WRITE_BIT1(buffer, offset, false); if (sample > minNeg)
offset++; minNeg = sample;
} else if (sample < threshold) { }
WRITE_BIT1(buffer, offset, true);
offset++; start += P25_RADIO_SYMBOL_LENGTH;
WRITE_BIT1(buffer, offset, false); if (start >= P25_LDU_FRAME_LENGTH_SAMPLES)
offset++; start -= P25_LDU_FRAME_LENGTH_SAMPLES;
} else { }
WRITE_BIT1(buffer, offset, true);
offset++; q15_t posThresh = (maxPos + minPos) >> 1;
WRITE_BIT1(buffer, offset, true); q15_t negThresh = (maxNeg + minNeg) >> 1;
offset++;
} q15_t centre = (posThresh + negThresh) >> 1;
start += P25_RADIO_SYMBOL_LENGTH; q15_t threshold = posThresh - centre;
if (start >= P25_LDU_FRAME_LENGTH_SAMPLES)
start -= P25_LDU_FRAME_LENGTH_SAMPLES; DEBUG5("P25RX: pos/neg/centre/threshold", posThresh, negThresh, centre, threshold);
}
} if (m_averagePtr == NOAVEPTR) {
for (uint8_t i = 0U; i < 16U; i++) {
void CP25RX::writeRSSILdu(uint8_t* ldu) m_centre[i] = centre;
{ m_threshold[i] = threshold;
#if defined(SEND_RSSI_DATA) }
if (m_rssiCount > 0U) {
uint16_t rssi = m_rssiAccum / m_rssiCount; m_averagePtr = 0U;
} else {
ldu[217U] = (rssi >> 8) & 0xFFU; m_centre[m_averagePtr] = centre;
ldu[218U] = (rssi >> 0) & 0xFFU; m_threshold[m_averagePtr] = threshold;
serial.writeP25Ldu(ldu, P25_LDU_FRAME_LENGTH_BYTES + 3U); m_averagePtr++;
} else { if (m_averagePtr >= 16U)
serial.writeP25Ldu(ldu, P25_LDU_FRAME_LENGTH_BYTES + 1U); m_averagePtr = 0U;
} }
#else
serial.writeP25Ldu(ldu, P25_LDU_FRAME_LENGTH_BYTES + 1U); m_centreVal = 0;
#endif m_thresholdVal = 0;
m_rssiAccum = 0U; for (uint8_t i = 0U; i < 16U; i++) {
m_rssiCount = 0U; m_centreVal += m_centre[i];
} m_thresholdVal += m_threshold[i];
}
m_centreVal >>= 4;
m_thresholdVal >>= 4;
}
void CP25RX::samplesToBits(uint16_t start, uint16_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold)
{
for (uint16_t i = 0U; i < count; i++) {
q15_t sample = m_buffer[start] - centre;
if (sample < -threshold) {
WRITE_BIT1(buffer, offset, false);
offset++;
WRITE_BIT1(buffer, offset, true);
offset++;
} else if (sample < 0) {
WRITE_BIT1(buffer, offset, false);
offset++;
WRITE_BIT1(buffer, offset, false);
offset++;
} else if (sample < threshold) {
WRITE_BIT1(buffer, offset, true);
offset++;
WRITE_BIT1(buffer, offset, false);
offset++;
} else {
WRITE_BIT1(buffer, offset, true);
offset++;
WRITE_BIT1(buffer, offset, true);
offset++;
}
start += P25_RADIO_SYMBOL_LENGTH;
if (start >= P25_LDU_FRAME_LENGTH_SAMPLES)
start -= P25_LDU_FRAME_LENGTH_SAMPLES;
}
}
void CP25RX::writeRSSILdu(uint8_t* ldu)
{
#if defined(SEND_RSSI_DATA)
if (m_rssiCount > 0U) {
uint16_t rssi = m_rssiAccum / m_rssiCount;
ldu[217U] = (rssi >> 8) & 0xFFU;
ldu[218U] = (rssi >> 0) & 0xFFU;
serial.writeP25Ldu(ldu, P25_LDU_FRAME_LENGTH_BYTES + 3U);
} else {
serial.writeP25Ldu(ldu, P25_LDU_FRAME_LENGTH_BYTES + 1U);
}
#else
serial.writeP25Ldu(ldu, P25_LDU_FRAME_LENGTH_BYTES + 1U);
#endif
m_rssiAccum = 0U;
m_rssiCount = 0U;
}

146
P25RX.h
View File

@ -1,72 +1,74 @@
/* /*
* Copyright (C) 2015,2016,2017 by Jonathan Naylor G4KLX * Copyright (C) 2015,2016,2017 by Jonathan Naylor G4KLX
* * Copyright (C) 2018 by Bryan Biedenkapp <gatekeep@gmail.com>
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * This program is free software; you can redistribute it and/or modify
* the Free Software Foundation; either version 2 of the License, or * it under the terms of the GNU General Public License as published by
* (at your option) any later version. * the Free Software Foundation; either version 2 of the License, or
* * (at your option) any later version.
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * This program is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * GNU General Public License for more details.
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software * You should have received a copy of the GNU General Public License
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * along with this program; if not, write to the Free Software
*/ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#if !defined(P25RX_H)
#define P25RX_H #if !defined(P25RX_H)
#define P25RX_H
#include "Config.h"
#include "P25Defines.h" #include "Config.h"
#include "P25Defines.h"
enum P25RX_STATE {
P25RXS_NONE, enum P25RX_STATE {
P25RXS_HDR, P25RXS_NONE,
P25RXS_LDU P25RXS_HDR,
}; P25RXS_LDU
};
class CP25RX {
public: class CP25RX {
CP25RX(); public:
CP25RX();
void samples(const q15_t* samples, uint16_t* rssi, uint8_t length);
void samples(const q15_t* samples, uint16_t* rssi, uint8_t length);
void reset();
void reset();
private:
P25RX_STATE m_state; private:
uint32_t m_bitBuffer[P25_RADIO_SYMBOL_LENGTH]; P25RX_STATE m_state;
q15_t m_buffer[P25_LDU_FRAME_LENGTH_SAMPLES]; uint32_t m_bitBuffer[P25_RADIO_SYMBOL_LENGTH];
uint16_t m_bitPtr; q15_t m_buffer[P25_LDU_FRAME_LENGTH_SAMPLES];
uint16_t m_dataPtr; uint16_t m_bitPtr;
uint16_t m_hdrStartPtr; uint16_t m_dataPtr;
uint16_t m_lduStartPtr; uint16_t m_hdrStartPtr;
uint16_t m_lduEndPtr; uint16_t m_lduStartPtr;
uint16_t m_minSyncPtr; uint16_t m_lduEndPtr;
uint16_t m_maxSyncPtr; uint16_t m_minSyncPtr;
uint16_t m_hdrSyncPtr; uint16_t m_maxSyncPtr;
uint16_t m_lduSyncPtr; uint16_t m_hdrSyncPtr;
q31_t m_maxCorr; uint16_t m_lduSyncPtr;
uint16_t m_lostCount; q31_t m_maxCorr;
uint8_t m_countdown; uint16_t m_lostCount;
q15_t m_centre[16U]; uint8_t m_countdown;
q15_t m_centreVal; q15_t m_centre[16U];
q15_t m_threshold[16U]; q15_t m_centreVal;
q15_t m_thresholdVal; q15_t m_threshold[16U];
uint8_t m_averagePtr; q15_t m_thresholdVal;
uint32_t m_rssiAccum; uint8_t m_averagePtr;
uint16_t m_rssiCount; uint32_t m_rssiAccum;
uint16_t m_rssiCount;
void processNone(q15_t sample); uint8_t m_duid;
void processHdr(q15_t sample);
void processLdu(q15_t sample); void processNone(q15_t sample);
bool correlateSync(); void processHdr(q15_t sample);
void calculateLevels(uint16_t start, uint16_t count); void processLdu(q15_t sample);
void samplesToBits(uint16_t start, uint16_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold); bool correlateSync();
void writeRSSILdu(uint8_t* ldu); void calculateLevels(uint16_t start, uint16_t count);
}; void samplesToBits(uint16_t start, uint16_t count, uint8_t* buffer, uint16_t offset, q15_t centre, q15_t threshold);
void writeRSSILdu(uint8_t* ldu);
#endif };
#endif