⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 71x_uart.c

📁 STR7系列32位ARM控制器的固件库
💻 C
📖 第 1 页 / 共 2 页
字号:
  {
    UARTx->CR |= 0x0080;
  }
  else
  {
    UARTx->CR &= ~0x0080;
  }
}

/*******************************************************************************
* Function Name  : UART_ByteSend
* Description    : This function is used to send a 7-bit byte or an 8-bit byte
*                  using the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: a pointer on the data byte to send.
* Output         : None.
* Return         : None.
*******************************************************************************/
void UART_ByteSend(UART_TypeDef *UARTx, u8 *Data)
{
  /* if FIFO ENABLED */
  if (UARTx->CR & 0x0400)
  {
    
    while ((UARTx->SR & UART_TxFull))
    {
      /* Wait until the TxFIFO contains at least 1 free place */
    }
  }
  /* if FIFO DISABLED */
  else
  {
    
    while (!(UARTx->SR & UART_TxEmpty))
    {
      /* Wait until the transmit shift register is empty */
    }
  }
  UARTx->TxBUFR = *Data;
}

/*******************************************************************************
* Function Name  : UART_9BitByteSend
* Description    : This function sends a 9-bit data using the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: a pointer on the 9-bit data to send.
* Output         : None.
* Return         : None.
*******************************************************************************/
void UART_9BitByteSend(UART_TypeDef *UARTx, u16 *Data)
{
  /* if FIFO ENABLED */
  if (UARTx->CR & 0x0400)
  {
    
    while ((UARTx->SR & UART_TxFull))
    {
    /* Wait until the TxFIFO contains at least 1 free place */
    }
  }
  /* if FIFO DISABLED */
  else
  {
    
    while (!(UARTx->SR & UART_TxEmpty))
    {
      /* Wait until the transmit shift register is empty */
    }
  }

  UARTx->TxBUFR = ((*Data) & 0x01FF);
}

/*******************************************************************************
* Function Name  : UART_DataSend
* Description    : This routine is used to send several 7-bit bytes or 8-bit
*                  bytes using the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: the bytes start address.
*                  - DataLength: the data length in bytes.
* Output         : None.
* Return         : None.
*******************************************************************************/
void UART_DataSend(UART_TypeDef *UARTx, u8 *Data, u8 DataLength)
{

  while (DataLength--)
  {
    UART_ByteSend(UARTx, Data);
    Data++;
  }
}

/*******************************************************************************
* Function Name  : UART_9BitDataSend
* Description    : This function sends several 9-bit data using the selected
*                  UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: the bytes start address.
*                  - DataLength: the data bytes number.
* Output         : None.
* Return         : None.
*******************************************************************************/
void UART_9BitDataSend(UART_TypeDef *UARTx, u16 *Data, u8 DataLength)
{
  while (DataLength--)
  {
    UART_9BitByteSend(UARTx, Data);
    Data++;
  }
}

/*******************************************************************************
* Function Name  : UART_StringSend
* Description    : This function sends a string to the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - String: a pointer on the string to send.
* Output         : None.
* Return         : None.
*******************************************************************************/
void UART_StringSend(UART_TypeDef *UARTx, u8 *String)
{
  u8 *Data = String;
  
  while (*Data != '\0')
  {
    UART_ByteSend(UARTx, Data++);
  }
  
  /* Send the character end of string  */
  *Data = '\0';
  UART_ByteSend(UARTx, Data);
}

/*******************************************************************************
* Function Name  : UART_ByteReceive
* Description    : This routine is used to get a 7 or an 8-bit byte from 
*                  the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: a pointer on the data where the data will be stored.
*                  - TimeOut: The time-out period value.
* Output         : The received 8-bit data.
* Return         : The UARTx_SR register content before reading the received
*                  data.
*******************************************************************************/
u16 UART_ByteReceive(UART_TypeDef *UARTx, u8 *Data, u8 TimeOut)
{
  u16 wStatus = 0;

  /* Reload the Timeout counter */
  UARTx->TOR = TimeOut;

  /* Wait while the UART_RxFIFO is empty and no Timeoutidle */
  while (!((wStatus = UARTx->SR) & (UART_TimeOutIdle | UART_RxBufNotEmpty)))
  {
    /* Wait */
  }

  /* then read the Receive Buffer Register */
  *Data = (u8)UARTx->RxBUFR;

  return wStatus;
}

/*******************************************************************************
* Function Name  : UART_9BitByteReceive
* Description    : This function gets a 9-bit data from the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: a pointer on the data where the data will be stored.
*                  - TimeOut: The time-out period value.
* Output         : The received 9-bit data.
* Return         : The UARTx_SR register content before reading the received
*                  data.
*******************************************************************************/
u16 UART_9BitByteReceive(UART_TypeDef *UARTx, u16 *Data, u8 TimeOut)
{
  u16 wStatus = 0;

  /* Reload the Timeout counter */
  UARTx->TOR = TimeOut;

  /* while the UART_RxFIFO is empty and no Timeoutidle */
  while (!((wStatus = UARTx->SR) & (UART_TimeOutIdle | UART_RxBufNotEmpty)))
  {
    /* Wait */
  }

  /* then read the RxBUFR*/
  *Data = (u16)UARTx->RxBUFR;
  return wStatus;
}

/*******************************************************************************
* Function Name  : UART_DataReceive
* Description    : This function gets 8 bits data bytes from the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: a pointer on the buffer where the data will be stored.
*                  - DataLength: the data length.
*                  - TimeOut: The time-out period value.
* Output         : The received 8-bit data buffer.
* Return         : The UARTx_SR register content before reading the received
*                  data.
*******************************************************************************/
u16 UART_DataReceive(UART_TypeDef *UARTx, u8 *Data, u8 DataLength, u8 TimeOut)
{
  u16 wStatus = 0;
  while (DataLength--)
  {
    wStatus = UART_ByteReceive(UARTx, Data++, TimeOut);
  }
  return wStatus;
}

/*******************************************************************************
* Function Name  : UART_9BitDataReceive
* Description    : This function gets several 9-bits data from the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: a pointer on the buffer where the data will be stored.
*                  - DataLength: the number of bytes to receive.
*                  - TimeOut: the time-out period value.
* Output         : The received 9-bit data buffer.
* Return         : The UARTx_SR register contents.
*******************************************************************************/
u16 UART_9BitDataReceive(UART_TypeDef *UARTx, u16 *Data, u8 DataLength,
                         u8 TimeOut)
{
  u16 wStatus = 0;

  while (DataLength--)
  {
    wStatus = UART_9BitByteReceive(UARTx, Data++, TimeOut);
  }
  return wStatus;
}

/*******************************************************************************
* Function Name  : UART_StringReceive
* Description    : This function gets a string which ends with: End of string
*                  or Carriage return characters from the selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
*                  - Data: a pointer on the buffer where the data will be stored.
* Output         : The received string.
* Return         : The UARTx_SR register content before reading the received
*                  data.
*******************************************************************************/
u16 UART_StringReceive(UART_TypeDef *UARTx, u8 *Data)
{
  u8 *pSTRING = Data;
  u16 wStatus = 0;

  do
  {
    /* while the UART_RxFIFO is empty  */
    while (!((wStatus = UARTx->SR) & (UART_RxHalfFull | UART_RxBufNotEmpty)))
    {
      /* Wait */
    }

    /* then read the RxBUFR */
    *(pSTRING++) = (u8)UARTx->RxBUFR;

  } while ((*(pSTRING - 1) != 0x0D) && (*(pSTRING - 1) != '\0'));

  *(pSTRING - 1) = '\0';

  return wStatus;
}

/*******************************************************************************
* Function Name  : UART_FlagStatus
* Description    : This routine returns the UARTx_SR register content of the
*                  selected UART.
* Input          : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* Output         : None.
* Return         : None.
*******************************************************************************/
u16 UART_FlagStatus(UART_TypeDef *UARTx)
{
  return UARTx->SR;
}

#ifdef USE_SERIAL_PORT

/*******************************************************************************
* Function Name  : SendChar
* Description    : This function sends a character using  the defined UART.
* Input          : - ch: a pointer on the character to send.
* Output         : None.
* Return         : None.
*******************************************************************************/
void SendChar( u8 *ch )
{
#ifdef USE_UART0
#define  UARTx  UART0
#endif /* Use_UART0 */

#ifdef USE_UART1
#define  UARTx  UART1
#endif /* Use_UART1 */

#ifdef USE_UART2
#define  UARTx  UART2
#endif /* Use_UART2 */

#ifdef USE_UART3
#define  UARTx  UART3
#endif /* Use_UART3 */

  UART_ByteSend(UARTx, (u8 *)ch);
}

#endif /* USE_SERIAL_PORT */

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -