📄 sci.c
字号:
/**********************************************************************
File: sci.c
Purpose: The following example code illustrates the use of the M-CORE
Peripherals Library SCI_D Level 1 driver. It is setup to
operation on an M-CORE 2107 CMB board.
The program initializes the SCI and waits for terminal input.
It echoes each character received, and when a carriage return
is detected, the program transmits the entire preceeding line.
This program will continue running until either an escape
character or end-of-transmission (control-D) is received.
If an error occurs, the program will exit with the line
number of where the failure occurred.
Time to run:
About 30 seconds.
General Setup:
General hardware and software setup to run this application
can be found in the 'readme_app.txt' file, located in the
'<library>/app' directory.
Application Specific Setup:
Development Board: MMC2107 CMB or EVB
(C) Copyright Motorola Inc, 2000. All rights reserved.
**********************************************************************/
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include "sci_d.h"
/*********************************************************************
Macro Definitions
/*********************************************************************/
/* macro to handle error checking and response */
#define HANDLE_ERROR(err) if (err != SCI_D_ERR_NONE) return(__LINE__);
#define EOT 4 /* end-of-transmission character */
#define ESC 033 /* escape character */
#define STRBUF 255 /* size of input string buffer */
#define EOS '\0' /* end of string */
/*********************************************************************
Function Prototypes
/*********************************************************************/
static void out_string (pSCI_D_t, char *); /* function prototype */
static int check_string(pSCI_D_t SCIPtr, char * str);
void print2( char *str );
/* macro to handle error checking and response */
#define HANDLE_ERROR(err) if (err != SCI_D_ERR_NONE) return(__LINE__);
#define EOT 4 /* end-of-transmission character */
#define ESC 033 /* escape character */
#define STRBUF 255 /* size of input string buffer */
#define EOS '\0' /* end of string */
int echo_SCI( void )
{
pSCI_D_t SCIPtr = (pSCI_D_t)__MMC2107_SCI2;
/*------------------------------------------------------------------*/
/* declare local variables */
/*------------------------------------------------------------------*/
/* return codes */
SCI_D_ReturnCode_t status = SCI_D_ERR_NONE; /* return value */
/* for reception */
UINT16 receiveVal; /* value to receive */
UINT16 * receivePtr = &receiveVal; /* pointer to receiveVal */
SCI_D_ReceiverStatus_t statusVal;
pSCI_D_ReceiverStatus_t statusPtr = &statusVal;/* status structure */
/* for transmission */
SCI_D_Boolean_t transmitDataPending; /* status boolean */
SCI_D_Boolean_t * transmitDataPendingPtr = &transmitDataPending;
/* line buffer */
static char lineBuffer[STRBUF]; /* line buffer */
char * lineBufferPtr = lineBuffer; /* pointer to line buffer*/
SCI_D_Boolean_t readyFlag = SCI_D_FALSE; /* flag for while loop */
/*------------------------------------------------------------------*/
/* transmit welcome message */
/*------------------------------------------------------------------*/
out_string(SCIPtr, "\r\n\nSCI Echo Program ready...\r\n\n");
/*------------------------------------------------------------------*/
/* Loop forever, echoing input */
/*------------------------------------------------------------------*/
for(;;)
{
/*------------------------------------------------------------------*/
/* Wait for a character to be received */
/*------------------------------------------------------------------*/
while (readyFlag == SCI_D_FALSE)
{
/* try to receive */
status = SCI_D_Receive ( SCIPtr, /* SCI base address */
receivePtr,/* pointer to return val */
statusPtr /* structure for status */
);
/* If an error code is returned, exit the program */
HANDLE_ERROR(status);
/* check reception status */
if ((statusPtr->receiveDataPending == SCI_D_TRUE) ||
(statusPtr->dataOverrun == SCI_D_TRUE) ||
(statusPtr->noiseDetected == SCI_D_TRUE) ||
(statusPtr->framingError == SCI_D_TRUE) ||
(statusPtr->parityError == SCI_D_TRUE) ||
(statusPtr->receiverActive == SCI_D_TRUE))
{
/* Did not successfully receive data, */
/* keep trying to receive */
}
else
{
/* successful reception, continue */
readyFlag = SCI_D_TRUE;
} /* end if-else */
} /* end while loop */
readyFlag = SCI_D_FALSE; /* reset readyFlag */
/*------------------------------------------------------------------*/
/* End program if a CTRL-D or ESC character was received */
/*------------------------------------------------------------------*/
if ((receiveVal == EOT) || (receiveVal == ESC))
{
out_string(SCIPtr, "\n\nGoodbye!\r\n\n");
/* disable transmitter and receiver */
/* Call SCI_D_ControlOperation with valid parameters */
status = SCI_D_ControlOperation(
SCIPtr, /* SCI base address */
SCI_D_DISABLE,/* disable transmitter */
SCI_D_DISABLE /* disable receiver */
);
/* break out of loop and go to end of function */
break;
} /* end if */
/*------------------------------------------------------------------*/
/* Copy character to line echo buffer */
/*------------------------------------------------------------------*/
*lineBufferPtr++ = (char)receiveVal;
/*------------------------------------------------------------------*/
/* Echo the character back through the transmitter */
/*------------------------------------------------------------------*/
status = SCI_D_Transmit( SCIPtr,
receiveVal,
transmitDataPendingPtr
);
/*------------------------------------------------------------------*/
/* If a carriage return character was received, echo the buffered */
/* line and reset line buffer. */
/*------------------------------------------------------------------*/
if (receiveVal == '\r')
{
out_string (SCIPtr, "\n"); /* echo line feed */
if (lineBufferPtr != lineBuffer + 1) /* non-empty line */
{
*lineBufferPtr++ = '\n'; /* add line feed to buffer */
*lineBufferPtr++ = EOS; /* add end of string char */
out_string(SCIPtr, lineBuffer);
}
lineBufferPtr = lineBuffer; /* reset buffer pointer */
} /* end if */
} /* end of for loop */
out_string(SCIPtr, "Bye!\r\n");
return (status);
}
int mon_SCI( int rc )
{
pSCI_D_t SCIPtr = (pSCI_D_t)__MMC2107_SCI2;
/*------------------------------------------------------------------*/
/* declare local variables */
/*------------------------------------------------------------------*/
/* return codes */
SCI_D_ReturnCode_t status = SCI_D_ERR_NONE; /* return value */
/* for reception */
UINT16 receiveVal; /* value to receive */
UINT16 * receivePtr = &receiveVal; /* pointer to receiveVal */
SCI_D_ReceiverStatus_t statusVal;
pSCI_D_ReceiverStatus_t statusPtr = &statusVal;/* status structure */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -