stm32f407 usart6 dma rx
时间: 2023-09-27 20:08:17 浏览: 140
To configure USART6 with DMA RX on STM32F407, follow these steps:
1. Enable RCC USART6 clock:
```c
RCC->APB2ENR |= RCC_APB2ENR_USART6EN;
```
2. Enable DMA2 clock:
```c
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
```
3. Configure GPIO pins for USART6:
```c
// Configure GPIO pins for USART6
GPIOC->MODER |= GPIO_MODER_MODE6_1 | GPIO_MODER_MODE7_1;
GPIOC->AFR[0] |= GPIO_AF8_USART6 << GPIO_AFRL_AFRL6_Pos |
GPIO_AF8_USART6 << GPIO_AFRL_AFRL7_Pos;
GPIOC->OSPEEDR |= GPIO_OSPEEDR_OSPEED6 | GPIO_OSPEEDR_OSPEED7;
```
4. Configure USART6:
```c
USART6->CR1 &= ~USART_CR1_UE; // Disable USART6
USART6->CR1 |= USART_CR1_RE; // Enable receiver
USART6->BRR = 0x138; // Set baud rate to 115200
USART6->CR3 |= USART_CR3_DMAR; // Enable DMA receiver
USART6->CR1 |= USART_CR1_UE; // Enable USART6
```
5. Configure DMA2 stream 1 for USART6 RX:
```c
DMA2_Stream1->CR &= ~DMA_SxCR_EN; // Disable DMA2 stream 1
DMA2_Stream1->PAR = (uint32_t)&USART6->DR; // Peripheral address
DMA2_Stream1->M0AR = (uint32_t)rx_buffer; // Memory address
DMA2_Stream1->NDTR = BUFFER_SIZE; // Number of data to transfer
DMA2_Stream1->CR |= DMA_SxCR_CHSEL_2 | DMA_SxCR_CHSEL_1 | DMA_SxCR_CHSEL_0; // Channel 5
DMA2_Stream1->CR |= DMA_SxCR_PL_0; // Priority level: medium
DMA2_Stream1->CR |= DMA_SxCR_MINC | DMA_SxCR_CIRC; // Memory increment mode, circular mode
DMA2_Stream1->CR |= DMA_SxCR_DIR_0; // Peripheral-to-memory direction
DMA2_Stream1->CR |= DMA_SxCR_TCIE; // Transfer complete interrupt enable
DMA2_Stream1->CR |= DMA_SxCR_EN; // Enable DMA2 stream 1
```
6. Start USART6 reception:
```c
USART6->SR &= ~USART_SR_RXNE; // Clear RXNE flag
USART6->CR1 |= USART_CR1_RE; // Enable receiver
```
7. Wait for DMA transfer complete interrupt:
```c
while (!dma_transfer_complete);
dma_transfer_complete = 0; // Reset flag
```
8. Process received data:
```c
void DMA2_Stream1_IRQHandler(void)
{
if (DMA2->LISR & DMA_LISR_TCIF1)
{
DMA2->LIFCR |= DMA_LIFCR_CTCIF1; // Clear transfer complete flag
dma_transfer_complete = 1; // Set flag
}
}
```
Note: `rx_buffer` is an array to store received data, and `BUFFER_SIZE` is its size. `dma_transfer_complete` is a flag to indicate DMA transfer complete interrupt. You can process received data in the main loop after the flag is set.
阅读全文