📄 uart.c
字号:
#include "dbldr_std.h"#include "dbldr_spec.h"#include "2410addr.h"UINT32 console_baud = 0;static INT32 which_uart = 0;void uart_init(INT32 pclk, INT32 baud){ volatile INT32 i; if(pclk == 0) pclk = SYS_CLK; rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable rUFCON1 = 0x0; //UART channel 1 FIFO control register, FIFO disable rUFCON2 = 0x0; //UART channel 2 FIFO control register, FIFO disable rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable rUMCON1 = 0x0; //UART chaneel 1 MODEM control register, AFC disable /* * ATTENTION: currently, arm-linux-gcc does NOT support float operations! * so i changed (pclk / 16.) to (pclk >> 4). */ //UART0 rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits // [10] [9] [8] [7] [6] [5] [4] [3:2] [1:0] // Clock Sel, Tx Int, Rx Int, Rx Time Out, Rx err, Loop-back, Send break, Transmit Mode, Receive Mode // 0 1 0 , 0 1 0 0 , 01 01 // PCLK Level Pulse Disable Generate Normal Normal Interrupt or Polling rUCON0 = 0x245; // Control register rUBRDIV0=((INT32)((pclk >> 4) / baud) - 1); //Baud rate divisior register 0 //UART1 rULCON1 = 0x3; rUCON1 = 0x245; rUBRDIV1=((INT32)((pclk >> 4) / baud) - 1); //UART2 rULCON2 = 0x3; rUCON2 = 0x245; rUBRDIV2=((INT32)((pclk >> 4) / baud) - 1); console_baud = baud; for(i = 0; i < 100; i++);}void uart_select(INT32 chan){ which_uart = chan;}void putch(INT8 ch){ VINT32 data = ch; if(which_uart==0) { if(data=='\n') { while(!(rUTRSTAT0 & 0x2)); Delay(1); //because the slow response of hyper_terminal WrUTXH0('\r'); } while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty. Delay(1); WrUTXH0(data); } else if(which_uart==1) { if(data=='\n') { while(!(rUTRSTAT1 & 0x2)); Delay(1); //because the slow response of hyper_terminal rUTXH1 = '\r'; } while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty. Delay(1); rUTXH1 = data; } else if(which_uart==2) { if(data=='\n') { while(!(rUTRSTAT2 & 0x2)); Delay(1); //because the slow response of hyper_terminal rUTXH2 = '\r'; } while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty. Delay(1); rUTXH2 = data; }}INT32 puts(INT8 *str){ INT32 len = 0; INT8 ch; if (!str) { return -1; } while ('\0' != (ch = str[len++])) { putch(ch); } return (len - 1);}INT8 getch(void){ if (0 == which_uart) { while (!(rUTRSTAT0 & 0x01)); return RdURXH0(); } else if (1 == which_uart) { while (!(rUTRSTAT1 & 0x01)); return RdURXH1(); } else if (2 == which_uart) { while (!(rUTRSTAT2 & 0x01)); return RdURXH2(); } return 0;}STATUS getch_timeout(INT8 *ch, INT32 ms){ INT32 i; for (i = 0; i < 10 * ms; i++) { if (rUTRSTAT0 & 0x01) { *ch = RdURXH0(); return OK; } Delay(1); } return ERROR;}#define DEFAULT_BACK_SPACE (0x08)#define MINICOM_BACK_SPACE (0x7f)INT32 gets(INT8 *str){ INT32 i = 0; INT8 ch; if (!str) { return -1; } while ('\r' != (ch = getch())) { if ((DEFAULT_BACK_SPACE == ch) || (MINICOM_BACK_SPACE == ch)) { if (i > 0) { i--; putch('\b'); /* go back */ putch(' '); /* erase */ putch('\b'); /* go back once again and wait for a new char */ } } else { str[i++] = ch; putch(ch); /* echo */ } } str[i] = '\0'; putch('\n'); /* echo */ return i;}/*****************************************************************************/#include "lib.h"/* use UART0 for xmodem */#define SERIAL_CHAR_READY() (rUTRSTAT0 & 0x00000001)#define SERIAL_READ_CHAR() rURXH0#define SERIAL_READ_STATUS() (rUERSTAT0 & 0x0000000f)#define SERIAL_WRITE_READY() (rUTRSTAT0 & 0x00000004)#define SERIAL_WRITE_CHAR(c) (rUTXH0 = (c))typedef void (*vfuncp)(void);INT32 getc_errno = 0;void xmodem_putc(INT8 ch){ while (!SERIAL_WRITE_READY()) { /* nothing */; } SERIAL_WRITE_CHAR(ch);}static UINT8 do_getc(vfuncp idler, UINT32 timeout, INT32 *statp){ UINT8 c, rxstat; INT32 do_timeout = timeout != 0; getc_errno = 0; /* reset errno */ /* loop *without* delay, different from getch_timeout(). */ while(!SERIAL_CHAR_READY()) { if (do_timeout) { if (!timeout) break; timeout--; } if (idler) idler(); } if (do_timeout && timeout == 0) { c = 0; rxstat = -1; } else { c = SERIAL_READ_CHAR(); rxstat = SERIAL_READ_STATUS(); } if (rxstat) { getc_errno = rxstat; /*printk("RXSTAT error. status = 0x%08lx", rxstat);*/ if (statp) *statp = rxstat; } return (c);}/* * Reads and returns a character from the serial port * - Times out after delay iterations checking for presence of character * - Sets *error_p to UART error bits or - on timeout * - On timeout, sets *error_p to -1 and returns 0 */INT8 xmodem_await_key(UINT32 delay, INT32 *error_p){ return (do_getc(NULL, delay, error_p));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -