📄 console.c
字号:
/***********************************************************************
* $Workfile: console.c $
* $Revision: 1.1 $
* $Author: TilburyC $
* $Date: Oct 14 2004 17:52:34 $
*
* Project: LH79524 EEPROM test
*
* Description: This file contains the basic eeprom tests
*
* Local Includes:
*
* Revision History:
* $Log:: $
*
* Rev 1.1 Oct 14 2004 17:52:34 TilburyC
* Fixed problem with reading eeprom at 400kbpS
*
* Rev 1.0 Oct 13 2004 16:58:52 TilburyC
* Initial revision.
*
*
***********************************************************************
*
* Copyright (c) 2004 Sharp Microelectronics of the Americas
*
* All rights reserved
*
* SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
* OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
* AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES,
* SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
*
* SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY
* FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A
* SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
* FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "sdk79524_board.h"
#include "lh79524_iocon.h"
#include "lh79524_gpio.h"
#include "console.h"
#include "lh79524_int_driver.h"
static INT_32 dev_uart;
/*
Implement all console functionality
*/
/**********************************************************************
*
* Function: init_console()
*
* Purpose:
* Initialize the UART 0 so that it can be used as a console for
* the purpose of recording the results of tests.
*
* Processing:
* See comments below.
*
* Parameters: None
*
* Outputs:
* UART configuration, VIC configuration, and IOCON regs.
*
* Returns:
* 0 upon success, otherwise retuns a non zero value that
* indicates an error has happened.
*
* Notes:
* 1. This funciton must be called before print_strg() or
* read_strg().
* 2. This function depends on having the VIC interface already
* open
*
*********************************************************************/
INT_32 init_console(void)
{
PFV handler;
/* Open UART */
/* UART0 default is 115200, 8N1 after open */
if ((dev_uart = uart_open(UART0_BASE, SDK79524_XTAL_IN)) == 0x0)
{
/* Error opening the device */
return 1;
}
uart_ioctl(dev_uart, UART_ENABLE_FIFO, 0);
uart_ioctl(dev_uart, UART_ENABLE_RX, 0);
uart_ioctl(dev_uart, UART_ENABLE_TX, 0);
uart_ioctl(dev_uart, UART_START, 0);
uart_ioctl(dev_uart, UART_SET_INT_MODE, 0);
uart_ioctl(dev_uart, UART_ENABLE_INT, // 1.7
(UART_INT_RX | UART_INT_TX | UART_INT_RT));
uart_ioctl(dev_uart, UART_SET_TX_FIFO_LVL, // 1.8
UART_FIFO_DEPTH_1_8);
uart_ioctl(dev_uart, UART_SET_RX_FIFO_LVL, // 1.8
UART_FIFO_DEPTH_1_8);
uart_ioctl(dev_uart, UART_ENABLE_INT,
(UART_INT_RX | UART_INT_TX | UART_INT_RT));
uart_ioctl(dev_uart, UART_GET_ISR, (INT_32)&handler);
/* instantiate the interrupt handler */
create_irq_interrupt(VIC_UARTINT0, VIC_VECT_0, handler);
IOCON->mux_ctl_5 &=
IOCON_MUX_MASK(IOCON_RES5_PA0_INT2_UARTRX2_UARTIRRX2); // 4.1
IOCON->mux_ctl_5 &=
IOCON_MUX_MASK(IOCON_RES5_PA1_INT3_UARTTX2_UARTIRTX2); // 4.1
IOCON->res_ctl_5 &=
IOCON_MUX_MASK(IOCON_RES5_PA0_INT2_UARTRX2_UARTIRRX2); // 4.2
IOCON->res_ctl_5 &=
IOCON_MUX_MASK(IOCON_RES5_PA1_INT3_UARTTX2_UARTIRTX2); // 4.2
return 0;
}
/**********************************************************************
*
* Function: run_menu()
*
* Purpose:
* Get input from a user on the console.
*
* Processing:
* Display the list and have the user type the number of his/her
* choice.
*
* Parameters:
* menu - list of menu data structures
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
* Depends on having called init_uart() before this.
*
*********************************************************************/
INT_32 run_menu(MENU_T *menu)
{
UNS_32 finished = 0;
CHAR buffer[80];
INT_32 cmd, max_cmd, i;
/* count the menu items */
for(max_cmd = 0; menu[max_cmd].strg != NULL; max_cmd++)
{ /* empty for() */; }
while(!finished)
{
/* display the menu */
print_strg("\r\n\r\nMenu:\r\n");
for(i = 0; menu[i].strg != NULL; i++)
{
print_strg("%3d. %s\r\n", i+1, menu[i].strg);
}
print_strg("\r\nEnter the number of your choice: ");
read_strg(buffer, sizeof(buffer));
cmd = atoi(buffer) - 1;
if(cmd < 0 || cmd > max_cmd)
print_strg("\r\nUnknown command\r\n");
else
finished = (*menu[cmd].func)();
}
return finished;
}
/**********************************************************************
*
* Function: print_strg()
*
* Purpose:
* Print a formatted string to the UART console.
*
* Processing:
* Format a string identical to printf().
*
* Parameters:
* fmt - format of the string, identical to printf()
* ... - variable parameters, like printf()
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
* Depends on having called init_uart() before this.
*
*********************************************************************/
void print_strg(char *fmt, ...)
{
va_list args;
char buffer[1024];
volatile unsigned int length;
va_start(args, fmt);
vsprintf(buffer, fmt, args);
va_end(args);
uart_write(dev_uart, buffer, strlen(buffer));
/* work-around where the text is occasionally displayed as a jumble */
#if 1
/* delay to allow buffer to empty */
length = strlen(buffer);
length *= 50; /* set this empirically */
do {
/* do nothing */
/* use a prime number to keep optimizer from using shifts */
length *= 7;
length /= 7;
/* pre-decrement to avoid divide by zero */
} while(--length);
#endif /* end w/a */
}
/**********************************************************************
*
* Function: read_strg()
*
* Purpose:
* Read a formatted string from the UART console. Implements
* basic line editing functionality, including backspace. Will
* read a maximum of (max_len - 1) characters before returning.
*
* Processing:
* Read a maximum of (max_len-1) characters into the buffer.
* Pressing the enter key indicates that the user is finihed
* editing the string. The string is retuned as edited.
* Pressing the ESC key indicates that the user wants to abort
* the edit and the string is not copied into the buffer. The
* backspace key causes the previous character to the cursor to
* be deleted and overwritten by the next character that is typed.
*
* Parameters:
* buffer - Character buffer supplied by the caller
* max_len - Maximum number of characters in the buffer
*
* Outputs: None
*
* Returns:
* The number of characters that were read from the console. If
* 0 is returned, it means that the edit has been aborted with the
* ESC key.
*
* Notes:
* Depends on having called init_uart() before this.
*
********************************************************************/
INT_32 read_strg(char *buffer, INT_32 max_len)
{
INT_32 index;
CHAR tbuf[2];
//INT_32 i;
memset(buffer, 0, max_len);
for(index = 0; index < (max_len - 1); /*index++*/)
{
// wait for some input
while(uart_read(dev_uart, tbuf, 1) == 0)
; /* empty while() */
tbuf[1] = 0;
switch(tbuf[0])
{
case 27: // abort input
memcpy(buffer, tbuf, 2);
return 0; // end this function
case '\r':
case '\n': // end of input
return index+1; // return actual number of char in the buf
case '\b': // backspace
index--;
if(index < 0)
index = 0;
buffer[index] = 0;
uart_write(dev_uart, "\b \b", 3);
break;
default: // add the character
memcpy(&buffer[index], tbuf, 2);
index++;
uart_write(dev_uart, tbuf, 1);
break;
}
}
// this happens if the buffer gets full
return index;
}
/**********************************************************************
*
* Function: get_number()
*
* Purpose:
* Parse a number from a string.
*
* Processing:
* Correct number format given as regular expression:
* 0x[0-9A-F] hex number
* 0b[0-1] binary number
* [-+ ][0-9] signed decimal number
*
* Parameters:
* strg - Character buffer supplied by the caller
* val - Pointer to place the result in
*
* Outputs: None
*
* Returns:
* Upon success return 0, else return a number indicating
* a parse failure.
*
* Notes:
* none
*
********************************************************************/
INT_32 get_number(CHAR *strg, void *val)
{
CHAR *tpt;
CHAR list[] = "0123456789ABCDEF";
INT_32 idx, count = 0;
if(strg == NULL || val == NULL)
return 1;
/* upcase the string to make it easier to parse */
for(tpt = strg; *tpt != 0; tpt++)
*tpt = toupper(*tpt);
tpt = strg;
if(*tpt == '0')
{
/* make it binary */
if(strg[1] == 'X')
{ /* parse a hex number */
tpt = &strg[2];
/* number of digits to convert */
count = strlen(tpt) - 1;
*((UNS_32 *)val) = 0;
for(/* assignment made previously */; *tpt != 0; tpt++)
{
/* find the character in the list */
for(idx = 0; *tpt != list[idx] && idx < 16; idx++)
; /* empty for() */
/* make sure it was actually in the list */
if(idx >= 16)
{
print_strg("\r\nError: incorrect hex number\r\n");
return 1;
}
/* index is binary value of digit */
*((UNS_32 *)val) |= idx << count * 4;
count--;
}
/* reach here, then val shoud be the binary number specified
in the string */
}
else if(strg[1] == 'B')
{ /* parse a binary number */
tpt = &strg[2];
/* number of digits to convert */
count = strlen(tpt) - 1;
*((UNS_32 *)val) = 0;
for(/* assignment made previously */; *tpt != 0; tpt++)
{
if(*tpt == '1')
*((UNS_32 *)val) |= 0x01 << count;
else if(*tpt != '0')
{ /* check for parse error */
print_strg("\r\nError: incorrect binary number\r\n");
return 1;
}
count--;
}
}
}
else if(!isdigit(*tpt))
{ /* if the first character in the string was not a digit,
then there is an error. */
print_strg("\r\nError: incorrect number format"
" (0x1A, 0b1101, -123)\r\n");
return 1;
}
else
{ /* parse a decimal number */
*((int *)val) = atoi(strg);
}
return 0;
}
/**********************************************************************
*
* Function: dump_buffer()
*
* Purpose:
* Display a buffer full of data "debug" style.
*
* Processing:
* See below
*
* Parameters:
* buffer - Character buffer supplied by the caller
* bufsize - Number of bytes to dump
*
* Outputs: None
*
* Returns:
* nothing
*
* Notes:
* none
*
********************************************************************/
void dump_buffer(CHAR *buffer, INT_32 bufsize, INT_32 st_addr)
{
UNS_32 i, j;
CHAR tbuf[2];
print_strg("\r\nDumping %d bytes; Hit a key to abort\r\n", bufsize);
for(i = 0; i < bufsize; /* empty */ )
{
print_strg("0x%04X ", i + st_addr);
for(j = 0; j < 8; j++)
{
if(i+j >= bufsize)
print_strg(" ");
else
print_strg(" %02X", buffer[i+j]);
}
print_strg(" -");
for( /* empty */ ; j < 16; j++)
{
if(i+j >= bufsize)
print_strg(" ");
else
print_strg(" %02X", buffer[i+j]);
}
print_strg(" ");
for(j = 0; j < 16; j++)
{
if(i+j >= bufsize)
break;
print_strg("%c",
((buffer[i+j] > ' ') && (buffer[i+j] < '~'))?
buffer[i+j]: '.');
}
print_strg("\r\n");
i += j;
if(uart_read(dev_uart, tbuf, 1) != 0)
{
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -