cancel
Showing results for 
Search instead for 
Did you mean: 

DMA Problem with USART

parisa
Senior

Hello,

I have written these codes for transmitting an array to USART.

1)The Message[21] Array fills up every 200us. It sets Flag=1 when data is ready.

2) If the user makes the PB11 pin low it starts DMA section to transmit the data.(All data will transmit in less than 150us).

3) After 500us, the timer set the Flag=0/Disable DMA to get new data.

So (Read data (200us), Start Timer , Send data (150us), (350us after sending the data) the timer disables DMA)

The code works fine for some packages, However, by changing the statues of PB11 it starts sending just First data (Message[0]) repetitively. I can't figure out this problem.

void DMA_Configuration(void){
 
	DMA_InitTypeDef DMA_InitStructure;	
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	DMA_DeInit(DMA1_Channel2);
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR;
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Message;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStructure.DMA_BufferSize =(uint8_t)sizeof(Message);
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode =DMA_Mode_Circular ;///DMA_Mode_Normal ;  //
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel2, &DMA_InitStructure);
	USART_Cmd(USART3,ENABLE);
}
 
void DMA1_Channel2_IRQHandler(void) // USART3_TX
	{
							
//							if(DMA_GetFlagStatus(DMA1_FLAG_TE2)!=RESET){
//								DMA_ClearITPendingBit(DMA1_FLAG_TE2);
//								DMA_Configuration2();
//							}
						//	if(DMA_GetFlagStatus(DMA1_FLAG_TE2)!=RESET){
							DMA_ClearITPendingBit(DMA1_IT_TC2);
							DMA_Cmd(DMA1_Channel2, DISABLE);
						//	}
	}
void Time(void){
TIM_TimeBaseInitTypeDef TimeStruct; 
NVIC_InitTypeDef nvicStructure;
	
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);	
nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
nvicStructure.NVIC_IRQChannelPreemptionPriority = 1;
nvicStructure.NVIC_IRQChannelSubPriority = 0;
nvicStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicStructure);
	
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TimeStruct.TIM_Prescaler=(700);
TimeStruct.TIM_Period=50;
TimeStruct.TIM_ClockDivision=TIM_CKD_DIV1;	
TimeStruct.TIM_CounterMode=TIM_CounterMode_Up ;
TIM_TimeBaseInit(TIM2, &TimeStruct);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	
}
 
void TIM2_IRQHandler(){
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
							
	Flag=0;						
	USART_DMACmd(USART3, USART_DMAReq_Tx, DISABLE);
	DMA_Cmd(DMA1_Channel2, DISABLE);		
	TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	TIM_Cmd(TIM2, DISABLE);	
   }
			
}
 
int main(void){
	char i;
	CLOCK();
	GPIOSYS();
	USARTConfig();
	DMA_Configuration();
	Flag=0;
	Time();
	while(1){
		
 
					if(Flag==1){
						i=GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) & 1;
							if(i==0)
							{						
								Flag=2;	
								USART_Cmd(USART3,ENABLE);
								DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
								USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);
						//		DMA_ITConfig(DMA1_Channel2,DMA_IT_TE,ENABLE);
								if(Flag!=0){DMA_Cmd(DMA1_Channel2,  ENABLE);}else{DMA_Cmd(DMA1_Channel2, DISABLE);}
							}
						}else if(Flag==0)
						{
 
						Message[0] up to Message[20] fill up with new Data		
						Flag=1;	
						TIM_Cmd(TIM2, ENABLE);	
							
						}
  	}
}
 

My MCU is STM32f103.

1 REPLY 1
ilyaz yilmaz
Associate III

I think Hal Library 2017 version has a bug. Try old version 2016 to HAL Library.