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

📄 uart.c

📁 RTOS系统
💻 C
📖 第 1 页 / 共 2 页
字号:
 *  * \note The UART baud rates are based on the system clock * frequency. This function assumes the clock driver * has been initialized with a7hal_clock_init(). */void a7hal_uart_setBaud( int port, int baudRate ){    int oldLineStatus;    int baud;    baud =        ( a7hal_clock_getFreq( 0 ) / ( baudRate * A7HAL_CLOCK_DIVISOR ) ) - 1;    /* Save The Line Control Register    */    oldLineStatus = serial[port].lineControl;    /* Set The DLAB Bit            */    serial[port].lineControl = A7HAL_DLAB;    /* Set The MSB Of The Baud Dividor    */    serial[port].intEnable = ( ( baud >> 8 ) & 0xff );    /* Set The LSB Of The Baud Dividor    */    serial[port].rxtx = ( baud & 0xff );    /* Restore The Line Control Register    */    serial[port].lineControl = oldLineStatus;}/** * \brief Get the baud rate of a UART. * * Gets the current baud rate of the UART specified by the \em port * parameter. * * \param port UART number (0-1) * * \return The current UART baud rate. * * \see a7hal_uart_setBaud * * \note The baud rate returnd by this function may not match the * exact value of the baud rate that was set using * a7hal_uart_setBaud(). This is due to the fact that the * baud rate is calculated from the UART divisor registers, * and the CPU clock speed may not be divisible by the baud rate. */int a7hal_uart_getBaud( int port ){    int oldLineStatus;    int baud;    /* Save The Line Control Register    */    oldLineStatus = serial[port].lineControl;    /* Set The DLAB Bit            */    serial[port].lineControl = A7HAL_DLAB;    /* Set The MSB Of The Baud Dividor    */    baud = ( ( serial[port].intEnable & 0xff ) << 8 );    baud |= ( serial[port].rxtx & 0xff );    baud = a7hal_clock_getFreq( 0 ) / ( baud * A7HAL_CLOCK_DIVISOR );    /* Restore The Line Control Register    */    serial[port].lineControl = oldLineStatus;    return ( baud );}/** * \brief Write a string to a UART. * * This function wirtes a NULL terminated string to STDIO. If the * string contains any \em \\n characters they will be written as * \em \\r\\n. * * \param string Pointer to data * * \return N/A * * \see a7hal_uart_putChar, a7hal_uart_getStdio, a7hal_uart_setStdio */void a7hal_uart_putString( char *string ){    while ( *string )    {        if ( *string == '\n' )            a7hal_uart_putChar( a7hal_uart_getStdio(  ), ( int ) '\r' );        a7hal_uart_putChar( a7hal_uart_getStdio(  ), ( int ) *string++ );    }}/** * \brief Reset a UART to a know state. * * This function resets the specified UART. * * \param port UART number (0-1) * * \return N/A */void a7hal_uart_reset( int port ){    serial[port].control = UART_CONTROL_RESET_VALUE;    serial[port].lineControl = UART_LINE_CONTROL_RESET_VALUE;    serial[port].intEnable = UART_INT_ENABLE_RESET_VALUE;    serial[port].intId = UART_FIFO_CTRL_RESET_VALUE;    serial[port].modemControl = UART_MODEM_CONTROL_RESET_VALUE;}/** * \brief Set which UART should be used as STDIO. * * \param port UART number (0-1) * * \return Non zero if STDIO was set successfully. * * \see a7hal_uart_getStdio, a7hal_uart_putString */int a7hal_uart_setStdio( int port ){    int ok = port < A7HAL_MAX_PORTS;    if ( ok )        _stdioChan = port;    return ( ok );}/** * \brief Get which UART is being used as STDIO. * * \return Which UART is currently being used for standard input and output. * * \see a7hal_uart_setSdtio, a7hal_uart_putString */int a7hal_uart_getStdio( void ){    return ( _stdioChan );}/** * \brief Get the number of UARTS the driver supports. * * \return The value of A7HAL_MAX_PORTS #defined in uart.h */int a7hal_uart_getCount( void ){    return A7HAL_MAX_PORTS;}/** * \brief Enable the UART modem control lines. * * Enable the modem control lines for the specified UART. * * \param port UART number (0-1) * * \return N/A * * \see a7hal_uart_setRTS, a7hal_uart_setDTR * * \note The FastChip project must be configured to support * modem control lines before this function is called. */void a7hal_uart_modemControlEnable( int port ){    int n;    for ( n = 0; n < A7HAL_MAX_PORTS; n++ )    {        /* Disable Modem Control On All Ports    */        serial[n].control = 0x00000100;    }    /* Enable Modem Control On The Selected Channel    */    serial[port].control = 0x00000300;    /* Set RTS and DTR    */    a7hal_uart_setRTS( 1 );    a7hal_uart_setDTR( 1 );    _modemChan = port;}/** * \brief Set the state of the RTS modem control line. * * \param onOff New state of the RTS line * * \return N/A * * \see a7hal_uart_onOff, a7hal_uart_setDTR, a7hal_uart_modemControlEnable */void a7hal_uart_setRTS( a7hal_uart_onOff onOff ){    if ( onOff == ON )        serial[_modemChan].modemControl |= A7HAL_RTS;    else if ( onOff == OFF )        serial[_modemChan].modemControl &= ~A7HAL_RTS;}/** * \brief Set the state of the DTR modem comtrol line. * * \param onOff New state of the DTR line * * \return N/A * * \see a7hal_uart_onOff, a7hal_uart_setRTS, a7hal_uart_modemControlEnable */void a7hal_uart_setDTR( a7hal_uart_onOff onOff ){    if ( onOff == ON )        serial[_modemChan].modemControl |= A7HAL_DTR;    else if ( onOff == OFF )        serial[_modemChan].modemControl &= ~A7HAL_DTR;}/** * \brief Enable/disable a UART TX interrupt. * * Sets the state of the TX interrupt on the specified UART. * * \param port UART number (0-1) * \param onOff New state of the DTR line * * \return N/A * * \see a7hal_uart_onOff, a7hal_uart_setRxInt */void a7hal_uart_setTxInt( int port, a7hal_uart_onOff onOff ){    if ( onOff == ON )        serial[port].intEnable |= A7HAL_THRE_INT;    else if ( onOff == OFF )        serial[port].intEnable &= ~A7HAL_THRE_INT;}/** * \brief Enable/disable a UART RX interrupt. * * Sets the state of the RX interrupt on the specified UART. * * \param port UART number (0-1) * \param onOff New state of the DTR line * * \return N/A * * \see a7hal_uart_onOff, a7hal_uart_setTxInt */void a7hal_uart_setRxInt( int port, a7hal_uart_onOff onOff ){    if ( onOff == ON )        serial[port].intEnable |= A7HAL_RX_INT;    else if ( onOff == OFF )        serial[port].intEnable &= ~A7HAL_RX_INT;}/** * \brief Get the interrupt status of a UART. * * \param port UART number (0-1) * * \return UART interrupt status. * * \see a7hal_uart_setTxInt, a7hal_uart_setRxInt */int a7hal_uart_getIntStatus( int port ){    return ( serial[port].intId );}/** * \brief Get the line status of a UART. * * \param port UART number (0-1) * * \return UART line status. */int a7hal_uart_getLineStatus( int port ){    return ( serial[port].lineStatus );}/** * \brief Configure a UART FIFO. * * Sets the UART FIFO buffer to the specified depth. * * \param port UART number (0-1) * \param depth Depth of the UART FIFO * * \return N/A * * \see a7hal_uart_fifoDepth * * \note Any characters in the TX or RX FIFOs are flushed. */void a7hal_uart_setFIFO( int port, a7hal_uart_fifoDepth depth ){    serial[port].intId =        A7HAL_FIFO_TX_CLEAR | A7HAL_FIFO_RX_CLEAR | A7HAL_FIFO_ENABLE | depth;}/** @} */#endif

⌨️ 快捷键说明

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