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

📄 consol.c

📁 The latest release, ver 1.16 of the ARM (7TDMI, 720T, 920T) port for uC/OS-II V2.61 (and higher) inc
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************
*                                                                         *
*   PROJECT     : ARM port for uC/OS-II                                   *
*                                                                         *
*   MODULE      : CONSOL.c                                                *
*                                                                         *
*   AUTHOR      : Michael Anburaj                                         *
*                 URL  : http://geocities.com/michaelanburaj/             *
*                 EMAIL: michaelanburaj@hotmail.com                       *
*                                                                         *
*   SPONSORS    : Thanks to Martin Li (mli00@yahoo.com) & Don Williams    *
*                 (donw@clearblu.net) for sponsoring hardware.            *
*                                                                         *
*   PROCESSOR   : LPC2xxx (32 bit ARM7TDMI-S RISC core from Philips)      *
*                                                                         *
*   TOOL-CHAIN  : SDT 2.51, ADS 1.2 or GCC                                *
*                                                                         *
*   DESCRIPTION :                                                         *
*   This is the CONSOL Driver module. Supports multiple RS232 console     *
*   interfaces.                                                           *
*                                                                         *
**************************************************************************/


#include "frmwrk_cfg.h"
#if (INCLUDE_CONSOL == 1)

#include "def.h"
#include "consol.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>

#include "lpc.h"


/* ********************************************************************* */
/* Global definitions */


/* ********************************************************************* */
/* File local definitions */

#define __isalpha(c) (c >'9')
#define __isupper(c) !(c & 0x20)
#define __ishex(c) (((c >= '0')&&(c <= '9'))||((c >= 'A')&&(c <= 'F')))
#define __ascii2hex(c) ((c <= '9')? c-'0': c-'A'+10)

static U8 __bU0FifoFlag, __bU0FifoCnt;

#if (INCLUDE_CONSOL1 == 1)
static U8 __bU1FifoFlag, __bU1FifoCnt;
#endif


/* ********************************************************************* */
/* Local functions */

/*
*********************************************************************************************
*                                       __hwPrintDataLines
*
* Description: This routine prints one page of hex data through the CONSOL port.
*
* Arguments  : pbData   - Pointer to data.
*              hwOffset - Offset to print from.
*              hwLen    - Data length.
*
* Return     : The new data offset.
*
* Note(s)    : 
*********************************************************************************************
*/

static U16 __hwPrintDataLines(U8 *pbData, U16 hwOffset, U16 hwLen)
{
    if(hwLen)
    {
        U8 bI, bJ;

        bI = 0;
        while(1)
        {
            CONSOL_PrintNum(16,4,False,'0',hwOffset);
			CONSOL_SendChar(':');
            for(bJ=0;bJ<16;bJ++)
            {
                CONSOL_SendChar(' ');
				CONSOL_PrintNum(16,2,False,'0',pbData[hwOffset++]);

                if(--hwLen == 0)
                {
                    CONSOL_SendCh('\n');
                    goto done;
                }
            }
            CONSOL_SendCh('\n');
        }
    }

done:
    return hwOffset;
}


/* ********************************************************************* */
/* Global functions */

/*
*********************************************************************************************
*                                       CONSOL_Init
*
* Description: This routine initializes the consol port by setting its baud rate & FIFO
*              buffer.
*
* Arguments  : hwBaudVal - Baud value corresponding to the Baud Rate in bit/sec.
*              bFifoFlag - FIFO flag.
*                          True  - FIFO enabled.
*                          False - FIFO disabled.
*
* Return     : none.
*
* Note(s)    : Before calling this, CONSOL_Select needs to be called for selecting the consol
*              port.
*********************************************************************************************
*/
void CONSOL_Init(U16 hwBaudVal, U8 bFifoFlag)
{
	__bU0FifoFlag = bFifoFlag;
	__bU0FifoCnt = 0;

	// set port pins for UART0
	rPINSEL0 = (rPINSEL0 & ~MSK_PINSEL0_U0) | VAL_PINSEL0_U0;

	/*  Init baudrate : 
	 *    1) enable DL-registers
	 *    2) set DLL + DLM divisor registers
	 *    3) disable DL-registers                                
	 */
	BIT_SET(rU0LCR, 0x80);
	rU0DLL = (U8)hwBaudVal;
	rU0DLM = (U8)(hwBaudVal>>8);
	BIT_CLR(rU0LCR, 0x80);

	/*  Init LCR: 
	 *    1) 8-bit data
	 *    2) 1 stop bit
	 *    3) no parity                                           
	 */
	rU0LCR = 0x03;

	if(bFifoFlag)
	{
		/*  Init FCR: 
		 *    1) enable FIFO
		 *    2) reset receiver FIFO
		 *    3) reset transmitter FIFO
		 *    4) set receive FIFO threshold to 4                     
		 */
		rU0FCR = 0x47;
	}
	else
	{
		rU0FCR = 0x00;
	}

	/* Clear interrupt bits */
	rU0IER = 0x00;
}

#if (INCLUDE_CONSOL1 == 1)
/*
*********************************************************************************************
*                                       CONSOL1_Init
*
* Description: This routine initializes the consol port by setting its baud rate & FIFO
*              buffer.
*
* Arguments  : hwBaudVal - Baud value corresponding to the Baud Rate in bit/sec.
*              bFifoFlag - FIFO flag.
*                          True  - FIFO enabled.
*                          False - FIFO disabled.
*
* Return     : none.
*
* Note(s)    : Before calling this, CONSOL_Select needs to be called for selecting the consol
*              port.
*********************************************************************************************
*/
void CONSOL1_Init(U16 hwBaudVal, U8 bFifoFlag)
{
    __bU1FifoFlag = bFifoFlag;
    __bU1FifoCnt = 0;

    // set port pins for UART1
    rPINSEL0 = (rPINSEL0 & ~MSK_PINSEL0_U1) | VAL_PINSEL0_U1;

    /*  Init baudrate : 
     *    1) enable DL-registers
     *    2) set DLL + DLM divisor registers
     *    3) disable DL-registers                                
     */
    BIT_SET(rU1LCR, 0x80);
    rU1DLL = (U8)hwBaudVal;
    rU1DLM = (U8)(hwBaudVal>>8);
    BIT_CLR(rU1LCR, 0x80);

    /*  Init LCR: 
     *    1) 8-bit data
     *    2) 1 stop bit
     *    3) no parity                                           
     */
    rU1LCR = 0x03;

    if(bFifoFlag)
    {
        /*  Init FCR: 
         *    1) enable FIFO
         *    2) reset receiver FIFO
         *    3) reset transmitter FIFO
         *    4) set receive FIFO threshold to 4                     
         */
        rU1FCR = 0x47;
    }
    else
    {
        rU1FCR = 0x00;
    }

    /* Clear interrupt bits */
    rU1IER = 0x00;
}
#endif

/*
*********************************************************************************************
*                                       CONSOL_GetCh
*
* Description: This routine waits for a character from the CONSOL port & returns it.
*
* Arguments  : none.
*
* Return     : Returns the character read from the consol port.
*
* Note(s)    : 
*********************************************************************************************
*/

char CONSOL_GetCh(void)
{
    while(!(rU0LSR & (0x01<<0)));
    return rU0RBR;
}

/*
*********************************************************************************************
*                                       CONSOL_GetChar
*
* Description: This routine reads a character from the consol port if available.
*
* Arguments  : pbData - Pointer to return the received data.
*
* Return     : The status of the RX buffer.
*              True  - Data returned is valid.
*              False - Data returned is invalid.
*
* Note(s)    : 
*********************************************************************************************
*/

char CONSOL_GetChar(char * pbData)
{
    if((rU0LSR & (0x01<<0)) != 0x00)
    {
        *pbData = rU0RBR;
        return True;
    }
    return False;
}

#if (INCLUDE_CONSOL1 == 1)
/*
*********************************************************************************************
*                                       CONSOL1_GetCh
*
* Description: This routine waits for a character from the CONSOL1 port & returns it.
*
* Arguments  : none.
*
* Return     : Returns the character read from the consol port.
*
* Note(s)    : 
*********************************************************************************************
*/

char CONSOL1_GetCh(void)
{
    while(!(rU1LSR & (0x01<<0)));
    return rU1RBR;
}

/*
*********************************************************************************************
*                                       CONSOL1_GetChar
*
* Description: This routine reads a character from the consol1 port if available.
*
* Arguments  : pbData - Pointer to return the received data.
*
* Return     : The status of the RX buffer.
*              True  - Data returned is valid.
*              False - Data returned is invalid.
*
* Note(s)    : 
*********************************************************************************************
*/

char CONSOL1_GetChar(char * pbData)
{
    if((rU1LSR & (0x01<<0)) != 0x00)
    {
        *pbData = rU1RBR;
        return True;
    }
    return False;
}
#endif

/*
*********************************************************************************************
*                                       CONSOL_GetString
*
* Description: This routine waits for a string from the CONSOL port & returns it.
*
* Arguments  : pbString - Pointer to return the received string.
*
* Return     : none.
*
* Note(s)    : 
*********************************************************************************************
*/

void CONSOL_GetString(char *pbString)
{
    char *pbString2=pbString;
    char bC;

    while((bC=CONSOL_GetCh())!='\r')
    {
        if(bC=='\b')
        {
            if((int)pbString2<(int)pbString){CONSOL_SendString("\b \b");pbString--;}
        }
        else
        {
            *pbString++=bC;CONSOL_SendCh(bC);
        }
    }
    *pbString='\0';
    CONSOL_SendCh('\n');
}

/*
*********************************************************************************************
*                                       CONSOL_GetIntNum
*
* Description: This routine waits for a Integer from the CONSOL port & returns it.
*
* Arguments  : none.
*
* Return     : Returns the received Integer value.
*
* Note(s)    : 
*********************************************************************************************
*/

int CONSOL_GetIntNum(void)
{
    char abStr[30];
    char *pbString=abStr;
    int wBase=10;
    int wMinus=0;
    int wLastIndex;
    int wResult=0;
    int wI;
        
    CONSOL_GetString(pbString);
        
    if(pbString[0]=='-'){wMinus=1;pbString++;}

    if(pbString[0]=='0' && (pbString[1]=='x' || pbString[1]=='X'))
    {
        wBase=16;
        pbString+=2;
    }
        
    wLastIndex=strlen(pbString)-1;
    if( pbString[wLastIndex]=='h' || pbString[wLastIndex]=='H' )
    {
        wBase=16;
        pbString[wLastIndex]=0;
        wLastIndex--;
    }

    if(wBase==10)
    {
        wResult=atoi(pbString);
        wResult=wMinus ? (-1*wResult):wResult;
    }
    else
    {
        for(wI=0; wI<=wLastIndex; wI++)
        {
            if(__isalpha(pbString[wI]))
            {
                if(__isupper(pbString[wI]))

⌨️ 快捷键说明

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