Hello all I have been trying to use DMA and ADC (Independent Mode) on E series.
I went through rickkas7’s photoAudio3 sample program and tried to implement it. My requirement is very similar to this example.
I have 8 KHz square wave coming in to the A1 pin and I want to sample it at 16KHz frequency (sample both high and low values of the signal). I tried to run timer 3 at 16KHz and changed values in the timer initialization that triggers ADC conversion.
If I run the ADC on Dual Simultaneous Mode, I do get some values via DMA but, as sampling frequency is 16KHz, the sampled 12 bit ADC values should be 4095-0-4095-0(Ideally) and so on.
The issue is that I do not get these expected values. I get some random values like 4095-4095-0-0-0-4095-4095-4095-4095-0-4095(This is totally random and changes every cycle).
As per my understanding, this issue could be due to one of these reasons:
- Incorrect Timer 3 frequency
- ADC Dual Simultaneous Mode with DMA Access Mode 1
I think timer 3 frequency and initialization is correct and something is wrong with ADC Dual Simultaneous Mode. I tried to change ADC configuration to the Independent Mode but, I am confused about other parameters that needs to be changed(DMA Access Mode, DMA Peripheral Address etc). Reference Manuel is also confusing for me.
I can use ADC in any mode as long as I get it to work. Can pros out there please throw some light on this?
Thanks!
Here’s my code below(Ignore syntax errors, I might have messed it up while writing this post):
#include "Particle.h"
//
// ADCDMA - Class to use Photon ADC in DMA Mode
//
#include "adc_hal.h"
#include "gpio_hal.h"
#include "pinmap_hal.h"
#include "pinmap_impl.h"
SYSTEM_THREAD(ENABLED);
void buttonHandler(system_event_t event, int data);
const size_t SAMPLE_BUF_SIZE = 100;
const int SAMPLE_PIN = A1;
const long SAMPLE_RATE = 16000;
uint16_t samples[SAMPLE_BUF_SIZE];
enum State { STATE_WAITING, STATE_CONNECT, STATE_RUNNING, STATE_FINISH };
State state = STATE_WAITING;
class ADCDMA {
public:
ADCDMA(int pin, uint16_t *buf, size_t bufSize);
virtual ~ADCDMA();
void start(size_t freqHZ);
void stop();
private:
int pin;
uint16_t *buf;
size_t bufSize;
};
ADCDMA::ADCDMA(int pin, uint16_t *buf, size_t bufSize) : pin(pin), buf(buf), bufSize(bufSize) {
}
ADCDMA::~ADCDMA() {
}
void ADCDMA::start(size_t freqHZ) {
// Using Dual ADC Regular Simultaneous DMA Mode 1
// Using Timer3. To change timers, make sure you edit all of:
// RCC_APB1Periph_TIM3, TIM3, ADC_ExternalTrigConv_T3_TRGO
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
// Set the pin as analog input
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
HAL_Pin_Mode(pin, AN_INPUT);
// Enable the DMA Stream IRQ Channel
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 60000000UL = 60 MHz Timer Clock = HCLK / 2
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = (60000000UL / freqHZ) - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); // ADC_ExternalTrigConv_T3_TRGO
TIM_Cmd(TIM3, ENABLE);
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
// DMA2 Stream0 channel0 configuration
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buf;
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40012308; // CDR_ADDRESS; Packed ADC1, ADC2
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = bufSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
// ADC Common Init
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
// ADC1 configuration
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Init(ADC2, &ADC_InitStructure);
STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
ADC_RegularChannelConfig(ADC1, PIN_MAP[pin].adc_channel, 1, ADC_SampleTime_15Cycles);
ADC_RegularChannelConfig(ADC2, PIN_MAP[pin].adc_channel, 1, ADC_SampleTime_15Cycles);
// Enable DMA request after last transfer (Multi-ADC mode)
ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
// Enable ADCs
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
ADC_SoftwareStartConv(ADC1);
}
void ADCDMA::stop() {
// Stop the ADC
ADC_Cmd(ADC1, DISABLE);
ADC_Cmd(ADC2, DISABLE);
DMA_Cmd(DMA2_Stream0, DISABLE);
// Stop the timer
TIM_Cmd(TIM3, DISABLE);
}
ADCDMA adcDMA(SAMPLE_PIN, samples, SAMPLE_BUF_SIZE);
void setup() {
Serial1.begin(9600);
System.on(button_click, buttonHandler);
adcDMA.start(SAMPLE_RATE);
}
void loop() {
switch(state) {
case STATE_WAITING:
break;
case STATE_CONNECT:
ADC_SoftwareStartConv(ADC1);
Serial1.println("starting");
state = STATE_RUNNING;
break;
case STATE_RUNNING:
if (DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0))
{
DMA_ClearFlag(DMA2_Stream0, DMA_FLAG_TCIF0);
state = STATE_FINISH;
}
break;
case STATE_FINISH:
for(uint16_t i = 0; i < SAMPLE_BUF_SIZE; i++)
{
Serial1.println(samples[i]);
samples[i] = 0;
}
Serial1.println("stopping");
state = STATE_WAITING;
break;
}
}
// button handler for the SETUP button, used to toggle recording on and off
void buttonHandler(system_event_t event, int data) {
switch(state) {
case STATE_WAITING:
state = STATE_CONNECT;
break;
}
}