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;