📄 uart.c
字号:
#undef MOD_GLOBALS
#define MOD_COM
#include "stdarg.h"
#include "snds.h"
#include "isr.h"
#include "uart.h"
#define rUARTLCON0 rULCONWL_0 | rSTOPBIT_0 | rULCON_PMD_0 | rCLKBIT_0
#define rUARTLCON1 rULCONWL_1 | rSTOPBIT_1 | rULCON_PMD_1 | rCLKBIT_1
#define rUARTCONT0 UCON_RXM_INTREQ | UCON_TXM_INTREQ | UCON_RXSTAT_INT
#define rUARTCONT1 UCON_RXM_INTREQ | UCON_TXM_INTREQ | UCON_RXSTAT_INT
/****************************************************************************
README
This file realized serial communication, include serial 0 and serial 1
Serial mode can be interrupt or query.
if use ucos, task suspend and resume is use at interrupt mode
*****************************************************************************/
#define BAUD_TABLE 7 /* No of baud rate table */
char putchar(char c);
char i_getkey (U32 channel);
const BaudTable U_BaudRate[BAUD_TABLE] = {
/* for 50MHz/2 UART clock */
9600, 0x00a20,
19200, 0x00500,
38400, 0x00280,
57600, 0x001a0,
115200, 0x000d0,
230400, 0x00060,
460800, 0x00020 // not available
};
uint32 UART_Initialize()
{
Disable_Int(nGLOBAL_INT);
UARTLCON0=0x03; //8位数据,1停止位,无校验,内部时钟
UARTLCON1=0x03;
UARTCONT0=0x0d; //Rx,Tx中断模式,无中断,无DSR
UARTCONT1=0x0d;
UARTBRD0 = 0x00500; //波特率19200
UARTBRD1 = 0x00500; //波特率19200
Enable_Int(nGLOBAL_INT); /* Global interrupt disabled */
}
uint32 gethex2dec(uint32 n)
{}
uint32 get_num(void)
{
return(get_digit());
}
uint32 get_digit(void)
{char ch;
ch=i_getkey (1);
if is_digit(ch)
return(ch-'0');
};
uint32 kbd_hit(void){} ;
/*
query mode putkey and get key
*/
void _q_putkey(uint32 channel,char ch)
{
if(channel) {
WaitXmitter(UARTSTAT1);
UARTTXH1 = ch;
}
else {
WaitXmitter(UARTSTAT0);
UARTTXH0 = ch;
}
}
char _q_getkey(uint32 channel)
{
char ch;
if(channel) { WaitRcver(UARTSTAT1);
ch = UARTRXB1;
}
else { WaitRcver(UARTSTAT0);
ch = UARTRXB0;
}
return ch;
}
char i_getkey (U32 channel) //未调试
{ //int i,j;
if(channel==1)
{//WaitRcver(UARTSTAT1); //等待输入
return(UARTRXB1);
}
else
{
WaitRcver(UARTSTAT0); //等待输入
return(UARTRXB0);
}
}
char get_char(U32 channel)
{char ch;
ch=i_getkey ( channel);
if is_digit(ch)
return(ch);
};
/****************************************************************************
CONSOLE FUNCTION
console is define by CONSOLE_TTY
char putchar (char c) put a char to console, if c is \n (0xa, line feed),
add a 0xd (carriage return) to console;
void getline (char *line, unsigned char n)
get a line to cosole, include echo input char at console,
realize backspace and del;
******************************************************************************/
#define CNTLQ 0x11
#define CNTLS 0x13
#define DEL 0x7F
#define BACKSPACE 0x08
#define CR 0x0D
#define LF 0x0A
/***************/
/* Line Editor */
/***************/
void getline (char *line, int n)
{
int cnt = 0;
char c;
do {
c = i_getkey (1); //从串口1输入
if (c == CR) c = LF; /* read character */
if (c == BACKSPACE || c == DEL) { /* process backspace */
if (cnt != 0) {
cnt--; /* decrement count */
line--; /* and line pointer */
putchar (0x08); /* echo backspace */
putchar (' ');
putchar (0x08);
}
}
else if (c != CNTLQ && c != CNTLS) { /* ignore Control S/Q */
putchar (*line = c); /* echo and store character */
line++; /* increment line pointer */
cnt++; /* and count */
}
} while (cnt < n - 1 && c != LF); /* check limit and line feed */
*line = 0; /* mark end of string */
}
char putchar (char c)
{ int i,j;
UARTTXH0=c; //同时从两个串口输出
UARTTXH1=c;
do{i=UARTSTAT0 & 0x80; //等待输出完成
j=UARTSTAT1 & 0x80;
}
while(j==0);
if(c=='\n')
putchar('\r');
}
/* formatted output string */
void i_printf(char *fmt, ...)
{
va_list argptr;
char temp_buf[256], *p;
p = temp_buf;
va_start(argptr, fmt);
vsprintf(temp_buf, fmt, argptr);
while (*p != '\0') putchar(*(p++));
va_end(argptr);
}
void bzero(unsigned char * buffer, int nbytes)
{
U8 *bufend = buffer + nbytes;
while (buffer < bufend) {
*buffer++ = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -