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

📄 uartdrv.c

📁 Vitesse 24port gigabit Switch Source Code
💻 C
字号:
/*

    Copyright (c) 2002-2005 Vitesse Semiconductor Corporation "Vitesse".  
    All Rights Reserved.  Unpublished rights reserved under the copyright laws
    of the United States of America, other countries and international treaties.
    The software is provided without a fee. Permission to use, copy, store and 
    modify, the software and its source code is granted. Permission to integrate
    into other products, disclose, transmit and distribute the software in an
    absolute machine readable format (e.g. HEX file) is also granted. 

    The source code of the software may not be disclosed, transmitted or
    distributed without the written permission of Vitesse. The software and its
    source code may only be used in products utilizing a Vitesse VSC73xx product.
 
    This copyright notice must appear in any copy, modification, disclosure,
    transmission or distribution of the software. Vitesse retains all ownership,
    copyright, trade secret and proprietary rights in the software.  

    THIS SOFTWARE HAS BEEN PROVIDED "AS IS," WITHOUT EXPRESS OR IMPLIED WARRANTY
    INCLUDING, WITHOUT LIMITATION, IMPLIED WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR USE AND NON-INFRINGEMENT.

*/
#include "common.h"
#include "hwconf.h"
#include "uartdrv.h"
#include "timer.h"

#ifndef NO_DEBUG_IF

/*
** Choose whether to use transmit buffer and interrupt driven transmit by 
** defining USE_TX_BUF. If USE_TX_BUF is not defined, characters will be sent 
** in-line.
*/

/* ************************************************************************ **
 *
 *
 * Public data
 *
 *
 *
 * ************************************************************************ */

bit uart_rx_buf_overrun = FALSE;
bit uart_framing_error  = FALSE;


/* ************************************************************************ **
 *
 *
 * Defines
 *
 *
 *
 * ************************************************************************ */

#define BAUD_RATE_TIMER_VALUE     (0x10000 - \
    (ushort) ((float)(CLOCK_FREQ / (32 * (float) BAUD_RATE)) + 0.5))


#define T_BAUD_L BAUD_RATE_TIMER_VALUE & 0x0FF
#define T_BAUD_H BAUD_RATE_TIMER_VALUE >> 8

/* size of receive buffer. Must be a power of 2, i.e. 2, 4, 8 ... 256 */
#define RX_BUF_SIZE 32

#ifdef USE_TX_BUF
/* size of transmit buffer. Must be a power of 2, i.e. 2, 4, 8 ... 256 */
#define TX_BUF_SIZE 256
#endif

/* ************************************************************************ **
 *
 *
 * Typedefs and enums
 *
 *
 *
 * ************************************************************************ */

/* ************************************************************************ **
 *
 *
 * Prototypes for local functions
 *
 *
 *
 * ************************************************************************ */

/* ************************************************************************ **
 *
 *
 * Local data
 *
 *
 *
 * ************************************************************************ */

static uchar idata rx_buf [RX_BUF_SIZE];

#if RX_BUF_SIZE > 256
static ushort data rx_head  = 0;
static ushort data rx_tail  = 0;
#else
static uchar data rx_head  = 0;
static uchar data rx_tail  = 0;
#endif /* RX_BUF_SIZE > 256 */

#ifdef USE_TX_BUF
static uchar xdata tx_buf [TX_BUF_SIZE];

static uchar data tx_head  = 0;
static uchar data tx_tail  = 0;

static bit tx_active = FALSE;
#else
static bit tx_done;
#endif


/* ************************************************************************ */
void uart_init (void)
/* ------------------------------------------------------------------------ --
 * Purpose     : Enable TX and RX on UART.
 * Remarks     : 
 * Restrictions:
 * See also    :
 * Example     :
 * ************************************************************************ */
{
    /* Set time according to baud rate */
    RCAP2H = T_BAUD_H;
    RCAP2L = T_BAUD_L;
    TH2    = T_BAUD_H;
    TL2    = T_BAUD_L;

    // Timer2 as baudrate generator for RCLK+TCLK  
    T2CON = 0x34;

    // mode 1, 8 bits, no parity, REN=1, TI=1
    SCON = 0x52;


    /* Set high priority for uart interrupt */
    PS = 1;

    /* Enable uart interrupt */
    ES = 1;
}


/* ************************************************************************ */
void uart_interrupt (void) small interrupt 4 using 2
/* ------------------------------------------------------------------------ --
 * Purpose     : Interrupt function. Save received char in buffer.
 * Remarks     : 
 * Restrictions:
 * See also    :
 * Example     :
 * ************************************************************************ */
{
    uchar ch;


    if (RI) {
        ch = SBUF;
        RI = 0;

        /* save received char if not buffer overrun */
        if ((rx_head + 1) == rx_tail) {
            uart_rx_buf_overrun = TRUE;
        }
        else {
            rx_buf[rx_head] = ch;
            rx_head = (rx_head + 1) & (RX_BUF_SIZE - 1);
        }
    }
    if (TI) {
        TI = 0;
#ifdef USE_TX_BUF
        if (tx_head != tx_tail) {
            SBUF = tx_buf[tx_tail];
            tx_tail = (tx_tail + 1) & (TX_BUF_SIZE - 1); 
        }
        else {
            tx_active = FALSE;
        }
#else
        tx_done = TRUE;
#endif
    }
}


/* ************************************************************************ */
bool uart_byte_ready (void) small
/* ------------------------------------------------------------------------ --
 * Purpose     : Check if any char received.
 * Remarks     : Returns TRUE, if char ready, otherwise FALSE.
 * Restrictions:
 * See also    : uart_get_byte
 * Example     :
 * ************************************************************************ */
{
    return (rx_head != rx_tail);
}

/* ************************************************************************ */
uchar uart_get_byte (void) small
/* ------------------------------------------------------------------------ --
 * Purpose     : Get received char.
 * Remarks     :
 * Restrictions: May only be called if uart_byte_ready has returned TRUE.
 * See also    : uart_byte_ready
 * Example     :
 * ************************************************************************ */
{
    uchar tmp;

    tmp = rx_buf[rx_tail];
    rx_tail = (rx_tail + 1) & (RX_BUF_SIZE - 1); 
    return tmp;
}

void uart_put_byte (uchar ch) small

{
#ifdef USE_TX_BUF
    uchar tx_tail_tmp;
#endif


#ifdef USE_TX_BUF

    /*
    ** If no more room in tx buffer, wait until next char transmitted
    */
    tx_tail_tmp = tx_tail;
    if (((tx_head + 1) & (TX_BUF_SIZE - 1)) == tx_tail_tmp) {
        start_timer(MSEC_20);
        
        while (tx_tail_tmp == tx_tail) { 
            if (timeout()) {
                /* This should not occur */
                return;
            }
        }
    }

    /*
    ** Transfer char to tx buffer. Possibly provoke interrupt.
    */
    ES = 0;
    tx_buf[tx_head] = ch;
    tx_head = (tx_head + 1) & (TX_BUF_SIZE - 1);
    if (!tx_active) {
        tx_active = TRUE;
        TI = 1;
    }
    ES = 1;
#else
    tx_done = FALSE;
    SBUF = ch;
    start_timer(MSEC_20);

    while (!tx_done && !timeout()) {
        /* do nothing but wait */
    }
#endif
}

#endif

⌨️ 快捷键说明

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