at91_serial.c

来自「eCos操作系统源码」· C语言 代码 · 共 579 行 · 第 1/2 页

C
579
字号
//==========================================================================////      devs/serial/arm/at91/at91_serial.c////      Atmel AT91/EB40 Serial I/O Interface Module (interrupt driven)////==========================================================================//####ECOSGPLCOPYRIGHTBEGIN####// -------------------------------------------// This file is part of eCos, the Embedded Configurable Operating System.// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.//// 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, tkoeller// Date:          2001-07-24// Purpose:       Atmel AT91/EB40 Serial I/O module (interrupt driven version)// Description: ////####DESCRIPTIONEND####////==========================================================================#include <pkgconf/hal.h>#include <pkgconf/infra.h>#include <pkgconf/system.h>#include <pkgconf/io_serial.h>#include <pkgconf/io.h>#include <pkgconf/kernel.h>#include <cyg/io/io.h>#include <cyg/hal/hal_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_type.h>#include <cyg/infra/cyg_ass.h>externC void * memcpy( void *, const void *, size_t );#ifdef CYGPKG_IO_SERIAL_ARM_AT91#include "at91_serial.h"#define RCVBUF_EXTRA 16#define RCV_TIMEOUT 10#define SIFLG_NONE          0x00#define SIFLG_TX_READY      0x01#define SIFLG_XMIT_BUSY     0x02#define SIFLG_XMIT_CONTINUE 0x04typedef struct at91_serial_info {    CYG_ADDRWORD   base;    CYG_WORD       int_num;    CYG_WORD       stat;    int            transmit_size;    cyg_interrupt  serial_interrupt;    cyg_handle_t   serial_interrupt_handle;    cyg_uint8      *rcv_buffer[2];    cyg_uint16     rcv_chunk_size;    cyg_uint8      curbuf;    cyg_uint8      flags;} at91_serial_info;static bool at91_serial_init(struct cyg_devtab_entry *tab);static bool at91_serial_putc_interrupt(serial_channel *chan, unsigned char c);#if (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL0) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BUFSIZE == 0) \ || (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL1) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BUFSIZE == 0)static bool at91_serial_putc_polled(serial_channel *chan, unsigned char c);#endifstatic Cyg_ErrNo at91_serial_lookup(struct cyg_devtab_entry **tab,                                     struct cyg_devtab_entry *sub_tab,                                    const char *name);static unsigned char at91_serial_getc_interrupt(serial_channel *chan);#if (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL0) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BUFSIZE == 0) \ || (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL1) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BUFSIZE == 0)static unsigned char at91_serial_getc_polled(serial_channel *chan);#endifstatic Cyg_ErrNo at91_serial_set_config(serial_channel *chan, cyg_uint32 key,                                        const void *xbuf, cyg_uint32 *len);static void at91_serial_start_xmit(serial_channel *chan);static void at91_serial_stop_xmit(serial_channel *chan);static cyg_uint32 at91_serial_ISR(cyg_vector_t vector, cyg_addrword_t data);static void       at91_serial_DSR(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t data);#if (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL0) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BUFSIZE > 0) \ || (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL1) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BUFSIZE > 0)static SERIAL_FUNS(at91_serial_funs_interrupt,                    at91_serial_putc_interrupt,                    at91_serial_getc_interrupt,                   at91_serial_set_config,                   at91_serial_start_xmit,                   at91_serial_stop_xmit    );#endif#if (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL0) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BUFSIZE == 0) \ || (defined(CYGPKG_IO_SERIAL_ARM_AT91_SERIAL1) && CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BUFSIZE == 0)static SERIAL_FUNS(at91_serial_funs_polled,                    at91_serial_putc_polled,                    at91_serial_getc_polled,                   at91_serial_set_config,                   at91_serial_start_xmit,                   at91_serial_stop_xmit    );#endif#ifdef CYGPKG_IO_SERIAL_ARM_AT91_SERIAL0static cyg_uint8 at91_serial_rcv_buffer_0    [2][CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_RCV_CHUNK_SIZE + RCVBUF_EXTRA];static at91_serial_info at91_serial_info0 = {    base            : (CYG_ADDRWORD) AT91_USART0,    int_num         : CYGNUM_HAL_INTERRUPT_USART0,    rcv_chunk_size  : CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_RCV_CHUNK_SIZE,    rcv_buffer      : {at91_serial_rcv_buffer_0[0], at91_serial_rcv_buffer_0[1]}};#if CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BUFSIZE > 0static unsigned char at91_serial_out_buf0[CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BUFSIZE];static unsigned char at91_serial_in_buf0[CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BUFSIZE];static SERIAL_CHANNEL_USING_INTERRUPTS(at91_serial_channel0,                                       at91_serial_funs_interrupt,                                        at91_serial_info0,                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BAUD),                                       CYG_SERIAL_STOP_DEFAULT,                                       CYG_SERIAL_PARITY_DEFAULT,                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,                                       CYG_SERIAL_FLAGS_DEFAULT,                                       &at91_serial_out_buf0[0], sizeof(at91_serial_out_buf0),                                       &at91_serial_in_buf0[0], sizeof(at91_serial_in_buf0)    );#elsestatic SERIAL_CHANNEL(at91_serial_channel0,                      at91_serial_funs_polled,                       at91_serial_info0,                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AT91_SERIAL0_BAUD),                      CYG_SERIAL_STOP_DEFAULT,                      CYG_SERIAL_PARITY_DEFAULT,                      CYG_SERIAL_WORD_LENGTH_DEFAULT,                      CYG_SERIAL_FLAGS_DEFAULT    );#endifDEVTAB_ENTRY(at91_serial_io0,              CYGDAT_IO_SERIAL_ARM_AT91_SERIAL0_NAME,             0,                     // Does not depend on a lower level interface             &cyg_io_serial_devio,              at91_serial_init,              at91_serial_lookup,     // Serial driver may need initializing             &at91_serial_channel0    );#endif //  CYGPKG_IO_SERIAL_ARM_AT91_SERIAL1#ifdef CYGPKG_IO_SERIAL_ARM_AT91_SERIAL1static cyg_uint8 at91_serial_rcv_buffer_1    [2][CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_RCV_CHUNK_SIZE + RCVBUF_EXTRA];static at91_serial_info at91_serial_info1 = {    base            : (CYG_ADDRWORD) AT91_USART1,    int_num         : CYGNUM_HAL_INTERRUPT_USART1,    rcv_chunk_size  : CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_RCV_CHUNK_SIZE,    rcv_buffer      : {at91_serial_rcv_buffer_1[0], at91_serial_rcv_buffer_1[1]}};#if CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BUFSIZE > 0static unsigned char at91_serial_out_buf1[CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BUFSIZE];static unsigned char at91_serial_in_buf1[CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BUFSIZE];static SERIAL_CHANNEL_USING_INTERRUPTS(at91_serial_channel1,                                       at91_serial_funs_interrupt,                                        at91_serial_info1,                                       CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BAUD),                                       CYG_SERIAL_STOP_DEFAULT,                                       CYG_SERIAL_PARITY_DEFAULT,                                       CYG_SERIAL_WORD_LENGTH_DEFAULT,                                       CYG_SERIAL_FLAGS_DEFAULT,                                       &at91_serial_out_buf1[0], sizeof(at91_serial_out_buf1),                                       &at91_serial_in_buf1[0], sizeof(at91_serial_in_buf1)    );#elsestatic SERIAL_CHANNEL(at91_serial_channel1,                      at91_serial_funs_polled,                       at91_serial_info1,                      CYG_SERIAL_BAUD_RATE(CYGNUM_IO_SERIAL_ARM_AT91_SERIAL1_BAUD),                      CYG_SERIAL_STOP_DEFAULT,                      CYG_SERIAL_PARITY_DEFAULT,                      CYG_SERIAL_WORD_LENGTH_DEFAULT,                      CYG_SERIAL_FLAGS_DEFAULT    );#endifDEVTAB_ENTRY(at91_serial_io1,              CYGDAT_IO_SERIAL_ARM_AT91_SERIAL1_NAME,             0,                     // Does not depend on a lower level interface             &cyg_io_serial_devio,              at91_serial_init,              at91_serial_lookup,     // Serial driver may need initializing             &at91_serial_channel1    );#endif //  CYGPKG_IO_SERIAL_ARM_AT91_SERIAL1// Internal function to actually configure the hardware to desired baud rate, etc.static boolat91_serial_config_port(serial_channel *chan, cyg_serial_info_t *new_config, bool init){    at91_serial_info * const at91_chan = (at91_serial_info *)chan->dev_priv;    const CYG_ADDRWORD base = at91_chan->base;    cyg_uint32 parity = select_parity[new_config->parity];    cyg_uint32 word_length = select_word_length[new_config->word_length-CYGNUM_SERIAL_WORD_LENGTH_5];    cyg_uint32 stop_bits = select_stop_bits[new_config->stop];    if ((word_length == 0xFF) ||        (parity == 0xFF) ||        (stop_bits == 0xFF)) {        return false;  // Unsupported configuration    }    // Reset device    HAL_WRITE_UINT32(base + AT91_US_CR, AT91_US_CR_RxRESET | AT91_US_CR_TxRESET);    // Configuration    HAL_WRITE_UINT32(base + AT91_US_MR, parity | word_length | stop_bits);    // Baud rate    HAL_WRITE_UINT32(base + AT91_US_BRG, AT91_US_BAUD(select_baud[new_config->baud]));    // Disable all interrupts    HAL_WRITE_UINT32(base + AT91_US_IDR, 0xFFFFFFFF);    // Start receiver    at91_chan->curbuf = 0;    HAL_WRITE_UINT32(base + AT91_US_RPR, (CYG_ADDRESS) at91_chan->rcv_buffer[0]);    HAL_WRITE_UINT32(base + AT91_US_RTO, RCV_TIMEOUT);    HAL_WRITE_UINT32(base + AT91_US_IER, AT91_US_IER_ENDRX | AT91_US_IER_TIMEOUT);    HAL_WRITE_UINT32(base + AT91_US_RCR, at91_chan->rcv_chunk_size);    // Enable RX and TX    HAL_WRITE_UINT32(      base + AT91_US_CR,      AT91_US_CR_RxENAB | AT91_US_CR_TxENAB | AT91_US_CR_RSTATUS | AT91_US_CR_STTTO    );    if (new_config != &chan->config) {        chan->config = *new_config;    }    return true;}// Function to initialize the device.  Called at bootstrap time.static bool at91_serial_init(struct cyg_devtab_entry *tab){    serial_channel * const chan = (serial_channel *) tab->priv;    at91_serial_info * const at91_chan = (at91_serial_info *) chan->dev_priv;    int res;#ifdef CYGDBG_IO_INIT

⌨️ 快捷键说明

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