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

📄 uartstdio.c

📁 Luminary cotex-M3 CAN总线 源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************
//
// uartstdio.c - Utility driver to provide simple UART console functions.
//
// Copyright (c) 2007 Luminary Micro, Inc.  All rights reserved.
// 
// Software License Agreement
// 
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
// 
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  Any use in violation
// of the foregoing restrictions may subject the user to criminal sanctions
// under applicable laws, as well as to civil liability for the breach of the
// terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 1387 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************

#include <stdarg.h>
#include "../hw_types.h"
#include "../hw_memmap.h"
#include "../hw_uart.h"
#include "../src/uart.h"
#include "../src/debug.h"
#include "../src/sysctl.h"

//*****************************************************************************
//
//! \addtogroup utilities_api
//! @{
//
//*****************************************************************************

//*****************************************************************************
//
// The base address of the chosen UART.
//
//*****************************************************************************
static unsigned long g_ulBase = 0;

//*****************************************************************************
//
// A mapping from an integer between 0 and 15 to its ASCII character
// equivalent.
//
//*****************************************************************************
static const char * const g_pcHex = "0123456789abcdef";

//*****************************************************************************
//
// The list of possible base addresses for the console UART.
//
//*****************************************************************************
static unsigned long g_ulUartBase[3] = { UART0_BASE, UART1_BASE, UART2_BASE };

//*****************************************************************************
//
// The list of UART peripherals.
//
//*****************************************************************************
static unsigned long g_ulUartPeriph[3] =
{
    SYSCTL_PERIPH_UART0, SYSCTL_PERIPH_UART1, SYSCTL_PERIPH_UART2
};

//*****************************************************************************
//
//! Initialize UART console.
//!
//! \param ulPortNum is the number of UART port to use for the serial
//! console (0-2)
//!
//! This function will initialize the specified serial port to be used
//! as a serial console.  The serial parameters will be set to
//! 115200,n,8,1.
//!
//! This function must be called prior to using any of the other UART
//! console functions: UARTprintf() or UARTgets().  In order for this
//! function to work correctly, SysCtlClockSet() must be called prior
//! to calling this function.
//!
//! This function is contained in <tt>utils/uartstdio.c</tt>, with
//! <tt>utils/uartstdio.h</tt> containing the API definition for use by
//! applications.
//!
//! \return None.
//
//*****************************************************************************
void
UARTStdioInit(unsigned long ulPortNum)
{
    //
    // Check the arguments.
    //
    ASSERT((ulPortNum == 0) || (ulPortNum == 1) ||
           (ulPortNum == 2));

    //
    // Check to make sure the UART peripheral is present.
    //
    if(!SysCtlPeripheralPresent(g_ulUartPeriph[ulPortNum]))
    {
        return;
    }

    //
    // Select the base address of the UART.
    //
    g_ulBase = g_ulUartBase[ulPortNum];

    //
    // Enable the UART peripheral for use.
    //
    SysCtlPeripheralEnable(g_ulUartPeriph[ulPortNum]);

    //
    // Configure the UART for 115200, n, 8, 1
    //
    UARTConfigSet(g_ulBase, 115200, UART_CONFIG_PAR_NONE |
                  UART_CONFIG_STOP_ONE | UART_CONFIG_WLEN_8);

    //
    // Enable the UART operation.
    //
    UARTEnable(g_ulBase);
}

//*****************************************************************************
//
//! Writes a string of characters to the UART output.
//!
//! \param pcBuf points to a buffer containing the string to transmit.
//! \param ulLen is the length of the string to transmit.
//!
//! This function will transmit the string to the UART output.  The number
//! of characters transmitted is determined by the parameter \b ulLen.
//! This function does no interpretation or translation of any characters.
//! Since the output is sent to a UART, any LF (/n) characters encountered
//! will be replaced with a CRLF pair.
//!
//! Besides using the \b ulLen parameter to stop transmitting the string,
//! if a null character (0) is encountered, then no more characters will
//! be transmitted and the function will return.
//!
//! This function is blocking and will not return until all the characters
//! have been written to the output FIFO.
//!
//! This function is contained in <tt>utils/uartstdio.c</tt>, with
//! <tt>utils/uartstdio.h</tt> containing the API definition for use by
//! applications.
//!
//! \return the count of characters written.
//
//*****************************************************************************
static int
UARTwrite(const char *pcBuf, unsigned long ulLen)
{
    unsigned int uIdx;

    //
    // Check for valid UART base address, and valid arguments.
    //
    ASSERT(g_ulBase != 0);
    ASSERT(pcBuf != 0);
    ASSERT(ulLen != 0);

    //
    // Send the characters
    //
    for(uIdx = 0; uIdx < ulLen; uIdx++)
    {
        //
        // If the character to the UART is \n, then add a \r before it
        // so that \n is translated to \n\r in the output.
        //
        if(pcBuf[uIdx] == '\n')
        {
            UARTCharPut(g_ulBase, '\r');
        }

        //
        // Send the character to the UART output.
        //
        UARTCharPut(g_ulBase, pcBuf[uIdx]);
    }

    //
    // Return the number of characters written.
    //
    return(uIdx);
}

//*****************************************************************************
//
//! A simple UART based get string function, with some line processing.
//!
//! \param pcBuf points to a buffer for the incoming string from the UART.
//! \param ulLen is the length of the buffer for storage of the string,
//! including the trailing 0.
//!
//! This function will receive a string from the UART input and store the
//! characters in the buffer pointed to by \b pcBuf.  The characters will
//! continue to be stored until a termination character is received.  The
//! termination characters are CR, LF, or ESC.  A CRLF pair is treated as
//! a single termination character.  The termination characters are not
//! stored in the string.  The string will be terminated with a 0 and the
//! function will return.  If more characters are received than will fit in
//! the buffer, then the extra characters will be ignored and not stored.
//!
//! Since the string will be null terminated, the user must ensure that
//! the buffer is sized to allow for the additional null character.
//!
//! This function is contained in <tt>utils/uartstdio.c</tt>, with
//! <tt>utils/uartstdio.h</tt> containing the API definition for use by
//! applications.
//!
//! \return the count of characters that were stored, not including the
//! trailing 0.
//
//*****************************************************************************
int
UARTgets(char *pcBuf, unsigned long ulLen)
{
    unsigned long ulCount = 0;
    char cChar;
    static char bLastWasCR = 0;

    //
    // Check the arguments.
    //
    ASSERT(pcBuf != 0);
    ASSERT(ulLen != 0);
    ASSERT(g_ulBase != 0);

    //
    // Adjust the length back by 1 to leave space for the trailing
    // null terminator.
    //
    ulLen--;

    //
    // Process characters until a newline is received.
    //
    do
    {
        //
        // Read the next character from the console.
        //
        cChar = UARTCharGet(g_ulBase);

        //
        // See if the backspace key was pressed.
        //
        if(cChar == '\b')
        {
            //
            // If there are any characters already in the buffer,
            // then delete the last.
            //
            if(ulCount)
            {
                //
                // Rub out the previous character.
                //
                UARTwrite("\b \b", 3);

                //
                // Decrement the number of characters in the buffer.
                //
                ulCount--;
            }

            //
            // Skip ahead to read the next character.
            //
            continue;
        }

        //
        // If this character is LF and last was CR, then just
        // gobble up the character because the EOL processing
        // was taken care of with the CR.
        //
        if((cChar == '\n') && bLastWasCR)
        {
            bLastWasCR = 0;
            continue;
        }

        //
        // See if a newline or escape character was received.
        //
        if((cChar == '\r') || (cChar == '\n') || (cChar == 0x1b))
        {
            //
            // If the character is a CR, then it may be followed by
            // a LF which should be paired with the CR.  So remember
            // that a CR was received.
            //
            if(cChar == '\r')
            {
                bLastWasCR = 1;
            }

            //
            // Stop processing the input and end the line.
            //
            break;
        }

        //
        // Process the received character as long as we are not at the
        // end of the buffer.  If the end of the buffer has been reached
        // then all additional characters are ignored until a newline
        // is received.
        //
        if(ulCount < ulLen)
        {
            //
            // Store the character in the caller supplied buffer.
            //
            pcBuf[ulCount] = cChar;

            //
            // Increment the count of characters received.
            //
            ulCount++;

            //
            // Reflect the character back to the user.
            //
            UARTCharPut(g_ulBase, cChar);
        }

    } while(1);

    //
    // Add a null termination to the string.
    //
    pcBuf[ulCount] = 0;

    //
    // Send a CRLF pair to the terminal to end the line.
    //
    UARTwrite("\r\n", 2);

    //
    // Return the count of chars in the buffer, not counting the
    // the trailing 0.
    //
    return(ulCount);
}

//*****************************************************************************
//
//! A simple UART based printf function supporting \%c, \%d, \%s, \%u, \%x, and
//! \%X.

⌨️ 快捷键说明

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