- 选择合适的DMA通道
- 代码
uint8_t txbuffer[] = "\n\rUSART DMA receive and transmit example, please input 10 bytes:\n\r";
#define ARRAYNUM(arr_name) (uint32_t)(sizeof(arr_name) / sizeof(*(arr_name)))
#define USART0_DATA_ADDRESS ((uint32_t)&USART_DATA(USART0))
void uart_dma_Init()
{
dma_parameter_struct dma_init_struct;
rcu_periph_clock_enable(RCU_DMA0);
dma_deinit(DMA0, DMA_CH3);
dma_struct_para_init(&dma_init_struct);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
dma_init_struct.memory_addr = (uint32_t)txbuffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = ARRAYNUM(txbuffer);
dma_init_struct.periph_addr = USART0_DATA_ADDRESS;
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA0, DMA_CH3, &dma_init_struct);
dma_circulation_disable(DMA0, DMA_CH3);
dma_memory_to_memory_disable(DMA0, DMA_CH3);
usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
dma_interrupt_enable(DMA0, DMA_CH3, DMA_INT_FTF);
nvic_irq_enable(DMA0_Channel3_IRQn, 5, 0);
}
void uart_dma_start()
{
dma_channel_enable(DMA0, DMA_CH3);
}
void DMA0_Channel6_IRQHandler(void)
{
if(dma_interrupt_flag_get(DMA0, DMA_CH6, DMA_INT_FLAG_FTF))
{
dma_interrupt_flag_clear(DMA0, DMA_CH6, DMA_INT_FLAG_G);
dma_interrupt_flag_clear(DMA0, DMA_CH6, DMA_INT_FLAG_FTF);
dma_channel_disable(DMA0, DMA_CH3);
}
}
void nvic_config(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE3_SUB1);
}