static rcu_periph_enum UART_GPIO_CLK[UARTn] = {RCU_GPIOA,RCU_GPIOA,RCU_GPIOB,RCU_GPIOC};
static rcu_periph_enum UART_CLK[UARTn] = {RCU_USART0,RCU_USART1,RCU_USART2,RCU_UART3};
static uint32_t UART_GPIO_PORT[UARTn] = {GPIOA,GPIOA,GPIOB,GPIOC};
static uint32_t UART_TX_PIN[UARTn] = {GPIO_PIN_9,GPIO_PIN_2,GPIO_PIN_10,GPIO_PIN_10};
static uint32_t UART_RX_PIN[UARTn] = {GPIO_PIN_10,GPIO_PIN_3,GPIO_PIN_11,GPIO_PIN_11};
static uint8_t UART_NVIC[UARTn]={USART0_IRQn,USART1_IRQn,USART2_IRQn,UART3_IRQn,UART4_IRQn};
static uint8_t UART_PRE_PRIORITY[UARTn]={2,4,3,5,6};
串口初始化调用// USART_Init(USART1,115200);
// USART_Init(USART0,115200);
USART_Init(USART2,115200);
// USART_Init(UART3,115200);
// USART_Init(UART4,115200);
串口初始化函数
void USART_Init(uint32_t com,uint32_t baud)
{
USART_Config(com,baud);
}
void USART_Config(uint32_t com,uint32_t baud)
{
uint32_t com_id = 0U;
if(com==USART0)com_id=0U;
else if(com==USART1)com_id=1U;
else if(com==USART2)com_id=2U;
else if(com==UART3)com_id=3U;
else if(com==UART4)com_id=4U;
/* configure COM */
if(com_id==4U){
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOD);
rcu_periph_clock_enable(RCU_UART4);
gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
gpio_init(GPIOD, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
}else{
rcu_periph_clock_enable(UART_GPIO_CLK[com_id]);
rcu_periph_clock_enable(UART_CLK[com_id]);
gpio_init(UART_GPIO_PORT[com_id], GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, UART_TX_PIN[com_id]);
gpio_init(UART_GPIO_PORT[com_id], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, UART_RX_PIN[com_id]);
}
usart_deinit(com);
usart_baudrate_set(com, baud);
usart_word_length_set(com, USART_WL_8BIT);
usart_stop_bit_set(com, USART_STB_1BIT);
usart_parity_config(com, USART_PM_NONE);
usart_hardware_flow_rts_config(com, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(com, USART_CTS_DISABLE);
usart_receive_config(com, USART_RECEIVE_ENABLE);
usart_transmit_config(com, USART_TRANSMIT_ENABLE);
usart_enable(com);
/* USART interrupt configuration */
nvic_irq_enable(UART_NVIC[com_id],UART_PRE_PRIORITY[com_id],0);
/* enable USART RBNE interrupt */
usart_interrupt_enable(com, USART_INT_RBNE);
}
串口2只要调用nvic_irq_enable(UART_NVIC[com_id],UART_PRE_PRIORITY[com_id],0);函数,系统直接死机,而其他串口调用都正常
|