⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 srecord.c

📁 56f8300E系列dsp的BOOTloader
💻 C
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************

	COPYRIGHT (c) 2003 MOTOROLA INC.
	ALL RIGHTS RESERVED

	This code is the property of Motorola Sector Technology and is Motorola 
	Confidential Proprietary Information.

	The copyright notice above does not evidence any actual or intended
	publication of such source code.
			
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <ctype.h>
#include <dos.h>
#include <time.h>
#include <math.h>
#include <windows.h>
#include <process.h>

#include <nican.h>		/* NI-CAN header file */
#include "srecord.h"

#define CL_KEYVAL_CHECK if ( keystr[2] != ':' ) {	res = FALSE; break; }

/* NI-CAN attribute IDs */
#define CL_READ_QLEN_ID 2

#define BAUD_RATE_PLACE 0
#define START_ON_OPEN_PLACE 1
#define READ_Q_LEN_PLACE 2
#define WRITE_Q_LEN_PLACE 3
#define TIMESTAMPING_PLACE 4
#define CAN_COMP_STD_PLACE 5
#define CAN_MASK_STD_PLACE 6
#define CAN_COMP_XTD_PLACE 7
#define CAN_MASK_XTD_PLACE 8

#define NUM_ATTR 9


static char Error_Code[_MAX_PATH][_MAX_PATH] = 
{
	"Receive error",
	"Character error",
	"Check sum error",
	"Format error",
	"Overrun error",
	"Flash error",
	"Internal error",
	"Reserved error",
	"Protected boot section error",
	"Flash secured error",
	"Flash protected error",
	"CAN error",
	"Low voltage error",
};

#define NUM_ERROR 13

static char Warning_Code[] = "Data in RAM or BootFlash were ignored!";

#define NUM_WARNING 100


/*static NCTYPE_ATTRID clAttrID[] = 
{ 
	NC_ATTR_BAUD_RATE,
	NC_ATTR_START_ON_OPEN,
	NC_ATTR_READ_Q_LEN,
	NC_ATTR_WRITE_Q_LEN,
	NC_ATTR_TIMESTAMPING,
	NC_ATTR_CAN_COMP_STD,
	NC_ATTR_CAN_MASK_STD,
	NC_ATTR_CAN_COMP_XTD,
	NC_ATTR_CAN_MASK_XTD
};
*/
//static NCTYPE_UINT32 clAttrValue[] = 
//{
//	CL_BAUD_RATE,	/* default baud rate */
//	NC_TRUE,		/* Start communication as soon as the CAN Object of this network interface port is opened */
//	CL_READ_QLEN_1,	/* Read CAN queue length */
//	CL_WRITE_QLEN,	/* Write CAN queue length */
//	NC_TRUE,		/* Time stamp enable */
//	0,				/* Set the standard and extended comparators and masks such that */
//	NC_CAN_MASK_STD_DONTCARE,	/* all CAN data frames are received by the CAN Network Interface Object */
//	0,
//	NC_CAN_MASK_XTD_DONTCARE
//};


/* Number of attributes */
//#define CL_N_ATTR ( sizeof(clAttrID) / sizeof(clAttrID[0]) )

static NCTYPE_ATTRID clAttrID[_MAX_PATH] ;
static NCTYPE_UINT32 clAttrValue[_MAX_PATH];

static char sRecordFileName[_MAX_PATH ];			/* File names */
static char logFileName[_MAX_PATH ];				
static unsigned long idCANRead;				
static unsigned long idCANWrite;	

BOOL logFile;
static char nameLoad [_MAX_PATH ];			/* File names */

static int timeout;			
static FILE *logOut, *sRecFile;						/* File handlers */
static int cntRecord = 0;
static int cntData = 0;
static int cntLine = 0;
static int dataSum = 0;
int wSysTimeCur, wSysTimeLast;
int newL = 0;
char *szBuffer;
BOOL lastLine;

//SCI

HANDLE  idComDev ;
static int vBaudrate;
static char NameCOMPort[5];				
//static char menuItem[1];				
static BOOL flagError = FALSE;

/* CAN Object handles */
static NCTYPE_OBJH clPortObjh_CAN0;
static NCTYPE_OBJH clPortObjh_CAN1;
static unsigned long sRecCount = 0;


//NCTYPE_CAN_FRAME_READ Receive;


/*	Private variables.	*/

static	char 	_copyright[] = 
"S-Record RS232/CAN Loader - Version: 1.0\n\
Copyright (c) Motorola, Inc. 2003. All rights reserved.\n";

static const char clUsage[] = "\n";

/*
static	char	_help[] =
"\nUsage: sRecLoad [-options] \n\
Options:\n\
	-i:<Interface> = The name of interface\n\n\
    CAN options:\n\n\
	-s:<S-Record> = S-Record file\n\
	-b:<CAN bound> = CAN baudrate in Kbit/s (default = 500000)\n\
	-r:<ID> = CAN ID for reading (no default)\n\
	-w:<ID> = CAN ID for writing (no default)\n\
	-l:<Logfile> = Log filename (optional)\n\
	-t:<timeout> = timeout (Default = 10s)\n\n\
    SCI options:\n\n\
	-s:<S-Record> = S-Record file\n\
	-b:<bound> = baudrate in Kbit/s (default = 115200)\n\
	-m:<menu item> = bootloader menu item (optional)\n\
	-l:<Logfile> = Log filename (optional)\n\
	-t:<timeout> = timeout (Default = 1s)\n\
";
*/


static	char	_help[] =
"\nUsage: sRecLoad [-options] \n\
Options:\n\
    -i:<Interface>     - the name of interface for download\n\
                         Serial or CAN device name should be used\n\
                         (COM1, COM2, CAN0, CAN1 ...)\n\n\
    -s:<S-Record file> - the S-Record file name\n\
    -l:<logfile>       - the logfile name to log utility operations\n\
    -b:<baudrate>      - the baudrate value defines SCI or CAN speed\n\
                         to communicate with bootloader in bit/s\n\
                         The default value for CAN is 500000 bit/s and\n\
                         for COM(rs232) is 115200 bit/s\n\
    -r:<CAN ID>        - the host request CAN ID to transmit S-record\n\
    -a:<CAN ID>        - bootloader reply (acknowledgement) CAN ID\n\
";


/*
static	char	_help[] =
"\nUsage: sRecLoad [-options] \n\
Options:\n\
	-i:<Interface> - the name of interface for download\n\ 
					 Serial or CAN device name should be used (COM1, COM2, CAN0, CAN1 ...)\n\n\
	-s:<S-Record file> - the S-Record file name\n\
	-l:<logfile> - the logfile name to log utility operations\n\
	-s:<S-Record> = S-Record file\n\
	-b:<baudrate> - the baudrate value defines SCI or CAN speed to communicate with bootloader in bit/s\n\
					The default value for CAN is 500000 bit/s and for COM(rs232) is 115200 bit/s\n\
	-r:<CAN ID> - the host request CAN ID to transmit S-record\n\
	-a:<CAN ID> - bootloader reply (acknowledgement) CAN ID\n\
";
*/

/*
-i:<Interface> - the name of interface for download. Serial or CAN device name should be used (COM1, COM2, CAN0, CAN1 ...);
-l:<logfile> - the logfile name to log utility operations;
-b:<baudrate> - the baudrate value defines SCI or CAN speed to communicate with bootloader in bit/s. The default value for CAN is 500000 bit/s and for COM(rs232) is 115200 bit/s
-r:<CAN ID> - the host request CAN ID to transmit S-record.
-a:<CAN ID> - bootloader reply (acknowledgement) CAN ID.

*/


//	-m:<menu item> = bootloader menu item (optional)\n\


/* Private functions prototypes	*/

static	int     parse_opt(char* keystr);		
static	void	help( void );
static	void	title( void );
static int clCANOpen(const char* portname, NCTYPE_OBJH_P porth);
static int writeData(char* buf);
static int clPrintStat(NCTYPE_STATUS status, char* err_source, FILE *out);
static void SetAttribute ();
static long GetFileSizeStream( FILE  *Stream );


//SCI

BOOL SetupConnection();
BOOL OpenConnection();
BOOL CloseConnection();
BOOL WriteCommBlock( LPSTR lpByte , DWORD dwBytesToWrite);
int ReadCommBlock( LPSTR lpszBlock, int nMaxLength );


/* macros */

/* Return with "1" code and print message if "rc" is NULL */
#define CL_NULL_ERRMSG(rc,api) if ( rc == NULL ) { fprintf(stderr, "%s: error code = %d\n", api, GetLastError()); return(1); }
/* Return with "rc" code if "rc" is non-zero */
#define CL_NONZERO_ERR(rc) if ( rc != 0 ) { return( rc ); }
/* Return with "rc" code and print message if "rc" is non-zero */
#define CL_NONZERO_ERRMSG(rc,api) if ( rc != 0 ) { fprintf(stderr, "%s: error code = %d\n", api, rc); return(rc); }
/* Set "a" to zero */
#define CL_MEMSETZ(a) memset(&a, 0, sizeof(a))
/* Check ':' presents for command line parsing */
#define CL_KEYVAL_CHECK if ( keystr[2] != ':' ) {	res = FALSE; break; }

/* shift for CAN ID transform */
#define CL_ADDR_SHIFT		5
/* adjust internal CAN ID presentation for external operations */
#define CL_ADDR_INT2EXT(addr) (addr << CL_ADDR_SHIFT)
/* adjust external CAN ID to internal presentation */
#define CL_ADDR_EXT2INT(addr) (addr >> CL_ADDR_SHIFT)




/*	Functions.	*/


/*
 *	 help - output help 
 */
static	void	help( void )	{ printf( _help ); }
/*
 *	 title - output program title 
 */
static	void 	title( void )	{ printf( _copyright ); }

/***************************************************************************
 * Function : clPrintStat
 *
 * Description: Prints a description of an NI-CAN error/warning
 *
 * Returns: 1 when state "error" occur;
 *			0 otherwise.
 *
 * Notes:
 *
 **************************************************************************/
static int clPrintStat(NCTYPE_STATUS status, char* err_source, FILE *out) 
{
	char statusString[512];
	int res;
		 
	res = 0;
	if (status != 0) 
	{
		ncStatusToString(status, sizeof(statusString), statusString);
		fprintf(out, "\n%s\nSource = %s\n", statusString, err_source);
		if (logFile)
			fprintf(logOut, "\n%s\nSource = %s\n", statusString, err_source);

		if (status < 0)
		{
			res = 1;
		};
	};
	return(res);
}


/***************************************************************************
 * Function : clCANOpen
 *
 * Description:	Configure the CAN Network Interface Objects and open its
 *
 * Returns: 0, if all OK;
 *			1 otherwise.
 *
 * Notes:
 *
 **************************************************************************/
static int clCANOpen(const char* portname, NCTYPE_OBJH_P porth)
{
	int res;
	NCTYPE_STATUS           Status;
 
	/* Configure the CAN Network Interface Objects */
	
	Status = ncConfig((NCTYPE_STRING)portname, NUM_ATTR, clAttrID, clAttrValue);
	res = clPrintStat(Status, "ncConfig", stderr);
	CL_NONZERO_ERR(res);

	/* Open the object */
	Status = ncOpenObject((NCTYPE_STRING)portname, porth);
	res = clPrintStat(Status, "ncOpenObject", stderr);
	CL_NONZERO_ERR(res);

	return(0);
}

/***************************************************************************
 * Function : writeData
 *
 * Description: Write data to NI-CAN 
 *
 * Returns: 1 when state "error" occur;
 *			0 otherwise.
 *
 * Notes:
 *
 **************************************************************************/
static int writeData( char* data ) 
{
	int res, i = 0;
	NCTYPE_CAN_FRAME		Transmit;
	char* p = data;
    NCTYPE_STATUS           Status;
	
	newL = 0;
	 
	res = 1;
	
	for (i = 0; i <= 7; i++)
	{
		if(*p != ' ' && *p != '\n' && *p != '\t' && *p != '\r')
		{
			Transmit.Data[i] = *p;
			if (*p == 'S' || *p == 's') //it is the new line
			{
				newL = 1;
				if (*(p + 1) != '0' && *(p + 1) != '7') //it is the line with data
					dataSum = 1;
				else
					dataSum = 0;

				if (*(p + 1) == '7') //it is the last line 
					lastLine = TRUE;
					
			}

			if (dataSum == 1)
				cntData++;	
			p++;
			szBuffer = p;

		}
		else
		{
			p++;
			szBuffer = p;
			break;
		}

	}

	if (i > 0 )
	{
		if (newL == 1)
		{
			cntLine ++;
//			printf(".");
		}

		if (lastLine)
			newL = 1;

		Transmit.ArbitrationId = idCANWrite;
		Transmit.IsRemote = 0;
		Transmit.DataLength = i;
		Status= ncWrite(clPortObjh_CAN0, sizeof(NCTYPE_CAN_FRAME), &Transmit);
		wSysTimeLast = GetTickCount();

		res = clPrintStat(Status, "ncWrite", stderr);
		CL_NONZERO_ERR(res);

		cntRecord =  cntRecord + Transmit.DataLength;
/*		if (logFile)
		{
			fprintf(logOut, "\nWritting Data: ");

			for (i = 0; i < Transmit.DataLength; i++)
			{
				fprintf(logOut, "%02x ", Transmit.Data[i]);
			}
			fprintf(logOut, " ID %08x ", Transmit.ArbitrationId);
			fprintf(logOut, " %i ms ", wSysTimeLast - wSysTimeCur);
 			fprintf(logOut, "\n");
		}
*/
		wSysTimeCur = wSysTimeLast;

	}

	return(res);
}

/***************************************************************************
 * Function : SetAttribute
 *
 * Description: Set Attribute
 *
 * Returns: none
 *			
 *
 * Notes:
 *
 **************************************************************************/
static void SetAttribute ()
{
	clAttrID[BAUD_RATE_PLACE] = NC_ATTR_BAUD_RATE;
	clAttrValue[BAUD_RATE_PLACE] = CL_BAUD_RATE;

	clAttrID[START_ON_OPEN_PLACE] = NC_ATTR_START_ON_OPEN;
	clAttrValue[START_ON_OPEN_PLACE] = NC_TRUE;

	clAttrID[READ_Q_LEN_PLACE] = NC_ATTR_READ_Q_LEN;
	clAttrValue[READ_Q_LEN_PLACE] = CL_READ_QLEN_1;

	clAttrID[WRITE_Q_LEN_PLACE] = NC_ATTR_WRITE_Q_LEN;
	clAttrValue[WRITE_Q_LEN_PLACE] = CL_WRITE_QLEN;

	clAttrID[TIMESTAMPING_PLACE] = NC_ATTR_TIMESTAMPING;
	clAttrValue[TIMESTAMPING_PLACE] = NC_TRUE;

	clAttrID[CAN_COMP_STD_PLACE] = NC_ATTR_CAN_COMP_STD;
	clAttrValue[CAN_COMP_STD_PLACE] = 0;

	clAttrID[CAN_MASK_STD_PLACE] = NC_ATTR_CAN_MASK_STD;
	clAttrValue[CAN_MASK_STD_PLACE] = NC_CAN_MASK_STD_DONTCARE;

	clAttrID[CAN_COMP_XTD_PLACE] = NC_ATTR_CAN_COMP_XTD;
	clAttrValue[CAN_COMP_XTD_PLACE] = 0;

	clAttrID[CAN_MASK_XTD_PLACE] = NC_ATTR_CAN_MASK_XTD;
	clAttrValue[CAN_MASK_XTD_PLACE] = NC_CAN_MASK_XTD_DONTCARE;

	return;
}


int	main( int argc, char *argv[], char *envp[] )
{
	int     res = 0, i,j;
	int     ReadCount = 0;
	char buf[2 * _MAX_PATH];
	int FirstTime = 0;
	char *pTemp;
	int  TempError;
    NCTYPE_STATUS           Status;
    NCTYPE_STATE            State;
 	NCTYPE_CAN_FRAME_TIMED		Receive;
	NCTYPE_CAN_BOOTLOADER_FRAME * BootlReceive;
	SYSTEMTIME wSysTime;
	int wSysTimeStart, wSysTimeEnd;
	int wSysTimeSt, wSysTimeE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -