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

📄 uart.c

📁 ~{WwU_J9SC5D~}ucos~{T4Bk#,1`RkA4=S5wJT>y?IRT#,4x~}uart~{2bJT~}
💻 C
📖 第 1 页 / 共 2 页
字号:
        /*-----------------------------------------------------------------*/
        if (c == BSP)
        {
            if (cur_pos > 0)
            {
                put_byte(' ');
                put_byte('\b');
            }
            return c;
        }
        else if (c == RUBOUT)
        {
            if (cur_pos > 0)
            {
            }
        }

        /*-----------------------------------------------------------------*/
        /* Don't allow the input buffer to overflow.                       */
        /*-----------------------------------------------------------------*/
        else if (cur_pos >= INP_LEN)

        SerialPollConout(7);
        else
            return c;
    }
}

/***********************************************************************/
/*    get_line: Get a line of input                                    */
/*                                                                     */
/*     OUTPUTS: inp_buff[] and inp_cnt are updated                     */
/*                                                                     */
/***********************************************************************/
static void get_line(void)
{
    UCHAR c;

    inp_cnt = 0;

    while ((c = getch()) != CR)
    {
        inp_buff[inp_cnt++] = c;
        SerialPollConout(c);
    }

    inp_buff[inp_cnt] = 0;
    SerialPollConout('\n');
    SerialPollConout('\r');
}

/***********************************************************************/
/* read_string: Read a string from the input                           */
/*                                                                     */
/*     RETURNS: number of characters in the string                     */
/*     OUTPUTS: *dest_ptr contains the null-terminated string.         */
/*                                                                     */
/***********************************************************************/
UCHAR read_string(char *dest_ptr)
{
    char *src_ptr, count;

    get_line();
    count = 0;
    src_ptr = inp_buff;
    while ((*dest_ptr++ = *src_ptr++) != 0)
        count++;

    return count;
}

/***********************************************************************/
/* read_number: Read a number from the input                           */
/*                                                                     */
/*     RETURNS: number                                                 */
/*                                                                     */
/***********************************************************************/

ULONG get_number(void)
{
    char num[10];
    ULONG number;

    read_string(num);
    read_num(num,10,&number);
    return number;
}

static UCHAR read_num(char *ptr, UCHAR base, ULONG *val_ptr)
{
    ULONG value;
    UCHAR c, hex;


    //dbg_out("\n You input the number : %s \n",ptr);

    value = 0;
    while (*ptr == ' ') ptr++;  /* Skip leading spaces */
    hex = (base == 16);

    if (*ptr == 0) return 0;

    /*---------------------------------------------------------------------*/
    /* If hex, allow leading $ or 0x.                                      */
    /*---------------------------------------------------------------------*/
    if (hex)
    {
        if (*ptr == '$')
            ptr++;
        else if ((*ptr == '0') && ((ptr[1] == 'x') || (ptr[1] == 'X')))
            ptr = ptr + 2;
    }

    /*---------------------------------------------------------------------*/
    /* Run through the string, adding the value represented by each        */
    /* character to the total.                                             */
    /*---------------------------------------------------------------------*/
    while (*ptr)
    {
        c = *ptr++;
        if ((c >= '0') && (c <= '9'))
            c -= '0';
        else if (hex && (c >= 'A') && (c <= 'F'))
            c = c - 'A' + 10;
        else if (hex && (c >= 'a') && (c <= 'f'))
            c = c - 'a' + 10;
        else
        {
            return 0;
        }
        if (hex)
            value = (value << 4) + c;
        else
            value = value * 10 + c;
    }

    *val_ptr = value;
    
    return 1;
}


void Prompt(char *string, PARM_TYPE ptype, void *parm_ptr)
{
    UCHAR rc;
    char line[80], *line_ptr, *seg_ptr;
    ULONG temp, ip_byte[4];
    int i;
    UCHAR *ptr;

    Print(string);
    switch (ptype)
    {
        case PARM_DECIMAL:
            get_line();
            if (read_num(inp_buff, 10, &temp))
               *((ULONG *)parm_ptr) = temp;
            break;

        case PARM_HEX:
            get_line();
            if (read_num(inp_buff, 16, &temp))
               *((ULONG *)parm_ptr) = temp;
            break;

        case PARM_FLAG:
            if ((rc = read_string(line)) != 0)
            {
                if (rc != 1) {}
                else if ((line[0] == 'Y') || (line[0] == 'y'))
                   *((UCHAR *)parm_ptr) = 1;
                else if ((line[0] == 'N') || (line[0] == 'n'))
                   *((UCHAR *)parm_ptr) = 0;
                else	{}
            }
        break;

    case PARM_CHAR:
        if ((rc = read_string(line)) != 0)
        {
            if (rc != 1)	{}
            else
               *((UCHAR *)parm_ptr) = line[0];
            }
        break;

    case PARM_STRING:
        if ((rc = read_string(line)) != 0)
            {
            line_ptr = line;
            ptr = (UCHAR *)parm_ptr;
            while ((*ptr++ = *line_ptr++) != 0);
            }
        break;

    case PARM_IP:

        if ((rc = read_string(line)) != 0)
            {
            line_ptr = line;
            for (i = 3; i >= 0; i--)
                {
                seg_ptr = line_ptr;
                while ((*line_ptr) && (*line_ptr != '.'))
                    line_ptr++;
                if ((*line_ptr == '.') && (line_ptr[1]))
                   *line_ptr++ = 0;
                if (!read_num(seg_ptr, 10, &ip_byte[i]) || (ip_byte[i] > 255))
                    {
                    return;
                    }
                }
           *((ULONG *)parm_ptr) = (ip_byte[3] << 24)
                                  + (ip_byte[2] << 16)
                                  + (ip_byte[1] << 8)
                                  + ip_byte[0];
            }
        break;
    }
}


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);
}



/* Transmit Que write function */
uint32 TxQWr(uint32 channel,uint8 data)
{
        int TxQEmpty=0;
        if(TxQ[channel].wptr+1 == TxQ[channel].rptr) 
          {
            return(ERROR); /* ring buffer full state */
          }
        if(TxQ[channel].wptr == TxQ[channel].rptr) 
          TxQEmpty=1;
        TxQ[channel].buff[TxQ[channel].wptr++] = data;
        if(TxQ[channel].wptr == MAXEVENT) 
          {
            TxQ[channel].wptr=0;
          }
        if(TxQEmpty) UARTTxIntOn(channel);     /* Transmit interrupt on */
        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++]);
}


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);
}



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);
}


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 {
                i = 0; // clear character counter
                decimal = 0;
                put_string("0x");
         }

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

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


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                                                            */
/*                                                                      */
/*      kbd_hit                                                         */
/*                                                                      */
/*  DESCRIPTION                                                         */
/*                                                                      */
/*                                                                      */
/*  AUTHOR                                                              */
/*                                                                      */
/*                                                                      */
/*  CALLED FROM                                                         */
/*                                                                      */
/*      Application routines                                            */
/*                                                                      */
/*  ROUTINES CALLED                                                     */
/*                                                                      */
/*                                                                      */
/*  INPUTS                                                              */
/*                                                                      */
/*                                                                      */
/*  OUTPUTS                                                             */
/*                                                                      */
/*      return(c)              return character                         */
/*                                                                      */
/* HISTORY                                                              */
/*                                                                      */
/*         NAME            DATE                    REMARKS              */
/*                                                                      */
/*         hbahn         10-16-1998     Created initial version 1.0     */
/************************************************************************/
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 ;
}


⌨️ 快捷键说明

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