#include "uart4.h"
#include "stm32f4xx.h"

void uart4_init(void)
{
    GPIO_InitTypeDef GPIO_StructInit;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);

    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_AF;
    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_StructInit.GPIO_Pin   = GPIO_Pin_10;
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
    GPIO_Init(GPIOC, &GPIO_StructInit);

    GPIO_StructInit.GPIO_Pin = GPIO_Pin_11;
    // GPIO_StructInit.GPIO_Mode  = GPIO_Mode_AF;
    // GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
    // GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
    // GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
    GPIO_Init(GPIOC, &GPIO_StructInit);

    NVIC_InitStructure.NVIC_IRQChannel                   = UART4_IRQn; // 串口1中断通道
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;          // 抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;          // 子优先级
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;     // IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);

    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate            = 115200;
    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
    USART_InitStructure.USART_Parity              = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(UART4, &USART_InitStructure); // 初始化串口

    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);

    USART_Cmd(UART4, ENABLE); // 使能串口
}
