From 7e103ccf2d1080885d8384074537053bc5691178 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Tue, 24 Jan 2023 18:56:49 +1100 Subject: [PATCH] Wait for full ADC Burst --- source/Core/BSP/Pinecilv2/IRQ.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/source/Core/BSP/Pinecilv2/IRQ.cpp b/source/Core/BSP/Pinecilv2/IRQ.cpp index 0d2acd93..623bba3c 100644 --- a/source/Core/BSP/Pinecilv2/IRQ.cpp +++ b/source/Core/BSP/Pinecilv2/IRQ.cpp @@ -23,11 +23,12 @@ void start_PWM_output(void); history ADC_Vin; history ADC_Temp; history ADC_Tip; +volatile uint8_t ADCBurstCounter = 0; void adc_fifo_irq(void) { if (ADC_GetIntStatus(ADC_INT_FIFO_READY) == SET) { - start_PWM_output(); // Restart the tip PWM // Read out all entries in the fifo while (ADC_Get_FIFO_Count()) { + ADCBurstCounter++; volatile uint32_t reading = ADC_Read_FIFO(); // As per manual, 26 bit reading; lowest 16 are the ADC uint16_t sample = reading & 0xFFFF; @@ -47,12 +48,18 @@ void adc_fifo_irq(void) { break; } } - // unblock the PID controller thread - if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { - BaseType_t xHigherPriorityTaskWoken = pdFALSE; - if (pidTaskNotification) { - vTaskNotifyGiveFromISR(pidTaskNotification, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + + if (ADCBurstCounter >= 8) { + ADCBurstCounter = 0; + start_PWM_output(); + + // unblock the PID controller thread + if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + if (pidTaskNotification) { + vTaskNotifyGiveFromISR(pidTaskNotification, &xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } } } } @@ -69,8 +76,6 @@ volatile bool lastPeriodWasFast = false; void start_PWM_output(void) { - TIMER_Enable(TIMER_CH0); - if (PWMSafetyTimer) { PWMSafetyTimer--; if (lastPeriodWasFast != fastPWM) { @@ -92,11 +97,12 @@ void start_PWM_output(void) { } else { PWM_Channel_Disable(PWM_Channel); } + TIMER_Enable(TIMER_CH0); } // Timer 0 is used to co-ordinate the ADC and the output PWM void timer0_comp0_callback(void) { - PWM_Channel_Disable(PWM_Channel); + TIMER_Disable(TIMER_CH0); ADC_Start(); } void timer0_comp1_callback(void) { PWM_Channel_Disable(PWM_Channel); } // Trigged at end of output cycle; turn off the tip PWM