From f9530ee82acb13e047812f1f9e7a8263ab8af1ad Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Sun, 26 Apr 2020 22:01:11 +0100 Subject: [PATCH 1/5] Pass the RX level to the FM controller. --- FM.cpp | 17 ++++++++++------- FM.h | 3 ++- IO.cpp | 5 ----- IO.h | 4 +--- SerialPort.cpp | 5 +++-- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/FM.cpp b/FM.cpp index ad8b5f5..074de3d 100644 --- a/FM.cpp +++ b/FM.cpp @@ -41,7 +41,8 @@ m_filterStage2(32768, 0,-32768, 32768, -50339, 19052), m_filterStage3(32768, -65536, 32768, 32768, -64075, 31460), m_blanking(), m_useCOS(true), -m_rxBoost(1U) +m_rxBoost(1U), +m_rxLevel(128 * 128) { } @@ -144,10 +145,11 @@ uint8_t CFM::setAck(const char* rfAck, uint8_t speed, uint16_t frequency, uint8_ return m_rfAck.setParams(rfAck, speed, frequency, level, level); } -uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFrequency, uint8_t ctcssThreshold, uint8_t ctcssLevel, uint8_t kerchunkTime, uint8_t hangTime, bool useCOS, uint8_t rxBoost, uint8_t maxDev) +uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFrequency, uint8_t ctcssThreshold, uint8_t ctcssLevel, uint8_t kerchunkTime, uint8_t hangTime, bool useCOS, uint8_t rxBoost, uint8_t maxDev, uint8_t rxLevel) { m_useCOS = useCOS; m_rxBoost = q15_t(rxBoost); + m_rxLevel = q15_t(rxLevel * 128); m_timeoutTimer.setTimeout(timeout, 0U); m_kerchunkTimer.setTimeout(kerchunkTime, 0U); @@ -401,13 +403,14 @@ void CFM::beginRelaying() q15_t CFM::getUnscaledSample(q15_t sample) { // sample / rxLevel - q15_t rxLevel = io.getRxLevel(); q31_t sample31 = q31_t(sample) << 16; - if (((sample31 >> 31) & 1) == ((rxLevel >> 15) & 1)) - sample31 += rxLevel >> 1; + + if (((sample31 >> 31) & 1) == ((m_rxLevel >> 15) & 1)) + sample31 += m_rxLevel >> 1; else - sample31 -= rxLevel >> 1; - sample31 /= rxLevel; + sample31 -= m_rxLevel >> 1; + + sample31 /= m_rxLevel; return q15_t(sample31); } diff --git a/FM.h b/FM.h index f2190d7..1a07eb8 100644 --- a/FM.h +++ b/FM.h @@ -54,7 +54,7 @@ public: uint8_t setCallsign(const char* callsign, uint8_t speed, uint16_t frequency, uint8_t time, uint8_t holdoff, uint8_t highLevel, uint8_t lowLevel, bool callsignAtStart, bool callsignAtEnd); uint8_t setAck(const char* rfAck, uint8_t speed, uint16_t frequency, uint8_t minTime, uint16_t delay, uint8_t level); - uint8_t setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFrequency, uint8_t ctcssThreshold, uint8_t ctcssLevel, uint8_t kerchunkTime, uint8_t hangTime, bool useCOS, uint8_t rxBoost, uint8_t maxDev); + uint8_t setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFrequency, uint8_t ctcssThreshold, uint8_t ctcssLevel, uint8_t kerchunkTime, uint8_t hangTime, bool useCOS, uint8_t rxBoost, uint8_t maxDev, uint8_t rxLevel); private: CFMKeyer m_callsign; @@ -78,6 +78,7 @@ private: CFMBlanking m_blanking; bool m_useCOS; q15_t m_rxBoost; + q15_t m_rxLevel; void stateMachine(bool validSignal, uint8_t length); void listeningState(bool validSignal); diff --git a/IO.cpp b/IO.cpp index afd56b0..a737693 100644 --- a/IO.cpp +++ b/IO.cpp @@ -592,8 +592,3 @@ bool CIO::hasLockout() const { return m_lockout; } - -q15_t CIO::getRxLevel() const -{ - return m_rxLevel; -} diff --git a/IO.h b/IO.h index f3ac4f9..07ed700 100644 --- a/IO.h +++ b/IO.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2017,2018 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2017,2018,2020 by Jonathan Naylor G4KLX * * 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 @@ -56,8 +56,6 @@ public: void selfTest(); - q15_t getRxLevel() const; - private: bool m_started; diff --git a/SerialPort.cpp b/SerialPort.cpp index f729cb3..7a6e609 100644 --- a/SerialPort.cpp +++ b/SerialPort.cpp @@ -408,7 +408,7 @@ uint8_t CSerialPort::setFMParams2(const uint8_t* data, uint8_t length) uint8_t CSerialPort::setFMParams3(const uint8_t* data, uint8_t length) { - if (length < 10U) + if (length < 11U) return 4U; uint16_t timeout = data[0U] * 5U; @@ -425,8 +425,9 @@ uint8_t CSerialPort::setFMParams3(const uint8_t* data, uint8_t length) uint8_t rxBoost = data[8U]; uint8_t maxDev = data[9U]; + uint8_t rxLevel = data[10U]; - return fm.setMisc(timeout, timeoutLevel, ctcssFrequency, ctcssThreshold, ctcssLevel, kerchunkTime, hangTime, useCOS, rxBoost, maxDev); + return fm.setMisc(timeout, timeoutLevel, ctcssFrequency, ctcssThreshold, ctcssLevel, kerchunkTime, hangTime, useCOS, rxBoost, maxDev, rxLevel); } uint8_t CSerialPort::setMode(const uint8_t* data, uint8_t length) From 4ffaa62855790d1efa359d8bed85440ec893ef7f Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Mon, 27 Apr 2020 11:16:06 +0200 Subject: [PATCH 2/5] Move division to CTCSSRX, change sample unscaling to 1/rxLevel --- FM.cpp | 17 ++--------------- FM.h | 2 -- FMCTCSSRX.cpp | 12 +++++++++--- FMCTCSSRX.h | 27 ++++++++++++++++++++------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/FM.cpp b/FM.cpp index 074de3d..75e12b3 100644 --- a/FM.cpp +++ b/FM.cpp @@ -55,7 +55,7 @@ void CFM::samples(bool cos, q15_t* samples, uint8_t length) for (; i < length; i++) { q15_t currentSample = samples[i];//save to a local variable to avoid indirection on every access - CTCSSState ctcssState = m_ctcssRX.process(getUnscaledSample(currentSample)); + CTCSSState ctcssState = m_ctcssRX.process(currentSample); if (CTCSS_NOT_READY(ctcssState) && m_modemState != STATE_FM) { //Not enough samples to determine if you have CTCSS, just carry on @@ -158,7 +158,7 @@ uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFreque m_timeoutTone.setParams(timeoutLevel); m_blanking.setParams(maxDev, timeoutLevel); - uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold); + uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold, m_rxLevel); if (ret != 0U) return ret; @@ -400,17 +400,4 @@ void CFM::beginRelaying() m_ackMinTimer.start(); } -q15_t CFM::getUnscaledSample(q15_t sample) -{ - // sample / rxLevel - q31_t sample31 = q31_t(sample) << 16; - if (((sample31 >> 31) & 1) == ((m_rxLevel >> 15) & 1)) - sample31 += m_rxLevel >> 1; - else - sample31 -= m_rxLevel >> 1; - - sample31 /= m_rxLevel; - - return q15_t(sample31); -} diff --git a/FM.h b/FM.h index 1a07eb8..b9d6469 100644 --- a/FM.h +++ b/FM.h @@ -91,8 +91,6 @@ private: void sendCallsign(); void beginRelaying(); - - q15_t getUnscaledSample(q15_t sample); }; #endif diff --git a/FMCTCSSRX.cpp b/FMCTCSSRX.cpp index 57fbfbe..180f1f3 100644 --- a/FMCTCSSRX.cpp +++ b/FMCTCSSRX.cpp @@ -86,12 +86,16 @@ m_threshold(0), m_count(0U), m_q0(0), m_q1(0), -m_result(CTS_NONE) +m_result(CTS_NONE), +m_rxLevelInverse(1) { } -uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold) +uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold, q15_t rxLevel) { + // Calculate 1/rxLevel + m_rxLevelInverse = q31_t(q15Division(65535 /* This value should be 32767 (q15 1). But this does not work.*/, rxLevel)); + m_coeffDivTwo = 0; for (uint8_t i = 0U; i < CTCSS_TABLE_DATA_LEN; i++) { @@ -111,6 +115,8 @@ uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold) CTCSSState CFMCTCSSRX::process(q15_t sample) { + q63_t sample31 = q31_t(sample) * m_rxLevelInverse; + m_result = m_result & (~CTS_READY); q31_t q2 = m_q1; @@ -122,7 +128,7 @@ CTCSSState CFMCTCSSRX::process(q15_t sample) q31_t t3 = t2 * 2; // m_q0 = m_coeffDivTwo * m_q1 * 2 - q2 + sample - m_q0 = t3 - q2 + q31_t(sample); + m_q0 = t3 - q2 + sample31; m_count++; if (m_count == N) { diff --git a/FMCTCSSRX.h b/FMCTCSSRX.h index a848cb9..7f36b32 100644 --- a/FMCTCSSRX.h +++ b/FMCTCSSRX.h @@ -53,7 +53,7 @@ class CFMCTCSSRX { public: CFMCTCSSRX(); - uint8_t setParams(uint8_t frequency, uint8_t threshold); + uint8_t setParams(uint8_t frequency, uint8_t threshold, q15_t rxLevel); CTCSSState process(q15_t sample); @@ -62,12 +62,25 @@ public: void reset(); private: - q63_t m_coeffDivTwo; - q31_t m_threshold; - uint16_t m_count; - q31_t m_q0; - q31_t m_q1; - CTCSSState m_result; + static inline q15_t q15Division(q15_t a, q15_t divisor) + { + q31_t a31 = q31_t(a) << 16; + + if (((a >> 31) & 1) == ((divisor >> 15) & 1)) + a31 += divisor >> 1; + else + a31 -= divisor >> 1; + + return a31 / divisor; + } + + q63_t m_coeffDivTwo; + q31_t m_threshold; + uint16_t m_count; + q31_t m_q0; + q31_t m_q1; + CTCSSState m_result; + q31_t m_rxLevelInverse; }; #endif From 93a8e6323317a801dd87b34f7d48ccde6bb30cae Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Mon, 27 Apr 2020 11:26:19 +0200 Subject: [PATCH 3/5] Remove m_rxLevel --- FM.cpp | 6 ++---- FM.h | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/FM.cpp b/FM.cpp index 75e12b3..85f9fb5 100644 --- a/FM.cpp +++ b/FM.cpp @@ -41,8 +41,7 @@ m_filterStage2(32768, 0,-32768, 32768, -50339, 19052), m_filterStage3(32768, -65536, 32768, 32768, -64075, 31460), m_blanking(), m_useCOS(true), -m_rxBoost(1U), -m_rxLevel(128 * 128) +m_rxBoost(1U) { } @@ -149,7 +148,6 @@ uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFreque { m_useCOS = useCOS; m_rxBoost = q15_t(rxBoost); - m_rxLevel = q15_t(rxLevel * 128); m_timeoutTimer.setTimeout(timeout, 0U); m_kerchunkTimer.setTimeout(kerchunkTime, 0U); @@ -158,7 +156,7 @@ uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFreque m_timeoutTone.setParams(timeoutLevel); m_blanking.setParams(maxDev, timeoutLevel); - uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold, m_rxLevel); + uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold, q15_t(rxLevel * 128)); if (ret != 0U) return ret; diff --git a/FM.h b/FM.h index b9d6469..c56d462 100644 --- a/FM.h +++ b/FM.h @@ -78,7 +78,6 @@ private: CFMBlanking m_blanking; bool m_useCOS; q15_t m_rxBoost; - q15_t m_rxLevel; void stateMachine(bool validSignal, uint8_t length); void listeningState(bool validSignal); From e8ed77241c5ef474fa9dff7846f7becf5bb86d02 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Mon, 27 Apr 2020 12:05:55 +0100 Subject: [PATCH 4/5] Simplify the calculations. --- FM.cpp | 4 +--- FMCTCSSRX.cpp | 7 +++---- FMCTCSSRX.h | 16 ++-------------- SerialPort.cpp | 2 +- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/FM.cpp b/FM.cpp index 85f9fb5..4a2eb70 100644 --- a/FM.cpp +++ b/FM.cpp @@ -156,7 +156,7 @@ uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFreque m_timeoutTone.setParams(timeoutLevel); m_blanking.setParams(maxDev, timeoutLevel); - uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold, q15_t(rxLevel * 128)); + uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold, rxLevel); if (ret != 0U) return ret; @@ -397,5 +397,3 @@ void CFM::beginRelaying() m_timeoutTimer.start(); m_ackMinTimer.start(); } - - diff --git a/FMCTCSSRX.cpp b/FMCTCSSRX.cpp index 180f1f3..00eebc6 100644 --- a/FMCTCSSRX.cpp +++ b/FMCTCSSRX.cpp @@ -91,10 +91,9 @@ m_rxLevelInverse(1) { } -uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold, q15_t rxLevel) +uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold, uint8_t level) { - // Calculate 1/rxLevel - m_rxLevelInverse = q31_t(q15Division(65535 /* This value should be 32767 (q15 1). But this does not work.*/, rxLevel)); + m_rxLevelInverse = 255 / q15_t(level); m_coeffDivTwo = 0; @@ -115,7 +114,7 @@ uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold, q15_t rxLeve CTCSSState CFMCTCSSRX::process(q15_t sample) { - q63_t sample31 = q31_t(sample) * m_rxLevelInverse; + q31_t sample31 = q31_t(sample) * m_rxLevelInverse; m_result = m_result & (~CTS_READY); diff --git a/FMCTCSSRX.h b/FMCTCSSRX.h index 7f36b32..4c1dfc3 100644 --- a/FMCTCSSRX.h +++ b/FMCTCSSRX.h @@ -53,7 +53,7 @@ class CFMCTCSSRX { public: CFMCTCSSRX(); - uint8_t setParams(uint8_t frequency, uint8_t threshold, q15_t rxLevel); + uint8_t setParams(uint8_t frequency, uint8_t threshold, uint8_t level); CTCSSState process(q15_t sample); @@ -62,25 +62,13 @@ public: void reset(); private: - static inline q15_t q15Division(q15_t a, q15_t divisor) - { - q31_t a31 = q31_t(a) << 16; - - if (((a >> 31) & 1) == ((divisor >> 15) & 1)) - a31 += divisor >> 1; - else - a31 -= divisor >> 1; - - return a31 / divisor; - } - q63_t m_coeffDivTwo; q31_t m_threshold; uint16_t m_count; q31_t m_q0; q31_t m_q1; CTCSSState m_result; - q31_t m_rxLevelInverse; + q15_t m_rxLevelInverse; }; #endif diff --git a/SerialPort.cpp b/SerialPort.cpp index 7a6e609..da81dfd 100644 --- a/SerialPort.cpp +++ b/SerialPort.cpp @@ -101,7 +101,7 @@ const uint8_t MMDVM_DEBUG5 = 0xF5U; #define HW_TYPE "MMDVM" #endif -#define DESCRIPTION "20200426 (D-Star/DMR/System Fusion/P25/NXDN/POCSAG/FM)" +#define DESCRIPTION "20200427 (D-Star/DMR/System Fusion/P25/NXDN/POCSAG/FM)" #if defined(GITVERSION) #define concat(h, a, b, c) h " " a " " b " GitID #" c "" From d08a3690b0716e9f3552b9b8d7408458eba81cc0 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Mon, 27 Apr 2020 13:07:03 +0100 Subject: [PATCH 5/5] Double the signal level to the CTCSS decoder. --- FMCTCSSRX.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FMCTCSSRX.cpp b/FMCTCSSRX.cpp index 00eebc6..9e81d3c 100644 --- a/FMCTCSSRX.cpp +++ b/FMCTCSSRX.cpp @@ -93,7 +93,7 @@ m_rxLevelInverse(1) uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold, uint8_t level) { - m_rxLevelInverse = 255 / q15_t(level); + m_rxLevelInverse = 511 / q15_t(level); m_coeffDivTwo = 0;