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

📄 buart.c

📁 Boot code for ADM5120 with serial console for Edimax router.
💻 C
字号:
/*****************************************************************************;;    Project : ADM5120;    Creator : David Weng;    File    : buart.c;    Abstract: UART functions for plugins;    Description: Taken almost verbaim from "admboot/src/uartdrv.c";;*****************************************************************************/#include <ctype.h>#include <mips4kc.h>#include <adm5120.h>#include <buart.h>#include <uartdrv.h>#include <bconfig.h>#define TRUE		1#define FALSE		0#define UART_REG_READ(_uart, _reg)			\	*((volatile unsigned long *)(((_uart)->io_base) + (_reg)))#define UART_REG_WRITE(_uart, _reg, _val)	\	*((volatile unsigned long *)(((_uart)->io_base) + (_reg))) = (_val)static UART_OBJ uart = { 0, PA2VA(ADM5120_UART0_BASE), 0, 0 };static UINT32 timeticks = 0;UINT32 UpTime(void){	unsigned long status;	status = ADM5120_SW_REG(Timer_int_REG);	if (status & SW_TIMER_INT) {		/* Acknowledge the timer interrupt */		ADM5120_SW_REG(Timer_int_REG) = status;		timeticks++;	}	return timeticks;}static int buart_read(){	UINT32 uartfr;	UINT32 uartsr, data;	uartfr = UART_REG_READ(&uart, UART_FR_REG);	if ( ! (uartfr & UART_RX_FIFO_EMPTY)) {        data = UART_REG_READ(&uart, UART_DR_REG);		uartsr = UART_REG_READ(&uart, UART_RSR_REG);		if (uartsr & UART_RX_ERROR) {			UART_REG_WRITE(&uart, UART_ECR_REG, uartsr);			return -1;		}		if (uartsr & UART_OVERRUN_ERR) {			UART_REG_WRITE(&uart, UART_ECR_REG, uartsr);		}		return (data & 0xff);	}    return -1;}static int buart_write(char c){	UINT32 uartfr;	uartfr = UART_REG_READ(&uart, UART_FR_REG);	if ( ! (uartfr & UART_TX_FIFO_EMPTY))		return -1;	UART_REG_WRITE(&uart, UART_DR_REG, (UINT32) c);    return 0;}void buart_print(char *str){	for ( ; *str; str++) {		while (buart_write(*str) == -1)			;	}	return;}void buart_put(char c){	while (buart_write (c) == -1)		;	return;}int buart_get (int timeout){    register int c;    register unsigned long ticks;    // If no wait specified then read the channel immediately    if (timeout == 0)        return buart_read();    // time-out set, block read    ticks = UpTime();	while ((int)(UpTime() - ticks) < (timeout * 100)) {		if ((c = buart_read()) != -1)			return c;	}	return -1;}char buart_getchar(void){	register int c;	while ((c = buart_get(0)) == -1)		;	return (char) c;}void ReadLine(char *buf, int num){	int i, userKey;	char readBuf[32];	char *preadBuf;	int cpNum;	int esc_cnt = 0;	for (i = 0; i < 32; i++)		readBuf[i] = '\0';	for (i = 0; i < 256; i++) {GETKEY:		/* UGH */		userKey = buart_getchar();				if (esc_cnt) {			esc_cnt--;			goto GETKEY;	/* UGH */		}		if (userKey == 0x0d || userKey == 0x0a) {			readBuf[i] = 0;			buart_print("\r\n");			break;		} else if (userKey == 0x1b) {			esc_cnt = 2;			goto GETKEY;	/* UGH */		} else if (userKey == 0x08) {			if (--i < 0) {				i = 0;				goto GETKEY;	/* UGH */			}			readBuf[i] = 0;			--i;		} else {			buart_put(userKey);			readBuf[i] = (char) userKey;		}	}	if (i < 0) {		*buf = 0;		return;	} else if (++i <= num)		cpNum = i;	else {		cpNum = num;				// truncate the input string.		buf[num-1] = 0;	}	preadBuf = readBuf;	while (cpNum > 0) {		cpNum--;		if ((*buf++ = *preadBuf++) == 0)			break;	}}

⌨️ 快捷键说明

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