📄 serial.c.org
字号:
/* * (C) Copyright 2002 * Sysgo Real-Time Solutions, GmbH <www.elinos.com> * Marius Groeger <mgroeger@sysgo.de> * * (C) Copyright 2002 * Sysgo Real-Time Solutions, GmbH <www.elinos.com> * Alex Zuepke <azu@sysgo.de> * * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#include "armboot.h"#include "s5n8947.h"volatile unsigned int *CNSL_TxBuf; volatile unsigned int *CNSL_RxBuf; int calcUartCnt(int BaudRate, int *UartCnt){ unsigned int tDIV, DivCnt0, DivCnt1, UartClk; /* Set Buad Rate Divisor value. */ /* You should select UART clock */ #if USE_UART_CLK UartClk = CPU_SPEED; #else UartClk = CPU_SPEED/2; #endif /*USE_UART_CLK*/ tDIV = UartClk / (16 * BaudRate); if(((tDIV%16)==0) || ((tDIV-1)>0xffff)) { DivCnt1 = 1; DivCnt0 = tDIV/16 -1; } else { DivCnt1 = 0; DivCnt0 = tDIV -1; } *UartCnt = DivCnt0*16 + DivCnt1; return 0;}void serial_setbrg(bd_t *bd, int baudrate){ unsigned int reg = 0; /* UART Control Regsiter */ REG_WRITE(ULCON,(INT_CLK | PARITY_NONE | ONE_STOP |WORD_LEN)); /* Poll Mode Set*/ REG_READ(UCON, reg); reg &= UCON_RX_TX_RESET; /**Reset RX and TX mode bits*/ reg |= (UCON_RX|UCON_TX); REG_WRITE(UCON, reg); intDisable (INT_LVL_UARTTX); intDisable (INT_LVL_UARTRX); /* set baudrate */ if (baudrate == 1200) calcUartCnt(1200, ®); else if (baudrate == 9600) calcUartCnt(9600, ®); else if (baudrate == 19200) calcUartCnt(19200, ®); else if (baudrate == 38400) calcUartCnt(38400, ®); else if (baudrate == 57600) calcUartCnt(57600, ®); else if (baudrate == 115200) calcUartCnt(115200, ®); else hang(); REG_WRITE(UBRDIV,0x00000130); /* 1152000 bps */// REG_WRITE(UBRDIV,0x00000390); /* 38400 bps */} /* * Initialise the serial port with the given baudrate. The settings * are always 8 data bits, no parity, 1 stop bit, no start bits. * */void serial_init(bd_t *bd){ const char *baudrate; if ((baudrate = getenv(bd, "baudrate")) != 0) bd->bi_baudrate = simple_strtoul(baudrate, NULL, 10); serial_setbrg(bd, bd->bi_baudrate); CNSL_TxBuf = &UARTTXH0; CNSL_RxBuf = &UARTRXB0;}/* * Output a single byte to the serial port. */void serial_putc(const char c){ while(!(UARTSTAT0 & UART_STAT_TXB_EMPTY)) ; *CNSL_TxBuf = c; if(c == '\n') { while(!(UARTSTAT0 & UART_STAT_TXB_EMPTY)) ; *CNSL_TxBuf = '\r'; }}/* * Read a single byte from the serial port. Returns 1 on success, 0 * otherwise. When the function is succesfull, the character read is * written into its argument c. */int serial_tstc(void){ ulong status; /* return !(IO_SYSFLG1 & SYSFLG1_URXFE);*/ REG_READ(USTAT, status); return !(status & USTAT_RX_READY);}/* * Read a single byte from the serial port. Returns 1 on success, 0 * otherwise. When the function is succesfull, the character read is * written into its argument c. */int serial_getc(void){ ulong status; char ch; do { REG_READ (USTAT, status); }while ((status & USTAT_RX_AVAIL) == 0x00); /* got a character */ REG_READ(URXBUF, ch); return (ch); /* while (IO_SYSFLG1 & SYSFLG1_URXFE) ; return IO_UARTDR1 & 0xff; */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -