Monday, March 4, 2013

STM32F4 and “Most” of What You Ever Wanted to Learn about Its Analog Digital Converter (ADC)



When developing a lab for an upcoming course, I finally got the opportunity to work with the STM32F4 ADC. The capabilities of the MCU and its ADC almost qualify it as digital signal processor (DSP). Not surprisingly the ADC ended up a highly-configurable but arguably complex beast. The novelty of the F4 series makes documented sample code on the web sparse. 
In this post, I summarize and exemplify the different operating modes of a single ADC that comes with the STM32F4 microcontroller (MCU) from ST microelectronics. Dual ADC modes are not discussed (therefore "most" and not everything).
This post is targeted at engineers, hobbyists, students, and engineers with working knowledge of see and some basic exposure to the STM32 peripheral libraries (any of their STM32Fxxx series). I am explaining and exemplifying the different operating modes of the ADC and show where to look for the right information at each step.

Overview of Operating Modes

STMs Application Note AN3116 explains the different operating modes quite nicely but fails to address any practical considerations of their implementation. For an overview, I am just reviewing the properties shortly. The ADC can perform a single conversion, or continuously convert values. The conversion can be performed on a single channel or multiple channels. The latter is referred to as scan configuration. The permutation of these modes gives the different operating modes.
  1. Single-channel & single-conversion
  2. Multi-channel (scan) & multiple-conversion
  3. Single-channel & continuous operation
  4. Multi-channel (scan) & continuous operation
Each of the four modes can be triggered by modifying a memory-mapped register or an external trigger such as a timer. In any mode the sample time per channel can be specified as number of ADC clock steps. Furthermore, the STM32F4 contains an analog watchdog that triggers an interrupt when values fall outside a specified range. In any case interrupts can be used to signal the completion of a conversion.
The first mode performs a single conversion on a single channel when triggered and then stops. This can be used to measure a calibration value or perform checks between different program states.
The second mode performs a scan conversion, reading one channel after the other (of up to 16 channels in a single scan). This is very useful to capture sensor data at discrete points in time such as positions.
The third mode converts a single channel continuously. This can be useful to get data on a critical analog input.
The last mode is just scan conversion performed continuously.

Setup Description

Being an ARM processor, peripherals need to be clocked to be switched on. This applies to all components that have anything to do with the clock configuration. Again the STM32 being high-end MCU from STM makes the clock three a bit of a Maze (at the time of writing to be found in Reference Manual 90, Figure 9).
Use their clock configuration utility that actually generates you the initialisation code; or, be brave, print the page and determines the register value yourself. Once the clock itself is set up, the individual peripherals can be enabled with the RCC_xxx functions, depending on the bus they are connected to.
In this example we use ADC1, which is connected to Advanced Peripheral Bus 2 (APB2), which is a subdivision of the ARM AMBA high-performance bus (AHB). In this example it is assumed that the main clock is configured at 168 MHz and that APB2 runs at 84 MHz. Furthermore to constrain the scope just to the ADC we use the internal reference channels VREF_int and Temperature as input for our experiments.

Single-Conversion Mode


Based on the setup-description we describe in this chapter how to perform a single-channel conversion.
The initialisation looks as follows:

/* Define ADC init structures */
 ADC_InitTypeDef       ADC_InitStructure;
 ADC_CommonInitTypeDef ADC_CommonInitStructure;

 /* IMPORTANT: populates structures with reset values */
 ADC_StructInit(&ADC_InitStructure);
 ADC_CommonStructInit(&ADC_CommonInitStructure);

 /* enable ADC clock */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

 /* init ADCs in independent mode, div clock by two */
 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
 ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
 ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
 ADC_CommonInit(&ADC_CommonInitStructure);

 /* init ADC1: 12bit, single-conversion */
 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
 ADC_InitStructure.ADC_ExternalTrigConvEdge = 0;
 ADC_InitStructure.ADC_ExternalTrigConv = 0;
 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
 ADC_InitStructure.ADC_NbrOfConversion = 1;
 ADC_Init(ADC1, &ADC_InitStructure);

 /* Enable VREF_INT & Temperature channel */
 ADC_TempSensorVrefintCmd(ENABLE);

 /* Enable ADC1 **************************************************************/
 ADC_Cmd(ADC1, ENABLE);
The most important take home is to always initialise the structures of the STM32 peripheral libraries. Since those structures are allocated from the stack the values are unpredictable. Using the InitStruct methods of the peripheral library, the structures will always reflect the reset state of the peripheral.
In addition, even though we are just using a single ADC channel on a single ADC, we have to initialise the entire ADC block. The ADC block can operate in interleaved or independent mode. To use a single channel only the ADC block has to be configured in independent mode.
Furthermore, for a single conversion the modes Scan and Continuous are disabled. The internal VREF_INT and TEMP channels have to be explicitly enabled. They are disabled by default to conserve power. Finally the ADC has to be enabled for further usage.

The read function is relatively uneventful. After the ADC has been initialised, all we have to specify is the ADC channel, the sampling time, and wait for the conversion to complete.

uint16_t adc_read(ADC_TypeDef* ADCx, uint8_t channel, uint8_t ADC_SampleTime) {
 /* Configure Channel */
 ADC_RegularChannelConfig(ADCx, channel, 1, ADC_SampleTime);

 /* check if conversion was started, if not start */
 ADC_SoftwareStartConv(ADCx);

 /* wait for end of conversion */
 while((ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET));

 return ADC_GetConversionValue(ADCx);
}
The temperature channel and the voltage can then be read individually as follows:
uint16_t temp = adc_read(ADC1, ADC_Channel_16, ADC_SampleTime_480Cycles);
uint16_t vrefint = adc_read(ADC1, ADC_Channel_17, ADC_SampleTime_480Cycles);
The temperature sensor is wired to ADC channel 16 and VREF_INT is wired to ADC channel 17. In both cases a sample time of 480 ADC clock cycles (approx 11.4uS) and 12 cycles (0.3uS) ramp up time. The respective STM32F4 datasheet gives the conversion values for the temperature and the voltage. Some examples for the STM32F4Discovery are shown below.
#define ADC_TEMPERATURE_V25       760  /* mV */
#define ADC_TEMPERATURE_AVG_SLOPE 2500 /* mV/C */

int32_t adc_value_to_temp(const uint16_t value, const uint16_t steps_per_volt) {
 /* convert reading to millivolts */
 int32_t mv = ((uint32_t)value * 1000)/steps_per_volt;
 return (mv - ADC_TEMPERATURE_V25) / 25 + 25;
}

uint16_t adc_steps_per_volt(const uint16_t vref_value) {
 return (vref_value * 10) / 12; /* assume 1.2V internal voltage */
}
The example shown so far can be used for simple ADC experiments, in which one has to programmatically read an analog channel non-periodically. This mode can be useful for the calibration of sensors or for testing environmental conditions, when entering a different program state.

ADC Scan-Mode with Single-Conversion Interrupt


After having demonstrated how to read individual channels programmatically, we will describe how the scan-mode of the ADC works.
Since the ADC only has a single register that stores the information of the last conversion, there are only two ways to retrieve the values that are sampled through the scan.

  • Configure an interrupt to trigger after each channel is sampled,
  • Configure the DMA controller to copy the data of the channels into a defined memory location.
In this section we will illustrate the former and describe the latter mode for continuous-mode operation later on. Since this example uses interrupts, the nested vectored interrupt controller (NVIC) needs to be initialised as well. The code for the initialisation is shown below:
/* Unchanged: Define ADC init structures */
        ADC_InitTypeDef       ADC_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;

        /* Unchanged: populate default values before use */
        ADC_StructInit(&ADC_InitStructure);
        ADC_CommonStructInit(&ADC_CommonInitStructure);

        /* Unchanged: enable ADC peripheral */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

        /* Unchanged: init ADC */
        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
        ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
        ADC_CommonInit(&ADC_CommonInitStructure);

        /* Changed: Enabled scan mode conversion*/
        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE; 
        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; 
        ADC_InitStructure.ADC_DataAlign= ADC_DataAlign_Right; 
        ADC_InitStructure.ADC_ExternalTrigConv= 0; 
        ADC_InitStructure.ADC_ExternalTrigConvEdge= 0; 
        ADC_InitStructure.ADC_NbrOfConversion= 2; 

        ADC_Init(ADC1, &ADC_InitStructure);

        /* Enable Vref & Temperature channel */
        ADC_TempSensorVrefintCmd(ENABLE);

        /* Configure channels */
        /* Temp sensor */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_480Cycles);
        /* VREF_int (2nd) */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_480Cycles);



        ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);

        /* Enable ADC interrupts */
        ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);

        /* Configure NVIC */
        NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
        NVIC_Init(&NVIC_InitStructure);

        /* Enable ADC1 **************************************************************/
        ADC_Cmd(ADC1, ENABLE);
In essence what changed is that now the ADC_ScanConvMode is set enabled, the channel initialisation has been moved into the initialisation routine, the NVIC configuration has been added.
The order of the channel conversion is controlled by assigning priorities (1...16) as third parameter to ADC_RegularChannelConfig. The total number of channels to be converted in sequence is specified by  ADC_InitStructure.ADC_NbrOfConversion.
As said before since the data register of the ADC only holds the last converted value, we have to trigger the interrupt on every conversion. This is done by calling ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE). This call enables the end-of-conversion flag after each channel, which triggers the end-of-conversion interrupt every time this flag is set.
In the interrupt service routine (ISR) the converted value can be copied to a global buffer and the ISR has to acknowledge the interrupt once the value is received. A global counter can be used to identify the channel that was converted.

uint16_t temp = 0;
uint16_t vref = 0;
uint16_t counter = 0;

void ADC_IRQHandler() {
        /* acknowledge interrupt */
        uint16_t value;
        ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);

        value = ADC_GetConversionValue(ADC1);
        if(counter % 2 == 0) {
                temp = value;
        } else {
                vref = value;
        }
        counter++;
}
It should be strongly noted that in the main program the scan is started by ADC_SoftwareStartConv(ADC1). The counter value can be checked against to determine when to read out the values.
        ADC_SoftwareStartConv(ADC1);
        while(counter <= 2);
        if(counter>= 2) {
                uint16_t v1, v2;
                v1 = temp;
                v2 = vref;
                /* … */
        }
This example shows how to read out multiple channels of an ADC in a single-scan. Interrupts are needed to identify the converted values of the individual channels. This mode is useful, when data points from multiple sources need to be taken at the same time.

ADC Continous-Scan Mode with DMA


After having discussed how to sample multiple values in a single sweep, we modify the example to execute the ADC continuously. Furthermore, because the interrupt after each channel conversion created substantial overhead, we use DMA in this example to copy the channel values to a cyclic buffer and just interrupt after the conversion of all channels is complete.
volatile uint16_t ADCBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA};

...

        /* Define ADC init structures */
        ADC_InitTypeDef       ADC_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        DMA_InitTypeDef DMA_InitStructure;

        /* Initialise DMA */
        DMA_StructInit(&DMA_InitStructure);

        /* Enable clock on DMA1 */
        /* Enable DMA2, thats where ADC is hooked on -> see Tab 20 (RM00090) */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);               
        /* config of DMAC */
        DMA_InitStructure.DMA_Channel = DMA_Channel_0;                     
        DMA_InitStructure.DMA_BufferSize = 2;                       /* 2 * memsize */
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;     /* direction */
        DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;      /* no FIFO */
        DMA_InitStructure.DMA_FIFOThreshold = 0;
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;            /* circular buffer */
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;        /* high priority */
        /* config of memory */
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADCBuffer; /* target address */
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; /* 16 bit */
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /* increment after wrt */
        /* config of peripheral */
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_Init(DMA2_Stream0, &DMA_InitStructure); /* See Table 20 for mapping */
        DMA_Cmd(DMA2_Stream0, ENABLE);

        ADC_StructInit(&ADC_InitStructure);
        ADC_CommonStructInit(&ADC_CommonInitStructure);

        /* init ADC clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

        /* init ADC */
        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
        ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
        ADC_CommonInit(&ADC_CommonInitStructure);

        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
        ADC_InitStructure.ADC_ExternalTrigConvEdge = 0;
        ADC_InitStructure.ADC_ExternalTrigConv = 0;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_NbrOfConversion = 2; /* 2 channels in total */
        ADC_Init(ADC1, &ADC_InitStructure);

        /* Enable Vref & Temperature channel */
        ADC_TempSensorVrefintCmd(ENABLE);

        /* Configure channels */
        /* Temp sensor */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_480Cycles);
        /* VREF_int (2nd) */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_480Cycles);

        /* Enable ADC interrupts */
        ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);

        /* Enable DMA request after last transfer (Single-ADC mode) */
        ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

        /* Enable ADC3 DMA */
        ADC_DMACmd(ADC1, ENABLE);
        /* Configure NVIC */
        NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
        NVIC_Init(&NVIC_InitStructure);

        /* Enable ADC1 **************************************************************/
        ADC_Cmd(ADC1, ENABLE);
The major change to the previous configurations is that now scan mode and continuous modes are enabled. Furthermore the DMA is configured to copy the buffer off the ADC register into the target location. It should be strongly noted that individual DMA peripherals and their channels are multiplexed among multiple components. The mapping is explained in Table 20 in RM00090. Furthermore, it should be noted that the DMA performs after the EOC interrupt triggers. A reliable exchange protocol would be to copy the previous buffer value of the DMA buffer in the scan conversion for the main control program or to configure DMA interrupts (which is beyond the scope of this article).
void ADC_IRQHandler() {
        /* acknowledge interrupt */
        ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);

        /* nothing to do here except for possibly copying DMA buffers */
}

This example shows how to convert ADC channels continuously and store them in a global buffer using DMA. The ADC interrupt can be used to implement a synchronization scheme with the main program. This mode is useful for applications, in which a stream of continuous data is needed. An alternative to the cyclic buffer described in the example would be to use a linear buffer and use a counter in the ADC to abort the sampling after a fixed number of samples have been converted.

ADC Continous-Scan Mode with DMA and Additional GPIO Channels


So far we have just discussed examples that read off the on-chip channels. We modify the previous example to add three additional external pins as input to the ADC. We have chosen ADC_IN 10 ... 12, which are mapped to PORTC 0 ... 2. Note the pin mapping can be obtained from the datasheet of your STM32F4 chip. For the STM32F4Discovery the mapping is highlighted in the above picture.
The modified initialisation looks as follows:

/* Define ADC init structures */
        ADC_InitTypeDef       ADC_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        DMA_InitTypeDef DMA_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;

        /* Enable clock on DMA1 & GPIOC */
        /* Enable DMA2, thats where ADC is hooked on -> see Tab 20 (RM00090) */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2, ENABLE);

        /* Initialise GPIOs C0 (ADC123_IN10), C1 (ADC123_IN11), C2 (ADC123_IN12)*/
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AIN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

        /* Initialise DMA */
        DMA_StructInit(&DMA_InitStructure);

        /* config of DMAC */
        DMA_InitStructure.DMA_Channel = DMA_Channel_0; /* See Tab 20 */
        DMA_InitStructure.DMA_BufferSize = 5; /* 5 * memsize */
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;/* direction */
        DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;/* no FIFO */
        DMA_InitStructure.DMA_FIFOThreshold = 0;
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /* circular buffer */
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;
        /* config of memory */
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADCBuffer;/* target addr.  */
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; /* 16 bit */
       /* config of peripheral */
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_Init(DMA2_Stream0, &DMA_InitStructure); /* See Table 20 for mapping */
        DMA_Cmd(DMA2_Stream0, ENABLE);

        ADC_StructInit(&ADC_InitStructure);
        ADC_CommonStructInit(&ADC_CommonInitStructure);

        /* init ADC clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

        /* init ADC */
        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
        ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
        ADC_CommonInit(&ADC_CommonInitStructure);

        /* ADC1 Init: this is mostly done with ADC1->CR */
        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
        ADC_InitStructure.ADC_ExternalTrigConvEdge = 0;
        ADC_InitStructure.ADC_ExternalTrigConv = 0;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_NbrOfConversion = 5; /* 5 channels in total */
        ADC_Init(ADC1, &ADC_InitStructure);

        /* Enable Vref & Temperature channel */
        ADC_TempSensorVrefintCmd(ENABLE);

        /* Configure channels */
        /* Temp sensor */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_480Cycles);        
        /* VREF_int (2nd) */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_480Cycles);         
        /* PC0 */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 3, ADC_SampleTime_480Cycles);
        /* PC1 */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 4, ADC_SampleTime_480Cycles);
        /* PC2 */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 5, ADC_SampleTime_480Cycles);

        /* Enable ADC interrupts */
        ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);

        /* Enable DMA request after last transfer (Single-ADC mode) */
        ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

        /* Enable ADC3 DMA */
        ADC_DMACmd(ADC1, ENABLE);

        /* Configure NVIC */
        NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
        NVIC_Init(&NVIC_InitStructure);

        /* Enable ADC1 **************************************************************/
        ADC_Cmd(ADC1, ENABLE);

The main difference to the previous example is that now a GPIO initialization has been added to the initialization, that the total number of conversions changed, and that the number and priorities of the input channels changed.
The GPIOs used as inputs need to be configured as analog inputs (AIN) and not be pulled up or down. The configured port speed is not relevant for the AIN mode. Furthermore, the clock (RCC) for the GPIO also needs to be enabled for the block to function properly.
The outcomes of this experiment are similar to the previous example, except that now three external pins are added to the buffer that contains the analog conversions.

Time-Triggered ADC-Scan Mode with DMA and Additional GPIO Channels

So far we have discussed the different operating modes of the ADC that are triggered by software. In addition to those modes the ADC can also be triggered by an external pin or a timer source. In this example, we will demonstrate how to modify the code of the previous example to be triggered from a timer.
In this case we enable Timer2 to provide the clock for the ADC. To synchronize the transfer we enable the interrupt of timer two. Note any timer is likely connected to a different part of the AHB bus structure than the ADC; it is always wise to look at the clock tree and validate the clock configuration.
The code is shown below:

 /* Define ADC init structures */
        ADC_InitTypeDef       ADC_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        DMA_InitTypeDef DMA_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

        /* Enable timer (timer runs at 21 MHz)*/
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
        TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseStructure.TIM_Period = 1999;
        TIM_TimeBaseStructure.TIM_Prescaler = 17999;
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
        TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);
        TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

        NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
        NVIC_Init(&NVIC_InitStructure);

        /* Enable clock on DMA1 & GPIOC */
        /* Enable DMA2, thats where ADC is hooked on -> see Tab 20 (RM00090) */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_DMA2, ENABLE);

        /* Initialise GPIOs C0 (ADC123_IN10), C1 (ADC123_IN11), C2 (ADC123_IN12)*/
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AIN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

        /* Initialise DMA */
        DMA_StructInit(&DMA_InitStructure);

        /* config of DMAC */
        DMA_InitStructure.DMA_Channel = DMA_Channel_0; /* See Tab 20 */
        DMA_InitStructure.DMA_BufferSize = 5; /* 5 * memsize */
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; /* direction */
        DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; /* no FIFO */
        DMA_InitStructure.DMA_FIFOThreshold = 0;
        DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
        DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /* circular buffer */
        DMA_InitStructure.DMA_Priority = DMA_Priority_High; /* high priority */
        /* config of memory */
        DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADCBuffer; /* target addr */
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; /* 16 bit */
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
        DMA_Init(DMA2_Stream0, &DMA_InitStructure); /* See Table 20 for mapping */
        DMA_Cmd(DMA2_Stream0, ENABLE);

        /* IMPORTANT: populate default values before use */
        ADC_StructInit(&ADC_InitStructure);
        ADC_CommonStructInit(&ADC_CommonInitStructure);

        /* reset configuration if needed, could be used for previous init */
        ADC_Cmd(ADC1, DISABLE);
        ADC_DeInit();

        /* init ADC clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

        /* init ADC */
        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
        ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
        ADC_CommonInit(&ADC_CommonInitStructure);

        /* ADC1 Init: this is mostly done with ADC1->CR */
        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;
        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
        ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        ADC_InitStructure.ADC_NbrOfConversion = 5; /* 5 channels in total */
        ADC_Init(ADC1, &ADC_InitStructure);

        /* Enable Vref & Temperature channel */
        ADC_TempSensorVrefintCmd(ENABLE);

        /* Configure channels */
        ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_480Cycles);         
        ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_480Cycles); 
        ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 3, ADC_SampleTime_480Cycles);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 4, ADC_SampleTime_480Cycles);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 5, ADC_SampleTime_480Cycles);

        /* Enable DMA request after last transfer (Single-ADC mode) */
        ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

        /* Enable ADC3 DMA */
        ADC_DMACmd(ADC1, ENABLE);

        /* Enable ADC1 **************************************************************/
        ADC_Cmd(ADC1, ENABLE);

        … /* in main */ 
        TIM_Cmd(TIM2, ENABLE);
As you can see the timer is enabled to feed a signal into the ADC. Upon expiry of the timer period an interrupt is triggered that initiates the ADC. During the conversion the DMA copies the samples into the cyclic buffer. The timer ISR can be used to synchronize the transfer to the main program, by copying the “previous” sample into a global buffer.
void TIM2_IRQHandler() {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        
        /* nothing to do here except for possibly copying DMA buffers of previous conversion */
}
This example shows how to use a timer as clock source to pace the ADC. This configuration is useful for applications that require a data points at periodic intervals. In this post we demonstrated the four basic operating modes of the ADC. We furthermore demonstrated practical modifications of these scenarios that involve an external trigger and data coming from an external pin. We have left out specific concepts such as injected channels or the interleaved operation of channels, which will be discussed in different posts.

References

1 comment:

  1. In the DMA examples enabling of the memory increment is missing but relevant:

    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

    ReplyDelete