CARME-M4 BSP  V1.5
dma.c

Explains how to use the DMA controller.

/*
* This example shows how to read from ADC Port 0 (the Poti) on CARME-IO2 and
* and write it to the LEDs on CARME-IO1.
*/
#include <stm32f4xx.h>
#include <carme.h>
#include <carme_io1.h>
#include <carme_io2.h>
#define ADC1_DR_ADDRESS ((uint32_t) &ADC1->DR)
#define LED_ADDRESS ((uint32_t) FSMC_CARME_EXTENSION1_BASE + 0x200)
int main(void) {
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
/*** Initialize DMA ***/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_Cmd(DMA2_Stream4, DISABLE);
DMA_DeInit(DMA2_Stream4); /* Set DMA registers to default values */
/*
* Configure the DMA
* - Source address: ADC1_DR_ADDRESS
* - Destination address: LED_ADDRESS
* - Buffer size: 2
* - Source datasize: 8 bit
* - Destination datasize: 8 bit
*/
DMA_InitStruct.DMA_Channel = DMA_Channel_0;
DMA_InitStruct.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;
DMA_InitStruct.DMA_Memory0BaseAddr = LED_ADDRESS;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_BufferSize = 2;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
DMA_InitStruct.DMA_Priority = DMA_Priority_High;
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream4, &DMA_InitStruct);
DMA_Cmd(DMA2_Stream4, ENABLE);
/*** Reinitialize ADC ***/
ADC_Cmd(ADC1, DISABLE);
ADC_StructInit(&ADC_InitStruct);
/*
* Configure the ADC1
* - Data align: right
* - Resolution: 8 bit
* - Continuous conversion: enabled
* - Nbr of conversions: 2
*/
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_Resolution = ADC_Resolution_8b;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_NbrOfConversion = 2;
ADC_InitStruct.ADC_ScanConvMode = ENABLE;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_Cmd(ADC1, ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_480Cycles);
/*** Enable and start ***/
ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 DMA */
ADC_SoftwareStartConv(ADC1); /* Start ADC1 conversion */
for (;;) {
}
return 0;
}