📄 env_tty.c
字号:
/************************************************************************
*
* env_tty.c
*
* TTY specific parts of ENV module
*
*
* ######################################################################
*
* Copyright (c) 1999-2000 MIPS Technologies, Inc. All rights reserved.
*
* Unpublished rights reserved under the Copyright Laws of the United States of
* America.
*
* This document contains information that is proprietary to MIPS Technologies,
* Inc. ("MIPS Technologies"). Any copying, modifying or use of this information
* (in whole or in part) which is not expressly permitted in writing by MIPS
* Technologies or a contractually-authorized third party is strictly
* prohibited. At a minimum, this information is protected under unfair
* competition laws and the expression of the information contained herein is
* protected under federal copyright laws. Violations thereof may result in
* criminal penalties and fines.
* MIPS Technologies or any contractually-authorized third party reserves the
* right to change the information contained in this document to improve
* function, design or otherwise. MIPS Technologies does not assume any
* liability arising out of the application or use of this information. Any
* license under patent rights or any other intellectual property rights owned
* by MIPS Technologies or third parties shall be conveyed by MIPS Technologies
* or any contractually-authorized third party in a separate license agreement
* between the parties.
* The information contained in this document constitutes one or more of the
* following: commercial computer software, commercial computer software
* documentation or other commercial items. If the user of this information, or
* any related documentation of any kind, including related technical data or
* manuals, is an agency, department, or other entity of the United States
* government ("Government"), the use, duplication, reproduction, release,
* modification, disclosure, or transfer of this information, or any related
* documentation of any kind, is restricted in accordance with Federal
* Acquisition Regulation 12.212 for civilian agencies and Defense Federal
* Acquisition Regulation Supplement 227.7202 for military agencies. The use of
* this information by the Government is further restricted in accordance with
* the terms of the license agreement(s) and/or applicable contract terms and
* conditions covering this information from MIPS Technologies or any
* contractually-authorized third party.
*
************************************************************************/
/************************************************************************
* Include files
************************************************************************/
#include <sysdefs.h>
#include <sys_api.h>
#include <syscon_api.h>
#include <serial_api.h>
#include <env_api.h>
#include <string.h>
#include <stdio.h>
/************************************************************************
* Definitions
************************************************************************/
typedef struct
{
UINT8 number;
char *s;
}
t_num_string_pair;
/************************************************************************
* Public variables
************************************************************************/
/************************************************************************
* Static variables
************************************************************************/
/* Default values of environment variables */
static char *default_modetty = DEFAULT_SERIAL_ENV;
static char *default_bootserport = DEFAULT_BOOTPORT_ENV;
/* Mappings between enumerated values and strings describing serial
* port parameters.
*/
static t_num_string_pair baudrate_num2s_array[] =
{
{ SERIAL_BAUDRATE_075_BPS, "75" },
{ SERIAL_BAUDRATE_110_BPS, "110" },
{ SERIAL_BAUDRATE_150_BPS, "150" },
{ SERIAL_BAUDRATE_300_BPS, "300" },
{ SERIAL_BAUDRATE_600_BPS, "600" },
{ SERIAL_BAUDRATE_1200_BPS, "1200" },
{ SERIAL_BAUDRATE_1800_BPS, "1800" },
{ SERIAL_BAUDRATE_2400_BPS, "2400" },
{ SERIAL_BAUDRATE_4800_BPS, "4800" },
{ SERIAL_BAUDRATE_7200_BPS, "7200" },
{ SERIAL_BAUDRATE_9600_BPS, "9600" },
{ SERIAL_BAUDRATE_14400_BPS, "14400" },
{ SERIAL_BAUDRATE_19200_BPS, "19200" },
{ SERIAL_BAUDRATE_38400_BPS, "38400" },
{ SERIAL_BAUDRATE_57600_BPS, "57600" },
{ SERIAL_BAUDRATE_115200_BPS, "115200" },
{ SERIAL_BAUDRATE_230400_BPS, "230400" },
{ SERIAL_BAUDRATE_460800_BPS, "460800" }
#if 0
/* Not supported */
{ SERIAL_BAUDRATE_921600_BPS, "921600" }
#endif
};
#define BAUDRATE_NUM2S_COUNT \
(sizeof(baudrate_num2s_array) / sizeof(t_num_string_pair))
static t_num_string_pair databits_num2s_array[] =
{
{ SERIAL_DATABITS_7, "7" },
{ SERIAL_DATABITS_8, "8" }
};
#define DATABITS_NUM2S_COUNT \
(sizeof(databits_num2s_array) / sizeof(t_num_string_pair))
static t_num_string_pair parity_num2s_array[] =
{
{ SERIAL_PARITY_NONE, "n" },
{ SERIAL_PARITY_ODD, "o" },
{ SERIAL_PARITY_EVEN, "e" }
};
#define PARITY_NUM2S_COUNT \
(sizeof(parity_num2s_array) / sizeof(t_num_string_pair))
static t_num_string_pair flowctrl_num2s_array[] =
{
{ SERIAL_FLOWCTRL_HARDWARE, "hw" },
/* Not supported yet */
{ SERIAL_FLOWCTRL_NONE, "none" },
{ SERIAL_FLOWCTRL_XON_XOFF, "xonxoff" }
};
#define FLOWCTRL_NUM2S_COUNT \
(sizeof(flowctrl_num2s_array) / sizeof(t_num_string_pair))
static t_num_string_pair stopbits_num2s_array[] =
{
{ SERIAL_STOPBITS_10, "1" },
{ SERIAL_STOPBITS_20, "2" }
#if 0
/* Not supported */
{ SERIAL_STOPBITS_15, "1.5" }
#endif
};
#define STOPBITS_NUM2S_COUNT \
(sizeof(stopbits_num2s_array) / sizeof(t_num_string_pair))
/************************************************************************
* Static function prototypes
************************************************************************/
static bool
search_s2num(
char *raw,
void *decoded,
UINT32 size,
t_num_string_pair *pairs,
UINT32 count );
static bool
search_num2s(
UINT8 param,
char **s,
t_num_string_pair *pairs,
UINT32 count );
static bool
modetty_s2num(
bool verify_baud,
UINT32 verify_id,
char *raw,
void *decoded,
UINT32 size );
static bool
modetty_s2num0(
char *raw,
void *decoded,
UINT32 size );
static bool
modetty_s2num1(
char *raw,
void *decoded,
UINT32 size );
static void
register_initial( void );
/************************************************************************
* Implementation : Static functions
************************************************************************/
/************************************************************************
*
* search_s2num
* Description :
* -------------
*
* Search for string and return corresponding enumerated value (if
* buffer is not NULL and size matches).
*
* Return values :
* ---------------
*
* TRUE if string is found, else FALSE
*
************************************************************************/
static bool
search_s2num(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size, /* Size of decoded data */
t_num_string_pair *pairs, /* Array holding the str/number pairs */
UINT32 count ) /* Array size */
{
UINT32 i;
if( !raw ) return FALSE;
for( i=0;
(i < count) &&
(strcmp( raw, pairs[i].s ) != 0);
i++ );
if( i == count ) return FALSE;
if( decoded )
{
if( size != sizeof(UINT8) ) return FALSE;
*(UINT8 *)decoded = pairs[i].number;
}
return TRUE;
}
/************************************************************************
*
* search_num2s
* Description :
* -------------
*
* Search for enumerated value and return corresponding string.
*
* Return values :
* ---------------
*
* TRUE if value is found, else FALSE
*
************************************************************************/
static bool
search_num2s(
UINT8 param, /* Numeric value of parameter */
char **s, /* env. string representing data */
t_num_string_pair *pairs, /* Array holding the str/number pairs */
UINT32 count ) /* Array size */
{
UINT32 i;
if( !s ) return FALSE;
for( i=0;
(i < count) &&
(param != pairs[i].number);
i++ );
if( i == count ) return FALSE;
*s = pairs[i].s;
return TRUE;
}
/************************************************************************
*
* modetty_s2num/
* modetty_s2num0/
* modetty_s2num1/
* Description :
* -------------
*
* Convert string describing serial port configuration (environment
* variable format) to UINT32 containing the coded values (if buffer
* for this is available and format is correct)
*
* modetty_s2num0/1 are the functions registered for TTY0 and TTY1.
* They call upon the common function modetty_s2num in order to
* perform the operation. Only difference is the SYSCON object used
* to verify the baudrate
*
* Return values :
* ---------------
*
* TRUE if conversion is possible, else FALSE
*
************************************************************************/
static bool
modetty_s2num0(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
return modetty_s2num( TRUE,
SYSCON_COM_TTY0_BAUDRATE_VERIFY_ID,
raw,
decoded,
size );
}
static bool
modetty_s2num1(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
return modetty_s2num( TRUE,
SYSCON_COM_TTY1_BAUDRATE_VERIFY_ID,
raw,
decoded,
size );
}
static bool
modetty_s2num(
bool verify_baud, /* TRUE -> Verify baudrate */
UINT32 verify_id, /* SYSCON ID used to verify baudrate */
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
#define NUMTOKENS 5
char *myarg[NUMTOKENS];
char myraw[24]; /* "921600,n,8,1,xonxoff" (max) */
char c;
int n;
char *p;
UINT8 baudrate, parity, databits, stopbits, flowctrl;
/* Check that we have the string */
if( !raw )
return FALSE;
/* Check size of variable for decoded data */
if( decoded && (size != sizeof(UINT32)) )
return FALSE;
if( strlen(raw) > sizeof(myraw)-1 )
return FALSE;
/* make a local copy of string data */
strcpy(myraw, raw);
/* Get exactly NUMTOKENS ','-separated tokens from the ascii input */
myarg[0] = myraw; /* first token is easy */
for (n=0, p = myraw; c= *p; p++)
{
if (c == ',')
{
if (n == NUMTOKENS-1) return FALSE; /* too many tokens */
*p = '\0'; /* token end */
myarg[++n] = p+1; /* next token */
}
}
if (n != NUMTOKENS-1) return FALSE; /* too few tokens */
/* Check validity of the 4 tokens and extract environment value
* for each.
*/
if( !env_baudrate_s2num( myarg[0], &baudrate, sizeof(UINT8)) )
return FALSE;
/* Port specific check.
* If the following write operation fails, the baudrate is
* not supported.
*/
if( verify_baud &&
(SYSCON_write( verify_id, &baudrate, sizeof(UINT8) ) != OK) )
{
return FALSE;
}
if( !env_parity_s2num( myarg[1], &parity, sizeof(UINT8)) )
return FALSE;
if( !env_databits_s2num( myarg[2], &databits, sizeof(UINT8)) )
return FALSE;
if( !env_stopbits_s2num( myarg[3], &stopbits, sizeof(UINT8)) )
return FALSE;
if( !env_flowctrl_s2num( myarg[4], &flowctrl, sizeof(UINT8)) )
return FALSE;
if (decoded)
{
/* Construct environment value (byte) */
*(UINT32 *)decoded =
baudrate |
(parity<<8) |
(databits<<16) |
((stopbits & 0x0f)<<24) |
((flowctrl & 0x0f)<<28);
}
return TRUE;
#undef NUMTOKENS
}
/************************************************************************
*
* register_initial
* Description :
* -------------
*
* Determine initial serial port settings and register these with SYSCON
*
* Return values :
* ---------------
*
* None
*
************************************************************************/
static void
register_initial( void )
{
UINT8 baudrate0, parity0, databits0, stopbits0, flowctrl0;
UINT8 baudrate1, parity1, databits1, stopbits1, flowctrl1;
char *raw;
bool init_from_env;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -