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

📄 stdinout.c

📁 优龙YLP270开发板 光盘自带的BIOS和实验例程源码 强烈推荐
💻 C
字号:
/******************************************************************************
**
**  COPYRIGHT (C) 2000, 2001 Intel Corporation.
**
**  This software as well as the software described in it is furnished under 
**  license and may only be used or copied in accordance with the terms of the 
**  license. The information in this file is furnished for informational use 
**  only, is subject to change without notice, and should not be construed as 
**  a commitment by Intel Corporation. Intel Corporation assumes no 
**  responsibility or liability for any errors or inaccuracies that may appear 
**  in this document or any software that may be provided in association with 
**  this document. 
**  Except as permitted by such license, no part of this document may be 
**  reproduced, stored in a retrieval system, or transmitted in any form or by 
**  any means without the express written consent of Intel Corporation. 
**
**  FILENAME:       stdinout.c
**
**  PURPOSE:        This file contains an implementation of the standard input
**                  input/output functions.
**
**  LAST MODIFIED:  $Modtime: 7/17/03 1:01p $
******************************************************************************/

/*
*******************************************************************************
*   HEADER FILES
*******************************************************************************
*/
#include <stdio.h>
#include <stdarg.h>
#include "systypes.h"
#include "dm_errors.h"
#include "xsuart.h"
#include "xsstuart.h"
#include "xsbtuart.h"
#include "xsffuart.h"
#include "DM_Serialinout.h"
#include "TstROS.h"
#include "boardControl.h"
#include "xslcdcontroller.h"

/*
*******************************************************************************
*   EXTERNAL DECLARATION
*******************************************************************************
*/
extern void doPrint(char * buffer, char * fmt, va_list ap);

/*
*******************************************************************************
*   GLOBAL DEFINITIONS
*******************************************************************************
*/
BOOL RunOnce = TRUE;
BOOL DmRunInteractive = TRUE;
BOOL PostRunInteractive = FALSE;
BOOL gMenuEnabled = TRUE;

// Make Printf work.
struct __FILE {
    int handle;
};
FILE __stdout;

/*
*******************************************************************************
*   LOCAL DECLARATION
*******************************************************************************
*/
#define CMD_STRING_SIZE     64
//static UartContextT * ctxP = (UartContextT *)GetPointerToSystemUart();
//static UartContextT * ctxP = &FFUart;

/*
*******************************************************************************
*
* FUNCTION:
*    fputc
*
* DESCRIPTION:
*    This function is called by either the ADS Run-Time-Library to output a
*    character via printf or by the write routine used when building with
*    the Nordheim toolchain.
*
* INPUT PARAMETERS:
*    int ch - character to output
*    FILE *f - file pointer
*
* RETURNS:
*    The character outputted.
*
* GLOBAL EFFECTS:
*    None.
*
* ASSUMPTIONS:
*    None.
*
* CALLS:
*    RKickit, GetPointerToSystemUart, writeUartFnP, ReadUserSwitches, XsLcdChar
*
* CALLED BY:
*    ADS Run-Time-Library of the write routine provided when using the Nordheim
*    toolchain.
*
* PROTOTYPE:
*    int fputc(int ch, FILE *f);
*
*******************************************************************************
*/
int fputc(int ch, FILE *f)
{
	static char chs;
	UartContextT * ctxP;

    RKickit();
	ctxP = GetPointerToSystemUart();
    chs = ch;
    ctxP->writeUartFnP(ctxP, &chs, 1);
    
    // A virtual switch can set this. No physical switch available
    if ((POSTRunState() & VS_LCD_TEXT_ON) == VS_LCD_TEXT_ON)
      XsLcdChar(chs);
    return ch;
}

#ifdef NDT_TOOLS
/*
*******************************************************************************
*
* FUNCTION:
*    _isdev
*
* DESCRIPTION:
*    This routine overrides the function in the Run-Time-Library provided with
*    the Nordheim toolchain. Its purpose is to disable buffered i/o when the
*    target device is stdout.
*
* INPUT PARAMETERS:
*    int fd - file descriptor
*
* RETURNS:
*    Returns true if the file descriptor is for stdout.
*
* GLOBAL EFFECTS:
*    When the file descriptor is for stdout, all output to stdout will be
*    unbuffered i/o to allow strings to be displayed without being terminated
*    with <cr><lf>.
*
* ASSUMPTIONS:
*    None.
*
* CALLS:
*    None.
*
* CALLED BY:
*    Nordheim Run-Time-Library.
*
* PROTOTYPE:
*    int _isdev(int fd);
*
*******************************************************************************
*/
int _isdev(int fd)
{
    return (fd == 1);
}

/*
*******************************************************************************
*
* FUNCTION:
*    write
*
* DESCRIPTION:
*    This routine replaces the write routine supplied with the Nordheim
*    Run-Time-Library. This routine will send the specified string out the
*    UART.
*
* INPUT PARAMETERS:
*    int fd - file descriptor, 1 = stdout
*    const void *buf - pointer to the text string.
*    unsigned int nbyte - number of bytes to output.
*
* RETURNS:
*    Number of bytes outputted.
*
* GLOBAL EFFECTS:
*    None.
*
* ASSUMPTIONS:
*    None.
*
* CALLS:
*    fputc
*
* CALLED BY:
*    Nordheim Run-Time-Library.
*
* PROTOTYPE:
*    int write(int fd, const void *buf, unsigned int nbyte);
*
*******************************************************************************
*/
int write(int fd, const void *buf, unsigned int nbyte)
{
	int i;
	for(i=0; i<nbyte; i++)
	{
		fputc(*(char *)((unsigned int)buf+i),0);
	}

    return nbyte;
}
#endif

int ferror(FILE *f)
{
    return 0;
}

// Flush the UART FIFO.
void DM_FlushKey(void)
{
    UartContextT * ctxP;
	ctxP = GetPointerToSystemUart();

    ctxP->clearRxUartFnP(ctxP);
//    SkPs2FlushRx(&SkPs2Ctxt);
}

// Get a hex value from the keyboard.
static
int GetKey(void)
{
    int key;
    UartContextT * ctxP;
	ctxP = GetPointerToSystemUart();

    ctxP->clearRxUartFnP(ctxP);

    while (ctxP->readUartFnP(ctxP, (char *)&key, 1) == 0) ;
    RKickit();

    // Translate the key
    key &= 0xff;
    if ((key >= '0' && key <= '9') || (key >= 'A' && key <= 'F') ||
        ( key == 0x08) || (key == 0x0d)) {
        return key;
    }
    else if (key >= 'a' && key <= 'f')
    {
        return key & ~0x20;
    }

    // We didn't translate the key.
    return 0xe;
}

/*
*******************************************************************************
*
* FUNCTION:
*    DM_GetKey
*
* DESCRIPTION:
*    Get a key from the serial port if currently enabled by the state of
*    DmRunInteractive.
*
* INPUT PARAMETERS:
*    None.
*
* RETURNS:
*    Hex value 0-F.
*
* GLOBAL EFFECTS:
*    None.
*
* ASSUMPTIONS:
*    The state of DmRunInteractive controls whether this routine waits for a
*    key from the user or returns a canned response for non-interactive mode.
*
* CALLS:
*    GetKey.
*
* CALLED BY:
*    POST infrastructure routines. Tests should use POST_GetKey.
*
* PROTOTYPE:
*    int DM_GetKey(void);
*
*******************************************************************************
*/
int DM_GetKey(void)
{
    // Check if we should wait for a key.
    if (!DmRunInteractive)
    {
        // No, act as if we didn't translate a key.
        return 0xe;
    }

    return (GetKey());
}

/*
*******************************************************************************
*
* FUNCTION:
*    POST_GetKey
*
* DESCRIPTION:
*    Get a key from the serial port if currently enabled by the state of
*    PostRunInteractive.
*
* INPUT PARAMETERS:
*    None.
*
* RETURNS:
*    Hex value 0-F.
*
* GLOBAL EFFECTS:
*    None.
*
* ASSUMPTIONS:
*    The state of PostRunInteractive controls whether this routine waits for a
*    key from the user or returns a canned response for non-interactive mode.
*
* CALLS:
*    GetKey.
*
* CALLED BY:
*    POST test routines. POST infrastructure routines should use DM_GetKey.
*
* PROTOTYPE:
*    int POST_GetKey(void);
*
*******************************************************************************
*/
int POST_GetKey(void)
{
    // Check if we should wait for a key.
    if (!PostRunInteractive)
    {
        // No, act as if we didn't translate a key.
        return 0xe;
    }

    return (GetKey());
}

/*----------------------------------------------------------------------
 * Test to see if a key has been pressed on the serial input device.
 */
static int keyPressedSerial(void)
{
    int kp = 0;
    char buff[CMD_STRING_SIZE];
	UartContextT * ctxP;
	ctxP = GetPointerToSystemUart();

    if (ctxP->readUartFnP(ctxP, buff, 1))
        kp = 1;
    return kp;
}

/*----------------------------------------------------------------------
 * Test to see if a key has been pressed on the current input device.
 * Check both the keyboard and pointer device.
 */
int DM_KeyPressed(void)
{
    int kp = 0;

//    if (TS_GetPenStatus() == TS_PENDOWN) kp = 1;
//    if (!SkPs2KeyboardLocked && SkPs2GetAscii() != EOF) kp++;
    kp += keyPressedSerial();
    return kp;
}

void DM_AnyKeyToContinue(char *s)
{
    // Display string
    printf("\r\n%s", s);

    // get a character from user
    DM_GetKey();
}

int DM_StopTest(char * s)
{
    int stop = 1;    // Assume non-stop action.
    
    // Check RunOnce flag.
    if (!RunOnce)
    {
        // Get stop flag based a response from the user.
        stop = DM_KeyPressed();

        // Check the response.
        if (stop)
        {
            // Throw away response.
            DM_GetKey();
        }
    }

    // Return stop flag.
    return stop;
}

void DM_Error(char *s)
{
    printf(s);
    DM_GetKey();
}

void DM_Warning(char *s)
{
    printf(s);
}

void DM_ErrPrintf(char * fmt, ...)
{
    char buffer[256];
    va_list ap;
    va_start(ap,fmt);

    doPrint(buffer,fmt,ap);
    DM_Error(buffer);
}

void DM_WarnPrintf(char * fmt, ...)
{
    char buffer[256];
    va_list ap;
    va_start(ap,fmt);

    doPrint(buffer,fmt,ap);
    printf(buffer);
}

⌨️ 快捷键说明

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