From 33527dd3e990405e016954a0e66acdcec6f0a2a4 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 28 Dec 2016 18:07:31 +0000 Subject: [PATCH] Add the RSSI calibration mode. --- CalRSSI.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ CalRSSI.h | 38 +++++++++++++++++++++++++++++ Globals.h | 3 +++ IO.cpp | 2 ++ MMDVM.cpp | 1 + MMDVM.ino | 1 + SerialPort.cpp | 39 ++++++++++++++++++++++++++---- SerialPort.h | 1 + 8 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 CalRSSI.cpp create mode 100644 CalRSSI.h diff --git a/CalRSSI.cpp b/CalRSSI.cpp new file mode 100644 index 0000000..4703bea --- /dev/null +++ b/CalRSSI.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2016 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 + * 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 + * 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 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "Config.h" +#include "Globals.h" +#include "CalRSSI.h" +#include "Utils.h" + +CCalRSSI::CCalRSSI() : +m_count(0U), +m_accum(0U), +m_min(0xFFFFFFU), +m_max(0x000000U) +{ +} + +void CCalRSSI::samples(const uint16_t* rssi, uint8_t length) +{ + for (uint16_t i = 0U; i < length; i++) { + uint16_t ss = rssi[i]; + + m_accum += ss; + + if (ss > m_max) + m_max = ss; + if (ss < m_min) + m_min = ss; + + m_count++; + if (m_count >= 24000U) { + uint16_t ave = m_accum / m_count; + + uint8_t buffer[6U]; + buffer[0U] = (m_max >> 8) & 0xFFU; + buffer[1U] = (m_max >> 0) & 0xFFU; + buffer[2U] = (m_min >> 8) & 0xFFU; + buffer[3U] = (m_min >> 0) & 0xFFU; + buffer[4U] = (ave >> 8) & 0xFFU; + buffer[5U] = (ave >> 0) & 0xFFU; + + serial.writeRSSIData(buffer, 6U); + + m_count = 0U; + m_accum = 0U; + m_min = 0xFFFFFFU; + m_max = 0x000000U; + } + } +} + diff --git a/CalRSSI.h b/CalRSSI.h new file mode 100644 index 0000000..2072243 --- /dev/null +++ b/CalRSSI.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2016 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 + * 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 + * 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 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#if !defined(CALRSSI_H) +#define CALRSSI_H + +#include "Config.h" + +class CCalRSSI { +public: + CCalRSSI(); + + void samples(const uint16_t* rssi, uint8_t length); + +private: + uint32_t m_count; + uint32_t m_accum; + uint16_t m_min; + uint16_t m_max; +}; + +#endif + diff --git a/Globals.h b/Globals.h index b0e29bc..d3bde54 100644 --- a/Globals.h +++ b/Globals.h @@ -43,6 +43,7 @@ enum MMDVM_STATE { STATE_P25 = 4, // Dummy states start at 90 + STATE_RSSICAL = 96, STATE_CWID = 97, STATE_DMRCAL = 98, STATE_DSTARCAL = 99 @@ -63,6 +64,7 @@ enum MMDVM_STATE { #include "CalDStarRX.h" #include "CalDStarTX.h" #include "CalDMR.h" +#include "CalRSSI.h" #include "CWIdTX.h" #include "Debug.h" #include "IO.h" @@ -113,6 +115,7 @@ extern CP25TX p25TX; extern CCalDStarRX calDStarRX; extern CCalDStarTX calDStarTX; extern CCalDMR calDMR; +extern CCalRSSI calRSSI; extern CCWIdTX cwIdTX; diff --git a/IO.cpp b/IO.cpp index 006a47c..5da8eb1 100644 --- a/IO.cpp +++ b/IO.cpp @@ -245,6 +245,8 @@ void CIO::process() ::arm_fir_fast_q15(&m_GMSKFilter, samples, GMSKVals, blockSize); calDStarRX.samples(GMSKVals, blockSize); + } else if (m_modemState == STATE_RSSICAL) { + calRSSI.samples(rssi, blockSize); } } } diff --git a/MMDVM.cpp b/MMDVM.cpp index 9a410ff..85b74e0 100644 --- a/MMDVM.cpp +++ b/MMDVM.cpp @@ -58,6 +58,7 @@ CP25TX p25TX; CCalDStarRX calDStarRX; CCalDStarTX calDStarTX; CCalDMR calDMR; +CCalRSSI calRSSI; CCWIdTX cwIdTX; diff --git a/MMDVM.ino b/MMDVM.ino index 7b30b41..c370a2f 100644 --- a/MMDVM.ino +++ b/MMDVM.ino @@ -55,6 +55,7 @@ CP25TX p25TX; CCalDStarRX calDStarRX; CCalDStarTX calDStarTX; CCalDMR calDMR; +CCalRSSI calRSSI; CCWIdTX cwIdTX; diff --git a/SerialPort.cpp b/SerialPort.cpp index 29535f7..afd18b8 100644 --- a/SerialPort.cpp +++ b/SerialPort.cpp @@ -33,6 +33,7 @@ const uint8_t MMDVM_SET_MODE = 0x03U; const uint8_t MMDVM_SET_FREQ = 0x04U; const uint8_t MMDVM_CAL_DATA = 0x08U; +const uint8_t MMDVM_RSSI_DATA = 0x09U; const uint8_t MMDVM_SEND_CWID = 0x0AU; @@ -68,9 +69,9 @@ const uint8_t MMDVM_DEBUG4 = 0xF4U; const uint8_t MMDVM_DEBUG5 = 0xF5U; #if defined(EXTERNAL_OSC) -const uint8_t HARDWARE[] = "MMDVM 20161124 TCXO (D-Star/DMR/System Fusion/P25/CW Id)"; +const uint8_t HARDWARE[] = "MMDVM 20161124 TCXO (D-Star/DMR/System Fusion/P25/RSSI/CW Id)"; #else -const uint8_t HARDWARE[] = "MMDVM 20161124 (D-Star/DMR/System Fusion/P25/CW Id)"; +const uint8_t HARDWARE[] = "MMDVM 20161124 (D-Star/DMR/System Fusion/P25/RSSI/CW Id)"; #endif const uint8_t PROTOCOL_VERSION = 1U; @@ -225,7 +226,7 @@ uint8_t CSerialPort::setConfig(const uint8_t* data, uint8_t length) MMDVM_STATE modemState = MMDVM_STATE(data[3U]); - if (modemState != STATE_IDLE && modemState != STATE_DSTAR && modemState != STATE_DMR && modemState != STATE_YSF && modemState != STATE_P25 && modemState != STATE_DSTARCAL && modemState != STATE_DMRCAL) + if (modemState != STATE_IDLE && modemState != STATE_DSTAR && modemState != STATE_DMR && modemState != STATE_YSF && modemState != STATE_P25 && modemState != STATE_DSTARCAL && modemState != STATE_DMRCAL && modemState != STATE_RSSICAL) return 4U; if (modemState == STATE_DSTAR && !dstarEnable) return 4U; @@ -298,7 +299,7 @@ uint8_t CSerialPort::setMode(const uint8_t* data, uint8_t length) if (modemState == m_modemState) return 0U; - if (modemState != STATE_IDLE && modemState != STATE_DSTAR && modemState != STATE_DMR && modemState != STATE_YSF && modemState != STATE_P25 && modemState != STATE_DSTARCAL && modemState != STATE_DMRCAL) + if (modemState != STATE_IDLE && modemState != STATE_DSTAR && modemState != STATE_DMR && modemState != STATE_YSF && modemState != STATE_P25 && modemState != STATE_DSTARCAL && modemState != STATE_DMRCAL && modemState != STATE_RSSICAL) return 4U; if (modemState == STATE_DSTAR && !m_dstarEnable) return 4U; @@ -371,6 +372,16 @@ void CSerialPort::setMode(MMDVM_STATE modemState) p25RX.reset(); cwIdTX.reset(); break; + case STATE_RSSICAL: + DEBUG1("Mode set to RSSI Calibrate"); + dmrIdleRX.reset(); + dmrDMORX.reset(); + dmrRX.reset(); + dstarRX.reset(); + ysfRX.reset(); + p25RX.reset(); + cwIdTX.reset(); + break; default: DEBUG1("Mode set to Idle"); // STATE_IDLE @@ -892,6 +903,26 @@ void CSerialPort::writeCalData(const uint8_t* data, uint8_t length) writeInt(1U, reply, count); } +void CSerialPort::writeRSSIData(const uint8_t* data, uint8_t length) +{ + if (m_modemState != STATE_RSSICAL) + return; + + uint8_t reply[30U]; + + reply[0U] = MMDVM_FRAME_START; + reply[1U] = 0U; + reply[2U] = MMDVM_RSSI_DATA; + + uint8_t count = 3U; + for (uint8_t i = 0U; i < length; i++, count++) + reply[count] = data[i]; + + reply[1U] = count; + + writeInt(1U, reply, count); +} + void CSerialPort::writeDebug(const char* text) { uint8_t reply[130U]; diff --git a/SerialPort.h b/SerialPort.h index 455dfdb..630f2e6 100644 --- a/SerialPort.h +++ b/SerialPort.h @@ -47,6 +47,7 @@ public: void writeP25Lost(); void writeCalData(const uint8_t* data, uint8_t length); + void writeRSSIData(const uint8_t* data, uint8_t length); void writeDebug(const char* text); void writeDebug(const char* text, int16_t n1);