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/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 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/IO.cpp b/IO.cpp index a737693..7b000fb 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)); } @@ -492,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 07ed700..aec26a8 100644 --- a/IO.h +++ b/IO.h @@ -21,8 +21,12 @@ #include "Globals.h" -#include "SampleRB.h" -#include "RSSIRB.h" +#include "RingBuffer.h" + +struct TSample { + volatile uint16_t sample; + volatile uint8_t control; +}; class CIO { public: @@ -57,11 +61,11 @@ public: void selfTest(); private: - bool m_started; + bool m_started; - CSampleRB m_rxBuffer; - CSampleRB m_txBuffer; - CRSSIRB m_rssiBuffer; + CRingBuffer m_rxBuffer; + CRingBuffer m_txBuffer; + CRingBuffer m_rssiBuffer; arm_biquad_casd_df1_inst_q31 m_dcFilter; q31_t m_dcState[4]; diff --git a/IODue.cpp b/IODue.cpp index b2aec0a..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 1d565fe..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 6cd92a2..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 aaece08..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/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/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 - diff --git a/RingBuffer.h b/RingBuffer.h index cfb8d53..79f7814 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) @@ -47,15 +47,15 @@ template class CRingBuffer { public: - CRingBuffer(uint16_t length); + CRingBuffer(uint16_t length = 370U); uint16_t getSpace() const; uint16_t getData() const; - bool put(TDATATYPE sample); + bool put(TDATATYPE item) volatile; - bool get(TDATATYPE& sample); + bool get(TDATATYPE& item) volatile; TDATATYPE peek() const; @@ -65,13 +65,13 @@ 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; 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 91% rename from RingBuffer.cpp rename to RingBuffer.impl.h index f350023..ddcb21c 100644 --- a/RingBuffer.cpp +++ 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(TDATATYPE sample) +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,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) volatile { if (m_head == m_tail && !m_full) return false; - sample = m_buffer[m_tail]; + item = m_buffer[m_tail]; m_full = false; @@ -112,8 +112,3 @@ template void CRingBuffer::reset() 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 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 - 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/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 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];