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

📄 uart.c

📁 RTOS系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: //depot/software/SDK/Triscend/a7hal/src/uart.c#18 $ *//* ********************************************************** *  uart.c *  Copyright(C) 2001-2004 Triscend Corporation. All Rights Reserved. *  This file is licensed under the terms of Triscend SDK License Agreement. ********************************************************** */#include "hal_conf.h"#if A7HAL_USE_UART/** * \file * The A7 contains two dedicated Universal Asynchronous Re-ceiver/Transmitters (UARTs) * Each UART is register and feature compatible with a 16C450/550-style device and is a full * duplex asynchronous communi-cation module that supports the following features. * - 5, 6, 7, or 8 bit data transmission * - Even, odd, or no parity bit generation and checking * - Start and stop bit generation and checking * - Line break detection and generation * - Receiver overrun and framing error checking * - Communications rates exceeding 1M baud * - Internal programmable baud-rate generator * - FIFO (16C550-style) or non-FIFO (16C450-style) operating modes * - Transmitter is buffered with 16-byte FIFO * - Receiver is buffered with 16-byte FIFO plus three error bits per data byte * - Exception handling using interrupt/polled modes * - Internal diagnostic capabilities with loop-back * - Modem handshake capability * In addition to the regular set of 16C550 registers, a few additional registers provide extra * flexibility to the serial ports. * * The functions within this file are only accessible if A7HAL_USE_UART * is #defined as YES in hal_conf.h *//** * \defgroup uart UART Driver * @{ */#include "uart.h"#include "clock.h"#include "triscend_a7.h"#if !A7HAL_USE_CLOCK#error The clock driver must be enabled in order to use this device.#endifstatic volatile a7hal_uart *serial;static int _stdioChan;static int _modemChan;int a7hal_uart_initialized;/** * \brief Initialize the UART. * * This function is called by a7hal_driverInit() * to initialize the UART to a known state and to set up * any data structures needed by the device driver. * * The line characteristics of the UART are set to; * * - No parity * - 8 Data bits * - 1 Stop bit * * If the #define \em A7HAL_BAUD_RATE is defined, the baud * rate of the UART is set to that value. If \em A7HAL_BAUD_RATE * is not defained the value of the #define \em A7HAL_DEFAULT_BAUD * is used (115200 by default). * * This function also enables the modem control lines and * configures UART 0 to be the STDIO UART by calling * a7hal_uart_setStdio(). Your FastChip project must have * the modem control lines connected. * * \return N/A * * \see a7hal_uart_setStdio, a7hal_clock_init * * \note The clock driver MUST be initialized by calling * a7hal_clock_init() before calling this function. * \em A7HAL_DEFAULT_BAUD is #defained in uart.h */void a7hal_uart_init( void ){    int port;    if ( a7hal_uart_initialized++ )        return;    /* Setup The Base Address Of The UART    */    serial = ( a7hal_uart * ) UART_BASE;    for ( port = 0; port < A7HAL_MAX_PORTS; port++ )    {        a7hal_uart_reset( port );        serial[port].control = 0x00000100;#ifndef    A7HAL_BAUD_RATE      /* defined in system.h */        a7hal_uart_setBaud( port, A7HAL_DEFAULT_BAUD );#else        a7hal_uart_setBaud( port, A7HAL_BAUD_RATE );#endif        /* 8 Data bits, 1 Stop bit, No Parity    */        a7hal_uart_setLineControl( port, 8, 1, 0 );        a7hal_uart_setFIFO( port, A7HAL_FIFO_14_BYTES );    }    a7hal_uart_setStdio( 0 );    a7hal_uart_modemControlEnable( 0 );#ifdef UART0_IS_USED  #if (UART0_IS_USED == TRUE)    a7hal_uart_setBaud( 0, UART0_SPEC_BAUDRATE );    #if (UART0_IS_MODEM_LINE_USED == TRUE)    a7hal_uart_modemControlEnable( 0 );    #endif    #if (UART0_PARITY == UART_NO_PARITY )    a7hal_uart_setLineControl( 0, UART0_DATA_BITS, UART0_STOP_BITS,                    A7HAL_PARITY_NONE );    #endif    #if (UART0_PARITY == UART_ODD_PARITY )    a7hal_uart_setLineControl( 0, UART0_DATA_BITS, UART0_STOP_BITS,                    A7HAL_PARITY_ODD );    #endif    #if (UART0_PARITY == UART_EVEN_PARITY )    a7hal_uart_setLineControl( 0, UART0_DATA_BITS, UART0_STOP_BITS,                    A7HAL_PARITY_EVEN );    #endif  #endif#endif#ifdef UART1_IS_USED  #if (UART1_IS_USED == TRUE)    a7hal_uart_setBaud( 1, UART1_SPEC_BAUDRATE );    #if (UART1_IS_MODEM_LINE_USED == TRUE)    a7hal_uart_modemControlEnable( 1 );    #endif    #if (UART1_PARITY == UART_NO_PARITY )    a7hal_uart_setLineControl( 1, UART1_DATA_BITS, UART1_STOP_BITS,                    A7HAL_PARITY_NONE );    #endif    #if (UART1_PARITY == UART_ODD_PARITY )    a7hal_uart_setLineControl( 1, UART1_DATA_BITS, UART1_STOP_BITS,                    A7HAL_PARITY_ODD );    #endif    #if (UART1_PARITY == UART_EVEN_PARITY )    a7hal_uart_setLineControl( 1, UART1_DATA_BITS, UART1_STOP_BITS,                    A7HAL_PARITY_EVEN );    #endif  #endif#endif}/** * \brief Set the UART line control information. * * This function should be used to set the UART line control characteristics. * * \param port UART number (0-1) * \param dataBits Number of data bits (5-8) * \param stopBits Number of stop bits (1-2) * \param parity Parity type * * \return N/A */void a7hal_uart_setLineControl( int port, int dataBits, int stopBits, int parity ){    if( stopBits == 1 )    {        switch( dataBits )        {        case 5:            serial[port].lineControl = A7HAL_BITS5_STOPS1;            break;        case 6:            serial[port].lineControl = A7HAL_BITS6_STOPS1;            break;        case 7:            serial[port].lineControl = A7HAL_BITS7_STOPS1;            break;        case 8:            serial[port].lineControl = A7HAL_BITS8_STOPS1;            break;        }    }    else    if( stopBits == 2 )    {        switch( dataBits )        {        case 6:            serial[port].lineControl = A7HAL_BITS6_STOPS2;            break;        case 7:            serial[port].lineControl = A7HAL_BITS7_STOPS2;            break;        case 8:            serial[port].lineControl = A7HAL_BITS8_STOPS2;            break;        }    }    if( parity == A7HAL_PARITY_NONE )        serial[port].lineControl |= A7HAL_PARITY_NONE;    else    if( parity == A7HAL_PARITY_ODD )        serial[port].lineControl |= A7HAL_PARITY_ENABLE | A7HAL_PARITY_ODD;    else    if( parity == A7HAL_PARITY_EVEN )        serial[port].lineControl |= A7HAL_PARITY_ENABLE | A7HAL_PARITY_EVEN;}/** * \brief Get the UART base address. * * \return The 32 bit base address of the UART device. */unsigned long a7hal_uart_getDeviceAddress( void ){    return ( ( unsigned long ) serial );}/** * \brief Read a character from a UART. * * This function reads one character from the specified UART. * * \param port UART number (0-1) * * \return The character read from the UART or -2 if no characters are available. * * \see a7hal_uart_putChar */int a7hal_uart_getChar( int port ){    /* Check To See If Data Is Ready To Be Read    */    if ( serial[port].lineStatus & A7HAL_DATA_READY )    {        /* Return The Received Char        */        return ( serial[port].rxtx );    }    else    {        /* No Chars To Be Read, Return Error    */        return ( -2 );    }}/** * \brief Write a character to a UART. * * This function blocks until the character is sent. * * \param port UART number (0-1) * \param c Character to send * * \return The character that was sent to the UART. * * \see a7hal_uart_getChar */int a7hal_uart_putChar( int port, int c ){    /* Wait For Thr Transmitter Holding Register Empty Bit    */    while ( ( serial[port].lineStatus & A7HAL_THRE ) == 0 ) ;    /* Send The Char To The Port                */    serial[port].rxtx = c;    return ( c );}/** * \brief Flush the UART transmitter. * * This function waits until the UART signals that the Transmitter Holding * Register (THR) is empty. * * \param port UART number (0-1) * * \return N/A */void a7hal_uart_flush( int port ){    /* Wait For Thr Transmitter Holding Register Empty Bit    */    while ( ( serial[port].lineStatus & A7HAL_TE ) == 0 ) ;}/** * \brief Set the baud rate of a UART. * * \param port UART number (0-1) * \param baudRate Baud rate to be set (9600-115200) * * \return N/A * * \see a7hal_clock_init, a7hal_clock_getFreq

⌨️ 快捷键说明

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