📄 uart.c
字号:
/* 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 */
switch(channel)
{
case 0:
UARTTXH0=ch;
break;
case 1:
UARTTXH1=ch;
break;
}
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);
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" */
/* */
/* 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 */
/************************************************************************/
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_printf("\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);
}
/************************************************************************/
/* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -