CONTEXT:
I'm trying to read 4 ADC channels at a rate of 48kHz each, using the GPT3 TRIGO as the trigger for the ADC1. Everytime the samples are converted, I store each channel in a fifo, to be able to read them later for DSP processing.
My clock is 180MHz which means that GPT3 is running at 90MHz.
Unfortunately, 4 channels at 48kHz each means that I have to sample at 192kHz, which is not a multiple of 90MHz.
The solution that I found is to read 5 channels which allows me to set GPT3's frequency to 90MHz, the timer interval at 375 and thus have a sampling frequency of 48kHz for each channlel. I just discard the value of the 5'th "virtual" channel.
PROBLEM:
Everytime I run the My problem is that I don't really understand the ADC callback in circular buffer mode. I have tried debugging my code and have noticed that my n i.e. the length of the converted buffer is always 16. Moreover, I saw that the address of this buffer is samplesBuffer for the first conversion, and then becomes samplesBuffer+160 for all the other conversions. I must have done something wrong, but I don't know what...
CODE:
initialisation of the ADC:
Code: Select all
#define ADC_BUF_DEPTH 32 // depth of buffer
#define ADC_CH_NUM 5 // number of used ADC channels
adcsample_t samplesMics[ADC_CH_NUM * ADC_BUF_DEPTH];
static ADCConversionGroup adc1Cfg = {
TRUE, // Circular buffer mode
(uint16_t)(ADC_CH_NUM), // number of channels
ovf_adc_mics_callback, // EoT callback
NULL, // Error callback
0, // CR1
ADC_CR2_EXTSEL_SRC(8) | ADC_CR2_EXTEN_RISING, // CR2
ADC_SMPR1_SMP_AN11(ADC_SAMPLE_3)
| ADC_SMPR1_SMP_AN14(ADC_SAMPLE_3)
| ADC_SMPR1_SMP_AN15(ADC_SAMPLE_3), // SMPR1
0, // SMPR2
ADC_SQR1_NUM_CH(ADC_CH_NUM), // SQR1
0, // SQR2
ADC_SQR3_SQ1_N(ADC_CHANNEL_IN7)
| ADC_SQR3_SQ2_N(ADC_CHANNEL_IN11)
| ADC_SQR3_SQ3_N(ADC_CHANNEL_IN14)
| ADC_SQR3_SQ4_N(ADC_CHANNEL_IN15) // SQR3
};
void ovf_adc1_init(void)
{
palSetGroupMode(GPIOC, PAL_PORT_BIT(1), 0, PAL_MODE_INPUT_ANALOG); //Pin PC1
palSetGroupMode(GPIOC, PAL_PORT_BIT(4), 0, PAL_MODE_INPUT_ANALOG); //Pin PC4
palSetGroupMode(GPIOC, PAL_PORT_BIT(5), 0, PAL_MODE_INPUT_ANALOG); //Pin PC5
palSetGroupMode(GPIOA, PAL_PORT_BIT(7), 0, PAL_MODE_INPUT_ANALOG); //Pin PC5
}
...
adcStart(&ADCD1, NULL);
adcStartConversion(&ADCD1, &adc1Cfg, samplesMics, ADC_BUF_DEPTH);
initialisation of the GPT:
Code: Select all
static const GPTConfig gpt3_ADC = {
.frequency = 90000000U,
.callback = NULL,
.cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
.dier = 0U
};
void ovf_tim3_init(void)
{
gptStart(&GPTD3, &gpt3_ADC);
gptStartContinuous(&GPTD3, 375); // 192 kHz
}
ADC Callback:
Code: Select all
void ovf_adc_callback(ADCDriver *adcp, adcsample_t *buffer, size_t n)
{
adc1_CB(buffer, n);
}
void adc1_CB(adcsample_t *samples, uint16_t length)
{
for(int i=0; i<length; i++)
{
fifo1.write(samples[i++]);
fifo2.write(samples[i++]);
fifo3.write(samples[i++]);
fifo4.write(samples[i++]);
}
}
Thanks in advance for any tips you can give me!