📄 io_merge.c
字号:
/***************************************************************************
* This code and information is provided "as is" without warranty of any *
* kind, either expressed or implied, including but not limited to the *
* implied warranties of merchantability and/or fitness for a particular *
* purpose. *
* *
* Copyright (C) 2005 Teridian SemiConductor, Corp. All Rights Reserved. *
* *
***************************************************************************/
//**************************************************************************
//
// DESCRIPTION: 71M652x POWER METER - IO Merge Utility.
//
// AUTHOR: MTF/RGV
//
// HISTORY: 2003 NOVEMBER 24; First Version.
// 2004 JANUARY 16; Prevent CE from turning ON.
// 2004 MARCH 18; Added I1=.. and I3=.. commands.
// 2005 AUGUST 19; Added MPU parameters.
// 2005 AUGUST 20; Added Serial Port (including 'echo') and DIO commands.
//
//**************************************************************************
//
// File: IO_MERGE.C
//
/*
File: io_merge.c
---- io_merge ver 0.3 March 18, 2004 ----
io_merge old.hex io.txt new.hex
Merges Intel hex files "old.hex" w/ "io.txt" file.
Outputs: Merged Intel hex file new.hex
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/timeb.h>
typedef unsigned char Bbool;
typedef unsigned char U08;
typedef unsigned short U16;
typedef unsigned long U32;
typedef signed char S08;
typedef signed short S16;
typedef signed long S32;
#define MAX_ROM_KSIZE 64
unsigned char Rom[ MAX_ROM_KSIZE * 1024 ];
/*** Public functions declared within this module ***/
S32 get_long (void); // Convert ascii decimal (or hex) long to binary number.
S32 get_long_decimal (U08 c); // Convert ascii decimal long to binary number.
U32 get_long_hex (void); // Convert ascii hexdecimal long to binary number.
S16 get_short (void); // Convert ascii decimal (or hex) short to binary number.
S16 get_short_decimal (void); // Convert ascii decimal short to binary number.
U16 get_short_hex (void); // Convert ascii hexdecimal short to binary number.
S08 get_num (void); // Convert ascii decimal (or hex) number to binary number.
S08 get_num_decimal (void); // Convert ascii decimal byte to binary number.
U08 get_num_hex (void); // Convert ascii hexdecimal byte to binary number.
U08 get_digit (U08 *d); // Get next decimal (or hex) digit from CLI buffer.
U08 get_char_d (U08 *d); // Get next character from CLI buffer and allow unget.
U08 get_char (void); // Get next character from CLI buffer.
/*** Public variables declared within this module ***/
// None.
/*** Private variables used within this module ***/
#define CLI_BASE 0
#define CLI_BUFF_SIZE 256 // 256 byte CLI buffer, no wrap;
U08 cli_buff[ CLI_BUFF_SIZE ];
U08 cli_index;
U08 cli_result;
#define OK_ID 1
#define ERROR_ID 2
#define CASE_ 0x20
#define CE_CODE 0x1000 // Start address of CE_CODE image.
#define CE_CODE_SIZE 0x1000 // 4K bytes.
#define IO_DATA 0x0900 // Start address of IO_DATA image.
#define MPU_DATA (IO_DATA + 0x31) // Start address of MPU r_Parms data
#define DEMO_VERSION (IO_DATA + 0x78) // Start address of MPU r_demo_version
#define CE_DATA 0x0A00 // Start address of CE_DATA image.
#define CE_DATA_SIZE 0x0200 // 512 bytes.
#define SELECT_TOTAL (IO_DATA + 0x1F)
#define SELECT_OUTPUT (IO_DATA + 0x20)
#define SELECT_RMS (IO_DATA + 0x21)
#define TEMPERATURE (IO_DATA + 0x22) // Alternate MUX sequence each XFER cycle.
#define SELECT_PORT (IO_DATA + 0x23)
#define RESERVED (IO_DATA + 0x24)
#define DIO21_16_DIR (IO_DATA + 0x25)
#define DIO15_08_DIR (IO_DATA + 0x26)
#define DIO07_00_DIR (IO_DATA + 0x27)
#define DIO21_16_VAL (IO_DATA + 0x28)
#define DIO15_08_VAL (IO_DATA + 0x2A)
#define DIO07_00_VAL (IO_DATA + 0x2B)
#define serial_rate_0 (IO_DATA + 0x2C)
#define SERIAL_PARM0 (IO_DATA + 0x2D)
#define serial_rate_1 (IO_DATA + 0x2E)
#define SERIAL_PARM1 (IO_DATA + 0x2F)
#define ECHO 0x10
#define PARITY_NONE 0x08
#define PARITY_ODD 0x04
#define STOP_BIT2 0x02
#define XON_XOFF 0x01
enum SERIAL_PORT { SERIAL0, SERIAL1 };
enum SERIAL_SPD { _RATE_300, _RATE_600, _RATE_1200, _RATE_2400, _RATE_4800, _RATE_9600, _RATE_19200, _RATE_38400 };
U08 dio2bit[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
/*** Private functions declared within this module ***/
static void cmd_ce_data_access (void); // ']' CE data access.
static void cmd_mpu_data_access (void); // ')' MPU data access.
static void cmd_sfr (void); // 'R' Special Function (R)egisters.
static void cmd_sfr_xdata (void); // 'RI' IO data access.
static void cmd_serial (void);
static void cmd_width_parity (void); // '8' Serial port width, parity, stop bit controls.
static void cmd_rate (void);
static void cmd_meter (void);
static void cmd_io (void); // 'O' User I(O) controls.
static void cmd_io_pin (enum USER_PIN pin); // 'OP' I(O) (P)in controls.
static Bbool done (U08 *c);
static Bbool cli (void);
/*performs fopen on fname with type. If result is NULL, exits with error*/
FILE *fopenorelse (char *fname, char *type)
{
FILE *fn;
fn = fopen (fname, type);
if (fn == NULL)
{
printf ("Could not open '%s'.\n", fname);
exit(0);
}
return(fn);
}
void Error (char *filename, int err, int ln)
{
if (ln)
fprintf (stderr, "\nERROR %d at line %d in %s\n", err, ln, filename);
else
fprintf (stderr, "\nUsage: io_merge old_demo.hex io.txt new_demo.hex\n");
exit(1);
}
int main (int argc, char * argv[])
{
unsigned int AddrH, AddrL, RecTyp, Byte;
unsigned int Addr, AddrMax, i, ByteCnt, ln, LnCSRd;
unsigned char LnCSCal;
FILE *in_api, *in_io; // Input files.
FILE *out_api; // Output file.
char *versionstring="ver 2.0";
char *in_api_name, *in_io_name, *out_api_name;
char *cptr;
if (argc != 4) Error ("Arguments", 1, 0);
in_api_name = argv[1];
in_io_name = argv[2];
out_api_name = argv[3];
printf ("\n---io_merge %s%s---\n", versionstring, __DATE__);
printf ("Beginning merge of DEMO w/ new IO data\n");
cptr = &in_api_name[ strlen (in_api_name) - 4 ];
if (strcmp (cptr, ".hex"))
{
printf("Error: %s file must end with .hex suffix\n", in_api_name);
exit(0);
}
cptr = &in_io_name[ strlen (in_io_name) - 4 ];
if (strcmp (cptr, ".txt"))
{
printf("Error: %s file must end with .txt suffix\n", in_io_name);
exit(0);
}
cptr = &out_api_name[ strlen (out_api_name) - 4 ];
if (strcmp (cptr, ".hex"))
{
printf("Error: %s file must end with .hex suffix\n", out_api_name);
exit(0);
}
in_api = fopenorelse (in_api_name, "r");
in_io = fopenorelse (in_io_name, "r");
out_api = fopenorelse (out_api_name, "w");
/* Initially fill Rom with FFs */
for (i = 0; i < sizeof (Rom); i++)
Rom[ i ] = 0xFF;
/* Read the input LAPIE hex file */
AddrMax = 0;
ln = 1;
do
{
i = fscanf (in_api, ":%2x%2x%2x%2x", &ByteCnt, &AddrH, &AddrL, &RecTyp);
if (i != 4)
Error (in_api_name, 1, ln);
if (RecTyp > 1)
Error (in_api_name, 2, ln);
Addr = (AddrH << 8) + AddrL;
LnCSCal = ByteCnt + AddrH + AddrL + RecTyp;
for (i = 0; i < ByteCnt; i++)
{
if (fscanf (in_api, "%2x", &Byte) != 1)
Error (in_api_name, 4, ln);
if (Addr < sizeof (Rom)) /* Ignore higher addresses. */
Rom[ Addr++ ] = Byte;
LnCSCal += Byte;
}
if (Addr > AddrMax)
AddrMax = (Addr - 1);
if (fscanf (in_api, "%2x\n", &LnCSRd) != 1)
Error (in_api_name, 5, ln);
LnCSCal += LnCSRd;
if (LnCSCal)
{
printf ("%2x %2x %2x %2x %2x %2x\n", ByteCnt, AddrH, AddrL, RecTyp, LnCSRd, LnCSCal);
Error (in_api_name, 6, ln);
}
ln++;
} while (RecTyp != 1);
printf ("Read thru line# %d in file %s\n", ln - 1, in_api_name);
/* Read the input IO data file */
ln = 1;
while (fgets (cli_buff, 120, in_io))
{
if (!cli ())
Error (in_io_name, 5, ln);
ln++;
}
printf ("Read thru line# %d in file %s\n", ln - 1, in_io_name);
ln = 1;
/* Output new hex file for whole Rom */
for (Addr = 0; Addr <= AddrMax;)
{
fprintf (out_api, ":20%4.4X00", Addr);
LnCSCal = 0x20 + (Addr >> 8) + (Addr & 0xFF);
for (i = 0; i < 0x20; i++)
{
fprintf (out_api, "%2.2X", Rom[ Addr ]);
LnCSCal += Rom[ Addr++ ];
}
LnCSCal = 0x100 - LnCSCal;
fprintf (out_api, "%2.2X\n", LnCSCal);
ln++;
}
fprintf (out_api, ":00000001FF\n");
printf ("Wrote thru line# %d in file %s\n", ln, out_api_name);
fclose (in_api);
fclose (in_io);
fclose (out_api);
printf ("Completed merge of DEMO w/ new IO data\n");
return (0);
}
// Exit control.
Bbool done (U08 *c)
{
U08 d;
*c = get_char_d (&d);
if (*c == '\0')
cli_index = d; // Unget last character.
return (*c == '\0' || *c == ';');
}
Bbool cli (void) // Command Line Interpreter.
{
U08 c, *vPtr;
cli_result = 0; // Default result code is NULL.
while (!cli_result && (!done (&c)) )
{ // Command processing.
switch (toupper (c))
{
case '/': // Comment, ignore.
case 'C':
cli_result = OK_ID;
break;
case ' ': // Update Serial port bit rate.
cmd_rate ();
break;
case '8':
cmd_width_parity (); // '8' Serial port width, parity, stop bit controls.
break;
case ']': // ']' CE Data Access.
cmd_ce_data_access ();
break;
case ')': // ')' MPU Data Access.
cmd_mpu_data_access ();
break;
case 'A':
Rom[ TEMPERATURE ] = get_num ();
break;
case 'I':
if (!done (&c))
{
if ('=' == c)
{
vPtr = &Rom[ DEMO_VERSION ];
while (!done(&c))
{
if (isprint(c))
*vPtr++ = c;
}
*vPtr = '\0';
}
}
break;
case 'M': // 'M' (M)eter controls.
cmd_meter ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -