⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ez80_serial.c

📁 這是一個實時嵌入式作業系統 實作了MCS51 ARM等MCU
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************** * arch/z80/src/ez08/ez80_serial.c * *   Copyright (C) 2008 Gregory Nutt. All rights reserved. *   Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * 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. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************//**************************************************************************** * Included Files ****************************************************************************/#include <nuttx/config.h>#include <sys/types.h>#include <unistd.h>#include <semaphore.h>#include <string.h>#include <errno.h>#include <debug.h>#include <nuttx/irq.h>#include <nuttx/arch.h>#include <nuttx/serial.h>#include <arch/serial.h>#include <arch/io.h>#include "chip/chip.h"#include "os_internal.h"#include "up_internal.h"#ifdef CONFIG_USE_SERIALDRIVER/**************************************************************************** * Definitions ****************************************************************************//* The system clock frequency is defined in the linkcmd file */extern unsigned long SYS_CLK_FREQ;#define _DEFCLK ((unsigned long)&SYS_CLK_FREQ)/**************************************************************************** * Private Types ****************************************************************************/struct ez80_dev_s{  uint16               uartbase;	/* Base address of UART registers */  unsigned int         baud;		/* Configured baud */  ubyte                irq;		/* IRQ associated with this UART */  ubyte                parity;		/* 0=none, 1=odd, 2=even */  ubyte                bits;		/* Number of bits (7 or 8) */  boolean              stopbits2;	/* TRUE: Configure with 2 (vs 1) */};/**************************************************************************** * Private Function Prototypes ****************************************************************************/static int     ez80_setup(struct uart_dev_s *dev);static void    ez80_shutdown(struct uart_dev_s *dev);static int     ez80_attach(struct uart_dev_s *dev);static void    ez80_detach(struct uart_dev_s *dev);static int     ez80_interrrupt(int irq, void *context);static int     ez80_ioctl(struct file *filep, int cmd, unsigned long arg);static int     ez80_receive(struct uart_dev_s *dev, unsigned int *status);static void    ez80_rxint(struct uart_dev_s *dev, boolean enable);static boolean ez80_rxavailable(struct uart_dev_s *dev);static void    ez80_send(struct uart_dev_s *dev, int ch);static void    ez80_txint(struct uart_dev_s *dev, boolean enable);static boolean ez80_txready(struct uart_dev_s *dev);static boolean ez80_txempty(struct uart_dev_s *dev);/**************************************************************************** * Private Variables ****************************************************************************/struct uart_ops_s g_uart_ops ={  ez80_setup,          /* setup */  ez80_shutdown,       /* shutdown */  ez80_attach,         /* attach */  ez80_detach,         /* detach */  ez80_ioctl,          /* ioctl */  ez80_receive,        /* receive */  ez80_rxint,          /* rxint */  ez80_rxavailable,    /* rxavailable */  ez80_send,           /* send */  ez80_txint,          /* txint */  ez80_txready,        /* txready */  ez80_txempty         /* txempty */};/* I/O buffers */static char g_uart0rxbuffer[CONFIG_UART0_RXBUFSIZE];static char g_uart0txbuffer[CONFIG_UART0_TXBUFSIZE];static char g_uart1rxbuffer[CONFIG_UART1_RXBUFSIZE];static char g_uart1txbuffer[CONFIG_UART1_TXBUFSIZE];/* This describes the state of the UART0 port. */static struct ez80_dev_s g_uart0priv ={  EZ80_UART0_BASE,          /* uartbase */  CONFIG_UART0_BAUD,        /* baud */  EZ80_UART0_IRQ,           /* irq */  CONFIG_UART0_PARITY,      /* parity */  CONFIG_UART0_BITS,        /* bits */  CONFIG_UART0_2STOP        /* stopbits2 */};static uart_dev_t g_uart0port ={  0,                        /* open_count */  FALSE,                    /* xmitwaiting */  FALSE,                    /* recvwaiting */#ifdef CONFIG_UART0_SERIAL_CONSOLE  TRUE,                     /* isconsole */#else  FALSE,                    /* isconsole */#endif  { 0 },                    /* closesem */  { 0 },                    /* xmitsem */  { 0 },                    /* recvsem */  {    { 0 },                  /* xmit.sem */    0,                      /* xmit.head */    0,                      /* xmit.tail */    CONFIG_UART0_TXBUFSIZE, /* xmit.size */    g_uart0txbuffer,        /* xmit.buffer */  },  {    { 0 },                  /* recv.sem */    0,                      /* recv.head */    0,                      /* recv.tail */    CONFIG_UART0_RXBUFSIZE, /* recv.size */    g_uart0rxbuffer,        /* recv.buffer */  },  &g_uart_ops,              /* ops */  &g_uart0priv,             /* priv */};/* This describes the state of the UART1 port. */static struct ez80_dev_s g_uart1priv ={  EZ80_UART1_BASE,          /* uartbase */  CONFIG_UART1_BAUD,        /* baud */  EZ80_UART1_IRQ,           /* irq */  CONFIG_UART1_PARITY,      /* parity */  CONFIG_UART1_BITS,        /* bits */  CONFIG_UART1_2STOP        /* stopbits2 */};static uart_dev_t g_uart1port ={  0,                        /* open_count */  FALSE,                    /* xmitwaiting */  FALSE,                    /* recvwaiting */#ifdef CONFIG_UART1_SERIAL_CONSOLE  TRUE,                     /* isconsole */#else  FALSE,                    /* isconsole */#endif  { 0 },                    /* closesem */  { 0 },                    /* xmitsem */  { 0 },                    /* recvsem */  {    { 0 },                  /* xmit.sem */    0,                      /* xmit.head */    0,                      /* xmit.tail */    CONFIG_UART1_TXBUFSIZE, /* xmit.size */    g_uart1txbuffer,        /* xmit.buffer */  },  {    { 0 },                  /* recv.sem */    0,                      /* recv.head */    0,                      /* recv.tail */    CONFIG_UART1_RXBUFSIZE, /* recv.size */    g_uart0rxbuffer,        /* recv.buffer */  },  &g_uart_ops,              /* ops */  &g_uart1priv,             /* priv */};/* Now, which one with be tty0/console and which tty1? */#ifdef CONFIG_UART0_SERIAL_CONSOLE# define CONSOLE_DEV     g_uart0port# define TTYS0_DEV       g_uart0port# define TTYS1_DEV       g_uart1port#else# define CONSOLE_DEV     g_uart1port# define TTYS0_DEV       g_uart1port# define TTYS1_DEV       g_uart0port#endif/**************************************************************************** * Private Functions ****************************************************************************//**************************************************************************** * Name: ez80_serialin ****************************************************************************/static inline ubyte ez80_serialin(struct ez80_dev_s *priv, ubyte offset){  return inp(priv->uartbase + offset);}/**************************************************************************** * Name: ez80_serialout ****************************************************************************/static inline void ez80_serialout(struct ez80_dev_s *priv, ubyte offset, ubyte value){  outp(priv->uartbase + offset, value);}/**************************************************************************** * Name: ez80_disableuartint ****************************************************************************/static inline void ez80_disableuartint(struct ez80_dev_s *priv){  ubyte ier = ez80_serialin(priv, EZ80_UART_IER);  ier &= ~EZ80_UARTEIR_INTMASK;  ez80_serialout(priv, EZ80_UART_IER, ier);}/**************************************************************************** * Name: ez80_restoreuartint ****************************************************************************/static inline void ez80_restoreuartint(struct ez80_dev_s *priv, ubyte bits){  ubyte ier = ez80_serialin(priv, EZ80_UART_IER);  ier |= bits & (EZ80_UARTEIR_TIE|EZ80_UARTEIR_RIE);  ez80_serialout(priv, EZ80_UART_IER, ier);}/**************************************************************************** * Name: ez80_waittxready ****************************************************************************/static inline void ez80_waittxready(struct ez80_dev_s *priv){  int tmp;  for (tmp = 1000 ; tmp > 0 ; tmp--)    {      if ((ez80_serialin(priv, EZ80_UART_LSR) & EZ80_UARTLSR_THRE) != 0)        {          break;        }    }}/**************************************************************************** * Name: ez80_setbaud ****************************************************************************/static inline void ez80_setbaud(struct ez80_dev_s *priv, uint24 baud){  uint24 brg_divisor;  ubyte lctl;  /* The resulting BAUD and depends on the system clock frequency and the   * BRG divisor as follows:   *   * BAUD = SYSTEM_CLOCK_FREQUENCY / (16 * BRG_Divisor)   *   * Or   *   * BRG_Divisor = SYSTEM_CLOCK_FREQUENCY / 16 / BAUD   */   brg_divisor = ( _DEFCLK + (baud << 3)) / (baud << 4);   /* Set the DLAB bit to enable access to the BRG registers */   lctl = ez80_serialin(priv, EZ80_UART_LCTL);   lctl |= EZ80_UARTLCTL_DLAB;   ez80_serialout(priv, EZ80_UART_LCTL, lctl);   ez80_serialout(priv, EZ80_UART_BRGL, (ubyte)(brg_divisor & 0xff));   ez80_serialout(priv, EZ80_UART_BRGH, (ubyte)(brg_divisor >> 8));   lctl &= ~EZ80_UARTLCTL_DLAB;   ez80_serialout(priv, EZ80_UART_LCTL, lctl);}/**************************************************************************** * Name: ez80_setup * * Description: *   Configure the UART baud, bits, parity, fifos, etc. This method is called *   the first time that the serial port is opened. * ****************************************************************************/static int ez80_setup(struct uart_dev_s *dev){#ifndef CONFIG_SUPPRESS_UART_CONFIG  struct ez80_dev_s *priv = dev->priv;  ubyte reg;  ubyte cval;  if (priv->bits == 7)    {      cval = EZ80_UARTCHAR_7BITS;    }  else    {      cval = EZ80_UARTCHAR_8BITS;    }  if (priv->stopbits2)    {      cval |= EZ80_UARTLCTL_2STOP;    }  if (priv->parity == 1)   /* Odd parity */    {      cval |= (EZ80_UARTLCTL_PEN);    }  else if (priv->parity == 2)  /* Even parity */    {      cval |= (EZ80_UARTLCTL_PEN|EZ80_UARTLCTL_EPS);    }  /* Set the baud rate */  ez80_disableuartint(priv);  ez80_setbaud(priv, priv->baud);  ez80_serialout(priv, EZ80_UART_MCTL, 0);  /* Set the character properties */  reg = (ez80_serialin(priv, EZ80_UART_LCTL) & ~EZ80_UARTLCTL_MASK) | cval;  ez80_serialout(priv, EZ80_UART_LCTL, reg);  /* Enable and flush the receive FIFO */  reg = EZ80_UARTFCTL_FIFOEN;  ez80_serialout(priv, EZ80_UART_FCTL, reg);  reg |= (EZ80_UARTFCTL_CLRTxF|EZ80_UARTFCTL_CLRRxF);  ez80_serialout(priv, EZ80_UART_FCTL, reg);  /* Set the receive trigger level to 4 */  reg |= EZ80_UARTTRIG_4;  ez80_serialout(priv, EZ80_UART_FCTL, reg);#endif  return OK;}/**************************************************************************** * Name: ez80_shutdown * * Description:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -