📄 console_drv.c
字号:
/*
* $Id: console_drv.c,v 1.0 2003/02/21 00:11:04 csm Exp $
* $Copyright: (c) 2002, 2003 Broadcom Corp.
* All Rights Reserved.$
*/
/***********************************************************************
* iMpacct Technology Corporation. Copyright 2001 - 2100. *
* ALL RIGHTS RESERVED *
*----------------------------------------------------------------------*
* File : console_drv.c
* Creator : Gary Chen / iMpacct 11/5/2001
* Description:
* To provide common routines for console, including
* get/set configuration, read/write a character
*
* History:
*
************************************************************************/
#include <sys/sys_typedef.h>
#include <sys/sys_config.h>
#include <console/console_drv.h>
CONSOLE_PARM_T console_parm_data;
cyg_io_handle_t console_serial_handle;
cyg_io_handle_t console_tty_handle;
cyg_mutex_t console_smid;
void dprintf(char *fmt, ... );
/*--------------------------------------------------------------
* ROUTINE NAME - console_drv_init
*---------------------------------------------------------------
* To initialize the console port and set UART parameter to be:
* 19200,N,8,1,binary,non-block mode
*
* INPUT:
* None
*
* OUTPUT:
* 0 -- OK
* None 0 -- Failed
*
---------------------------------------------------------------*/
int console_drv_init(void)
{
cyg_io_handle_t handle;
Cyg_ErrNo err;
CONSOLE_PARM_T *parm=&console_parm_data;
cyg_mutex_init(&console_smid);
err = cyg_io_lookup(CONSOLE_SERIAL_NAME, &handle );
if (err) while (1) ;
console_serial_handle=handle;
err = cyg_io_lookup(CONSOLE_TTY_NAME, &handle );
if (err) while (1) ;
console_tty_handle=handle;
// set default UART parameters 38400,N,8,1,binary,non-block
parm->baud_rate = 38400;
parm->data_bits = CYGNUM_SERIAL_WORD_LENGTH_8;
parm->parity = CYGNUM_SERIAL_PARITY_NONE;
parm->stop_bits = CYGNUM_SERIAL_STOP_1;
parm->bin_mode = CONSOLE_BINARY_MODE;
parm->block_mode = CONSOLE_NONBLOCK_MODE;
parm->echo_mode = CONSOLE_NO_ECHO_MODE;
parm->nl_to_crlf = CONSOLE_NL2CRLF;
console_set_parm(parm);
return(0);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_scanchar
*---------------------------------------------------------------
* Scan the console port and read a character if got within 10ms
*
* INPUT:
* UI8_T *datap -- Ponits to data buffer
*
* OUTPUT:
* 0 -- OK
* 1 (CONSOLE_TIMEOUT)-- TIMEOUT
* others -- ERROR
---------------------------------------------------------------*/
UI8_T console_scanchar(char *datap)
{
Cyg_ErrNo err;
int len=1;
err=cyg_io_read(console_tty_handle,(void *)datap, &len);
if ((err==-EAGAIN) || (err==0 && len==0)) // timeout (-11)
{
cyg_thread_delay(1);
return(CONSOLE_TIMEOUT);
}
return(err);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_cli_scanchar
*---------------------------------------------------------------
* Scan the console port and read a character if got within 10ms
*
* INPUT:
* UI8_T *datap -- Ponits to data buffer
*
* OUTPUT:
* 0 -- OK
* 1 (CONSOLE_TIMEOUT)-- TIMEOUT
* others -- ERROR
---------------------------------------------------------------*/
UI8_T console_cli_scanchar(char *datap, CLICMD_SESSION_T *cli)
{
return(console_scanchar(datap));
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_getchar
*---------------------------------------------------------------
* This routine read one character from the uart console.
* The console driver will timeout every 1 second.
*
* INPUT:
* datap -- character to read from the uart driver
---------------------------------------------------------------*/
UI32_T console_getchar(UI8_T *datap)
{
UI32_T ticks, end, rc=1;
ticks=cyg_current_time();
end=ticks+TICKS_PER_SEC;
for (; ticks<=end && rc;)
{
rc=console_scanchar(datap);
if (rc)
ticks=cyg_current_time();
}
return rc;
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_putstr
*---------------------------------------------------------------
* Output a string to UART port
*
* INPUT:
* datap -- character string to send to the uart driver
---------------------------------------------------------------*/
UI32_T console_putstr(char *datap)
{
Cyg_ErrNo err;
int i, len;
int oldlen=strlen(datap);
cyg_serial_buf_info_t serial_buf;
cyg_mutex_lock(&console_smid);
len=oldlen;
err=cyg_io_write(console_tty_handle,(void *)datap, (int *)&len);
if (err!=0)
{
len = sizeof(cyg_serial_buf_info_t);
err = cyg_io_get_config(console_serial_handle,
CYG_IO_GET_CONFIG_SERIAL_BUFFER_INFO,
(void *)&serial_buf, &len);
if (serial_buf.tx_bufsize)
{
cyg_io_get_config(console_serial_handle,
CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN,
0, 0);
sys_sleep(ONE_SECOND/10);
}
}
cyg_mutex_unlock(&console_smid);
return(err);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_cli_putstr
*---------------------------------------------------------------
* Output a string to UART port
*
* INPUT:
* datap -- character string to send to the uart driver
---------------------------------------------------------------*/
UI32_T console_cli_putstr(char *datap, CLICMD_SESSION_T *cli)
{
return(console_putstr(datap));
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_output_flush
*---------------------------------------------------------------
* Flush all waiting character to console port
---------------------------------------------------------------*/
void console_output_flush()
{
Cyg_ErrNo rc;
cyg_mutex_lock(&console_smid);
rc = cyg_io_get_config(console_serial_handle, CYG_IO_GET_CONFIG_SERIAL_OUTPUT_DRAIN, 0, 0);
if (rc != ENOERR) {
dprintf("Can't get serial drain config - DEVIO error: %lu\n", rc);
while (rc);
}
cyg_mutex_unlock(&console_smid);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_get_baudrate
*---------------------------------------------------------------
* Get the current baudrate of UART driver.
---------------------------------------------------------------*/
UI32_T console_drv_Baudrate()
{
return(console_parm_data.baud_rate);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_set_baudrate
*---------------------------------------------------------------
* Set the desired baudrate
*
* INPUT:
* baudrate: desired baudrate to be set.
---------------------------------------------------------------*/
UI32_T console_set_baudrate(UI32_T baudrate)
{
if (baudrate==console_parm_data.baud_rate)
return(0);
if ((baudrate==9600) || (baudrate==19200) ||
(baudrate==38400)|| (baudrate==57600))
{
console_parm_data.baud_rate=baudrate;
console_set_parm();
return(0);
}
else
return(1);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_get_databits
*---------------------------------------------------------------
* Get the current uart driver data bits in use.
*
* RETURN:
* Data bits in use of the uart driver.
---------------------------------------------------------------*/
UI32_T console_get_databits()
{
return(console_parm_data.data_bits);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_set_databits
*---------------------------------------------------------------
* Set the desired data bits to the uart device.
*
* INPUT:
* databits: desired data bits to set.
---------------------------------------------------------------*/
UI32_T console_set_databits(UI32_T databits)
{
if (databits==console_parm_data.data_bits)
return(0);
if ((databits>=CONSOLE_WORD_LENGTH_5) ||
(databits<=CONSOLE_WORD_LENGTH_8))
{
console_parm_data.data_bits=databits;
console_set_parm();
return(0);
}
else
return(1);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_get_parity
*---------------------------------------------------------------
* Get the current uart driver parity in use.
*
* RETURN:
* Parity in use of the uart driver.
---------------------------------------------------------------*/
UI32_T console_get_parity()
{
return(console_parm_data.parity);
}
/*--------------------------------------------------------------
* ROUTINE NAME - console_set_parity
*---------------------------------------------------------------
* Set the desired parity to the uart device.
*
* INPUT:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -