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

📄 uart.c

📁 网络服务器上实现操作系统和嵌入式协议栈的 结合
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  CALLED FROM                                                         */
/*      Application routines                                            */
/*  OUTPUTS                                                             */
/*      return(c)              return character                         */
/************************************************************************/
/* Transmit Que write function */
uint32 TxQWr(uint32 channel,uint8 data)
{
        if(TxQ[channel].wptr+1 == TxQ[channel].rptr) 
        {
            return(ERROR); /* ring buffer full state */
        }

        TxQ[channel].buff[TxQ[channel].wptr++] = data;

        if(TxQ[channel].wptr == MAXEVENT) 
        {
            TxQ[channel].wptr=0;
        }
        return(SUCCESS);
}


/* Receive Que read function */
uint8 RxQRd(uint32 channel)
{
     if(RxQ[channel].rptr == MAXEVENT) 
           RxQ[channel].rptr=0; /*loop back*/

     if(RxQ[channel].rptr == RxQ[channel].wptr) 
           return('\0');

     return(RxQ[channel].buff[RxQ[channel].rptr++]);
}

/************************************************************************/
/*  FUNCTION                               "UART INTERRUPT FUNCTIONS"   */
/*      i_getc()                                                        */
/*      i_gets()                                                        */
/*  DESCRIPTION                                                         */
/*      This function attempts to get a character from the serial       */
/*      driver's input queue.                                           */
/*  CALLED FROM                                                         */
/*      Service routines that request serial input                      */
/************************************************************************/
uint8 i_getc(uint32 channel)
{
    //uint8 InChar;

    UARTRxIntOn(channel);

    //InChar = RxQRd(channel);
    return(RxQRd(channel));

    //if(InChar != NULL) return(InChar);
    //else return(NULL);
}

/* Interrupt get string */
uint32 i_gets(uint32 channel, uint8 *s)
{
     uint32   count;
     uint8    c;

     count = 0; 

     while((c = (uint8)i_getc(channel)) != CR)
     {
            count++;
            *s++ = c;
     }

     *s = (uint8)NULL; 

     return(count);
}


/************************************************************************/
/*                                                                      */
/*  FUNCTION                               "UART INTERRUPT FUNCTIONS"   */
/*                                                                      */
/*      i_putc()     This function attempts to out a character from     */
/*                   the serial                                         */ 
/*      i_puts()     This function prints a null-terminated string to   */
/*                   the terminal.                                      */ 
/*      i_prinf()    Formatted output string.                           */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*      driver's transmit queue to uart serial channel.                 */
/*  CALLED FROM                                                         */
/*      Service routines that request serial output                     */
/************************************************************************/
uint32  i_putc(uint32 channel, uint8 ch)
{
    if(U_TX_COMPLETE(channel))               /* check transmit complet */
    {
           if(TxQWr(channel, ch))            /* write data to tx que */
           {
               UARTTxIntOn(channel);         /* Transmit interrupt on */
               while(!U_BUFF_EMPTY(channel)); /* wait for tx buffer empty */

               return(SUCCESS);
           }
    }

    return(ERROR);                 
}

/* UART Interrupt put string */
uint32  i_puts(uint32 channel, uint8 *str)
{    
       uint32     i;      /* Working variable         */
       uint32     sz;    

       sz = strlen((const char *)str); 

        /* Loop to print out all characters.  */
        for(i=0; i < sz ; i++) 
        {
                /* Call i_putc to print each character.  */
                while(!i_putc(channel, *(str + i)));
        }
 
        return(SUCCESS);
}

/* formatted output string */
void i_printf(char *fmt, ...)
{
    va_list argptr;
    char temp_buf[256];

    va_start(argptr, fmt);
    vsprintf(temp_buf, fmt, argptr);
    sputs((uint8 *)temp_buf);
    va_end(argptr);
}

/************************************************************************/
/*                                                                      */
/*  FUNCTION                            "UART POLL FUNCTIONS"           */
/*                                                                      */
/*      put_char               put character to uart channel by polling.*/
/*      get_char               get character from uart channel by       */
/*                             polling method.                          */
/*      put_string             put string to uart channel by polling.   */
/*      dbg_out                formatted output to uart channel.        */
/*  DESCRIPTION                                                         */
/*      These serial I/O functions are useful in POLLING mode.          */
/*  CALLED FROM                                                         */
/*      Application routines                                            */
/************************************************************************/
void put_char(uint32 channel,char ch) 
{
    if(channel) {
        WaitXmitter(UARTSTAT1);
        UARTTXH1 = ch;
    }
    else {
        WaitXmitter(UARTSTAT0);
        UARTTXH0 = ch;
    }
}


char get_char(uint32 channel) 
{ 
  char ch;
 
    if(channel) { WaitRcver(UARTSTAT1);
                  ch = UARTRXB1;
    }
    else { WaitRcver(UARTSTAT0);
           ch = UARTRXB0;
    }
    return ch;
}


void put_string(char *ptr ) 
{
    while(*ptr )  
    {
         put_byte(*ptr++ );
    }
}


void dbg_out(char *fmt, ...)
{
    va_list argptr;
    char temp_buf[256];

    va_start(argptr, fmt);
    vsprintf(temp_buf, fmt, argptr);
    put_string(temp_buf);
    va_end(argptr);
}


/************************************************************************/
/*  FUNCTION                                                            */
/*      gethex2dec             get hex-string from hexdecimal digit     */
/*  DESCRIPTION                                                         */
/*      s = gethex2dec(3fff);                                           */
/*      return value s = "3fff"                                         */
/*  CALLED FROM                                                         */
/*      Application routines                                            */
/************************************************************************/
uint32 gethex2dec(uint32 digitcnt)
{
    uint32   decimal = 0;
    uint8    hex;
    uint32   i;

    put_string("0x");

    hex = get_byte();

    for(i=0;(i<digitcnt)&&(!is_control(hex));i++) {
         hex = to_upper(hex);

         if(is_xdigit(hex)) {

              if(is_digit(hex))  /* '0' - '9' */
                   decimal = decimal * 16 + hex - '0';
              else               /* 'A' - 'F' */
                   decimal = decimal * 16 + 10 + hex - 'A';
         }
         else {
                /*Print("\r[ERROR] Re-enter %d hexdigit\r",digitcnt);*/
                i = 0; // clear character counter
                decimal = 0;
                put_string("0x");
         }

         hex = get_byte(); //get next character
    }

    put_byte('\r');
    return(decimal);
}


/************************************************************************/
/*  FUNCTION                                                            */
/*      get_num                get decimal number from hexa digit       */
/*  CALLED FROM                                                         */
/*      Application routines                                            */
/************************************************************************/
uint32 get_num(void)
{
	char RcvData[10] ;
	unsigned RcvNum=0 ;
	int i = 0 ;

	while ( (RcvData[i] = get_byte() ) != CR )
	{
		if ( (RcvData[i] >= '0') && (RcvData[i] <= '9') )
			RcvNum=(RcvNum<<4) + (RcvData[i]-'0') ;
		else if ( (RcvData[i] >= 'A') && (RcvData[i] <= 'F') )
			RcvNum=(RcvNum<<4) + (RcvData[i]-'A'+ 10) ;
		else if ( (RcvData[i] >= 'a') && (RcvData[i] <= 'f') )
			RcvNum=(RcvNum<<4) + (RcvData[i]-'a'+ 10) ;
		i++ ;
	}

	return RcvNum ;
}


/************************************************************************/
/*  FUNCTION                                                            */
/*      get_digit              get decimal number from ASCII data.      */
/*  DESCRIPTION                                                         */
/*      ex)  If input character is "123",get_digit()'s return value     */
/*           will be 123.                                               */
/************************************************************************/
uint32  get_digit(void)
{
	uint8     RcvData[10] ;
	int       RcvNum=0 ;
	int       RcvDataSize=0 ;
	int       i ;

	while ((RcvData[RcvDataSize] = get_byte()) != CR) RcvDataSize++ ;

	for (i=0;i < RcvDataSize ; i++)
		RcvNum = RcvNum*10 + (RcvData[i]-'0') ; 

	return RcvNum ;
}


/************************************************************************/
/*  FUNCTION                                                            */
/*      kbd_hit                                                         */
/************************************************************************/
uint32 kbd_hit(void) 
{
    uint32 KbdHit ;
    int8   ch ;

    #ifdef PORT1
	if (UARTSTAT1 & USTAT_RCV_READY) 
		{
		KbdHit = 1 ;
        	ch = UARTRXB1;
		}
	else KbdHit = 0 ;
    #else
	if (UARTSTAT0 & USTAT_RCV_READY) 
		{
		KbdHit = 1 ;
        	ch = UARTRXB0;
		}
	else KbdHit = 0 ;
    #endif

    return KbdHit ;
}

/************************************************************************/
/*  FUNCTION                              "BaudRateVal"                 */
/*  DESCRIPTION                                                         */
/*      This function return the value of UART baudrate divisior,       */
/*      UBRDIVn according to the baudrate.                              */
/*  CALLED FROM                                                         */
/*      UART_Initialize                                                 */
/*  INPUTS                                                              */
/*      baud              UART baud rate(ex. 38400,57600,..)            */
/*  OUTPUTS                                                             */
/*     baudrate divisor value                                           */
/************************************************************************/
uint32 BaudRateVal(uint32 baud)
{
     uint32 index;

     for(index = 0; index < BAUD_TABLE; index++) 
     {
           if(U_BaudRate[index].baud == baud) return(index);          
     }

     return(0); /* baudrate data doesn't in table */
}


uint32 BaudRate(uint32 div)
{
     uint32 index;

     for(index = 0; index < BAUD_TABLE; index++) 
     {
           if(U_BaudRate[index].div == div) return(index);          
     }

     return(0); /* baudrate data doesn't in table */
}

⌨️ 快捷键说明

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