From 53cb9f23346c4969c5d9cc30de45840391ce5d78 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 26 Jan 2008 23:46:09 +0000 Subject: Structure serial driver interface to support different interrupt architectures git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@571 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/arch/arm/src/c5471/c5471_serial.c | 228 ++++++++++++++++++++------------ 1 file changed, 140 insertions(+), 88 deletions(-) (limited to 'nuttx/arch/arm/src/c5471') diff --git a/nuttx/arch/arm/src/c5471/c5471_serial.c b/nuttx/arch/arm/src/c5471/c5471_serial.c index ceeba8a36..22ce3c063 100644 --- a/nuttx/arch/arm/src/c5471/c5471_serial.c +++ b/nuttx/arch/arm/src/c5471/c5471_serial.c @@ -1,7 +1,7 @@ -/************************************************************ +/**************************************************************************** * c5471/c5471_serial.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 3. Neither the name NuttX nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,11 +31,11 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Included Files - ************************************************************/ + ****************************************************************************/ #include @@ -55,9 +55,9 @@ #include "os_internal.h" #include "up_internal.h" -/************************************************************ +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ #define BASE_BAUD 115200 @@ -65,9 +65,9 @@ # define CONFIG_UART_HWFLOWCONTROL #endif -/************************************************************ +/**************************************************************************** * Private Types - ************************************************************/ + ****************************************************************************/ struct uart_regs_s { @@ -82,13 +82,11 @@ struct uart_regs_s struct up_dev_s { - unsigned int uartbase; /* Base address of UART - * registers */ + unsigned int uartbase; /* Base address of UART registers */ unsigned int baud_base; /* Base baud for conversions */ unsigned int baud; /* Configured baud */ ubyte xmit_fifo_size; /* Size of transmit FIFO */ - ubyte irq; /* IRQ associated with - * this UART */ + ubyte irq; /* IRQ associated with this UART */ ubyte parity; /* 0=none, 1=odd, 2=even */ ubyte bits; /* Number of bits (7 or 8) */ #ifdef CONFIG_UART_HWFLOWCONTROL @@ -100,39 +98,42 @@ struct up_dev_s struct uart_regs_s regs; /* Shadow copy of readonly regs */ }; -/************************************************************ +/**************************************************************************** * Private Function Prototypes - ************************************************************/ + ****************************************************************************/ static int up_setup(struct uart_dev_s *dev); static void up_shutdown(struct uart_dev_s *dev); +static int up_attach(struct uart_dev_s *dev); +static void up_detach(struct uart_dev_s *dev); static int up_interrupt(int irq, void *context); static int up_ioctl(struct file *filep, int cmd, unsigned long arg); static int up_receive(struct uart_dev_s *dev, unsigned int *status); static void up_rxint(struct uart_dev_s *dev, boolean enable); -static boolean up_rxfifonotempty(struct uart_dev_s *dev); +static boolean up_rxavailable(struct uart_dev_s *dev); static void up_send(struct uart_dev_s *dev, int ch); static void up_txint(struct uart_dev_s *dev, boolean enable); -static boolean up_txfifonotfull(struct uart_dev_s *dev); -static boolean up_txfifoempty(struct uart_dev_s *dev); +static boolean up_txready(struct uart_dev_s *dev); +static boolean up_txempty(struct uart_dev_s *dev); -/************************************************************ +/**************************************************************************** * Private Variables - ************************************************************/ + ****************************************************************************/ struct uart_ops_s g_uart_ops = { .setup = up_setup, .shutdown = up_shutdown, - .handler = up_interrupt, + .attach = up_attach, + .detach = up_detach, .ioctl = up_ioctl, .receive = up_receive, .rxint = up_rxint, - .rxfifonotempty = up_rxfifonotempty, + .rxavailable = up_rxavailable, .send = up_send, .txint = up_txint, - .txfifonotfull = up_txfifonotfull, - .txfifoempty = up_txfifoempty, + .txready = up_txready, + .txempty = up_txempty, }; /* I/O buffers */ @@ -150,6 +151,7 @@ static struct up_dev_s g_irdapriv = .baud_base = BASE_BAUD, .uartbase = UART_IRDA_BASE, .baud = CONFIG_UART_IRDA_BAUD, + .irq = C5471_IRQ_UART_IRDA, .parity = CONFIG_UART_IRDA_PARITY, .bits = CONFIG_UART_IRDA_BITS, #ifdef CONFIG_UART_IRDA_HWFLOWCONTROL @@ -160,7 +162,6 @@ static struct up_dev_s g_irdapriv = static uart_dev_t g_irdaport = { - .irq = C5471_IRQ_UART_IRDA, .recv = { .size = CONFIG_UART_IRDA_RXBUFSIZE, @@ -183,6 +184,7 @@ static struct up_dev_s g_modempriv = .baud_base = BASE_BAUD, .uartbase = UART_MODEM_BASE, .baud = CONFIG_UART_MODEM_BAUD, + .irq = C5471_IRQ_UART, .parity = CONFIG_UART_MODEM_PARITY, .bits = CONFIG_UART_MODEM_BITS, #ifdef CONFIG_UART_MODEM_HWFLOWCONTROL @@ -193,7 +195,6 @@ static struct up_dev_s g_modempriv = static uart_dev_t g_modemport = { - .irq = C5471_IRQ_UART, .recv = { .size = CONFIG_UART_MODEM_RXBUFSIZE, @@ -220,31 +221,31 @@ static uart_dev_t g_modemport = # define TTYS1_DEV g_irdaport #endif -/************************************************************ +/**************************************************************************** * Private Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Name: up_inserial - ************************************************************/ + ****************************************************************************/ static inline uint32 up_inserial(struct up_dev_s *priv, uint32 offset) { return getreg32(priv->uartbase + offset); } -/************************************************************ +/**************************************************************************** * Name: up_serialout - ************************************************************/ + ****************************************************************************/ static inline void up_serialout(struct up_dev_s *priv, uint32 offset, uint32 value) { putreg32(value, priv->uartbase + offset); } -/************************************************************ +/**************************************************************************** * Name: up_disableuartint - ************************************************************/ + ****************************************************************************/ static inline void up_disableuartint(struct up_dev_s *priv, uint16 *ier) { @@ -256,9 +257,9 @@ static inline void up_disableuartint(struct up_dev_s *priv, uint16 *ier) up_serialout(priv, UART_IER_OFFS, priv->regs.ier); } -/************************************************************ +/**************************************************************************** * Name: up_restoreuartint - ************************************************************/ + ****************************************************************************/ static inline void up_restoreuartint(struct up_dev_s *priv, uint16 ier) { @@ -266,11 +267,11 @@ static inline void up_restoreuartint(struct up_dev_s *priv, uint16 ier) up_serialout(priv, UART_IER_OFFS, priv->regs.ier); } -/************************************************************ - * Name: up_waittxfifonotfull - ************************************************************/ +/**************************************************************************** + * Name: up_waittxready + ****************************************************************************/ -static inline void up_waittxfifonotfull(struct up_dev_s *priv) +static inline void up_waittxready(struct up_dev_s *priv) { int tmp; @@ -282,9 +283,9 @@ static inline void up_waittxfifonotfull(struct up_dev_s *priv) } } } -/************************************************************ +/**************************************************************************** * Name: up_disablebreaks - ************************************************************/ + ****************************************************************************/ static inline void up_disablebreaks(struct up_dev_s *priv) { @@ -292,9 +293,9 @@ static inline void up_disablebreaks(struct up_dev_s *priv) up_serialout(priv, UART_LCR_OFFS, priv->regs.lcr); } -/************************************************************ +/**************************************************************************** * Name: up_enablebreaks - ************************************************************/ + ****************************************************************************/ static inline void up_enablebreaks(struct up_dev_s *priv) { @@ -302,9 +303,9 @@ static inline void up_enablebreaks(struct up_dev_s *priv) up_serialout(priv, UART_LCR_OFFS, priv->regs.lcr); } -/************************************************************ +/**************************************************************************** * Name: up_setrate - ************************************************************/ + ****************************************************************************/ static inline void up_setrate(struct up_dev_s *priv, unsigned int rate) { @@ -342,7 +343,7 @@ static inline void up_setrate(struct up_dev_s *priv, unsigned int rate) up_serialout(priv, UART_DIV_BIT_RATE_OFFS, div_bit_rate); } -/************************************************************ +/**************************************************************************** * Name: up_setup * * Description: @@ -350,7 +351,7 @@ static inline void up_setrate(struct up_dev_s *priv, unsigned int rate) * method is called the first time that the serial port is * opened. * - ************************************************************/ + ****************************************************************************/ static int up_setup(struct uart_dev_s *dev) { @@ -447,13 +448,13 @@ static int up_setup(struct uart_dev_s *dev) return OK; } -/************************************************************ +/**************************************************************************** * Name: up_shutdown * * Description: * Disable the UART. This method is called when the serial port is closed * - ************************************************************/ + ****************************************************************************/ static void up_shutdown(struct uart_dev_s *dev) { @@ -461,7 +462,58 @@ static void up_shutdown(struct uart_dev_s *dev) up_disableuartint(priv, NULL); } -/************************************************************ +/**************************************************************************** + * Name: up_attach + * + * Description: + * Configure the UART to operation in interrupt driven mode. This method is + * called when the serial port is opened. Normally, this is just after the + * the setup() method is called, however, the serial console may operate in + * a non-interrupt driven mode during the boot phase. + * + * RX and TX interrupts are not enabled when by the attach method (unless the + * hardware supports multiple levels of interrupt enabling). The RX and TX + * interrupts are not enabled until the txint() and rxint() methods are called. + * + ****************************************************************************/ + +static int up_attach(struct uart_dev_s *dev) +{ + struct up_dev_s *priv = (struct up_dev_s*)dev->priv; + int ret; + + /* Attach and enable the IRQ */ + + ret = irq_attach(priv->irq, up_interrupt); + if (ret == OK) + { + /* Enable the interrupt (RX and TX interrupts are still disabled + * in the UART + */ + + up_enable_irq(priv->irq); + } + return ret; +} + +/**************************************************************************** + * Name: up_detach + * + * Description: + * Detach UART interrupts. This method is called when the serial port is + * closed normally just before the shutdown method is called. The exception is + * the serial console which is never shutdown. + * + ****************************************************************************/ + +static void up_detach(struct uart_dev_s *dev) +{ + struct up_dev_s *priv = (struct up_dev_s*)dev->priv; + up_disable_irq(priv->irq); + irq_detach(priv->irq); +} + +/**************************************************************************** * Name: up_interrupt * * Description: @@ -472,7 +524,7 @@ static void up_shutdown(struct uart_dev_s *dev) * must be able to map the 'irq' number into the approprite * uart_dev_s structure in order to call these functions. * - ************************************************************/ + ****************************************************************************/ static int up_interrupt(int irq, void *context) { @@ -480,11 +532,11 @@ static int up_interrupt(int irq, void *context) struct up_dev_s *priv; volatile uint32 cause; - if (g_irdaport.irq == irq) + if (g_irdapriv.irq == irq) { dev = &g_irdaport; } - else if (g_modemport.irq == irq) + else if (g_modempriv.irq == irq) { dev = &g_modemport; } @@ -546,13 +598,13 @@ static int up_interrupt(int irq, void *context) return OK; } -/************************************************************ +/**************************************************************************** * Name: up_ioctl * * Description: * All ioctl calls will be routed through this method * - ************************************************************/ + ****************************************************************************/ static int up_ioctl(struct file *filep, int cmd, unsigned long arg) { @@ -604,7 +656,7 @@ static int up_ioctl(struct file *filep, int cmd, unsigned long arg) return ret; } -/************************************************************ +/**************************************************************************** * Name: up_receive * * Description: @@ -612,7 +664,7 @@ static int up_ioctl(struct file *filep, int cmd, unsigned long arg) * the UART. Error bits associated with the receipt are provided in the * the return 'status'. * - ************************************************************/ + ****************************************************************************/ static int up_receive(struct uart_dev_s *dev, unsigned int *status) { @@ -634,13 +686,13 @@ static int up_receive(struct uart_dev_s *dev, unsigned int *status) return rhr & 0x000000ff; } -/************************************************************ +/**************************************************************************** * Name: up_rxint * * Description: * Call to enable or disable RX interrupts * - ************************************************************/ + ****************************************************************************/ static void up_rxint(struct uart_dev_s *dev, boolean enable) { @@ -659,27 +711,27 @@ static void up_rxint(struct uart_dev_s *dev, boolean enable) } } -/************************************************************ - * Name: up_rxfifonotempty +/**************************************************************************** + * Name: up_rxavailable * * Description: * Return TRUE if the receive fifo is not empty * - ************************************************************/ + ****************************************************************************/ -static boolean up_rxfifonotempty(struct uart_dev_s *dev) +static boolean up_rxavailable(struct uart_dev_s *dev) { struct up_dev_s *priv = (struct up_dev_s*)dev->priv; return up_inserial(priv, UART_LSR_OFFS) & UART_RX_FIFO_NOEMPTY; } -/************************************************************ +/**************************************************************************** * Name: up_send * * Description: * This method will send one byte on the UART * - ************************************************************/ + ****************************************************************************/ static void up_send(struct uart_dev_s *dev, int ch) { @@ -687,13 +739,13 @@ static void up_send(struct uart_dev_s *dev, int ch) up_serialout(priv, UART_THR_OFFS, (ubyte)ch); } -/************************************************************ +/**************************************************************************** * Name: up_txint * * Description: * Call to enable or disable TX interrupts * - ************************************************************/ + ****************************************************************************/ static void up_txint(struct uart_dev_s *dev, boolean enable) { @@ -712,39 +764,39 @@ static void up_txint(struct uart_dev_s *dev, boolean enable) } } -/************************************************************ - * Name: up_txfifonotfull +/**************************************************************************** + * Name: up_txready * * Description: * Return TRUE if the tranmsit fifo is not full * - ************************************************************/ + ****************************************************************************/ -static boolean up_txfifonotfull(struct uart_dev_s *dev) +static boolean up_txready(struct uart_dev_s *dev) { struct up_dev_s *priv = (struct up_dev_s*)dev->priv; return (up_inserial(priv, UART_SSR_OFFS) & UART_SSR_TXFULL) == 0; } -/************************************************************ - * Name: up_txfifoempty +/**************************************************************************** + * Name: up_txempty * * Description: * Return TRUE if the transmit fifo is empty * - ************************************************************/ + ****************************************************************************/ -static boolean up_txfifoempty(struct uart_dev_s *dev) +static boolean up_txempty(struct uart_dev_s *dev) { struct up_dev_s *priv = (struct up_dev_s*)dev->priv; return (up_inserial(priv, UART_LSR_OFFS) & UART_LSR_TREF) != 0; } -/************************************************************ +/**************************************************************************** * Public Funtions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Name: up_serialinit * * Description: @@ -752,7 +804,7 @@ static boolean up_txfifoempty(struct uart_dev_s *dev) * debug so that the serial console will be available * during bootup. This must be called before up_serialinit. * - ************************************************************/ + ****************************************************************************/ void up_earlyserialinit(void) { @@ -763,14 +815,14 @@ void up_earlyserialinit(void) up_setup(&CONSOLE_DEV); } -/************************************************************ +/**************************************************************************** * Name: up_serialinit * * Description: * Register serial console and serial ports. This assumes * that up_earlyserialinit was called previously. * - ************************************************************/ + ****************************************************************************/ void up_serialinit(void) { @@ -779,14 +831,14 @@ void up_serialinit(void) (void)uart_register("/dev/ttyS1", &TTYS1_DEV); } -/************************************************************ +/**************************************************************************** * Name: up_putc * * Description: * Provide priority, low-level access to support OS debug * writes * - ************************************************************/ + ****************************************************************************/ int up_putc(int ch) { @@ -794,7 +846,7 @@ int up_putc(int ch) uint16 ier; up_disableuartint(priv, &ier); - up_waittxfifonotfull(priv); + up_waittxready(priv); up_serialout(priv, UART_THR_OFFS, (ubyte)ch); /* Check for LF */ @@ -803,11 +855,11 @@ int up_putc(int ch) { /* Add CR */ - up_waittxfifonotfull(priv); + up_waittxready(priv); up_serialout(priv, UART_THR_OFFS, '\r'); } - up_waittxfifonotfull(priv); + up_waittxready(priv); up_restoreuartint(priv, ier); return ch; } -- cgit v1.2.3