DMA配置:
void dma_config(void)
{
dma_parameter_struct dma_init_struct;
//nvic_irq_enable(USART0_IRQn, 0, 0);
rcu_periph_clock_enable(RCU_DMA);
/* deinitialize DMA channel2 (USART0 rx) */
dma_deinit(DMA_CH2);
dma_struct_para_init(&dma_init_struct);
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
dma_init_struct.memory_addr = (uint32_t)usart0_rx.data;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = 256;
dma_init_struct.periph_addr = (uint32_t)&USART_RDATA(USART0);
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_CH2, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA_CH2);
/* enable DMA channel2 */
dma_channel_enable(DMA_CH2);
}
void usart0_gpio_config(void)
{
/* enable COM GPIO clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* connect port to USARTx_Tx */
gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_6);
/* connect port to USARTx_Rx */
gpio_af_set(GPIOB, GPIO_AF_0, GPIO_PIN_7);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_6);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_7);
}
void usart0_config(void)
{
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* USART configure */
usart_deinit(USART0);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_baudrate_set(USART0, 115200U);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_dma_receive_config(USART0, USART_DENR_ENABLE);
usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
usart_enable(USART0);
}
void uart_init(void)
{
nvic_irq_enable(USART0_IRQn, 0, 0);
dma_config();
usart0_gpio_config();
usart0_config();
usart1_gpio_config();
usart1_config();
while(RESET == usart_flag_get(USART0, USART_FLAG_IDLE));
usart_flag_clear(USART0, USART_FLAG_IDLE);
usart_interrupt_enable(USART0, USART_INT_IDLE);
usart_interrupt_enable(USART0, USART_INT_RBNE);
printf("version: %s\r\n",VERSION);
printf("uart_init over\r\n");
} |