#include "Bsp.h"

/**
 * @brief  使能GPIO的时钟
 * @param  [in]  GPIOx  GPIO组
 * @retval void
 * @note   void
 * @example void
 */
void UsartClockEnable(uint32_t ComId, FunctionalState State)
{
    switch (ComId) {
    case USART1_BASE:
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, State);
	break;
    case USART2_BASE:
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, State);
	break;
    case USART3_BASE:
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, State);
	break;
    case UART4_BASE:
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, State);
	break;
    case UART5_BASE:
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, State);
	break;
    default:
	break;
    }
}

/**
 * @brief  初始化USART口外设的时钟
 * @param  void
 * @retval void
 * @note   void
 * @example void
 */
void BspInitUsartClock(void)
{
    #ifdef USE_USART1
    UsartClockEnable(COM0,ENABLE);
    #endif
    #ifdef USE_USART2
    UsartClockEnable(COM1,ENABLE);
    #endif
    #ifdef USE_USART3
    UsartClockEnable(COM2,ENABLE);
    #endif
    #ifdef USE_USART4
    UsartClockEnable(COM3,ENABLE);
    #endif
    #ifdef USE_USART5
    UsartClockEnable(COM4,ENABLE);
    #endif
}

/**
 * @brief 配置USART口
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void UsartConfig(uint32_t ComId, uint32_t baud, uint8_t DataBits, uint8_t StopBits, uint8_t Parity)
{
    USART_InitTypeDef UsartInitSt;
    
    //初始化串口
    USART_StructInit(&UsartInitSt);
    UsartInitSt.USART_BaudRate = baud;
    UsartInitSt.USART_WordLength = DataBits;
    UsartInitSt.USART_StopBits = StopBits;
    UsartInitSt.USART_Parity = Parity;
    UsartInitSt.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    UsartInitSt.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init((USART_TypeDef*)ComId, &UsartInitSt);

    //配置中断
    USART_ITConfig((USART_TypeDef*)ComId, USART_IT_RXNE, ENABLE);

    //启用串口
    USART_Cmd((USART_TypeDef*)ComId, ENABLE);
    USART_GetFlagStatus((USART_TypeDef*)ComId, USART_FLAG_TC);
}

/**
 * @brief USART口发送数据
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void UsartSendChar(uint32_t ComId,uint8_t Data)
 {
    USART_SendData((USART_TypeDef*)ComId, Data);
    while (USART_GetFlagStatus((USART_TypeDef*)ComId, USART_FLAG_TC)  != SET);
 }
 /**
 * @brief USART口发送字符串
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void UsartSendStr(uint32_t ComId,uint8_t* Data, uint64_t Len)
 {
    for (uint64_t i = 0;i < Len;i++)
    {
        USART_SendData((USART_TypeDef*)ComId, Data[i]);
        while (USART_GetFlagStatus((USART_TypeDef*)ComId, USART_FLAG_TC) != SET);
    }
 }

/**
 * @brief USART口发送数据
 * @param
 * @retval void
 * @note   void
 * @example void
 */
uint8_t UsartReceiveChar(uint32_t ComId)
 {
    return USART_ReceiveData((USART_TypeDef*)ComId);
 }

/**
 * @brief 通用USART配置
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void UsartStdConfig(uint32_t ComId, uint32_t baud)
{
    UsartConfig(ComId, baud, DataBit_8, StopBit_1, Parity_No);
}

/**
 * @brief 高级串口配置
 * @param
 * @retval void
 * @note   void
 * @example void
 */
void UsartAdvConfig(uint32_t ComId, uint32_t baud, uint8_t DataBits, uint8_t StopBits, uint8_t Parity)
{
    UsartConfig(ComId, baud, DataBits, StopBits, Parity);
}



