当前位置:网站首页>STM32 - DMA notes

STM32 - DMA notes

2022-07-26 03:00:00 The poorest are beggars and immortals will always come out

One 、DMA What is it?

DMA Transfer data from a The address space is copied to another address space . When CPU Initialize this transfer action , The transmission action itself is caused by DMA controller To implement and complete . A typical example is moving a block of external memory into the chip faster Memory area . Operations like this don't delay the processor , Instead, it can be rescheduled to deal with other jobs do .DMA There is no need for transmission CPU direct Control transmission , Through hardware for RAM And I/O equipment Open up a path for direct data transmission , Can make CPU Greatly improved the efficiency of .

Two 、DMA The role of

“ Carry ”, Move external data into MCU, There is no need to CPU To do ,CPU You can deal with other things .

  3、 ... and 、DMA1 The channel corresponding to “ Portable source ”

Four 、DMA Related configuration of

  The first three red boxes are mainly configured , The fourth purple box is selected according to the length of one handling , There is one 8 position 、16 position 、32 position .

u16 ADC1_Value[C_Channel_Sample][C_ADC_Channel] = { 0 };    // DMA Storage place for handling data ( Array )

DMA Configuration of :

void DMA_Init_JX(void)
{
	DMA_InitTypeDef DMA_InitStructure;
	//NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 			//  Can make DMA The clock 

	
	// DMA_CH1(ADC1) Parameter setting 
	//--------------------------------------------------------------------------------------------------------------------------
	DMA_DeInit(DMA1_Channel1); 													//  take DMA The passage of 1 Register reset to default 
	DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; 					// DMA peripherals ADC Base address 
	DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC1_Value; 					// DMA Memory base address 
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 							//  Memory as the destination of data transmission 
	DMA_InitStructure.DMA_BufferSize = C_ADC_Channel * C_Channel_Sample; 		// DMA The tunnel DMA Cache size 
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 			//  The peripheral address register remains unchanged 
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 					//  The memory address register is incremented 
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //  Data width is 16 position 
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; 		//  Data width is 16 position 
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 							//  Working in circular cache mode 
	DMA_InitStructure.DMA_Priority = DMA_Priority_High; 						// DMA passageway  x Have high priority 
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 								// DMA passageway x Not set to memory to memory transfer 
	DMA_Init(DMA1_Channel1, &DMA_InitStructure); 								//  according to DMA_InitStruct The parameter specified in DMA The passage of 
	//--------------------------------------------------------------------------------------------------------------------------
	
	DMA_Cmd(DMA1_Channel1, ENABLE);		//  start-up DMA passageway 
	
}

ADC Channel configuration :

void ADC1_Rocker_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef ADC_InitStructure; 
	// NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1, ENABLE);	//  Can make PA Port and ADC1 The clock 
	

	//  Analog input :PA1、PA2、PA3、PA6
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;			//  Analog input pins 
	//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;		//  The input mode does not need to set the port speed 
	GPIO_Init(GPIOA, &GPIO_InitStructure);	

	
	//  To configure ADC1_CH1、ADC1_CH2、ADC1_CH3、ADC1_CH6
	// ADC Working conditions :ADC The clock frequency of <=14MHz && ADC sampling frequency <=1MHz
	// ADC Total conversion period of ( must >=1us) = ( Sampling period (1.5~239.5) + 12.5( Fixed conversion period )) / ADC clock frequency 
	//  When :ADC The clock frequency of ==12MHz、 Sampling period ==1.5,ADC Conversion cycle ≈1.17us
	//---------------------------------------------------------------------------------------------------------------------
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);						// ADC The clock 6 frequency division :72M/6=12M

	ADC_DeInit(ADC1);  										//  Reset ADC1 

	//  Set up ADC1 Working mode of 
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;		// ADC Working mode :ADC1 and ADC2 Working in independent mode 
	ADC_InitStructure.ADC_ScanConvMode = ENABLE;			// " Scan conversion mode " Can make . namely :ADC Working in multi-channel mode 
	ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;		// " Continuous conversion mode " Disability . namely :ADC Working in single conversion mode 
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;		//  The conversion is initiated by software ( It can also be set as an external trigger )
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;	// ADC Data alignment : Right alignment 
	ADC_InitStructure.ADC_NbrOfChannel = C_ADC_Channel;		//  Set the order for rule conversion ADC The number of channels 
	ADC_Init(ADC1, &ADC_InitStructure);						//  initialization ADC1  
	
	
	//  In order to correctly configure each  ADC  passageway , The user is calling  ADC_Init() after ,
	//  Must call ADC_xxxChannelConfig() To configure the conversion order and sampling time of each used channel .
	//--------------------------------------------------------------------
	ADC_RegularChannelConfig(ADC1, N_ADC_Channel_1, 1, ADC_SampleTime_239Cycles5);	//  Parameters :ADC1, To set up ADC Channel serial number , Rule group sampling order , Sampling period 
	//  This function : The configuration is "ADC1" The passage of "1", Its sampling order in the rule group is "1", The sampling period is "ADC_SampleTime_239Cycles5"
	
	ADC_RegularChannelConfig(ADC1, N_ADC_Channel_3, 2, ADC_SampleTime_239Cycles5);
	//  This function : The configuration is "ADC1" The passage of "3", Its sampling order in the rule group is "2", The sampling period is "ADC_SampleTime_239Cycles5"
	//---------------------------------------------------------------------------------------------------------------------
	
	
	/*
	//ADC1 The interrupt NVIC Set up 
	//---------------------------------------------------------------------------------
	NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;  			// ADC1 interrupt 
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;  	//  preemption 3 level 
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  		//  Sub priority 3 level 
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 			//  Can make ADC1_2_IRQn interrupt 
	NVIC_Init(&NVIC_InitStructure);  							//  initialization NVIC register 
	
	ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE ); 						//  allow ADC1 Of EOC interrupt 
	//---------------------------------------------------------------------------------
	*/
	
	
	//  Turn on ADC Of DMA Support 
	ADC_DMACmd(ADC1, ENABLE);					//  To achieve DMA function , Independent configuration is also required DMA Channel and other parameters 
	
	
	ADC_Cmd(ADC1, ENABLE);						//  Can make ADC1
	
	
	ADC_ResetCalibration(ADC1);					//  Can make ADC1 Reset calibration   
	 
	while(ADC_GetResetCalibrationStatus(ADC1));	//  wait for ADC1 Reset calibration is over 
	
	ADC_StartCalibration(ADC1);	 				//  Turn on ADC1 calibration 
 
	while(ADC_GetCalibrationStatus(ADC1));	 	//  wait for ADC1 End of calibration 

}

ADC collection :

When in the configuration , Configured as software trigger mode . In the use of ADC when , Use the program to enable ADC1 transformation , That's the first sentence .

During handling DMA One move 16 Bit data , front 8 Position as ADC1 passageway 1 Value , Put it in the first column of the two-dimensional array

                                                    after 8 Position as ADC1 passageway 3 Value , Put it in the second column of the two-dimensional array

void ADC1_Value_average(void)
{
	//  Rocker detection 
	//------------------------------------------------------------------------
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);					//  Software enables ADC1 transformation 
	
	//  After the conversion ,DMA Automatically move the data to the specified location 
	//while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));		//  Wait for the conversion to finish 
	
	AM_ADC_Channel1_Sample += ADC1_Value[C_ADC_Sample][0];	//  obtain 
		
	AM_ADC_Channel3_Sample += ADC1_Value[C_ADC_Sample][1];

	C_ADC_Sample++ ;	// ADC Actual sampling times 

	//  Determine whether samples have been taken 10 Time 
	if(C_ADC_Sample >= C_Channel_Sample)
	{ 
		C_ADC_Sample = 0; 
		
		AV_ADC_Channel1_Sample = AM_ADC_Channel1_Sample / C_Channel_Sample;		//  Seek channel 1 Average value 
		
		AM_ADC_Channel1_Sample = 0 ;
		
		
		AV_ADC_Channel3_Sample = AM_ADC_Channel3_Sample / C_Channel_Sample;		//  Seek channel 3 Average value 
		
		AM_ADC_Channel3_Sample = 0 ;
	}

}

原网站

版权声明
本文为[The poorest are beggars and immortals will always come out]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260259402267.html