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

📄 bsp_serial.c

📁 嵌入式的tcpip协议栈
💻 C
字号:
#include <includes.h>

/*
*********************************************************************************************************
*********************************************************************************************************
**                                       Serial Port Communications
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                                   Ser_Init()
*
* Description : Initialize a serial port for communication.
*
* Argument(s) : Serial Port to initilize.
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  Ser_Init (CPU_INT08U COM)
{
    CPU_INT16U  divisor;                                        /* Baud rate divisor.                                       */
    CPU_INT08U  divlo;
    CPU_INT08U  divhi;
    CPU_INT32U  pclk_freq;
    CPU_INT32U  pinsel;


    if (COM == SER_UART_0){
                                                                /* Compute divisor for desired baud rate.                   */
    pclk_freq =  BSP_CPU_PclkFreq(PCLK_UART0);                  /* Get the CPU clock frequency.                             */

    divisor   = (CPU_INT16U)(((2 * pclk_freq / 16 / 115200) + 1) / 2);
    divlo     =  divisor & 0x00FF;                              /* Split divisor into LOW and HIGH bytes.                   */
    divhi     = (CPU_INT08U)(divisor >> 8) & 0x00FF;

                                                                /* Configure P0.2 & P0.3 for UART0.                         */
    pinsel    = PINSEL0;
    pinsel   &= 0xFFFFFF0F;
    pinsel   |= 0x00000050;
    PINSEL0   = pinsel;

    U0LCR     = DEF_BIT_07;                                     /* Set divisor access bit.                                  */
    U0DLL     = divlo;                                          /* Load divisor.                                            */
    U0DLM     = divhi;
    U0LCR     = DEF_BIT_00 | DEF_BIT_01;                        /* 8 Bits, 1 Stop, No Parity.                               */
    U0IER     = 0x00;                                           /* Disable both Rx and Tx interrupts.                       */
    U0FCR     = DEF_BIT_00 | DEF_BIT_01 | DEF_BIT_02;           /* Enable FIFO, flush Rx & Tx.                              */
    }

    if (COM== SER_UART_1){
                                                                /* Compute divisor for desired baud rate.                   */
    pclk_freq =  BSP_CPU_PclkFreq(PCLK_UART1);                  /* Get the CPU clock frequency.                             */

    divisor   = (CPU_INT16U)(((2 * pclk_freq / 16 / 115200) + 1) / 2);
    divlo     =  divisor & 0x00FF;                              /* Split divisor into LOW and HIGH bytes.                   */
    divhi     = (CPU_INT08U)(divisor >> 8) & 0x00FF;

                                                                /* Configure P3.16 & P3.17 for UART1.                       */
    pinsel    = PINSEL7;
    pinsel   &= 0xFFFFFFF0;
    pinsel   |= 0x0000000F;
    PINSEL7   = pinsel;

    U1LCR     = DEF_BIT_07;                                   /* Set divisor access bit.                                  */
    U1DLL     = divlo;                                        /* Load divisor.                                            */
    U1DLM     = divhi;
    U1LCR     = DEF_BIT_00 | DEF_BIT_01;                      /* 8 Bits, 1 Stop, No Parity.                               */
    U1IER     = 0x00;                                         /* Disable both Rx and Tx interrupts.                       */
    U1FCR     = DEF_BIT_00 | DEF_BIT_01 | DEF_BIT_02;         /* Enable FIFO, flush Rx & Tx.                              */
  }
}

/*
*********************************************************************************************************
*                                                Ser_WrByte()
*
* Description : Writes a single byte to a serial port.
*
* Argument(s) : tx_byte     The character to output. 
*             : COM         Serial Port (0,1)
*
* Return(s)   : none.
*
* Note(s)     : (1) This function blocks until room is available in the UART for the byte to be sent.
*********************************************************************************************************
*/

void  Ser_WrByte (CPU_CHAR tx_byte,CPU_INT08U COM)
{
  if (COM == SER_UART_0){
    while ((U0LSR & DEF_BIT_05) == 0) {
        ;
    }

    U0THR = tx_byte;
  }

  if (COM == SER_UART_1){
    while ((U1LSR & DEF_BIT_05) == 0) {
        ;
    }

    U1THR = tx_byte;
  }
}

/*
*********************************************************************************************************
*                                                Ser_WrStr()
*
* Description : Write a character string to a serial port.
*
* Argument(s) : tx_str      A character string.
*             : COM         Serial Port (0,1)
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  Ser_WrStr (CPU_CHAR *tx_str,CPU_INT08U   COM)
{
    while ((*tx_str) != 0) {
      
     if (COM == SER_UART_0)
        Ser_WrByte(*tx_str++,SER_UART_0);
     else
        Ser_WrByte(*tx_str++,SER_UART_1); 
 
    }
}

/*
*********************************************************************************************************
*                                                Ser_RdByte()
*
* Description : Read a byte from a serial port and echo byte to port.
*
* Argument(s) : none.
*
* Return(s)   : A byte containing the value of the received charcater.
*             : COM         Serial Port (0,1)
*
* Note(s)     : (1) This function blocks until a character appears at the port.
*********************************************************************************************************
*/

CPU_INT08U  Ser_RdByte (CPU_INT08U COM)
{
    CPU_INT08U  rx_byte;


    if (COM == SER_UART_0){
      while ((U0LSR & DEF_BIT_00) == 0) {
          OSTimeDly(1);
      }

      rx_byte = (CPU_INT08U)(U0RBR);                              /* Remove the data from the holding register.               */
      
    }


    if (COM == SER_UART_1){
      while ((U1LSR & DEF_BIT_00) == 0) {
          OSTimeDly(1);
      }
      rx_byte = (CPU_INT08U)(U1RBR);                              /* Remove the data from the holding register.               */
     
    }
    
    return (rx_byte);
}

/*
*********************************************************************************************************
*                                                Ser_RdStr()
*
* Description : This function reads a string using a UART.
*
* Arguments   : rx_str      A pointer to a buffer at which the string can be stored
*               len         The size of the string that will be read
*             : COM         Serial Port (0,1)
*
* Returns     : none.
*********************************************************************************************************
*/

void  Ser_RdStr (CPU_CHAR    *rx_str,
                 CPU_INT32U   len,
                 CPU_INT08U   COM)
{
    CPU_CHAR  input;
    CPU_CHAR  input_ix;


    input_ix  = 0;
    rx_str[0] = 0;

    while (1)
    {
      
      if (COM  == SER_UART_0)
        input = Ser_RdByte(SER_UART_0);
      else
        input = Ser_RdByte(SER_UART_1); 


        if ((input == '\r') ||
            (input == '\n')) {
             if ( COM== SER_UART_0)
               Ser_WrByte('\n',SER_UART_0);
             else
               Ser_WrByte('\n',SER_UART_1); 
              
               rx_str[input_ix] = 0;
               return;
        }

        if (input == '\b') {
            if (input_ix > 0) {
                if(COM == SER_UART_0)
                  Ser_WrStr((CPU_CHAR *)"\b \b",SER_UART_0);
                else
                  Ser_WrStr((CPU_CHAR *)"\b \b",SER_UART_1); 
                input_ix--;
                rx_str[input_ix] = 0;
            }
        }

        if (Str_IsPrint(input)) {
            if(COM == SER_UART_0)
               Ser_WrByte(input,SER_UART_0);
            else
               Ser_WrByte(input,SER_UART_1); 
         
            rx_str[input_ix] = input;
            input_ix++;
            if (input_ix >= len) {
               input_ix = len;
            }
        }
    }
}
/*
*********************************************************************************************************
*                                                Ser_RdStrNoEcho()
*
* Description : This function reads a string using a UART.
*
* Arguments   : rx_str      A pointer to a buffer at which the string can be stored
*               len         The size of the string that will be read
*             : COM         Serial Port (0,1)
*
* Returns     : none.
*********************************************************************************************************
*/

void  Ser_RdStrNoEcho (CPU_CHAR    *rx_str,
                 CPU_INT32U   len,
                 CPU_INT08U   COM)
{
    CPU_CHAR  input;
    CPU_CHAR  input_ix;


    input_ix  = 0;
    rx_str[0] = 0;

    while (1)
    {
      if (COM  == SER_UART_0){
         if((U0LSR & DEF_BIT_00) == 0) {
           return;
       }
      }else{
         if((U1LSR & DEF_BIT_00) == 0) {
           return;
         }
      }
      
       
         if (COM  == SER_UART_0)
           input = Ser_RdByte(SER_UART_0); 

         else
          input = Ser_RdByte(SER_UART_1); 


        rx_str[input_ix] = input;
        input_ix++;
        if (input_ix >= len) {
            return;
        }
    }

}
/*
*********************************************************************************************************
*                                                Ser_Printf()
*
* Description : Formatted outout to the serial port.
*
* Argument(s) : format      Format string following the C format convention.
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  Ser_Printf (CPU_CHAR *format, ...)
{
    static  CPU_CHAR  buffer[80 + 1];
            va_list   vArgs;


    va_start(vArgs, format);
    vsprintf((char *)buffer, (char const *)format, vArgs);
    va_end(vArgs);
#if (SER_COMM_SEL == SER_UART_0)
    Ser_WrStr((CPU_CHAR*) buffer,SER_UART_0);
#else 
    Ser_WrStr((CPU_CHAR*) buffer,SER_UART_1); 
#endif   
    
}

⌨️ 快捷键说明

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