📄 ser_16x5x.c
字号:
//==========================================================================
//
// io/serial/generic/16x5x/ser_16x5x.c
//
// Generic 16x5x serial driver
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
// Copyright (C) 2003 Gary Thomas
//
// eCos 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 or (at your option) any later version.
//
// eCos 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 eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): gthomas
// Contributors: gthomas, jlarmour, jskov
// Date: 1999-02-04
// Purpose: 16x5x generic serial driver
// Description:
//
//####DESCRIPTIONEND####
//
//==========================================================================
#include <pkgconf/system.h>
#include <pkgconf/io_serial.h>
#include <pkgconf/io.h>
#include <cyg/io/io.h>
#include <cyg/hal/hal_intr.h>
#include <cyg/io/devtab.h>
#include <cyg/io/serial.h>
#include <cyg/infra/diag.h>
#include <cyg/infra/cyg_ass.h>
#include <cyg/hal/hal_io.h>
// Only compile driver if an inline file with driver details was selected.
#ifdef CYGDAT_IO_SERIAL_GENERIC_16X5X_INL
#ifndef CYGPRI_IO_SERIAL_GENERIC_16X5X_STEP
#define CYGPRI_IO_SERIAL_GENERIC_16X5X_STEP 1
#endif
#define SER_REG(_x_) ((_x_)*CYGPRI_IO_SERIAL_GENERIC_16X5X_STEP)
// Receive control Registers
#define REG_rhr SER_REG(0) // Receive holding register
#define REG_isr SER_REG(2) // Interrupt status register
#define REG_lsr SER_REG(5) // Line status register
#define REG_msr SER_REG(6) // Modem status register
#define REG_scr SER_REG(7) // Scratch register
// Transmit control Registers
#define REG_thr SER_REG(0) // Transmit holding register
#define REG_ier SER_REG(1) // Interrupt enable register
#define REG_fcr SER_REG(2) // FIFO control register
#define REG_lcr SER_REG(3) // Line control register
#define REG_mcr SER_REG(4) // Modem control register
#define REG_ldl SER_REG(0) // LSB of baud rate
#define REG_mdl SER_REG(1) // MSB of baud rate
// Interrupt Enable Register
#define IER_RCV 0x01
#define IER_XMT 0x02
#define IER_LS 0x04
#define IER_MS 0x08
// Line Control Register
#define LCR_WL5 0x00 // Word length
#define LCR_WL6 0x01
#define LCR_WL7 0x02
#define LCR_WL8 0x03
#define LCR_SB1 0x00 // Number of stop bits
#define LCR_SB1_5 0x04 // 1.5 -> only valid with 5 bit words
#define LCR_SB2 0x04
#define LCR_PN 0x00 // Parity mode - none
#define LCR_PE 0x18 // Parity mode - even
#define LCR_PO 0x08 // Parity mode - odd
#define LCR_PM 0x28 // Forced "mark" parity
#define LCR_PS 0x38 // Forced "space" parity
#define LCR_DL 0x80 // Enable baud rate latch
// Line Status Register
#define LSR_RSR 0x01
#define LSR_OE 0x02
#define LSR_PE 0x04
#define LSR_FE 0x08
#define LSR_BI 0x10
#define LSR_THE 0x20
#define LSR_TEMT 0x40
#define LSR_FIE 0x80
// Modem Control Register
#define MCR_DTR 0x01
#define MCR_RTS 0x02
#define MCR_INT 0x08 // Enable interrupts
#define MCR_LOOP 0x10 // Loopback mode
// Interrupt status Register
#define ISR_MS 0x00
#define ISR_nIP 0x01
#define ISR_Tx 0x02
#define ISR_Rx 0x04
#define ISR_LS 0x06
#define ISR_RxTO 0x0C
#define ISR_64BFIFO 0x20
#define ISR_FIFOworks 0x40
#define ISR_FIFOen 0x80
// Modem Status Register
#define MSR_DCTS 0x01
#define MSR_DDSR 0x02
#define MSR_TERI 0x04
#define MSR_DDCD 0x08
#define MSR_CTS 0x10
#define MSR_DSR 0x20
#define MSR_RI 0x40
#define MSR_CD 0x80
// FIFO Control Register
#define FCR_FE 0x01 // FIFO enable
#define FCR_CRF 0x02 // Clear receive FIFO
#define FCR_CTF 0x04 // Clear transmit FIFO
#define FCR_DMA 0x08 // DMA mode select
#define FCR_F64 0x20 // Enable 64 byte fifo (16750+)
#define FCR_RT14 0xC0 // Set Rx trigger at 14
#define FCR_RT8 0x80 // Set Rx trigger at 8
#define FCR_RT4 0x40 // Set Rx trigger at 4
#define FCR_RT1 0x00 // Set Rx trigger at 1
static unsigned char select_word_length[] = {
LCR_WL5, // 5 bits / word (char)
LCR_WL6,
LCR_WL7,
LCR_WL8
};
static unsigned char select_stop_bits[] = {
0,
LCR_SB1, // 1 stop bit
LCR_SB1_5, // 1.5 stop bit
LCR_SB2 // 2 stop bits
};
static unsigned char select_parity[] = {
LCR_PN, // No parity
LCR_PE, // Even parity
LCR_PO, // Odd parity
LCR_PM, // Mark parity
LCR_PS, // Space parity
};
// selec_baud[] must be define by the client
typedef struct pc_serial_info {
cyg_addrword_t base;
int int_num;
cyg_interrupt serial_interrupt;
cyg_handle_t serial_interrupt_handle;
#ifdef CYGPKG_IO_SERIAL_GENERIC_16X5X_FIFO
enum {
sNone = 0,
s8250,
s16450,
s16550,
s16550a
} deviceType;
#endif
} pc_serial_info;
static bool pc_serial_init(struct cyg_devtab_entry *tab);
static bool pc_serial_putc(serial_channel *chan, unsigned char c);
static Cyg_ErrNo pc_serial_lookup(struct cyg_devtab_entry **tab,
struct cyg_devtab_entry *sub_tab,
const char *name);
static unsigned char pc_serial_getc(serial_channel *chan);
static Cyg_ErrNo pc_serial_set_config(serial_channel *chan, cyg_uint32 key,
const void *xbuf, cyg_uint32 *len);
static void pc_serial_start_xmit(serial_channel *chan);
static void pc_serial_stop_xmit(serial_channel *chan);
static cyg_uint32 pc_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);
static void pc_serial_DSR(cyg_vector_t vector, cyg_ucount32 count,
cyg_addrword_t data);
static SERIAL_FUNS(pc_serial_funs,
pc_serial_putc,
pc_serial_getc,
pc_serial_set_config,
pc_serial_start_xmit,
pc_serial_stop_xmit
);
#include CYGDAT_IO_SERIAL_GENERIC_16X5X_INL
#ifndef CYG_IO_SERIAL_GENERIC_16X5X_INT_PRIORITY
# define CYG_IO_SERIAL_GENERIC_16X5X_INT_PRIORITY 4
#endif
// Internal function to actually configure the hardware to desired
// baud rate, etc.
static bool
serial_config_port(serial_channel *chan,
cyg_serial_info_t *new_config, bool init)
{
pc_serial_info *ser_chan = (pc_serial_info *)chan->dev_priv;
cyg_addrword_t base = ser_chan->base;
unsigned short baud_divisor = select_baud[new_config->baud];
unsigned char _lcr, _ier;
if (baud_divisor == 0) return false; // Invalid configuration
// Disable port interrupts while changing hardware
HAL_READ_UINT8(base+REG_ier, _ier);
HAL_WRITE_UINT8(base+REG_ier, 0);
_lcr = select_word_length[new_config->word_length - CYGNUM_SERIAL_WORD_LENGTH_5] |
select_stop_bits[new_config->stop] |
select_parity[new_config->parity];
HAL_WRITE_UINT8(base+REG_lcr, _lcr | LCR_DL);
HAL_WRITE_UINT8(base+REG_mdl, baud_divisor >> 8);
HAL_WRITE_UINT8(base+REG_ldl, baud_divisor & 0xFF);
HAL_WRITE_UINT8(base+REG_lcr, _lcr);
if (init) {
#ifdef CYGPKG_IO_SERIAL_GENERIC_16X5X_FIFO
unsigned char _fcr_thresh;
cyg_uint8 b;
/* First, find out what kind of device it is. */
ser_chan->deviceType = sNone;
HAL_WRITE_UINT8(base+REG_mcr, MCR_LOOP); // enable loopback mode
HAL_READ_UINT8(base+REG_msr, b);
if (0 == (b & 0xF0)) { // see if MSR had CD, RI, DSR or CTS set
HAL_WRITE_UINT8(base+REG_mcr, MCR_LOOP|MCR_DTR|MCR_RTS);
HAL_READ_UINT8(base+REG_msr, b);
if (0xF0 != (b & 0xF0)) // check that all of CD,RI,DSR and CTS set
ser_chan->deviceType = s8250;
}
HAL_WRITE_UINT8(base+REG_mcr, 0); // disable loopback mode
if (ser_chan->deviceType == s8250) {
// Check for a scratch register; scratch register
// indicates 16450 or above.
HAL_WRITE_UINT8(base+REG_scr, 0x55);
HAL_READ_UINT8(base+REG_scr, b);
if (b == 0x55) {
HAL_WRITE_UINT8(base+REG_scr, 0xAA);
HAL_READ_UINT8(base+REG_scr, b);
if (b == 0xAA)
ser_chan->deviceType = s16450;
}
}
if (ser_chan->deviceType == s16450) {
// Check for a FIFO
HAL_WRITE_UINT8(base+REG_fcr, FCR_FE);
HAL_READ_UINT8(base+REG_isr, b);
if (b & ISR_FIFOen)
ser_chan->deviceType = s16550; // but FIFO doesn't
// necessarily work
if (b & ISR_FIFOworks)
ser_chan->deviceType = s16550a; // 16550a FIFOs work
}
if (ser_chan->deviceType == s16550a) {
switch(CYGPKG_IO_SERIAL_GENERIC_16X5X_FIFO_RX_THRESHOLD) {
default:
case 1:
_fcr_thresh=FCR_RT1; break;
case 4:
_fcr_thresh=FCR_RT4; break;
case 8:
_fcr_thresh=FCR_RT8; break;
case 14:
_fcr_thresh=FCR_RT14; break;
}
_fcr_thresh|=FCR_FE|FCR_CRF|FCR_CTF;
HAL_WRITE_UINT8(base+REG_fcr, _fcr_thresh); // Enable and clear FIFO
}
else
HAL_WRITE_UINT8(base+REG_fcr, 0); // make sure it's disabled
#endif
if (chan->out_cbuf.len != 0) {
_ier = IER_RCV;
} else {
_ier = 0;
}
// Master interrupt enable
HAL_WRITE_UINT8(base+REG_mcr, MCR_INT|MCR_DTR|MCR_RTS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -