cancel
Showing results for 
Search instead for 
Did you mean: 

DMA1 with UART2 TX not transmitting

ayennguyen
Associate

I am trying to implement usart2 tx with dma1 stream 6 channel 4. I have tested the usart2 tx independently and it worked perfectly, but the dma part was not that lucky, it just wasn't send anything and I couldn't find anything wrong in my code. Appreciate for any help.S here's my code:

void RCC_SetUp(){
	RCC->APB1ENR |= (0x1<<0);//TIMER2 enable (84MHz)

	RCC->AHB1ENR |= (0x1<<0);//A

	RCC->APB1ENR |= (0x1<<17);//USART2 enable
	RCC->AHB1ENR |= (0x1<<21);//DMA1 enable
}

void GPIO_SetUp(){
	GPIOD->MODER |= (0x1<<26);
	GPIOD->MODER &= ~(0x1<<27);

	//PA2 alternate function
	GPIOA->MODER &= ~(0x01<<4);
	GPIOA->MODER |= (0x01<<5);

	//PA2 Tx
	GPIOA->AFR[0] |= (0x01<<8);
	GPIOA->AFR[0] |= (0x01<<9);
	GPIOA->AFR[0] |= (0x01<<10);
	GPIOA->AFR[0] &= ~(0x01<<11);

}

void DMA1_Stream6_IRQHandler(void)
{
	if(DMA1->HISR & (0x1<<21))
	{
		DMA1->HIFCR |= (0x1<<21);
	}
}

volatile uint8_t buffer[] = "USART2_TEST\r\n";

void DMA1_SetUp()
{
	//make sure dma is off
	DMA1_Stream6->CR = 0;
	while(DMA1_Stream6->CR & (0x1<<0))
	{
		//wait for DMA to disable completely
	}

	//channel 4 selected
	DMA1_Stream6->CR |= (0x1<<25);
	DMA1_Stream6->CR |= (0x1<<26);
	DMA1_Stream6->CR |= (0x1<<27);

	//DMA transfer complete interrupt
	DMA1_Stream6->CR |= (0x1<<4);

	//Memory increment
	DMA1_Stream6->CR |= (0x1<<10);

	//Circulation enable
//	DMA1_Stream6->CR |= (0x1<<8);

	//Medium Priority
	DMA1_Stream6->CR |= (0x1<<16);

	//memory to peripheral
	DMA1_Stream6->CR |= (0x1<<6);

	//src mem address
	DMA1_Stream6->M0AR = (uint32_t)buffer;

	//dst periph address
	DMA1_Stream6->PAR = (uint32_t)&(USART2->DR);

	//number of item for dma2
	DMA1_Stream6->NDTR = sizeof(buffer);

	//setup NVIC
	NVIC->ISER[2] |= (0x1<<6);

	//enable DMA
	DMA1_Stream6->CR |= (0x1<<0);
}

void USART2_SetUp(void)
{
	USART2->CR3 |= (0x1<<7);//DMA transmit enable
	USART2->CR1 |= (0x1<<9);
	//BR = 115200
	USART2->BRR = (22<<4);
	USART2->BRR |= 13;
	USART2->CR1 |= (0x1<<3);//Tx enable
	USART2->CR1 |= (0x1<<13);//Usart2 enable
	USART2->SR &= ~(0x1<<6);//clear tc bit
}

int main(void)
{
	Clock_SetUp();
	RCC_SetUp();
	GPIO_SetUp();
	USART2_SetUp();
	DMA1_SetUp();
	while(1)
	{

	}
}

 

1 REPLY 1
NoNamed
Associate II

First of all, welcome to the community. I skimmed through your code and gotta say its was pretty clean. All are correct, except the lines:

	//channel 4 selected
	DMA1_Stream6->CR |= (0x1<<25);
	DMA1_Stream6->CR |= (0x1<<26);
	DMA1_Stream6->CR |= (0x1<<27);

bit 25 and 26 should be 0 right?