📄 serial-sa11x0.c
字号:
/* * serial-sa11x0.c: StrongARM SA11x0 serial port driver * * Copyright (C) 2002 Erik Mouw <J.A.K.Mouw@its.tudelft.nl> * * $Id: serial-sa11x0.c,v 1.2 2002/01/03 16:07:18 erikm Exp $ * * 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 * */#ident "$Id: serial-sa11x0.c,v 1.2 2002/01/03 16:07:18 erikm Exp $"#ifdef HAVE_CONFIG_H# include <blob/config.h>#endif#include <blob/arch.h>#include <blob/errno.h>#include <blob/sa1100.h>#include <blob/serial.h>#include <blob/types.h>#if defined USE_SERIAL1# define SerialUTSR0 Ser1UTSR0# define SerialUTSR1 Ser1UTSR1# define SerialUTDR Ser1UTDR# define SerialUTCR0 Ser1UTCR0# define SerialUTCR1 Ser1UTCR1# define SerialUTCR2 Ser1UTCR2# define SerialUTCR3 Ser1UTCR3#else /* defined USE_SERIAL3 *//* using an SA11x0 CPU and not having any serial port defined is an * error, but because we're using the driver in the library we can't * bail out over here */# define SerialUTSR0 Ser3UTSR0# define SerialUTSR1 Ser3UTSR1# define SerialUTDR Ser3UTDR# define SerialUTCR0 Ser3UTCR0# define SerialUTCR1 Ser3UTCR1# define SerialUTCR2 Ser3UTCR2# define SerialUTCR3 Ser3UTCR3#endif/* flush serial input queue. returns 0 on success or negative error * number otherwise */static int sa11x0_serial_flush_input(void){ volatile u32 tmp; /* keep on reading as long as the receiver is not empty */ while(SerialUTSR1 & UTSR1_RNE) { if(SerialUTSR1 & (UTSR1_PRE | UTSR1_FRE | UTSR1_ROR)) return -ESERIAL; tmp = SerialUTDR; } return 0;}/* flush output queue. returns 0 on success or negative error number * otherwise */static int sa11x0_serial_flush_output(void){ /* wait until the transmitter is no longer busy */ while(SerialUTSR1 & UTSR1_TBY) { } return 0;}/* initialise serial port at the request baudrate. returns 0 on * success, or a negative error number otherwise */static int sa11x0_serial_init(serial_baud_t baud){ u32 divisor; /* get correct divisor */ switch(baud) { case baud_1200: divisor = 191; break; case baud_9600: divisor = 23; break; case baud_19200: divisor = 11; break; case baud_38400: divisor = 5; break; case baud_57600: divisor = 3; break; case baud_115200: divisor = 1; break; case baud_230400: divisor = 0; break; default: return -ERANGE; }#if defined USE_SERIAL1 /* select UART use for serial port 1 or otherwise we won't see * anything at all */ Ser1SDCR0 = SDCR0_UART;#endif sa11x0_serial_flush_output(); /* switch receiver and transmitter off */ SerialUTCR3 = 0x00; /* clear sticky bits in control register 3 */ SerialUTSR0 = 0xff; /* set the port to sensible defaults (no break, no interrupts, * no parity, 8 databits, 1 stopbit, transmitter and receiver * enabled) */ SerialUTCR0 = ( UTCR0_1StpBit | UTCR0_8BitData ); SerialUTCR1 = 0; SerialUTCR2 = divisor; /* turn the receiver and transmitter back on */ SerialUTCR3 = ( UTCR3_RXE | UTCR3_TXE ); return 0;}/* check if there is a character available to read. returns 1 if there * is a character available, 0 if not, and negative error number on * failure */static int sa11x0_serial_poll(void){ /* check for errors */ if(SerialUTSR1 & (UTSR1_PRE | UTSR1_FRE | UTSR1_ROR)) return -ESERIAL; if(SerialUTSR1 & UTSR1_RNE) return 1; else return 0;}/* read one character from the serial port. return character (between * 0 and 255) on success, or negative error number on failure. this * function is blocking */static int sa11x0_serial_read(void){ int rv; for(;;) { rv = sa11x0_serial_poll(); if(rv < 0) return rv; if(rv > 0) return SerialUTDR & 0xff; }}/* write character to serial port. return 0 on success, or negative * error number on failure. this function is blocking */static int sa11x0_serial_write(int c){ /* wait for room in the transmit FIFO */ while((SerialUTSR0 & UTSR0_TFS) == 0) { } SerialUTDR = c & 0xff; return 0;}/* export serial driver */serial_driver_t sa11x0_serial_driver = { init: sa11x0_serial_init, read: sa11x0_serial_read, write: sa11x0_serial_write, poll: sa11x0_serial_poll, flush_input: sa11x0_serial_flush_input, flush_output: sa11x0_serial_flush_output};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -