From 7302dc8bd71d05aa423554367b99b499c293be7b Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 9 May 2020 11:08:18 +0200 Subject: [PATCH 01/11] Replace RSSI RB with template RB --- IO.h | 10 +++--- RSSIRB.cpp | 102 ----------------------------------------------------- RSSIRB.h | 59 ------------------------------- 3 files changed, 5 insertions(+), 166 deletions(-) delete mode 100644 RSSIRB.cpp delete mode 100644 RSSIRB.h diff --git a/IO.h b/IO.h index 07ed700..49a6a16 100644 --- a/IO.h +++ b/IO.h @@ -22,7 +22,7 @@ #include "Globals.h" #include "SampleRB.h" -#include "RSSIRB.h" +#include "RingBuffer.h" class CIO { public: @@ -57,11 +57,11 @@ public: void selfTest(); private: - bool m_started; + bool m_started; - CSampleRB m_rxBuffer; - CSampleRB m_txBuffer; - CRSSIRB m_rssiBuffer; + CSampleRB m_rxBuffer; + CSampleRB m_txBuffer; + CRingBuffer m_rssiBuffer; arm_biquad_casd_df1_inst_q31 m_dcFilter; q31_t m_dcState[4]; diff --git a/RSSIRB.cpp b/RSSIRB.cpp deleted file mode 100644 index 7607773..0000000 --- a/RSSIRB.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* -TX fifo control - Copyright (C) KI6ZUM 2015 -Copyright (C) 2015,2016 by Jonathan Naylor G4KLX - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public -License as published by the Free Software Foundation; either -version 2 of the License, or (at your option) any later version. - -This library 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 -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the -Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, -Boston, MA 02110-1301, USA. -*/ - -#include "RSSIRB.h" - -CRSSIRB::CRSSIRB(uint16_t length) : -m_length(length), -m_head(0U), -m_tail(0U), -m_full(false), -m_overflow(false) -{ - m_rssi = new uint16_t[length]; -} - -uint16_t CRSSIRB::getSpace() const -{ - uint16_t n = 0U; - - if (m_tail == m_head) - n = m_full ? 0U : m_length; - else if (m_tail < m_head) - n = m_length - m_head + m_tail; - else - n = m_tail - m_head; - - if (n > m_length) - n = 0U; - - return n; -} - -uint16_t CRSSIRB::getData() const -{ - if (m_tail == m_head) - return m_full ? m_length : 0U; - else if (m_tail < m_head) - return m_head - m_tail; - else - return m_length - m_tail + m_head; -} - -bool CRSSIRB::put(uint16_t rssi) -{ - if (m_full) { - m_overflow = true; - return false; - } - - m_rssi[m_head] = rssi; - - m_head++; - if (m_head >= m_length) - m_head = 0U; - - if (m_head == m_tail) - m_full = true; - - return true; -} - -bool CRSSIRB::get(uint16_t& rssi) -{ - if (m_head == m_tail && !m_full) - return false; - - rssi = m_rssi[m_tail]; - - m_full = false; - - m_tail++; - if (m_tail >= m_length) - m_tail = 0U; - - return true; -} - -bool CRSSIRB::hasOverflowed() -{ - bool overflow = m_overflow; - - m_overflow = false; - - return overflow; -} diff --git a/RSSIRB.h b/RSSIRB.h deleted file mode 100644 index f3db3de..0000000 --- a/RSSIRB.h +++ /dev/null @@ -1,59 +0,0 @@ -/* -Serial fifo control - Copyright (C) KI6ZUM 2015 -Copyright (C) 2015,2016 by Jonathan Naylor G4KLX - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public -License as published by the Free Software Foundation; either -version 2 of the License, or (at your option) any later version. - -This library 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 -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the -Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, -Boston, MA 02110-1301, USA. -*/ - -#if !defined(RSSIRB_H) -#define RSSIRB_H - -#if defined(STM32F4XX) -#include "stm32f4xx.h" -#elif defined(STM32F7XX) -#include "stm32f7xx.h" -#elif defined(STM32F105xC) -#include "stm32f1xx.h" -#include -#else -#include -#endif - -class CRSSIRB { -public: - CRSSIRB(uint16_t length); - - uint16_t getSpace() const; - - uint16_t getData() const; - - bool put(uint16_t rssi); - - bool get(uint16_t& rssi); - - bool hasOverflowed(); - -private: - uint16_t m_length; - volatile uint16_t* m_rssi; - volatile uint16_t m_head; - volatile uint16_t m_tail; - volatile bool m_full; - bool m_overflow; -}; - -#endif - From 6c3dd265abe4cb880dadf18c6e2604119f6ec5bd Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 9 May 2020 13:33:05 +0200 Subject: [PATCH 02/11] No more need to specify each types for ringBuffer --- RingBuffer.h | 6 +++--- RingBuffer.cpp => RingBuffer.impl.h | 13 ++++--------- 2 files changed, 7 insertions(+), 12 deletions(-) rename RingBuffer.cpp => RingBuffer.impl.h (89%) diff --git a/RingBuffer.h b/RingBuffer.h index cfb8d53..4878d8c 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -53,9 +53,9 @@ public: uint16_t getData() const; - bool put(TDATATYPE sample); + bool put(const TDATATYPE item); - bool get(TDATATYPE& sample); + bool get(TDATATYPE& item); TDATATYPE peek() const; @@ -72,6 +72,6 @@ private: bool m_overflow; }; - +#include "RingBuffer.impl.h" #endif \ No newline at end of file diff --git a/RingBuffer.cpp b/RingBuffer.impl.h similarity index 89% rename from RingBuffer.cpp rename to RingBuffer.impl.h index f350023..6839255 100644 --- a/RingBuffer.cpp +++ b/RingBuffer.impl.h @@ -56,7 +56,7 @@ template uint16_t CRingBuffer::getData() const return m_length - m_tail + m_head; } -template bool CRingBuffer::put(TDATATYPE sample) +template bool CRingBuffer::put(const TDATATYPE sample) { if (m_full) { m_overflow = true; @@ -80,12 +80,12 @@ template TDATATYPE CRingBuffer::peek() const return m_buffer[m_tail]; } -template bool CRingBuffer::get(TDATATYPE& sample) +template bool CRingBuffer::get(TDATATYPE& item) { if (m_head == m_tail && !m_full) return false; - sample = m_buffer[m_tail]; + item = m_buffer[m_tail]; m_full = false; @@ -111,9 +111,4 @@ template void CRingBuffer::reset() m_tail = 0U; m_full = false; m_overflow = false; -} - -//Add here any declarations you need -template class CRingBuffer; -template class CRingBuffer; -template class CRingBuffer; \ No newline at end of file +} \ No newline at end of file From 26a703c68c96bd5d54317260c9af3930c8b44ba8 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 9 May 2020 17:27:52 +0200 Subject: [PATCH 03/11] IO RXbuffer now uses template RingBuffer --- IO.cpp | 9 +++++---- IO.h | 15 ++++++++++++++- IODue.cpp | 2 +- IOSTM.cpp | 2 +- IOSTM_CMSIS.cpp | 2 +- IOTeensy.cpp | 2 +- RingBuffer.h | 4 ++-- RingBuffer.impl.h | 4 ++-- 8 files changed, 27 insertions(+), 13 deletions(-) diff --git a/IO.cpp b/IO.cpp index a737693..2e61358 100644 --- a/IO.cpp +++ b/IO.cpp @@ -272,15 +272,16 @@ void CIO::process() uint16_t rssi[RX_BLOCK_SIZE]; for (uint16_t i = 0U; i < RX_BLOCK_SIZE; i++) { - uint16_t sample; - m_rxBuffer.get(sample, control[i]); + TSample sample; + m_rxBuffer.get(sample); + control[i] = sample.control; m_rssiBuffer.get(rssi[i]); // Detect ADC overflow - if (m_detect && (sample == 0U || sample == 4095U)) + if (m_detect && (sample.sample == 0U || sample.sample == 4095U)) m_adcOverflow++; - q15_t res1 = q15_t(sample) - m_rxDCOffset; + q15_t res1 = q15_t(sample.sample) - m_rxDCOffset; q31_t res2 = res1 * m_rxLevel; samples[i] = q15_t(__SSAT((res2 >> 15), 16)); } diff --git a/IO.h b/IO.h index 49a6a16..dd6dde9 100644 --- a/IO.h +++ b/IO.h @@ -24,6 +24,19 @@ #include "SampleRB.h" #include "RingBuffer.h" +struct TSample { + volatile uint16_t sample; + volatile uint8_t control; + + TSample operator=(const volatile TSample& other) volatile { + return {other.sample, other.control}; + } + + volatile TSample operator=(const TSample& other) volatile { + return {other.sample, other.control}; + } +}; + class CIO { public: CIO(); @@ -59,7 +72,7 @@ public: private: bool m_started; - CSampleRB m_rxBuffer; + CRingBuffer m_rxBuffer; CSampleRB m_txBuffer; CRingBuffer m_rssiBuffer; diff --git a/IODue.cpp b/IODue.cpp index b2aec0a..493d4e2 100644 --- a/IODue.cpp +++ b/IODue.cpp @@ -192,7 +192,7 @@ void CIO::interrupt() DACC->DACC_CDR = sample; sample = ADC->ADC_CDR[ADC_CDR_Chan]; - m_rxBuffer.put(sample, control); + m_rxBuffer.put({sample, control}); #if defined(SEND_RSSI_DATA) m_rssiBuffer.put(ADC->ADC_CDR[RSSI_CDR_Chan]); diff --git a/IOSTM.cpp b/IOSTM.cpp index 1d565fe..13a2e58 100644 --- a/IOSTM.cpp +++ b/IOSTM.cpp @@ -1346,7 +1346,7 @@ void CIO::interrupt() ADC_ClearFlag(ADC1, ADC_FLAG_EOC); ADC_SoftwareStartConv(ADC1); - m_rxBuffer.put(sample, control); + m_rxBuffer.put({sample, control}); m_rssiBuffer.put(rawRSSI); m_watchdog++; diff --git a/IOSTM_CMSIS.cpp b/IOSTM_CMSIS.cpp index 6cd92a2..c596a0f 100644 --- a/IOSTM_CMSIS.cpp +++ b/IOSTM_CMSIS.cpp @@ -420,7 +420,7 @@ void CIO::interrupt() // Read value from ADC1 and ADC2 sample = ADC1->DR; // read conversion result; EOC is cleared by this read - m_rxBuffer.put(sample, control); + m_rxBuffer.put({sample, control}); #if defined(SEND_RSSI_DATA) rawRSSI = ADC2->DR; m_rssiBuffer.put(rawRSSI); diff --git a/IOTeensy.cpp b/IOTeensy.cpp index aaece08..d501030 100644 --- a/IOTeensy.cpp +++ b/IOTeensy.cpp @@ -169,7 +169,7 @@ void CIO::interrupt() if ((ADC0_SC1A & ADC_SC1_COCO) == ADC_SC1_COCO) { sample = ADC0_RA; - m_rxBuffer.put(sample, control); + m_rxBuffer.put({sample, control}); } #if defined(SEND_RSSI_DATA) diff --git a/RingBuffer.h b/RingBuffer.h index 4878d8c..ffd346e 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -53,9 +53,9 @@ public: uint16_t getData() const; - bool put(const TDATATYPE item); + bool put(const volatile TDATATYPE item) volatile; - bool get(TDATATYPE& item); + bool get(volatile TDATATYPE& item) volatile; TDATATYPE peek() const; diff --git a/RingBuffer.impl.h b/RingBuffer.impl.h index 6839255..13c52e6 100644 --- a/RingBuffer.impl.h +++ b/RingBuffer.impl.h @@ -56,7 +56,7 @@ template uint16_t CRingBuffer::getData() const return m_length - m_tail + m_head; } -template bool CRingBuffer::put(const TDATATYPE sample) +template bool CRingBuffer::put(const TDATATYPE sample) volatile { if (m_full) { m_overflow = true; @@ -80,7 +80,7 @@ template TDATATYPE CRingBuffer::peek() const return m_buffer[m_tail]; } -template bool CRingBuffer::get(TDATATYPE& item) +template bool CRingBuffer::get(volatile TDATATYPE& item) volatile { if (m_head == m_tail && !m_full) return false; From 05d21c0a14b79dbed754c8fd3d27301454fb96b5 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 9 May 2020 17:44:44 +0200 Subject: [PATCH 04/11] TX Buffer now using Templae RB --- IO.cpp | 4 +- IO.h | 3 +- IODue.cpp | 11 +++-- IOSTM.cpp | 15 ++++--- IOSTM_CMSIS.cpp | 11 +++-- IOTeensy.cpp | 11 +++-- SampleRB.cpp | 106 ------------------------------------------------ SampleRB.h | 60 --------------------------- 8 files changed, 25 insertions(+), 196 deletions(-) delete mode 100644 SampleRB.cpp delete mode 100644 SampleRB.h diff --git a/IO.cpp b/IO.cpp index 2e61358..7b000fb 100644 --- a/IO.cpp +++ b/IO.cpp @@ -493,9 +493,9 @@ void CIO::write(MMDVM_STATE mode, q15_t* samples, uint16_t length, const uint8_t m_dacOverflow++; if (control == NULL) - m_txBuffer.put(res3, MARK_NONE); + m_txBuffer.put({res3, MARK_NONE}); else - m_txBuffer.put(res3, control[i]); + m_txBuffer.put({res3, control[i]}); } } diff --git a/IO.h b/IO.h index dd6dde9..5955d7a 100644 --- a/IO.h +++ b/IO.h @@ -21,7 +21,6 @@ #include "Globals.h" -#include "SampleRB.h" #include "RingBuffer.h" struct TSample { @@ -73,7 +72,7 @@ private: bool m_started; CRingBuffer m_rxBuffer; - CSampleRB m_txBuffer; + CRingBuffer m_txBuffer; CRingBuffer m_rssiBuffer; arm_biquad_casd_df1_inst_q31 m_dcFilter; diff --git a/IODue.cpp b/IODue.cpp index 493d4e2..2c76ef7 100644 --- a/IODue.cpp +++ b/IODue.cpp @@ -185,14 +185,13 @@ void CIO::startInt() void CIO::interrupt() { if ((ADC->ADC_ISR & ADC_ISR_EOC_Chan) == ADC_ISR_EOC_Chan) { // Ensure there was an End-of-Conversion and we read the ISR reg - uint8_t control = MARK_NONE; - uint16_t sample = DC_OFFSET; + TSample sample = {DC_OFFSET, MARK_NONE}; - m_txBuffer.get(sample, control); - DACC->DACC_CDR = sample; + m_txBuffer.get(sample); + DACC->DACC_CDR = sample.sample; - sample = ADC->ADC_CDR[ADC_CDR_Chan]; - m_rxBuffer.put({sample, control}); + sample.sample = ADC->ADC_CDR[ADC_CDR_Chan]; + m_rxBuffer.put(sample); #if defined(SEND_RSSI_DATA) m_rssiBuffer.put(ADC->ADC_CDR[RSSI_CDR_Chan]); diff --git a/IOSTM.cpp b/IOSTM.cpp index 13a2e58..0c2a62a 100644 --- a/IOSTM.cpp +++ b/IOSTM.cpp @@ -1318,25 +1318,24 @@ void CIO::startInt() void CIO::interrupt() { - uint8_t control = MARK_NONE; - uint16_t sample = DC_OFFSET; + TSample sample = {DC_OFFSET, MARK_NONE}; uint16_t rawRSSI = 0U; - m_txBuffer.get(sample, control); + m_txBuffer.get(sample); // Send the value to the DAC #if defined(STM32F4_NUCLEO) && defined(STM32F4_NUCLEO_ARDUINO_HEADER) - DAC_SetChannel2Data(DAC_Align_12b_R, sample); + DAC_SetChannel2Data(DAC_Align_12b_R, sample.sample); #else - DAC_SetChannel1Data(DAC_Align_12b_R, sample); + DAC_SetChannel1Data(DAC_Align_12b_R, sample.sample); #endif // Read value from ADC1 and ADC2 if ((ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)) { // shouldn't be still in reset at this point so null the sample value? - sample = 0U; + sample.sample = 0U; } else { - sample = ADC_GetConversionValue(ADC1); + sample.sample = ADC_GetConversionValue(ADC1); #if defined(SEND_RSSI_DATA) rawRSSI = ADC_GetConversionValue(ADC2); #endif @@ -1346,7 +1345,7 @@ void CIO::interrupt() ADC_ClearFlag(ADC1, ADC_FLAG_EOC); ADC_SoftwareStartConv(ADC1); - m_rxBuffer.put({sample, control}); + m_rxBuffer.put(sample); m_rssiBuffer.put(rawRSSI); m_watchdog++; diff --git a/IOSTM_CMSIS.cpp b/IOSTM_CMSIS.cpp index c596a0f..03339a5 100644 --- a/IOSTM_CMSIS.cpp +++ b/IOSTM_CMSIS.cpp @@ -397,8 +397,7 @@ void CIO::startInt() void CIO::interrupt() { - uint8_t control = MARK_NONE; - uint16_t sample = DC_OFFSET; + TSample sample = {DC_OFFSET, MARK_NONE}; #if defined(SEND_RSSI_DATA) uint16_t rawRSSI = 0U; #endif @@ -415,12 +414,12 @@ void CIO::interrupt() *rssi_adon = 1; #endif - m_txBuffer.get(sample, control); - DAC->DHR12R1 = sample; // Send the value to the DAC + m_txBuffer.get(sample); + DAC->DHR12R1 = sample.sample; // Send the value to the DAC // Read value from ADC1 and ADC2 - sample = ADC1->DR; // read conversion result; EOC is cleared by this read - m_rxBuffer.put({sample, control}); + sample.sample = ADC1->DR; // read conversion result; EOC is cleared by this read + m_rxBuffer.put(sample); #if defined(SEND_RSSI_DATA) rawRSSI = ADC2->DR; m_rssiBuffer.put(rawRSSI); diff --git a/IOTeensy.cpp b/IOTeensy.cpp index d501030..8cb285e 100644 --- a/IOTeensy.cpp +++ b/IOTeensy.cpp @@ -161,15 +161,14 @@ void CIO::startInt() void CIO::interrupt() { - uint8_t control = MARK_NONE; - uint16_t sample = DC_OFFSET; + TSample sample = {DC_OFFSET, MARK_NONE}; - m_txBuffer.get(sample, control); - *(int16_t *)&(DAC0_DAT0L) = sample; + m_txBuffer.get(sample); + *(int16_t *)&(DAC0_DAT0L) = sample.sample; if ((ADC0_SC1A & ADC_SC1_COCO) == ADC_SC1_COCO) { - sample = ADC0_RA; - m_rxBuffer.put({sample, control}); + sample.sample = ADC0_RA; + m_rxBuffer.put(sample); } #if defined(SEND_RSSI_DATA) diff --git a/SampleRB.cpp b/SampleRB.cpp deleted file mode 100644 index 5fa2ef6..0000000 --- a/SampleRB.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* -TX fifo control - Copyright (C) KI6ZUM 2015 -Copyright (C) 2015,2016 by Jonathan Naylor G4KLX - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public -License as published by the Free Software Foundation; either -version 2 of the License, or (at your option) any later version. - -This library 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 -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the -Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, -Boston, MA 02110-1301, USA. -*/ - -#include "SampleRB.h" - -CSampleRB::CSampleRB(uint16_t length) : -m_length(length), -m_head(0U), -m_tail(0U), -m_full(false), -m_overflow(false) -{ - m_samples = new uint16_t[length]; - m_control = new uint8_t[length]; -} - -uint16_t CSampleRB::getSpace() const -{ - uint16_t n = 0U; - - if (m_tail == m_head) - n = m_full ? 0U : m_length; - else if (m_tail < m_head) - n = m_length - m_head + m_tail; - else - n = m_tail - m_head; - - if (n > m_length) - n = 0U; - - return n; -} - -uint16_t CSampleRB::getData() const -{ - if (m_tail == m_head) - return m_full ? m_length : 0U; - else if (m_tail < m_head) - return m_head - m_tail; - else - return m_length - m_tail + m_head; -} - -bool CSampleRB::put(uint16_t sample, uint8_t control) -{ - if (m_full) { - m_overflow = true; - return false; - } - - m_samples[m_head] = sample; - m_control[m_head] = control; - - m_head++; - if (m_head >= m_length) - m_head = 0U; - - if (m_head == m_tail) - m_full = true; - - return true; -} - -bool CSampleRB::get(uint16_t& sample, uint8_t& control) -{ - if (m_head == m_tail && !m_full) - return false; - - sample = m_samples[m_tail]; - control = m_control[m_tail]; - - m_full = false; - - m_tail++; - if (m_tail >= m_length) - m_tail = 0U; - - return true; -} - -bool CSampleRB::hasOverflowed() -{ - bool overflow = m_overflow; - - m_overflow = false; - - return overflow; -} - diff --git a/SampleRB.h b/SampleRB.h deleted file mode 100644 index e0b6269..0000000 --- a/SampleRB.h +++ /dev/null @@ -1,60 +0,0 @@ -/* -Serial fifo control - Copyright (C) KI6ZUM 2015 -Copyright (C) 2015,2016 by Jonathan Naylor G4KLX - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public -License as published by the Free Software Foundation; either -version 2 of the License, or (at your option) any later version. - -This library 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 -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the -Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, -Boston, MA 02110-1301, USA. -*/ - -#if !defined(SAMPLERB_H) -#define SAMPLERB_H - -#if defined(STM32F4XX) -#include "stm32f4xx.h" -#elif defined(STM32F7XX) -#include "stm32f7xx.h" -#elif defined(STM32F105xC) -#include "stm32f1xx.h" -#include -#else -#include -#endif - -class CSampleRB { -public: - CSampleRB(uint16_t length); - - uint16_t getSpace() const; - - uint16_t getData() const; - - bool put(uint16_t sample, uint8_t control); - - bool get(uint16_t& sample, uint8_t& control); - - bool hasOverflowed(); - -private: - uint16_t m_length; - volatile uint16_t* m_samples; - volatile uint8_t* m_control; - volatile uint16_t m_head; - volatile uint16_t m_tail; - volatile bool m_full; - bool m_overflow; -}; - -#endif - From b0ba3b5dd8980f1bf0fee0f24c076343d7b00652 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sat, 9 May 2020 19:57:40 +0200 Subject: [PATCH 05/11] SerialRB now inherits from RingBuffer --- RingBuffer.h | 2 +- SerialRB.cpp | 76 +++++----------------------------------------------- SerialRB.h | 30 ++------------------- 3 files changed, 9 insertions(+), 99 deletions(-) diff --git a/RingBuffer.h b/RingBuffer.h index ffd346e..7a066b9 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -56,7 +56,7 @@ public: bool put(const volatile TDATATYPE item) volatile; bool get(volatile TDATATYPE& item) volatile; - + TDATATYPE peek() const; bool hasOverflowed(); diff --git a/SerialRB.cpp b/SerialRB.cpp index f6b1578..ca3350d 100644 --- a/SerialRB.cpp +++ b/SerialRB.cpp @@ -21,80 +21,16 @@ Boston, MA 02110-1301, USA. #include "SerialRB.h" CSerialRB::CSerialRB(uint16_t length) : -m_length(length), -m_head(0U), -m_tail(0U), -m_full(false) +CRingBuffer(length) { - m_buffer = new uint8_t[length]; -} - -void CSerialRB::reset() -{ - m_head = 0U; - m_tail = 0U; - m_full = false; -} - -uint16_t CSerialRB::getSpace() const -{ - uint16_t n = 0U; - - if (m_tail == m_head) - n = m_full ? 0U : m_length; - else if (m_tail < m_head) - n = m_length - m_head + m_tail; - else - n = m_tail - m_head; - - if (n > m_length) - n = 0U; - - return n; -} - -uint16_t CSerialRB::getData() const -{ - if (m_tail == m_head) - return m_full ? m_length : 0U; - else if (m_tail < m_head) - return m_head - m_tail; - else - return m_length - m_tail + m_head; -} - -bool CSerialRB::put(uint8_t c) -{ - if (m_full) - return false; - - m_buffer[m_head] = c; - - m_head++; - if (m_head >= m_length) - m_head = 0U; - - if (m_head == m_tail) - m_full = true; - - return true; -} - -uint8_t CSerialRB::peek() const -{ - return m_buffer[m_tail]; } uint8_t CSerialRB::get() { - uint8_t value = m_buffer[m_tail]; - - m_full = false; - - m_tail++; - if (m_tail >= m_length) - m_tail = 0U; - - return value; + uint8_t value; + if(CRingBuffer::get(value)) + return value; + + return 0U; } diff --git a/SerialRB.h b/SerialRB.h index 7b6b7b9..1357732 100644 --- a/SerialRB.h +++ b/SerialRB.h @@ -21,41 +21,15 @@ Boston, MA 02110-1301, USA. #if !defined(SERIALRB_H) #define SERIALRB_H -#if defined(STM32F4XX) -#include "stm32f4xx.h" -#elif defined(STM32F7XX) -#include "stm32f7xx.h" -#elif defined(STM32F105xC) -#include "stm32f1xx.h" -#include -#else -#include -#endif +#include "RingBuffer.h" const uint16_t SERIAL_RINGBUFFER_SIZE = 370U; -class CSerialRB { +class CSerialRB : public CRingBuffer{ public: CSerialRB(uint16_t length = SERIAL_RINGBUFFER_SIZE); - uint16_t getSpace() const; - - uint16_t getData() const; - - void reset(); - - bool put(uint8_t c); - - uint8_t peek() const; - uint8_t get(); - -private: - uint16_t m_length; - volatile uint8_t* m_buffer; - volatile uint16_t m_head; - volatile uint16_t m_tail; - volatile bool m_full; }; #endif From 55df5fe04c9d13ae286f1b954761bb6fe39d43f5 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 10 May 2020 00:03:05 +0200 Subject: [PATCH 06/11] Remove useless stuff --- IO.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/IO.h b/IO.h index 5955d7a..aec26a8 100644 --- a/IO.h +++ b/IO.h @@ -26,14 +26,6 @@ struct TSample { volatile uint16_t sample; volatile uint8_t control; - - TSample operator=(const volatile TSample& other) volatile { - return {other.sample, other.control}; - } - - volatile TSample operator=(const TSample& other) volatile { - return {other.sample, other.control}; - } }; class CIO { From 16c3d418eb48020fe92e405cfddaea486df1168f Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 10 May 2020 00:03:32 +0200 Subject: [PATCH 07/11] Buffer no longer volatile, not needed --- RingBuffer.h | 10 ++++++---- RingBuffer.impl.h | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/RingBuffer.h b/RingBuffer.h index 7a066b9..d8de703 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -47,15 +47,17 @@ template class CRingBuffer { public: - CRingBuffer(uint16_t length); + CRingBuffer(uint16_t length = 370U); uint16_t getSpace() const; uint16_t getData() const; - bool put(const volatile TDATATYPE item) volatile; + bool put(TDATATYPE item) volatile; - bool get(volatile TDATATYPE& item) volatile; + bool get(TDATATYPE& item); + + TDATATYPE get(); TDATATYPE peek() const; @@ -65,7 +67,7 @@ public: private: uint16_t m_length; - volatile TDATATYPE* m_buffer; + TDATATYPE* m_buffer; volatile uint16_t m_head; volatile uint16_t m_tail; volatile bool m_full; diff --git a/RingBuffer.impl.h b/RingBuffer.impl.h index 13c52e6..27bb8f6 100644 --- a/RingBuffer.impl.h +++ b/RingBuffer.impl.h @@ -56,14 +56,14 @@ template uint16_t CRingBuffer::getData() const return m_length - m_tail + m_head; } -template bool CRingBuffer::put(const TDATATYPE sample) volatile +template bool CRingBuffer::put(TDATATYPE item) volatile { if (m_full) { m_overflow = true; return false; } - m_buffer[m_head] = sample; + m_buffer[m_head] = item; m_head++; if (m_head >= m_length) @@ -80,7 +80,7 @@ template TDATATYPE CRingBuffer::peek() const return m_buffer[m_tail]; } -template bool CRingBuffer::get(volatile TDATATYPE& item) volatile +template bool CRingBuffer::get(TDATATYPE& item) { if (m_head == m_tail && !m_full) return false; @@ -111,4 +111,13 @@ template void CRingBuffer::reset() m_tail = 0U; m_full = false; m_overflow = false; -} \ No newline at end of file +} + +template TDATATYPE CRingBuffer::get() +{ + TDATATYPE value; + if(get(value)) + return value; + + //return 0U; +} From 03f18451f76dbfeada1fdeb6f7781a9450703171 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 10 May 2020 06:50:35 +0200 Subject: [PATCH 08/11] Remove CSerialBuffer, use CRingBuffer --- DMRDMOTX.cpp | 2 +- DMRDMOTX.h | 4 ++-- DMRTX.cpp | 2 +- DMRTX.h | 4 ++-- DStarTX.cpp | 13 ++++++++----- DStarTX.h | 4 ++-- NXDNTX.cpp | 3 ++- NXDNTX.h | 4 ++-- P25TX.cpp | 6 ++++-- P25TX.h | 4 ++-- POCSAGTX.cpp | 3 ++- POCSAGTX.h | 4 ++-- RingBuffer.h | 2 -- RingBuffer.impl.h | 9 --------- SerialPort.h | 4 ++-- YSFTX.cpp | 3 ++- YSFTX.h | 4 ++-- 17 files changed, 36 insertions(+), 39 deletions(-) diff --git a/DMRDMOTX.cpp b/DMRDMOTX.cpp index 141ed38..4cf458e 100644 --- a/DMRDMOTX.cpp +++ b/DMRDMOTX.cpp @@ -74,7 +74,7 @@ void CDMRDMOTX::process() m_poLen = m_txDelay; } else { for (unsigned int i = 0U; i < DMR_FRAME_LENGTH_BYTES; i++) - m_poBuffer[i] = m_fifo.get(); + m_fifo.get(m_poBuffer[i]); for (unsigned int i = 0U; i < 39U; i++) m_poBuffer[i + DMR_FRAME_LENGTH_BYTES] = PR_FILL[i]; diff --git a/DMRDMOTX.h b/DMRDMOTX.h index 62ef2c1..a5a3d6d 100644 --- a/DMRDMOTX.h +++ b/DMRDMOTX.h @@ -23,7 +23,7 @@ #include "Config.h" #include "DMRDefines.h" -#include "SerialRB.h" +#include "RingBuffer.h" class CDMRDMOTX { public: @@ -38,7 +38,7 @@ public: uint8_t getSpace() const; private: - CSerialRB m_fifo; + CRingBuffer m_fifo; arm_fir_interpolate_instance_q15 m_modFilter; q15_t m_modState[16U]; // blockSize + phaseLength - 1, 4 + 9 - 1 plus some spare uint8_t m_poBuffer[1200U]; diff --git a/DMRTX.cpp b/DMRTX.cpp index 81437ec..2541671 100644 --- a/DMRTX.cpp +++ b/DMRTX.cpp @@ -293,7 +293,7 @@ void CDMRTX::createData(uint8_t slotIndex) { if (m_fifo[slotIndex].getData() >= DMR_FRAME_LENGTH_BYTES && m_frameCount >= STARTUP_COUNT && m_abortCount[slotIndex] >= ABORT_COUNT) { for (unsigned int i = 0U; i < DMR_FRAME_LENGTH_BYTES; i++) { - m_poBuffer[i] = m_fifo[slotIndex].get(); + m_fifo[slotIndex].get(m_poBuffer[i]); m_markBuffer[i] = MARK_NONE; } } else { diff --git a/DMRTX.h b/DMRTX.h index 1e2f617..8539e15 100644 --- a/DMRTX.h +++ b/DMRTX.h @@ -23,7 +23,7 @@ #include "Config.h" #include "DMRDefines.h" -#include "SerialRB.h" +#include "RingBuffer.h" enum DMRTXSTATE { DMRTXSTATE_IDLE, @@ -59,7 +59,7 @@ public: void setColorCode(uint8_t colorCode); private: - CSerialRB m_fifo[2U]; + CRingBuffer m_fifo[2U]; arm_fir_interpolate_instance_q15 m_modFilter; q15_t m_modState[16U]; // blockSize + phaseLength - 1, 4 + 9 - 1 plus some spare DMRTXSTATE m_state; diff --git a/DStarTX.cpp b/DStarTX.cpp index 63eb910..0489ba1 100644 --- a/DStarTX.cpp +++ b/DStarTX.cpp @@ -216,12 +216,13 @@ void CDStarTX::process() for (uint16_t i = 0U; i < m_txDelay; i++) m_poBuffer[m_poLen++] = BIT_SYNC; } else { + uint8_t dummy; // Pop the type byte off - m_buffer.get(); + m_buffer.get(dummy); uint8_t header[DSTAR_HEADER_LENGTH_BYTES]; for (uint8_t i = 0U; i < DSTAR_HEADER_LENGTH_BYTES; i++) - header[i] = m_buffer.get(); + m_buffer.get(header[i]); uint8_t buffer[86U]; txHeader(header, buffer + 2U); @@ -239,17 +240,19 @@ void CDStarTX::process() if (type == DSTAR_DATA && m_poLen == 0U) { // Pop the type byte off - m_buffer.get(); + uint8_t dummy; + m_buffer.get(dummy); for (uint8_t i = 0U; i < DSTAR_DATA_LENGTH_BYTES; i++) - m_poBuffer[m_poLen++] = m_buffer.get(); + m_buffer.get(m_poBuffer[m_poLen++]); m_poPtr = 0U; } if (type == DSTAR_EOT && m_poLen == 0U) { // Pop the type byte off - m_buffer.get(); + uint8_t dummy; + m_buffer.get(dummy); for (uint8_t j = 0U; j < 3U; j++) { for (uint8_t i = 0U; i < DSTAR_EOT_LENGTH_BYTES; i++) diff --git a/DStarTX.h b/DStarTX.h index 24876d7..47852c5 100644 --- a/DStarTX.h +++ b/DStarTX.h @@ -21,7 +21,7 @@ #include "Config.h" -#include "SerialRB.h" +#include "RingBuffer.h" class CDStarTX { public: @@ -38,7 +38,7 @@ public: uint8_t getSpace() const; private: - CSerialRB m_buffer; + CRingBuffer m_buffer; arm_fir_interpolate_instance_q15 m_modFilter; q15_t m_modState[20U]; // blockSize + phaseLength - 1, 8 + 9 - 1 plus some spare uint8_t m_poBuffer[600U]; diff --git a/NXDNTX.cpp b/NXDNTX.cpp index 45d8ffa..c4f5845 100644 --- a/NXDNTX.cpp +++ b/NXDNTX.cpp @@ -82,7 +82,8 @@ void CNXDNTX::process() m_poBuffer[m_poLen++] = NXDN_PREAMBLE[2U]; } else { for (uint8_t i = 0U; i < NXDN_FRAME_LENGTH_BYTES; i++) { - uint8_t c = m_buffer.get(); + uint8_t c; + m_buffer.get(c); m_poBuffer[m_poLen++] = c; } } diff --git a/NXDNTX.h b/NXDNTX.h index eec74d4..f86279d 100644 --- a/NXDNTX.h +++ b/NXDNTX.h @@ -21,7 +21,7 @@ #include "Config.h" -#include "SerialRB.h" +#include "RingBuffer.h" class CNXDNTX { public: @@ -36,7 +36,7 @@ public: uint8_t getSpace() const; private: - CSerialRB m_buffer; + CRingBuffer m_buffer; arm_fir_interpolate_instance_q15 m_modFilter; arm_fir_instance_q15 m_sincFilter; q15_t m_modState[16U]; // blockSize + phaseLength - 1, 4 + 9 - 1 plus some spare diff --git a/P25TX.cpp b/P25TX.cpp index 9db1747..6501f4e 100644 --- a/P25TX.cpp +++ b/P25TX.cpp @@ -76,9 +76,11 @@ void CP25TX::process() for (uint16_t i = 0U; i < m_txDelay; i++) m_poBuffer[m_poLen++] = P25_START_SYNC; } else { - uint8_t length = m_buffer.get(); + uint8_t length; + m_buffer.get(length); for (uint8_t i = 0U; i < length; i++) { - uint8_t c = m_buffer.get(); + uint8_t c; + m_buffer.get(c); m_poBuffer[m_poLen++] = c; } } diff --git a/P25TX.h b/P25TX.h index 1d542f9..75be712 100644 --- a/P25TX.h +++ b/P25TX.h @@ -21,7 +21,7 @@ #include "Config.h" -#include "SerialRB.h" +#include "RingBuffer.h" class CP25TX { public: @@ -36,7 +36,7 @@ public: uint8_t getSpace() const; private: - CSerialRB m_buffer; + CRingBuffer m_buffer; arm_fir_interpolate_instance_q15 m_modFilter; arm_fir_instance_q15 m_lpFilter; q15_t m_modState[16U]; // blockSize + phaseLength - 1, 4 + 9 - 1 plus some spare diff --git a/POCSAGTX.cpp b/POCSAGTX.cpp index d53f9b7..0c252d8 100644 --- a/POCSAGTX.cpp +++ b/POCSAGTX.cpp @@ -61,7 +61,8 @@ void CPOCSAGTX::process() m_poBuffer[m_poLen++] = POCSAG_SYNC; } else { for (uint8_t i = 0U; i < POCSAG_FRAME_LENGTH_BYTES; i++) { - uint8_t c = m_buffer.get(); + uint8_t c; + m_buffer.get(c); m_poBuffer[m_poLen++] = c; } } diff --git a/POCSAGTX.h b/POCSAGTX.h index d640862..298a603 100644 --- a/POCSAGTX.h +++ b/POCSAGTX.h @@ -21,7 +21,7 @@ #include "Config.h" -#include "SerialRB.h" +#include "RingBuffer.h" class CPOCSAGTX { public: @@ -40,7 +40,7 @@ public: bool busy(); private: - CSerialRB m_buffer; + CRingBuffer m_buffer; arm_fir_instance_q15 m_modFilter; q15_t m_modState[170U]; // NoTaps + BlockSize - 1, 6 + 160 - 1 plus some spare uint8_t m_poBuffer[200U]; diff --git a/RingBuffer.h b/RingBuffer.h index d8de703..c69a470 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -56,8 +56,6 @@ public: bool put(TDATATYPE item) volatile; bool get(TDATATYPE& item); - - TDATATYPE get(); TDATATYPE peek() const; diff --git a/RingBuffer.impl.h b/RingBuffer.impl.h index 27bb8f6..06374ba 100644 --- a/RingBuffer.impl.h +++ b/RingBuffer.impl.h @@ -112,12 +112,3 @@ template void CRingBuffer::reset() m_full = false; m_overflow = false; } - -template TDATATYPE CRingBuffer::get() -{ - TDATATYPE value; - if(get(value)) - return value; - - //return 0U; -} diff --git a/SerialPort.h b/SerialPort.h index 004c626..d123bc7 100644 --- a/SerialPort.h +++ b/SerialPort.h @@ -21,7 +21,7 @@ #include "Config.h" #include "Globals.h" -#include "SerialRB.h" +#include "RingBuffer.h" class CSerialPort { @@ -66,7 +66,7 @@ private: uint8_t m_ptr; uint8_t m_len; bool m_debug; - CSerialRB m_repeat; + CRingBuffer m_repeat; void sendACK(); void sendNAK(uint8_t err); diff --git a/YSFTX.cpp b/YSFTX.cpp index 4f0400d..5328418 100644 --- a/YSFTX.cpp +++ b/YSFTX.cpp @@ -76,7 +76,8 @@ void CYSFTX::process() m_poBuffer[m_poLen++] = YSF_START_SYNC; } else { for (uint8_t i = 0U; i < YSF_FRAME_LENGTH_BYTES; i++) { - uint8_t c = m_buffer.get(); + uint8_t c; + m_buffer.get(c); m_poBuffer[m_poLen++] = c; } } diff --git a/YSFTX.h b/YSFTX.h index 647767c..0e88a8c 100644 --- a/YSFTX.h +++ b/YSFTX.h @@ -21,7 +21,7 @@ #include "Config.h" -#include "SerialRB.h" +#include "RingBuffer.h" class CYSFTX { public: @@ -38,7 +38,7 @@ public: void setParams(bool on, uint8_t txHang); private: - CSerialRB m_buffer; + CRingBuffer m_buffer; arm_fir_interpolate_instance_q15 m_modFilter; q15_t m_modState[16U]; // blockSize + phaseLength - 1, 4 + 9 - 1 plus some spare uint8_t m_poBuffer[1200U]; From 807f01ba72ced995b092b60bb1d5c0bed4d60ebf Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 10 May 2020 07:00:58 +0200 Subject: [PATCH 09/11] Get rid of annoying PI warning --- Globals.h | 1 + RingBuffer.h | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Globals.h b/Globals.h index 5cb147a..9a6bbf8 100644 --- a/Globals.h +++ b/Globals.h @@ -28,6 +28,7 @@ #include "STM32Utils.h" #else #include +#undef PI //Undefine PI to get rid of annoying warning as it is also defined in arm_math.h. #endif #if defined(__SAM3X8E__) || defined(STM32F105xC) diff --git a/RingBuffer.h b/RingBuffer.h index c69a470..7615de0 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -20,7 +20,6 @@ #if !defined(RINGBUFFER_H) #define RINGBUFFER_H - #if defined(STM32F4XX) #include "stm32f4xx.h" #elif defined(STM32F7XX) @@ -30,6 +29,7 @@ #include #else #include +#undef PI #endif #if defined(__SAM3X8E__) || defined(STM32F105xC) @@ -56,7 +56,7 @@ public: bool put(TDATATYPE item) volatile; bool get(TDATATYPE& item); - + TDATATYPE peek() const; bool hasOverflowed(); From 0b31c40c2b37d1df0ad5d156cc4644c287fc7ae4 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 10 May 2020 08:01:10 +0200 Subject: [PATCH 10/11] Made Get volatile --- RingBuffer.h | 2 +- RingBuffer.impl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/RingBuffer.h b/RingBuffer.h index 7615de0..79f7814 100644 --- a/RingBuffer.h +++ b/RingBuffer.h @@ -55,7 +55,7 @@ public: bool put(TDATATYPE item) volatile; - bool get(TDATATYPE& item); + bool get(TDATATYPE& item) volatile; TDATATYPE peek() const; diff --git a/RingBuffer.impl.h b/RingBuffer.impl.h index 06374ba..ddcb21c 100644 --- a/RingBuffer.impl.h +++ b/RingBuffer.impl.h @@ -80,7 +80,7 @@ template TDATATYPE CRingBuffer::peek() const return m_buffer[m_tail]; } -template bool CRingBuffer::get(TDATATYPE& item) +template bool CRingBuffer::get(TDATATYPE& item) volatile { if (m_head == m_tail && !m_full) return false; From 0e8fdb381ab8390e9ac9e427a3972ae41d984e67 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 10 May 2020 09:07:21 +0200 Subject: [PATCH 11/11] Fix State machine not changing state --- FM.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FM.cpp b/FM.cpp index 8bf2448..a6085c3 100644 --- a/FM.cpp +++ b/FM.cpp @@ -85,8 +85,8 @@ void CFM::samples(bool cos, const q15_t* samples, uint8_t length) q15_t currentExtSample; bool inputExt = m_inputExtRB.get(currentExtSample);//always consume the external input data so it does not overflow - if ((!inputExt || CTCSS_NOT_READY(ctcssState)) && m_modemState != STATE_FM) { - //Not enough samples to determine if you have CTCSS, just carry on. + if (!inputExt && (CTCSS_NOT_READY(ctcssState)) && m_modemState != STATE_FM) { + //Not enough samples to determine if you have CTCSS, just carry on. But only if we haven't any external data in the queue continue; } else if ((inputExt || CTCSS_READY(ctcssState)) && m_modemState != STATE_FM) { //we had enough samples for CTCSS and we are in some other mode than FM