2010-09-27 09:28:45 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_ADC ADC Functions
|
|
|
|
* @brief STM32 ADC PIOS interface
|
|
|
|
* @{
|
|
|
|
*
|
2013-05-19 16:37:30 +02:00
|
|
|
* @file pios_adc.c
|
2012-05-01 18:11:31 +02:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
2010-09-27 09:28:45 +02:00
|
|
|
* @brief Analog to Digital converstion routines
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*****************************************************************************/
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2010-09-27 09:28:45 +02:00
|
|
|
* (at your option) any later version.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* This program 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 General Public License
|
2010-09-27 09:28:45 +02:00
|
|
|
* for more details.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-09-27 09:28:45 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
2013-03-15 19:25:30 +01:00
|
|
|
|
|
|
|
#ifdef PIOS_INCLUDE_ADC
|
|
|
|
|
2011-01-16 03:45:39 +01:00
|
|
|
#include <pios_adc_priv.h>
|
2011-01-16 03:45:22 +01:00
|
|
|
|
2012-05-01 18:11:31 +02:00
|
|
|
// Private types
|
|
|
|
|
|
|
|
enum pios_adc_dev_magic {
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_ADC_DEV_MAGIC = 0x58375124,
|
2012-05-01 18:11:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pios_adc_dev {
|
2013-05-19 16:37:30 +02:00
|
|
|
const struct pios_adc_cfg *cfg;
|
|
|
|
ADCCallback callback_function;
|
2012-05-01 18:11:31 +02:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
xQueueHandle data_queue;
|
2012-05-01 18:11:31 +02:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
volatile int16_t *valid_data_buffer;
|
|
|
|
volatile uint8_t adc_oversample;
|
|
|
|
uint8_t dma_block_size;
|
|
|
|
uint16_t dma_half_buffer_size;
|
2012-05-01 18:11:31 +02:00
|
|
|
#if defined(PIOS_INCLUDE_ADC)
|
2013-05-19 16:37:30 +02:00
|
|
|
int16_t fir_coeffs[PIOS_ADC_MAX_SAMPLES + 1] __attribute__((aligned(4)));
|
|
|
|
volatile int16_t raw_data_buffer[PIOS_ADC_MAX_SAMPLES] __attribute__((aligned(4))); // Double buffer that DMA just used
|
|
|
|
float downsampled_buffer[PIOS_ADC_NUM_CHANNELS] __attribute__((aligned(4)));
|
2012-05-01 18:11:31 +02:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
enum pios_adc_dev_magic magic;
|
2012-05-01 18:11:31 +02:00
|
|
|
};
|
|
|
|
|
2013-05-27 01:34:55 +02:00
|
|
|
float PIOS_ADC_PinGetVolt(uint32_t pin){
|
|
|
|
return ((float)PIOS_ADC_PinGet(pin)) * PIOS_ADC_VOLTAGE_SCALE;
|
|
|
|
}
|
|
|
|
|
2012-05-01 18:11:31 +02:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_adc_dev *pios_adc_dev;
|
2012-05-01 18:11:31 +02:00
|
|
|
#endif
|
|
|
|
|
2011-01-16 03:45:22 +01:00
|
|
|
// Private functions
|
|
|
|
void PIOS_ADC_downsample_data();
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_adc_dev *PIOS_ADC_Allocate();
|
2012-05-01 18:11:31 +02:00
|
|
|
static bool PIOS_ADC_validate(struct pios_adc_dev *);
|
2011-01-16 03:45:22 +01:00
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
/* Local Variables */
|
|
|
|
static GPIO_TypeDef *ADC_GPIO_PORT[PIOS_ADC_NUM_PINS] = PIOS_ADC_PORTS;
|
|
|
|
static const uint32_t ADC_GPIO_PIN[PIOS_ADC_NUM_PINS] = PIOS_ADC_PINS;
|
2013-05-19 16:37:30 +02:00
|
|
|
static const uint32_t ADC_CHANNEL[PIOS_ADC_NUM_PINS] = PIOS_ADC_CHANNELS;
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static ADC_TypeDef *ADC_MAPPING[PIOS_ADC_NUM_PINS] = PIOS_ADC_MAPPING;
|
2010-09-27 09:28:45 +02:00
|
|
|
static const uint32_t ADC_CHANNEL_MAPPING[PIOS_ADC_NUM_PINS] = PIOS_ADC_CHANNEL_MAPPING;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static bool PIOS_ADC_validate(struct pios_adc_dev *dev)
|
2012-05-01 18:11:31 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (dev == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-01 18:11:31 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return dev->magic == PIOS_ADC_DEV_MAGIC;
|
2012-05-01 18:11:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
static struct pios_adc_dev *PIOS_ADC_Allocate()
|
2012-05-01 18:11:31 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct pios_adc_dev *adc_dev;
|
|
|
|
|
|
|
|
adc_dev = (struct pios_adc_dev *)pvPortMalloc(sizeof(*adc_dev));
|
|
|
|
if (!adc_dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
adc_dev->magic = PIOS_ADC_DEV_MAGIC;
|
|
|
|
return adc_dev;
|
2012-05-01 18:11:31 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#error Not implemented
|
|
|
|
#endif
|
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
/**
|
2011-01-16 17:02:53 +01:00
|
|
|
* @brief Initialise the ADC Peripheral, configure to run at the max oversampling
|
2011-01-16 03:45:22 +01:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t PIOS_ADC_Init(const struct pios_adc_cfg *cfg)
|
2012-05-01 18:11:31 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
pios_adc_dev = PIOS_ADC_Allocate();
|
|
|
|
if (pios_adc_dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pios_adc_dev->cfg = cfg;
|
|
|
|
pios_adc_dev->callback_function = NULL;
|
2012-05-01 18:11:31 +02:00
|
|
|
|
2011-02-03 03:42:43 +01:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
pios_adc_dev->data_queue = NULL;
|
2011-02-03 03:42:43 +01:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
/* Setup analog pins */
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
|
|
|
|
|
|
|
|
/* Enable each ADC pin in the array */
|
|
|
|
for (int32_t i = 0; i < PIOS_ADC_NUM_PINS; i++) {
|
|
|
|
GPIO_InitStructure.GPIO_Pin = ADC_GPIO_PIN[i];
|
|
|
|
GPIO_Init(ADC_GPIO_PORT[i], &GPIO_InitStructure);
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_ADC_Config(PIOS_ADC_MAX_OVERSAMPLING);
|
|
|
|
|
|
|
|
return 0;
|
2011-01-16 17:02:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Configure the ADC to run at a fixed oversampling
|
|
|
|
* @param[in] oversampling the amount of oversampling to run at
|
|
|
|
*/
|
|
|
|
void PIOS_ADC_Config(uint32_t oversampling)
|
2013-05-19 16:37:30 +02:00
|
|
|
{
|
|
|
|
pios_adc_dev->adc_oversample = (oversampling > PIOS_ADC_MAX_OVERSAMPLING) ? PIOS_ADC_MAX_OVERSAMPLING : oversampling;
|
|
|
|
|
|
|
|
ADC_DeInit(ADC1);
|
|
|
|
ADC_DeInit(ADC2);
|
|
|
|
|
|
|
|
/* Disable interrupts */
|
|
|
|
DMA_ITConfig(pios_adc_dev->cfg->dma.rx.channel, pios_adc_dev->cfg->dma.irq.flags, DISABLE);
|
|
|
|
|
|
|
|
/* Enable ADC clocks */
|
|
|
|
PIOS_ADC_CLOCK_FUNCTION;
|
|
|
|
|
|
|
|
/* Map channels to conversion slots depending on the channel selection mask */
|
|
|
|
for (int32_t i = 0; i < PIOS_ADC_NUM_PINS; i++) {
|
|
|
|
ADC_RegularChannelConfig(ADC_MAPPING[i], ADC_CHANNEL[i],
|
|
|
|
ADC_CHANNEL_MAPPING[i],
|
|
|
|
PIOS_ADC_SAMPLE_TIME);
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
#if (PIOS_ADC_USE_TEMP_SENSOR)
|
2013-05-19 16:37:30 +02:00
|
|
|
ADC_TempSensorVrefintCmd(ENABLE);
|
|
|
|
ADC_RegularChannelConfig(PIOS_ADC_TEMP_SENSOR_ADC, ADC_Channel_16,
|
|
|
|
PIOS_ADC_TEMP_SENSOR_ADC_CHANNEL,
|
|
|
|
PIOS_ADC_SAMPLE_TIME);
|
2010-09-27 09:28:45 +02:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
// return
|
|
|
|
/* Configure ADCs */
|
|
|
|
ADC_InitTypeDef ADC_InitStructure;
|
|
|
|
ADC_StructInit(&ADC_InitStructure);
|
|
|
|
ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
|
|
|
|
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
|
|
|
|
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
|
|
|
|
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
|
|
|
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
|
|
|
ADC_InitStructure.ADC_NbrOfChannel = ((PIOS_ADC_NUM_CHANNELS + 1) >> 1);
|
|
|
|
ADC_Init(ADC1, &ADC_InitStructure);
|
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
#if (PIOS_ADC_USE_ADC2)
|
2013-05-19 16:37:30 +02:00
|
|
|
ADC_Init(ADC2, &ADC_InitStructure);
|
|
|
|
|
|
|
|
/* Enable ADC2 external trigger conversion (to synch with ADC1) */
|
|
|
|
ADC_ExternalTrigConvCmd(ADC2, ENABLE);
|
2010-09-27 09:28:45 +02:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
RCC_ADCCLKConfig(PIOS_ADC_ADCCLK);
|
|
|
|
|
|
|
|
/* Enable ADC1->DMA request */
|
|
|
|
ADC_DMACmd(ADC1, ENABLE);
|
|
|
|
|
|
|
|
/* ADC1 calibration */
|
|
|
|
ADC_Cmd(ADC1, ENABLE);
|
|
|
|
ADC_ResetCalibration(ADC1);
|
|
|
|
while (ADC_GetResetCalibrationStatus(ADC1)) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
ADC_StartCalibration(ADC1);
|
|
|
|
while (ADC_GetCalibrationStatus(ADC1)) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2010-09-27 09:28:45 +02:00
|
|
|
#if (PIOS_ADC_USE_ADC2)
|
2013-05-19 16:37:30 +02:00
|
|
|
/* ADC2 calibration */
|
|
|
|
ADC_Cmd(ADC2, ENABLE);
|
|
|
|
ADC_ResetCalibration(ADC2);
|
|
|
|
while (ADC_GetResetCalibrationStatus(ADC2)) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
ADC_StartCalibration(ADC2);
|
|
|
|
while (ADC_GetCalibrationStatus(ADC2)) {
|
|
|
|
;
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
/* This makes sure we have an even number of transfers if using ADC2 */
|
|
|
|
pios_adc_dev->dma_block_size = ((PIOS_ADC_NUM_CHANNELS + PIOS_ADC_USE_ADC2) >> PIOS_ADC_USE_ADC2) << PIOS_ADC_USE_ADC2;
|
|
|
|
pios_adc_dev->dma_half_buffer_size = pios_adc_dev->dma_block_size * pios_adc_dev->adc_oversample;
|
|
|
|
|
|
|
|
/* Configure DMA channel */
|
|
|
|
DMA_InitTypeDef dma_init = pios_adc_dev->cfg->dma.rx.init;
|
|
|
|
dma_init.DMA_MemoryBaseAddr = (uint32_t)&pios_adc_dev->raw_data_buffer[0];
|
|
|
|
dma_init.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
|
|
|
dma_init.DMA_BufferSize = pios_adc_dev->dma_half_buffer_size; /* x2 for double buffer /2 for 32-bit xfr */
|
|
|
|
DMA_Init(pios_adc_dev->cfg->dma.rx.channel, &dma_init);
|
|
|
|
DMA_Cmd(pios_adc_dev->cfg->dma.rx.channel, ENABLE);
|
|
|
|
|
|
|
|
/* Trigger interrupt when for half conversions too to indicate double buffer */
|
|
|
|
DMA_ITConfig(pios_adc_dev->cfg->dma.rx.channel, DMA_IT_TC, ENABLE);
|
|
|
|
DMA_ITConfig(pios_adc_dev->cfg->dma.rx.channel, DMA_IT_HT, ENABLE);
|
|
|
|
|
|
|
|
/* Configure DMA interrupt */
|
|
|
|
NVIC_Init(&pios_adc_dev->cfg->dma.irq.init);
|
|
|
|
|
|
|
|
/* Finally start initial conversion */
|
|
|
|
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
|
|
|
|
|
|
|
/* Use simple averaging filter for now */
|
|
|
|
for (int32_t i = 0; i < pios_adc_dev->adc_oversample; i++) {
|
|
|
|
pios_adc_dev->fir_coeffs[i] = 1;
|
|
|
|
}
|
|
|
|
pios_adc_dev->fir_coeffs[pios_adc_dev->adc_oversample] = pios_adc_dev->adc_oversample;
|
|
|
|
|
|
|
|
/* Enable DMA1 clock */
|
|
|
|
RCC_AHBPeriphClockCmd(pios_adc_dev->cfg->dma.ahb_clk, ENABLE);
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-16 03:45:22 +01:00
|
|
|
* Returns value of an ADC Pin
|
|
|
|
* \param[in] pin number
|
|
|
|
* \return ADC pin value - resolution depends on the selected oversampling rate
|
|
|
|
* \return -1 if pin doesn't exist
|
|
|
|
*/
|
2010-09-27 09:28:45 +02:00
|
|
|
int32_t PIOS_ADC_PinGet(uint32_t pin)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Check if pin exists */
|
|
|
|
if (pin >= PIOS_ADC_NUM_CHANNELS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return last conversion result */
|
|
|
|
return pios_adc_dev->downsampled_buffer[pin];
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-16 03:45:22 +01:00
|
|
|
* @brief Set a callback function that is executed whenever
|
2013-05-19 16:37:30 +02:00
|
|
|
* the ADC double buffer swaps
|
2011-01-16 03:45:22 +01:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void PIOS_ADC_SetCallback(ADCCallback new_function)
|
2010-09-27 09:28:45 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
pios_adc_dev->callback_function = new_function;
|
2011-01-16 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
2011-02-03 03:42:43 +01:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Register a queue to add data to when downsampled
|
2011-02-03 03:42:43 +01:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void PIOS_ADC_SetQueue(xQueueHandle data_queue)
|
2011-02-03 03:42:43 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
pios_adc_dev->data_queue = data_queue;
|
2011-02-03 03:42:43 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-16 03:45:22 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Return the address of the downsampled data buffer
|
2011-01-16 03:45:22 +01:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
float *PIOS_ADC_GetBuffer(void)
|
2011-01-16 03:45:22 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return pios_adc_dev->downsampled_buffer;
|
2011-01-16 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Return the address of the raw data data buffer
|
2011-01-16 03:45:22 +01:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int16_t *PIOS_ADC_GetRawBuffer(void)
|
2011-01-16 03:45:22 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return (int16_t *)pios_adc_dev->valid_data_buffer;
|
2011-01-16 03:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the amount of over sampling
|
|
|
|
*/
|
|
|
|
uint8_t PIOS_ADC_GetOverSampling(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
return pios_adc_dev->adc_oversample;
|
2011-01-16 03:45:22 +01:00
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2011-01-16 03:45:22 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @brief Set the fir coefficients. Takes as many samples as the
|
2011-01-16 03:45:22 +01:00
|
|
|
* current filter order plus one (normalization)
|
|
|
|
*
|
|
|
|
* @param new_filter Array of adc_oversampling floats plus one for the
|
|
|
|
* filter coefficients
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
void PIOS_ADC_SetFIRCoefficients(float *new_filter)
|
2011-01-16 03:45:22 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Less than or equal to get normalization constant
|
|
|
|
for (int i = 0; i <= pios_adc_dev->adc_oversample; i++) {
|
|
|
|
pios_adc_dev->fir_coeffs[i] = new_filter[i];
|
|
|
|
}
|
2011-01-16 03:45:22 +01:00
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2011-01-16 03:45:22 +01:00
|
|
|
/**
|
|
|
|
* @brief Downsample the data for each of the channels then call
|
|
|
|
* callback function if installed
|
2013-05-19 16:37:30 +02:00
|
|
|
*/
|
2011-01-16 03:45:22 +01:00
|
|
|
void PIOS_ADC_downsample_data()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
uint16_t chan;
|
|
|
|
uint16_t sample;
|
|
|
|
float *downsampled_buffer = &pios_adc_dev->downsampled_buffer[0];
|
|
|
|
|
|
|
|
for (chan = 0; chan < PIOS_ADC_NUM_CHANNELS; chan++) {
|
|
|
|
int32_t sum = 0;
|
|
|
|
for (sample = 0; sample < pios_adc_dev->adc_oversample; sample++) {
|
|
|
|
sum += pios_adc_dev->valid_data_buffer[chan + sample * pios_adc_dev->dma_block_size] * pios_adc_dev->fir_coeffs[sample];
|
|
|
|
}
|
|
|
|
downsampled_buffer[chan] = (float)sum / pios_adc_dev->fir_coeffs[pios_adc_dev->adc_oversample];
|
|
|
|
}
|
|
|
|
|
2011-02-03 03:42:43 +01:00
|
|
|
#if defined(PIOS_INCLUDE_FREERTOS)
|
2013-05-19 16:37:30 +02:00
|
|
|
if (pios_adc_dev->data_queue) {
|
|
|
|
static portBASE_TYPE xHigherPriorityTaskWoken;
|
|
|
|
xQueueSendFromISR(pios_adc_dev->data_queue, pios_adc_dev->downsampled_buffer, &xHigherPriorityTaskWoken);
|
|
|
|
portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
|
|
|
|
}
|
2011-02-03 03:42:43 +01:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
if (pios_adc_dev->callback_function) {
|
|
|
|
pios_adc_dev->callback_function(pios_adc_dev->downsampled_buffer);
|
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
}
|
|
|
|
|
2011-01-16 03:45:22 +01:00
|
|
|
/**
|
|
|
|
* @brief Interrupt for half and full buffer transfer
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
2011-01-16 03:45:22 +01:00
|
|
|
* This interrupt handler swaps between the two halfs of the double buffer to make
|
|
|
|
* sure the ahrs uses the most recent data. Only swaps data when AHRS is idle, but
|
2013-05-19 16:37:30 +02:00
|
|
|
* really this is a pretense of a sanity check since the DMA engine is consantly
|
2011-01-16 03:45:22 +01:00
|
|
|
* running in the background. Keep an eye on the ekf_too_slow variable to make sure
|
|
|
|
* it's keeping up.
|
|
|
|
*/
|
|
|
|
void PIOS_ADC_DMA_Handler(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!PIOS_ADC_validate(pios_adc_dev)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DMA_GetFlagStatus(pios_adc_dev->cfg->full_flag /*DMA1_IT_TC1*/)) { // whole double buffer filled
|
|
|
|
pios_adc_dev->valid_data_buffer = &pios_adc_dev->raw_data_buffer[pios_adc_dev->dma_half_buffer_size];
|
|
|
|
DMA_ClearFlag(pios_adc_dev->cfg->full_flag);
|
|
|
|
PIOS_ADC_downsample_data();
|
|
|
|
} else if (DMA_GetFlagStatus(pios_adc_dev->cfg->half_flag /*DMA1_IT_HT1*/)) {
|
|
|
|
pios_adc_dev->valid_data_buffer = &pios_adc_dev->raw_data_buffer[0];
|
|
|
|
DMA_ClearFlag(pios_adc_dev->cfg->half_flag);
|
|
|
|
PIOS_ADC_downsample_data();
|
|
|
|
} else {
|
|
|
|
// This should not happen, probably due to transfer errors
|
|
|
|
DMA_ClearFlag(pios_adc_dev->cfg->dma.irq.flags /*DMA1_FLAG_GL1*/);
|
|
|
|
}
|
2011-01-16 03:45:22 +01:00
|
|
|
}
|
2010-09-27 09:28:45 +02:00
|
|
|
|
2013-03-15 19:25:30 +01:00
|
|
|
#endif /* PIOS_INCLUDE_ADC */
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
/**
|
2010-09-27 09:28:45 +02:00
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|