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

📄 consol.c

📁 EP7312的入门例子
💻 C
字号:
/*--------------------------------------------------------------------------------------------------------------------------------
 *
 * consol.c - 
 *
 *
 * Copyright (c) 1999 SUN Sirius, Inc.
 *
--------------------------------------------------------------------------------------------------------------------------------*/
#include "type.h"
#include "ep7312.h"
#include "consol.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static int wChannel = 1;

#define is_alpha(c) (c > '9')
#define is_upper(c) !(c & 0x20)

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_select
 * Description	: This routine selects the consol port for active usage.
 * Arguments	: wCh - UART channel number. It must be the following value. UART1 or UART2
 * Return		: None
-------------------------------------------------------------------------------------------------------------------------*/
void consol_select(int wCh)
{
	wChannel = wCh;
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_init
 * Description	: This routine initializes the consol port by setting its baud rate & FIFO buffer.
 * Arguments	: wBaud		- Baud Rate in bit/sec.
					: bFifoFlag	- FIFO Flag. If True, then FIFO enabled. If False, then FIFO disabled.	 
 * Return		: None
 * Note(s)		: Before calling this, consol_select needs to called for selecting the consol port.
-------------------------------------------------------------------------------------------------------------------------*/
void consol_init(int wBaud, u8 bFifoFlag)
{
	if(wChannel == 1)
	{
		rUBRLCR1 = ((0x03<<17)|(bFifoFlag<<16)|((115200*2)/wBaud-1));
		rSYSCON1 |= (0x01<<8);
	}
	else
	{
		rUBRLCR2 = ((0x03<<17)|(bFifoFlag<<16)|((115200*2)/wBaud-1));
		rSYSCON2 |= (0x01<<8);
	}
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_getch
 * Description	: This routine waits for a character from the consol port & returns it.
 * Arguments	: None
 * Return		: Returns the character read from the consol port.
-------------------------------------------------------------------------------------------------------------------------*/
char consol_getch(void)
{
	if(wChannel == 1)
	{
		while(rSYSFLG1 & (0x01<<22));
		return rUARTDR1;
	}
	else
	{
		while(rSYSFLG2 & (0x01<<22));
		return rUARTDR2;
	}
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_getchar
 * Description	: This routine reads a character from the consol port if available.
 * Arguments	: pbData		- Pointer to return the received data.
 * Return		: Returns the state of the RX buffer.
 *						True --Data returned is valid.  False  -- Data retuened is invalid.
-------------------------------------------------------------------------------------------------------------------------*/
char consol_getchar(char *pbData)
{
	if(wChannel == 1)
	{
		if((rSYSFLG1 & (0x01<<22)) == 0x00)
		{
			*pbData = rUARTDR1;
			return True;
		}
		return False;
	}
	else
	{
		if((rSYSFLG2 & (0x01<<22)) == 0x00)
		{
			*pbData = rUARTDR2;
			return True;
		}
		return False;
	}
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_getstring
 * Description	: This routine waits for a string from the consol port & returns it.
 * Arguments	: pbString		- Pointer to return the received string.
 * Return		: None
-------------------------------------------------------------------------------------------------------------------------*/
void consol_getstring(char *pbString)
{	
	char bC;
	char *pbTmpString = pbString;

	while((bC = consol_getch()) != '\r')
	{
		if(bC == 'b')
		{
			if((int)pbTmpString < (int)pbString)
			{
				consol_printf("\b \b");
				pbString--;
			}
		}
		else
		{
			*pbString++ = bC;
			consol_sendch(bC);
		}
	}
	*pbString = '\0';
	consol_sendch('\n');
}


/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_getinteger
 * Description	: This routine waits for a Integer from the consol port & returns it.
 * Arguments	: None.
 * Return		: Returns the received Integer value.
-------------------------------------------------------------------------------------------------------------------------*/
int consol_getinteger(void)
{
	int wI;
	int	 wLastIndex;
	int wBase	= 10;
	int wMinus	= 0;
	int wResult	= 0;
	char abStr[30];
	char *pbString = abStr;

	consol_getstring(pbString);

	if(pbString[0] == '-')
	{
		wMinus = 1;
		pbString++;
	}

	if((pbString[0] == '0') && (pbString[1] == 'x' || pbString[1] == 'X'))
	{
		wBase = 16;
		pbString += 2;
	}
	
	wLastIndex = strlen(pbString) - 1;
	if(pbString[wLastIndex] == 'h' || pbString[wLastIndex] == 'H')
	{
		wBase = 16;
		pbString[wLastIndex] = 0;
		wLastIndex--;
	}

	if(wBase == 10)
	{
		wResult = atoi(pbString);
		wResult = wMinus ? (-1*wResult):wResult;
	}
	else
	{
		for(wI=0; wI<=wLastIndex; wI++)
		{
			if(is_alpha(pbString[wI]))
			{
				if(is_upper(pbString[wI]))
					wResult = (wResult<<4) + pbString[wI] - 'A' +10;
				else
					wResult = (wResult<<4) + pbString[wI] - 'a' +10;
			}
			else
			{
				wResult = (wResult<<4) + pbString[wI] - '0';
			}

		}
		wResult = wMinus ? (-1*wResult):wResult;
	}

	return wResult;
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_sendchar
 * Description	: This routine waits till the character is sent. 
 * Arguments	: bData	- Data to be sent.
 * Return		: None.
-------------------------------------------------------------------------------------------------------------------------*/
void consol_sendchar(char bData)
{
	if(wChannel == 1)
	{
		while(rSYSFLG1 & (0x01<<23));	//Wait until THR is empty.
			//Delay(4);
		rUARTDR1 = bData;
	}
	else
	{
		while(rSYSFLG2 & (0x01<<23));	//Wait until THR is empty.
			//Delay(4);
		rUARTDR2 = bData;
	}
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_sendch
 * Description	: This routine waits till the character is sent. It also sends an extra carriage return character when sending a new 
 *					  line character.
 * Arguments	: bData	- Data to be sent.
 * Return		: None.
-------------------------------------------------------------------------------------------------------------------------*/
void consol_sendch(char bData)
{
	if(bData == '\n')
		consol_sendchar('\r');

	consol_sendchar(bData);
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_sendstring
 * Description	: This routine waits till the string is sent.
 * Arguments	: pbString	- String to be sent.
 * Return		: None.
-------------------------------------------------------------------------------------------------------------------------*/
void consol_sendstring(char *pbString)
{
	while(*pbString)
	{
		consol_sendch(*pbString++);
	}
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_scanf
 * Description	: Reads input from the consol system, under control of the string pointed to by format that specifies(指定) the
 *					  admissible input sequences and how they are to be converted for assignment(任务), using subsequent(后来的) 
 *					  arguments as pointers to the objects to receive the converted input. If there are insufficient(不足的) arguments
 *					  for the format, the behavior is undefined. If the format is exhausted(用尽) while arguments remain(剩余), the 
 *					  excess(过度的,额外的) arguments are ignored.
 * Arguments	: pcFmt		- Format string. It can contain only the following format specifiers:
							%s	- string
							%c	- character
							%i		- integer
 *					  ...			- Are the passed parameters (pointers to the objects to receive the converted input).
 * Return		: None.
 * Note(s)		:
 *					admissible : 可容许的, 可接纳的
 *					studies of the rules for forming admissible words.组成合法词语的规则研究。
-------------------------------------------------------------------------------------------------------------------------*/
void consol_scanf(char *pcFmt,...)
{
	va_list	pArg;
	char		cChar;
	int			*pwInt;
	char		*pbChar;

	va_start(pArg, pcFmt);
	while((cChar = *pcFmt++) != '\0')
	{
		if(cChar != '%')
			continue;

		switch(*pcFmt)
		{
			case 's':
			case 'S':
				pbChar = va_arg(pArg, char *);
				consol_getstring(pbChar);
				break;
 			case 'i':
			case 'I':
				pwInt = va_arg(pArg, int *);
				*pwInt = consol_getinteger();
				break;
			case 'c':
			case 'C':
				pbChar = va_arg(pArg, char *);
				*pbChar = consol_getch();
				break;
		}
	}

	va_end(pArg);
}

/*-------------------------------------------------------------------------------------------------------------------------
 * Routine		: consol_printf
 * Description	: Writes output to the consol system, under control of the string pointed to by format that insufficient arguments
 *					  for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess 
 *					  arguments are ignored.
 * Arguments	: pcFmt		- Format string. It can contain all the format specifies.
 *					  ...			- Are the passed parameters (pointers to the objects to receive the converted input).
 * Return		: None.
-------------------------------------------------------------------------------------------------------------------------*/
void consol_printf(char *pcFmt,...)
{
	va_list ap;
	char pbString[256];

	va_start(ap, pcFmt);
	vsprintf(pbString, pcFmt, ap);
	consol_sendstring(pbString);
	va_end(ap);
}

⌨️ 快捷键说明

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