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

📄 uart.c

📁 移植在鱼板上运行的ucos
💻 C
📖 第 1 页 / 共 3 页
字号:
	{
             Temp=RxQ[1].wptr+1;
             if(Temp==MAXEVENT)
             	Temp=0;/*loop back*/
             if(Temp!= RxQ[1].rptr)
             {
                   RxQ[1].buff[RxQ[1].wptr] = UARTRXB1;
                   RxQ[1].wptr = Temp; 
             }
	}

        UARTRxIntOff(1);
}



void UARTTxIntOn(uint32 channel)
{
    if(channel) {
	/* Enable Interrupt */
	Enable_Int(nUART1_TX_INT);
	SetPendingBit(nUART1_TX_INT);
    }
    else {
	/* Enable Interrupt */
	Enable_Int(nUART0_TX_INT);
	SetPendingBit(nUART0_TX_INT);
    }
}

void UARTRxIntOn(uint32 channel)
{
     if(channel) {
	/* Enable Interrupt */
	Enable_Int(nUART1_RX_ERR_INT);
     }
     else {
	/* Enable Interrupt */
	Enable_Int(nUART0_RX_ERR_INT);
     }
}


void UARTTxIntOff(uint32 channel)
{
     if(channel) {
		/* Enable Interrupt */
		Disable_Int(nUART1_TX_INT);
        Clear_PendingBit(nUART1_TX_INT) ;
     }
     else {
		/* Disable Interrupt */
		Disable_Int(nUART0_TX_INT);
        Clear_PendingBit(nUART0_TX_INT) ;
     }
}

void UARTRxIntOff(uint32 channel)
{
     if(channel) {
		/* Disable Interrupt */
		Disable_Int(nUART1_RX_ERR_INT);
        //Clear_PendingBit(nUART1_RX_ERR_INT) ;
     }
     else {
        /* Disable Interrupt */
		Disable_Int(nUART0_RX_ERR_INT);
        //Clear_PendingBit(nUART0_RX_ERR_INT) ;
     }
}


/************************************************************************/
/*                                                                      */
/*  FUNCTION                               "UART INTERRUPT FUNCTIONS"   */
/*                                                                      */
/*                                                                      */
/*      TxQWr                    Write data to transmit Que.            */
/*      RxQRd                    Read data from receive Que.            */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*      These Que management functions can be used in interrupt mode.   */
/*                                                                      */
/*  AUTHOR                                                              */
/*                                                                      */
/*                                                                      */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      Application routines                                            */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*                                                                      */
/*  OUTPUTS                                                             */
/*                                                                      */
/*      return(c)              return character                         */
/*                                                                      */
/* HISTORY                                                              */
/*                                                                      */
/*         NAME            DATE                    REMARKS              */
/*                                                                      */
/*         in4maker     10-16-1998      Created initial version 1.0     */
/************************************************************************/

/* Transmit Que write function */
uint32 TxQWr(uint32 channel,uint8 data)
{
        TxQ[channel].buff[TxQ[channel].wptr++] = data;

        if(TxQ[channel].wptr == MAXEVENT) 
        {
            TxQ[channel].wptr=0;
        }
        
        if(TxQ[channel].wptr==TxQ[channel].rptr)
        	return(ERROR);
        	
        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.                                           */
/*                                                                      */
/*  AUTHOR                                                              */
/*                                                                      */
/*                                                                      */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      Service routines that request serial input                      */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*                                                                      */
/*  OUTPUTS                                                             */
/*                                                                      */
/*      return(c)              return character                         */
/*                                                                      */
/* HISTORY                                                              */
/*                                                                      */
/*         NAME            DATE                    REMARKS              */
/*                                                                      */
/*         in4maker     06-01-1999      Created initial version 1.0     */
/*                                                                      */
/************************************************************************/
uint8 i_getc(uint32 channel)
{
    UARTRxIntOn(channel);
    return(RxQRd(channel));
}

/* 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.                 */
/*                                                                      */
/*  AUTHOR                                                              */
/*                                                                      */
/*                                                                      */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      Service routines that request serial output                     */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*                                                                      */
/*  OUTPUTS                                                             */
/*                                                                      */
/*                                                                      */
/* HISTORY                                                              */
/*                                                                      */
/*         NAME            DATE                    REMARKS              */
/*                                                                      */
/*         in4maker     06-01-1999      Created initial version 1.0     */
/*                                                                      */
/************************************************************************/
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.          */
/*                                                                      */
/*  AUTHOR                                                              */
/*                                                                      */
/*                                                                      */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      Application routines                                            */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*                                                                      */
/*  OUTPUTS                                                             */
/*                                                                      */
/*      return(c)              return character                         */
/*                                                                      */
/* HISTORY                                                              */
/*                                                                      */
/*         NAME            DATE                    REMARKS              */
/*                                                                      */
/*         in4maker     10-16-1998      Created initial version 1.0     */
/************************************************************************/
void put_char(uint32 channel,char ch) 
{
    if(channel) {
        WaitXmitter(UARTSTAT1);

⌨️ 快捷键说明

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