From e97c1dc746277419d772337f915c2741440cc761 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Fri, 11 Nov 2016 06:56:02 +0000 Subject: [PATCH] More work on Teensy interrupts. --- IO.h | 2 +- IODue.cpp | 6 ++--- IOSTM.cpp | 6 ++--- IOTeensy.cpp | 71 +++++++++++++++++++++++++++++++--------------------- 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/IO.h b/IO.h index 9e4dced..2113358 100644 --- a/IO.h +++ b/IO.h @@ -40,7 +40,7 @@ public: void setADCDetection(bool detect); void setMode(); - void interrupt(); + void interrupt(uint8_t source); void setParameters(bool rxInvert, bool txInvert, bool pttInvert, uint8_t rxLevel, uint8_t cwIdTXLevel, uint8_t dstarTXLevel, uint8_t dmrTXLevel, uint8_t ysfTXLevel, uint8_t p25TXLevel); diff --git a/IODue.cpp b/IODue.cpp index e465da3..17d4fa4 100644 --- a/IODue.cpp +++ b/IODue.cpp @@ -73,7 +73,7 @@ const uint16_t DC_OFFSET = 2048U; extern "C" { void ADC_Handler() { - io.interrupt(); + io.interrupt(0U); } } @@ -97,7 +97,7 @@ void CIO::initInt() void CIO::startInt() { if (ADC->ADC_ISR & ADC_ISR_EOC_Chan) // Ensure there was an End-of-Conversion and we read the ISR reg - io.interrupt(); + io.interrupt(0U); // Set up the ADC NVIC_EnableIRQ(ADC_IRQn); // Enable ADC interrupt vector @@ -157,7 +157,7 @@ void CIO::startInt() digitalWrite(PIN_LED, HIGH); } -void CIO::interrupt() +void CIO::interrupt(uint8_t source) { 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; diff --git a/IOSTM.cpp b/IOSTM.cpp index 39e4e99..fdfb2ae 100644 --- a/IOSTM.cpp +++ b/IOSTM.cpp @@ -68,7 +68,7 @@ extern "C" { void TIM2_IRQHandler() { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); - io.interrupt(); + io.interrupt(0U); } } } @@ -163,7 +163,7 @@ void CIO::initInt() void CIO::startInt() { if ((ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != RESET)) - io.interrupt(); + io.interrupt(0U); // ADC1 PA0 analog input // ADC2 PA1 analog input @@ -283,7 +283,7 @@ void CIO::startInt() GPIO_SetBits(PORT_LED, PIN_LED); } -void CIO::interrupt() +void CIO::interrupt(uint8_t source) { uint8_t control = MARK_NONE; uint16_t sample = DC_OFFSET; diff --git a/IOTeensy.cpp b/IOTeensy.cpp index c555f7d..1cd90f9 100644 --- a/IOTeensy.cpp +++ b/IOTeensy.cpp @@ -55,10 +55,17 @@ const uint16_t DC_OFFSET = 2048U; extern "C" { - void pdb_isr() + void adc0_isr() { - io.interrupt(); + io.interrupt(0U); } + +#if defined(SEND_RSSI_DATA) + void adc1_isr() + { + io.interrupt(1U); + } +#endif } void CIO::initInt() @@ -80,6 +87,14 @@ void CIO::initInt() void CIO::startInt() { + if ((ADC0_SC1A & ADC_SC1_COCO) == ADC_SC1_COCO) + io.interrupt(0U); + +#if defined(SEND_RSSI_DATA) + if ((ADC1_SC1A & ADC_SC1_COCO) == ADC_SC1_COCO) + io.interrupt(1U); +#endif + // Initialise ADC0 ADC0_CFG1 = ADC_CFG1_ADIV(1) | ADC_CFG1_ADICLK(1) | ADC_CFG1_MODE(1) | ADC_CFG1_ADLSMP; // Single-ended 12 bits, long sample time @@ -95,6 +110,9 @@ void CIO::startInt() sum0 = (sum0 / 2U) | 0x8000U; ADC0_PG = sum0; + ADC0_SC1A = ADC_SC1_AIEN | PIN_ADC; // Enable ADC interrupt, use A0 + NVIC_ENABLE_IRQ(IRQ_ADC0); + #if defined(SEND_RSSI_DATA) // Initialise ADC1 ADC1_CFG1 = ADC_CFG1_ADIV(1) | ADC_CFG1_ADICLK(1) | ADC_CFG1_MODE(1) | @@ -110,6 +128,9 @@ void CIO::startInt() uint16_t sum1 = ADC1_CLPS + ADC1_CLP4 + ADC1_CLP3 + ADC1_CLP2 + ADC1_CLP1 + ADC1_CLP0; // Plus side gain sum1 = (sum1 / 2U) | 0x8000U; ADC1_PG = sum1; + + ADC1_SC1A = ADC_SC1_AIEN | PIN_RSSI; // Enable ADC interrupt, use A2 + NVIC_ENABLE_IRQ(IRQ_ADC1); #endif // Setup PDB for ADC0 at 24 kHz @@ -138,41 +159,35 @@ void CIO::startInt() digitalWrite(PIN_LED, HIGH); } -void CIO::interrupt() +void CIO::interrupt(uint8_t source) { - uint8_t control = MARK_NONE; - uint16_t sample = DC_OFFSET; + if (source == 0U) { // ADC0 + uint8_t control = MARK_NONE; + uint16_t sample = DC_OFFSET; - m_txBuffer.get(sample, control); - *(int16_t *)&(DAC0_DAT0L) = sample; + m_txBuffer.get(sample, control); + *(int16_t *)&(DAC0_DAT0L) = sample; - ADC0_SC1A = PIN_ADC; // Start read on A0 - - // Wait for the read to complete - while ((ADC0_SC1A & ADC_SC1_COCO) != ADC_SC1_COCO) - ; - - sample = ADC0_RA; - m_rxBuffer.put(sample, control); + if ((ADC0_SC1A & ADC_SC1_COCO) == ADC_SC1_COCO) { + sample = ADC0_RA; + m_rxBuffer.put(sample, control); #if !defined(SEND_RSSI_DATA) - m_rssiBuffer.put(0U); + m_rssiBuffer.put(0U); #endif + } + + m_watchdog++; + } #if defined(SEND_RSSI_DATA) - ADC1_SC1A = PIN_RSSI; // Start read on A2 - - // Wait for the read to complete - while ((ADC1_SC1A & ADC_SC1_COCO) != ADC_SC1_COCO) - ; - - uint16_t rssi = ADC1_RA; - m_rssiBuffer.put(rssi); + if (source == 1U) { // ADC1 + if ((ADC1_SC1A & ADC_SC1_COCO) == ADC_SC1_COCO) { + uint16_t rssi = ADC1_RA; + m_rssiBuffer.put(rssi); + } + } #endif - - PDB0_SC &= ~PDB_SC_PDBIF; // Clear interrupt flag - - m_watchdog++; } bool CIO::getCOSInt()