#include "rs485.h"

#define BUF_NUM 16

#ifdef EN_UART1_RX

uint8_t RS485_RX_BUF[16];
uint8_t RS485_RX_CNT = 0;
/**
 *******************************************************************************
 * @brief   interrupt config.
 * @param   None
 * @param   None
 * @retval  None.
 ******************************************************************************/
void UART1_IRQHandler_Config(void)
{
  uint8_t res;

  if(UART_GetIntFlagStatus(UART1, UART_INT_FLAG_RXNE) != RESET)
  {

    res = UART_ReceiveData(UART1);

    if(RS485_RX_CNT < BUF_NUM)
    {
      RS485_RX_BUF[RS485_RX_CNT] = res;
      RS485_RX_CNT++;
    }
  }
}

#endif
/**
 *******************************************************************************
 * @brief   Init Uart1.
 * @param   None
 * @param   None
 * @retval  None.
 ******************************************************************************/
void RS485_Init(void)
{
  /* Enable clock */
  RCC_Unlock();
  RCC_AHBPeriphClockCmd(RCC_AHB_PERIPH_GPIO, ENABLE);
  RCC_APBPeriphClockCmd(RCC_APB_PERIPH_UART1, ENABLE);
  RCC_Lock();

  UART_InitTypeStruct UART_InitStruct;
  GPIO_InitTypeStruct GPIO_InitStruct;
  GPIO_InitStruct.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1;
  GPIO_InitStruct.GPIO_Mode = GPIO_MODE_ALT_FUNC;
  GPIO_InitStruct.GPIO_Speed = GPIO_SPEED_HIGH;
  GPIO_InitStruct.GPIO_OType = GPIO_OTYPE_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PUPD_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStruct);
  GPIO_AltFuncConfig(GPIOD, GPIO_PIN_0 | GPIO_PIN_1, GPIO_ALT_FUNC_1);
  UART_StructInit(&UART_InitStruct);
  //DIR
  GPIO_InitStruct.GPIO_Mode = GPIO_MODE_OUTPUT;
  GPIO_Init(GPIOB, &GPIO_InitStruct);

  UART_InitStruct.UART_DataLen = UART_DATA_LEN_8;
  UART_InitStruct.UART_StopBits = UART_STOP_BITS_1;
  UART_InitStruct.UART_Parity = UART_PARITY_NO;
  UART_InitStruct.UART_BaudRate = 115200;
  UART_Init(UART1, &UART_InitStruct);
  UART_Cmd(UART1, ENABLE);


#ifdef EN_UART1_RX
  /* Enable interrupt */
  UART_IntCmd(UART1, UART_INT_RXNE_IE, ENABLE);
  NVIC_SetPriority(UART1_IRQn, 1);
  NVIC_EnableIRQ(UART1_IRQn);

#endif

  RS485_TX_EN(0);      //默认为接收模式

}

/**
 *******************************************************************************
 * @brief   Send bytes.
 * @param   [in]  buf: Sending area header address.
 *          [in]  len：send length.
 * @retval  None.
 ******************************************************************************/
void RS485_Send_Data(uint8_t *buf, uint8_t len, uint32_t timeOut)
{
  uint32_t time = GetTick() + timeOut;

  uint8_t t;
  /* Set to send mode */
  RS485_TX_EN(1);

  /* Loop sending data */
  for(t = 0; t < len; t++)
  {
    while(UART_GetIntFlagStatus(UART1, UART_INT_FLAG_TXE) == RESET && (time > GetTick()));

    UART_SendData(UART1, buf[t]);
  }

  while(UART_GetIntFlagStatus(UART1, UART_INT_FLAG_TXE) == RESET && (time > GetTick()));

  RS485_RX_CNT = 0;
  /* Set to receive mode */
  RS485_TX_EN(0);
}

/**
 *******************************************************************************
 * @brief   Query received data.
 * @param   [in]  buf: receive area header address.
 *          [in]  len：send length.
 * @retval  None.
 ******************************************************************************/
void RS485_Receive_Data(uint8_t *buf, uint16_t *len)
{
  uint8_t rxlen = RS485_RX_CNT;
  uint8_t i = 0;
  *len = 0;
  /* Wait for 10ms, if no data is received for more than 10ms continuously,
  it is considered that the reception has ended */
  DelayMs(10);

  /* Received data and completed reception */
  if(rxlen == RS485_RX_CNT && rxlen)
  {
    for(i = 0; i < rxlen; i++)
    {
      buf[i] = RS485_RX_BUF[i];
    }

    *len = RS485_RX_CNT;
    RS485_RX_CNT = 0;
  }
}



