mirror of
https://github.com/g4klx/MMDVM.git
synced 2026-04-06 06:55:05 +00:00
Update 48k version
In to NXDN possible problem on sinc filter anc isinc filter, think they must be recalculated but do not know how to do it. On D-STAR, DMR, C4FM and P25 it work.
This commit is contained in:
parent
62f3e01aa5
commit
694b83123a
75 changed files with 7038 additions and 2324 deletions
621
P25RX.cpp
621
P25RX.cpp
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (C) 2016 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
|
||||
|
|
@ -16,227 +17,270 @@
|
|||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
// #define WANT_DEBUG
|
||||
|
||||
#include "Config.h"
|
||||
#include "Globals.h"
|
||||
#include "P25RX.h"
|
||||
#include "Utils.h"
|
||||
|
||||
const unsigned int BUFFER_LENGTH = 200U;
|
||||
const q15_t SCALING_FACTOR = 18750; // Q15(0.57)
|
||||
|
||||
const q15_t SCALING_FACTOR = 18750; // Q15(0.55)
|
||||
const uint8_t CORRELATION_COUNTDOWN = 10U;//5U;
|
||||
|
||||
const uint32_t PLLMAX = 0x10000U;
|
||||
const uint32_t PLLINC = PLLMAX / P25_RADIO_SYMBOL_LENGTH;
|
||||
const uint32_t INC = PLLINC / 32U;
|
||||
const uint8_t MAX_SYNC_BIT_START_ERRS = 2U;
|
||||
const uint8_t MAX_SYNC_BIT_RUN_ERRS = 4U;
|
||||
|
||||
const uint8_t SYNC_SYMBOL_ERRS = 0U;
|
||||
const uint8_t MAX_SYNC_SYMBOLS_ERRS = 2U;
|
||||
|
||||
const uint8_t SYNC_BIT_START_ERRS = 2U;
|
||||
const uint8_t SYNC_BIT_RUN_ERRS = 4U;
|
||||
|
||||
const unsigned int MAX_SYNC_FRAMES = 3U + 1U;
|
||||
|
||||
const uint8_t BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U};
|
||||
const uint8_t BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
|
||||
|
||||
#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])
|
||||
#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7])
|
||||
|
||||
const uint8_t NOAVEPTR = 99U;
|
||||
|
||||
const uint16_t NOENDPTR = 9999U;
|
||||
|
||||
const unsigned int MAX_SYNC_FRAMES = 4U + 1U;
|
||||
|
||||
CP25RX::CP25RX() :
|
||||
m_pll(0U),
|
||||
m_prev(false),
|
||||
m_state(P25RXS_NONE),
|
||||
m_bitBuffer(0x00U),
|
||||
m_symbols(),
|
||||
m_outBuffer(),
|
||||
m_buffer(NULL),
|
||||
m_bufferPtr(0U),
|
||||
m_symbolPtr(0U),
|
||||
m_bitBuffer(),
|
||||
m_buffer(),
|
||||
m_bitPtr(0U),
|
||||
m_dataPtr(0U),
|
||||
m_hdrStartPtr(NOENDPTR),
|
||||
m_lduStartPtr(NOENDPTR),
|
||||
m_lduEndPtr(NOENDPTR),
|
||||
m_minSyncPtr(NOENDPTR),
|
||||
m_maxSyncPtr(NOENDPTR),
|
||||
m_hdrSyncPtr(NOENDPTR),
|
||||
m_lduSyncPtr(NOENDPTR),
|
||||
m_maxCorr(0),
|
||||
m_lostCount(0U),
|
||||
m_centre(0),
|
||||
m_threshold(0)
|
||||
m_countdown(0U),
|
||||
m_centre(),
|
||||
m_centreVal(0),
|
||||
m_threshold(),
|
||||
m_thresholdVal(0),
|
||||
m_averagePtr(NOAVEPTR),
|
||||
m_rssiAccum(0U),
|
||||
m_rssiCount(0U),
|
||||
m_duid(0U)
|
||||
{
|
||||
m_buffer = m_outBuffer + 1U;
|
||||
}
|
||||
|
||||
void CP25RX::reset()
|
||||
{
|
||||
m_pll = 0U;
|
||||
m_prev = false;
|
||||
m_state = P25RXS_NONE;
|
||||
m_bitBuffer = 0x00U;
|
||||
m_bufferPtr = 0U;
|
||||
m_symbolPtr = 0U;
|
||||
m_lostCount = 0U;
|
||||
m_centre = 0;
|
||||
m_threshold = 0;
|
||||
m_state = P25RXS_NONE;
|
||||
m_dataPtr = 0U;
|
||||
m_bitPtr = 0U;
|
||||
m_maxCorr = 0;
|
||||
m_averagePtr = NOAVEPTR;
|
||||
m_hdrStartPtr = NOENDPTR;
|
||||
m_lduStartPtr = NOENDPTR;
|
||||
m_lduEndPtr = NOENDPTR;
|
||||
m_hdrSyncPtr = NOENDPTR;
|
||||
m_lduSyncPtr = NOENDPTR;
|
||||
m_minSyncPtr = NOENDPTR;
|
||||
m_maxSyncPtr = NOENDPTR;
|
||||
m_centreVal = 0;
|
||||
m_thresholdVal = 0;
|
||||
m_lostCount = 0U;
|
||||
m_countdown = 0U;
|
||||
m_rssiAccum = 0U;
|
||||
m_rssiCount = 0U;
|
||||
m_duid = 0U;
|
||||
}
|
||||
|
||||
void CP25RX::samples(const q15_t* samples, uint8_t length)
|
||||
void CP25RX::samples(const q15_t* samples, uint16_t* rssi, uint8_t length)
|
||||
{
|
||||
for (uint16_t i = 0U; i < length; i++) {
|
||||
bool bit = samples[i] < 0;
|
||||
for (uint8_t i = 0U; i < length; i++) {
|
||||
q15_t sample = samples[i];
|
||||
|
||||
if (bit != m_prev) {
|
||||
if (m_pll < (PLLMAX / 2U))
|
||||
m_pll += INC;
|
||||
else
|
||||
m_pll -= INC;
|
||||
m_rssiAccum += rssi[i];
|
||||
m_rssiCount++;
|
||||
|
||||
m_bitBuffer[m_bitPtr] <<= 1;
|
||||
if (sample < 0)
|
||||
m_bitBuffer[m_bitPtr] |= 0x01U;
|
||||
|
||||
m_buffer[m_dataPtr] = sample;
|
||||
|
||||
switch (m_state) {
|
||||
case P25RXS_HDR:
|
||||
processHdr(sample);
|
||||
break;
|
||||
case P25RXS_LDU:
|
||||
processLdu(sample);
|
||||
break;
|
||||
default:
|
||||
processNone(sample);
|
||||
break;
|
||||
}
|
||||
|
||||
m_prev = bit;
|
||||
|
||||
m_pll += PLLINC;
|
||||
|
||||
if (m_pll >= PLLMAX) {
|
||||
m_pll -= PLLMAX;
|
||||
|
||||
if (m_state == P25RXS_NONE)
|
||||
processNone(samples[i]);
|
||||
else
|
||||
processData(samples[i]);
|
||||
m_dataPtr++;
|
||||
if (m_dataPtr >= P25_LDU_FRAME_LENGTH_SAMPLES) {
|
||||
m_dataPtr = 0U;
|
||||
m_duid = 0U;
|
||||
}
|
||||
|
||||
m_bitPtr++;
|
||||
if (m_bitPtr >= P25_RADIO_SYMBOL_LENGTH)
|
||||
m_bitPtr = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
void CP25RX::processNone(q15_t sample)
|
||||
{
|
||||
m_symbolBuffer <<= 1;
|
||||
if (sample < 0)
|
||||
m_symbolBuffer |= 0x01U;
|
||||
|
||||
m_symbols[m_symbolPtr] = sample;
|
||||
|
||||
// Fuzzy matching of the data sync bit sequence
|
||||
if (countBits32((m_symbolBuffer & P25_SYNC_SYMBOLS_MASK) ^ P25_SYNC_SYMBOLS) <= SYNC_SYMBOL_ERRS) {
|
||||
q15_t max = -16000;
|
||||
q15_t min = 16000;
|
||||
|
||||
for (uint8_t i = 0U; i < P25_SYNC_LENGTH_SYMBOLS; i++) {
|
||||
q15_t val = m_symbols[i];
|
||||
if (val > max)
|
||||
max = val;
|
||||
if (val < min)
|
||||
min = val;
|
||||
}
|
||||
|
||||
q15_t centre = (max + min) >> 1;
|
||||
|
||||
q31_t v1 = (max - centre) * SCALING_FACTOR;
|
||||
q15_t threshold = q15_t(v1 >> 15);
|
||||
|
||||
uint16_t ptr = m_symbolPtr + 1U;
|
||||
if (ptr >= P25_SYNC_LENGTH_SYMBOLS)
|
||||
ptr = 0U;
|
||||
|
||||
for (uint8_t i = 0U; i < P25_SYNC_LENGTH_SYMBOLS; i++) {
|
||||
q15_t sample = m_symbols[ptr] - centre;
|
||||
|
||||
if (sample < -threshold) {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x01U;
|
||||
} else if (sample < 0) {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x00U;
|
||||
} else if (sample < threshold) {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x02U;
|
||||
} else {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x03U;
|
||||
}
|
||||
|
||||
ptr++;
|
||||
if (ptr >= P25_SYNC_LENGTH_SYMBOLS)
|
||||
ptr = 0U;
|
||||
}
|
||||
|
||||
// Fuzzy matching of the data sync bit sequence
|
||||
if (countBits64((m_bitBuffer & P25_SYNC_BITS_MASK) ^ P25_SYNC_BITS) <= SYNC_BIT_START_ERRS) {
|
||||
DEBUG5("P25RX: sync found in None min/max/centre/threshold", min, max, centre, threshold);
|
||||
for (uint8_t i = 0U; i < P25_SYNC_LENGTH_BYTES; i++)
|
||||
m_buffer[i] = P25_SYNC_BYTES[i];
|
||||
m_centre = centre;
|
||||
m_threshold = threshold;
|
||||
m_lostCount = MAX_SYNC_FRAMES;
|
||||
m_bufferPtr = P25_SYNC_LENGTH_BITS;
|
||||
m_state = P25RXS_DATA;
|
||||
bool ret = correlateSync();
|
||||
if (ret) {
|
||||
// On the first sync, start the countdown to the state change
|
||||
if (m_countdown == 0U) {
|
||||
m_rssiAccum = 0U;
|
||||
m_rssiCount = 0U;
|
||||
|
||||
io.setDecode(true);
|
||||
io.setADCDetection(true);
|
||||
|
||||
m_averagePtr = NOAVEPTR;
|
||||
|
||||
m_countdown = CORRELATION_COUNTDOWN;
|
||||
}
|
||||
}
|
||||
|
||||
m_symbolPtr++;
|
||||
if (m_symbolPtr >= P25_SYNC_LENGTH_SYMBOLS)
|
||||
m_symbolPtr = 0U;
|
||||
if (m_countdown > 0U)
|
||||
m_countdown--;
|
||||
|
||||
if (m_countdown == 1U) {
|
||||
// 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;
|
||||
|
||||
m_maxSyncPtr = m_hdrSyncPtr + P25_HDR_FRAME_LENGTH_SAMPLES + 1U;
|
||||
if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
|
||||
m_state = P25RXS_HDR;
|
||||
m_countdown = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
void CP25RX::processData(q15_t sample)
|
||||
void CP25RX::processHdr(q15_t sample)
|
||||
{
|
||||
sample -= m_centre;
|
||||
|
||||
if (sample < -m_threshold) {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x01U;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, false);
|
||||
m_bufferPtr++;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, true);
|
||||
m_bufferPtr++;
|
||||
} else if (sample < 0) {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x00U;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, false);
|
||||
m_bufferPtr++;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, false);
|
||||
m_bufferPtr++;
|
||||
} else if (sample < m_threshold) {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x02U;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, true);
|
||||
m_bufferPtr++;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, false);
|
||||
m_bufferPtr++;
|
||||
if (m_minSyncPtr < m_maxSyncPtr) {
|
||||
if (m_dataPtr >= m_minSyncPtr && m_dataPtr <= m_maxSyncPtr)
|
||||
correlateSync();
|
||||
} else {
|
||||
m_bitBuffer <<= 2;
|
||||
m_bitBuffer |= 0x03U;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, true);
|
||||
m_bufferPtr++;
|
||||
WRITE_BIT1(m_buffer, m_bufferPtr, true);
|
||||
m_bufferPtr++;
|
||||
if (m_dataPtr >= m_minSyncPtr || m_dataPtr <= m_maxSyncPtr)
|
||||
correlateSync();
|
||||
}
|
||||
|
||||
// Search for an early sync to indicate an LDU following a header
|
||||
if (m_bufferPtr >= (P25_HDR_FRAME_LENGTH_BITS + P25_SYNC_LENGTH_BITS - 1U) && m_bufferPtr <= (P25_HDR_FRAME_LENGTH_BITS + P25_SYNC_LENGTH_BITS + 1U)) {
|
||||
// Fuzzy matching of the data sync bit sequence
|
||||
if (countBits64((m_bitBuffer & P25_SYNC_BITS_MASK) ^ P25_SYNC_BITS) <= SYNC_BIT_RUN_ERRS) {
|
||||
DEBUG2("P25RX: found LDU sync in Data, pos", m_bufferPtr - P25_SYNC_LENGTH_BITS);
|
||||
if (m_dataPtr == m_maxSyncPtr) {
|
||||
uint16_t nidStartPtr = m_hdrStartPtr + P25_SYNC_LENGTH_SAMPLES;
|
||||
if (nidStartPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
nidStartPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
|
||||
m_outBuffer[0U] = 0x01U;
|
||||
serial.writeP25Hdr(m_outBuffer, P25_HDR_FRAME_LENGTH_BYTES + 1U);
|
||||
uint8_t nid[2U];
|
||||
samplesToBits(nidStartPtr, (2U * 4U), nid, 0U, m_centreVal, m_thresholdVal);
|
||||
// DEBUG3("P25RX: nid (b0 - b1)", nid[0U], nid[1U]);
|
||||
|
||||
// Restore the sync that's now in the wrong place
|
||||
for (uint8_t i = 0U; i < P25_SYNC_LENGTH_BYTES; i++)
|
||||
m_buffer[i] = P25_SYNC_BYTES[i];
|
||||
m_duid = nid[1U] & 0x0F;
|
||||
|
||||
m_lostCount = MAX_SYNC_FRAMES;
|
||||
m_bufferPtr = P25_SYNC_LENGTH_BITS;
|
||||
switch (m_duid) {
|
||||
case P25_DUID_HDU: {
|
||||
calculateLevels(m_hdrStartPtr, P25_HDR_FRAME_LENGTH_SYMBOLS);
|
||||
|
||||
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);
|
||||
|
||||
frame[0U] = 0x01U;
|
||||
serial.writeP25Hdr(frame, P25_HDR_FRAME_LENGTH_BYTES + 1U);
|
||||
}
|
||||
break;
|
||||
case P25_DUID_TSDU: {
|
||||
calculateLevels(m_hdrStartPtr, P25_TSDU_FRAME_LENGTH_SYMBOLS);
|
||||
|
||||
DEBUG4("P25RX: sync found in TSDU pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal);
|
||||
|
||||
uint8_t frame[P25_TSDU_FRAME_LENGTH_BYTES + 1U];
|
||||
samplesToBits(m_hdrStartPtr, P25_TSDU_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
|
||||
|
||||
frame[0U] = 0x01U;
|
||||
serial.writeP25Hdr(frame, P25_TSDU_FRAME_LENGTH_BYTES + 1U);
|
||||
}
|
||||
break;
|
||||
case P25_DUID_TDU: {
|
||||
calculateLevels(m_hdrStartPtr, P25_TERM_FRAME_LENGTH_SYMBOLS);
|
||||
|
||||
DEBUG4("P25RX: sync found in TDU pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal);
|
||||
|
||||
uint8_t frame[P25_TERM_FRAME_LENGTH_BYTES + 1U];
|
||||
samplesToBits(m_hdrStartPtr, P25_TERM_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
|
||||
|
||||
frame[0U] = 0x01U;
|
||||
serial.writeP25Hdr(frame, P25_TERM_FRAME_LENGTH_BYTES + 1U);
|
||||
}
|
||||
break;
|
||||
case P25_DUID_TDULC: {
|
||||
calculateLevels(m_hdrStartPtr, P25_TERMLC_FRAME_LENGTH_SYMBOLS);
|
||||
|
||||
DEBUG4("P25RX: sync found in TDULC pos/centre/threshold", m_hdrSyncPtr, m_centreVal, m_thresholdVal);
|
||||
|
||||
uint8_t frame[P25_TERMLC_FRAME_LENGTH_BYTES + 1U];
|
||||
samplesToBits(m_hdrStartPtr, P25_TERMLC_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
|
||||
|
||||
frame[0U] = 0x01U;
|
||||
serial.writeP25Hdr(frame, P25_TERMLC_FRAME_LENGTH_BYTES + 1U);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
m_minSyncPtr = m_lduSyncPtr + P25_LDU_FRAME_LENGTH_SAMPLES - 1U;
|
||||
if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
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;
|
||||
|
||||
m_state = P25RXS_LDU;
|
||||
m_maxCorr = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void CP25RX::processLdu(q15_t sample)
|
||||
{
|
||||
if (m_minSyncPtr < m_maxSyncPtr) {
|
||||
if (m_dataPtr >= m_minSyncPtr && m_dataPtr <= m_maxSyncPtr)
|
||||
correlateSync();
|
||||
} else {
|
||||
if (m_dataPtr >= m_minSyncPtr || m_dataPtr <= m_maxSyncPtr)
|
||||
correlateSync();
|
||||
}
|
||||
|
||||
// Only search for a sync in the right place +-2 symbols
|
||||
if (m_bufferPtr >= (P25_SYNC_LENGTH_BITS - 2U) && m_bufferPtr <= (P25_SYNC_LENGTH_BITS + 2U)) {
|
||||
// Fuzzy matching of the data sync bit sequence
|
||||
if (countBits64((m_bitBuffer & P25_SYNC_BITS_MASK) ^ P25_SYNC_BITS) <= SYNC_BIT_RUN_ERRS) {
|
||||
DEBUG2("P25RX: found sync in Data, pos", m_bufferPtr - P25_SYNC_LENGTH_BITS);
|
||||
m_lostCount = MAX_SYNC_FRAMES;
|
||||
m_bufferPtr = P25_SYNC_LENGTH_BITS;
|
||||
if (m_dataPtr == m_lduEndPtr) {
|
||||
// Only update the centre and threshold if they are from a good sync
|
||||
if (m_lostCount == MAX_SYNC_FRAMES) {
|
||||
m_minSyncPtr = m_lduSyncPtr + P25_LDU_FRAME_LENGTH_SAMPLES - 1U;
|
||||
if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Send a data frame to the host if the required number of bits have been received
|
||||
if (m_bufferPtr == P25_LDU_FRAME_LENGTH_BITS) {
|
||||
calculateLevels(m_lduStartPtr, P25_LDU_FRAME_LENGTH_SYMBOLS);
|
||||
|
||||
DEBUG4("P25RX: sync found in Ldu pos/centre/threshold", m_lduSyncPtr, m_centreVal, m_thresholdVal);
|
||||
|
||||
uint8_t frame[P25_LDU_FRAME_LENGTH_BYTES + 3U];
|
||||
samplesToBits(m_lduStartPtr, P25_LDU_FRAME_LENGTH_SYMBOLS, frame, 8U, m_centreVal, m_thresholdVal);
|
||||
|
||||
// We've not seen a data sync for too long, signal RXLOST and change to RX_NONE
|
||||
m_lostCount--;
|
||||
if (m_lostCount == 0U) {
|
||||
|
|
@ -247,16 +291,235 @@ void CP25RX::processData(q15_t sample)
|
|||
|
||||
serial.writeP25Lost();
|
||||
|
||||
m_state = P25RXS_NONE;
|
||||
} else {
|
||||
m_outBuffer[0U] = m_lostCount == (MAX_SYNC_FRAMES - 1U) ? 0x01U : 0x00U;
|
||||
|
||||
serial.writeP25Ldu(m_outBuffer, P25_LDU_FRAME_LENGTH_BYTES + 1U);
|
||||
|
||||
// Start the next frame
|
||||
::memset(m_outBuffer, 0x00U, P25_LDU_FRAME_LENGTH_BYTES + 3U);
|
||||
m_bufferPtr = 0U;
|
||||
m_state = P25RXS_NONE;
|
||||
m_lduEndPtr = NOENDPTR;
|
||||
m_averagePtr = NOAVEPTR;
|
||||
m_countdown = 0U;
|
||||
m_maxCorr = 0;
|
||||
m_duid = 0U;
|
||||
} else {
|
||||
frame[0U] = m_lostCount == (MAX_SYNC_FRAMES - 1U) ? 0x01U : 0x00U;
|
||||
writeRSSILdu(frame);
|
||||
m_maxCorr = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CP25RX::correlateSync()
|
||||
{
|
||||
if (countBits32((m_bitBuffer[m_bitPtr] & P25_SYNC_SYMBOLS_MASK) ^ P25_SYNC_SYMBOLS) <= MAX_SYNC_SYMBOLS_ERRS) {
|
||||
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;
|
||||
|
||||
q31_t corr = 0;
|
||||
q15_t min = 16000;
|
||||
q15_t max = -16000;
|
||||
|
||||
for (uint8_t i = 0U; i < P25_SYNC_LENGTH_SYMBOLS; i++) {
|
||||
q15_t val = m_buffer[ptr];
|
||||
|
||||
if (val > max)
|
||||
max = val;
|
||||
if (val < min)
|
||||
min = val;
|
||||
|
||||
switch (P25_SYNC_SYMBOLS_VALUES[i]) {
|
||||
case +3:
|
||||
corr -= (val + val + val);
|
||||
break;
|
||||
case +1:
|
||||
corr -= val;
|
||||
break;
|
||||
case -1:
|
||||
corr += val;
|
||||
break;
|
||||
default: // -3
|
||||
corr += (val + val + val);
|
||||
break;
|
||||
}
|
||||
|
||||
ptr += P25_RADIO_SYMBOL_LENGTH;
|
||||
if (ptr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
ptr -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
}
|
||||
|
||||
if (corr > m_maxCorr) {
|
||||
if (m_averagePtr == NOAVEPTR) {
|
||||
m_centreVal = (max + min) >> 1;
|
||||
|
||||
q31_t v1 = (max - m_centreVal) * SCALING_FACTOR;
|
||||
m_thresholdVal = q15_t(v1 >> 15);
|
||||
}
|
||||
|
||||
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)
|
||||
startPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
|
||||
uint8_t sync[P25_SYNC_BYTES_LENGTH];
|
||||
samplesToBits(startPtr, P25_SYNC_LENGTH_SYMBOLS, sync, 0U, m_centreVal, m_thresholdVal);
|
||||
|
||||
uint8_t maxErrs;
|
||||
if (m_state == P25RXS_NONE)
|
||||
maxErrs = MAX_SYNC_BIT_START_ERRS;
|
||||
else
|
||||
maxErrs = MAX_SYNC_BIT_RUN_ERRS;
|
||||
|
||||
uint8_t errs = 0U;
|
||||
for (uint8_t i = 0U; i < P25_SYNC_BYTES_LENGTH; i++)
|
||||
errs += countBits8(sync[i] ^ P25_SYNC_BYTES[i]);
|
||||
|
||||
if (errs <= maxErrs) {
|
||||
m_maxCorr = corr;
|
||||
m_lostCount = MAX_SYNC_FRAMES;
|
||||
|
||||
m_lduSyncPtr = m_dataPtr;
|
||||
|
||||
// These are the positions of the start and end of an LDU
|
||||
m_lduStartPtr = startPtr;
|
||||
|
||||
m_lduEndPtr = m_dataPtr + P25_LDU_FRAME_LENGTH_SAMPLES - P25_SYNC_LENGTH_SAMPLES - 1U;
|
||||
if (m_lduEndPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
m_lduEndPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
|
||||
if (m_state == P25RXS_NONE) {
|
||||
m_hdrSyncPtr = m_dataPtr;
|
||||
|
||||
// This is the position of the start of a HDR
|
||||
m_hdrStartPtr = startPtr;
|
||||
|
||||
// These are the range of positions for a sync for an LDU following a HDR
|
||||
m_minSyncPtr = m_dataPtr + P25_HDR_FRAME_LENGTH_SAMPLES - 1U;
|
||||
if (m_minSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
m_minSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
|
||||
m_maxSyncPtr = m_dataPtr + P25_HDR_FRAME_LENGTH_SAMPLES + 1U;
|
||||
if (m_maxSyncPtr >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
m_maxSyncPtr -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CP25RX::calculateLevels(uint16_t start, uint16_t count)
|
||||
{
|
||||
q15_t maxPos = -16000;
|
||||
q15_t minPos = 16000;
|
||||
q15_t maxNeg = 16000;
|
||||
q15_t minNeg = -16000;
|
||||
|
||||
for (uint16_t i = 0U; i < count; i++) {
|
||||
q15_t sample = m_buffer[start];
|
||||
|
||||
if (sample > 0) {
|
||||
if (sample > maxPos)
|
||||
maxPos = sample;
|
||||
if (sample < minPos)
|
||||
minPos = sample;
|
||||
} else {
|
||||
if (sample < maxNeg)
|
||||
maxNeg = sample;
|
||||
if (sample > minNeg)
|
||||
minNeg = sample;
|
||||
}
|
||||
|
||||
start += P25_RADIO_SYMBOL_LENGTH;
|
||||
if (start >= P25_LDU_FRAME_LENGTH_SAMPLES)
|
||||
start -= P25_LDU_FRAME_LENGTH_SAMPLES;
|
||||
}
|
||||
|
||||
q15_t posThresh = (maxPos + minPos) >> 1;
|
||||
q15_t negThresh = (maxNeg + minNeg) >> 1;
|
||||
|
||||
q15_t centre = (posThresh + negThresh) >> 1;
|
||||
|
||||
q15_t threshold = posThresh - centre;
|
||||
|
||||
DEBUG5("P25RX: pos/neg/centre/threshold", posThresh, negThresh, centre, threshold);
|
||||
|
||||
if (m_averagePtr == NOAVEPTR) {
|
||||
for (uint8_t i = 0U; i < 16U; i++) {
|
||||
m_centre[i] = centre;
|
||||
m_threshold[i] = threshold;
|
||||
}
|
||||
|
||||
m_averagePtr = 0U;
|
||||
} else {
|
||||
m_centre[m_averagePtr] = centre;
|
||||
m_threshold[m_averagePtr] = threshold;
|
||||
|
||||
m_averagePtr++;
|
||||
if (m_averagePtr >= 16U)
|
||||
m_averagePtr = 0U;
|
||||
}
|
||||
|
||||
m_centreVal = 0;
|
||||
m_thresholdVal = 0;
|
||||
|
||||
for (uint8_t i = 0U; i < 16U; i++) {
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue