串口&DMA初始化
//串口发送buffer
uint8_t txbuffer[128] = {0};
//串口接收buffer
uint8_t rxbuffer[4] = {0};
extern uint8_t com_recv_STA;//串口DMA接收状态 1=数据接收完成
extern uint8_t com_recv_num;//串口接收字节数
/* 串口IO初始化 */
void com_gpio_init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
}
/* 串口初始化 */
void com_usart_init(void)
{
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* USART configure */
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200U);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
}
void com_nvic_config(void)
{
nvic_irq_enable(DMA_Channel1_2_IRQn, 0);
}
/* 串口DMA初始化 */
void com_dma_init(void)
{
dma_parameter_struct dma_init_struct;
/* enable DMA clock */
rcu_periph_clock_enable(RCU_DMA);
/* initilize the com */
com_gpio_init();
com_usart_init();
com_nvic_config();
/* initialize DMA channel1 */
dma_deinit(DMA_CH1);
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_TDATA_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(DMA_CH1, &dma_init_struct);
/* initialize DMA channel2 */
dma_deinit(DMA_CH2);
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
dma_init_struct.memory_addr = (uint32_t)rxbuffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = ARRAYNUM(rxbuffer);
dma_init_struct.periph_addr = USART0_RDATA_ADDRESS;
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.memory_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA_CH2, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA_CH1);
dma_memory_to_memory_disable(DMA_CH1);
dma_circulation_disable(DMA_CH2);
dma_memory_to_memory_disable(DMA_CH2);
/* USART DMA enable for reception */
usart_dma_receive_config(USART0, USART_DENR_ENABLE);
/* enable DMA channel2 transfer complete interrupt */
dma_interrupt_enable(DMA_CH2, DMA_INT_FTF);
/* enable DMA channel2 */
dma_channel_enable(DMA_CH2);
/* USART DMA enable for transmission */
usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
/* enable DMA channel1 transfer complete interrupt */
dma_interrupt_enable(DMA_CH1, DMA_INT_FTF);
/* enable DMA channel1 */
dma_channel_enable(DMA_CH1);
}
|