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

📄 bsldemo.c

📁 MSP430操作TUSB3410,USB接口芯片的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
			}
		
		linelen= strlen(strdata);
		
		if (strdata[0] == '@')
			/* if @ => new address => send frame and set new addr. */
			{ 
			if (dataframelen > 0)
				{ 
				error= programBlk(currentAddr, dataframelen, action);
				byteCtr+= dataframelen; /* Byte Counter */
				dataframelen=0; 
				}
			sscanf(&strdata[1], "%lx\n", &currentAddr);
			continue; 
			}
		
		/* Transfer data in line into blkout: */
		for(linepos= 0; 
		linepos < linelen-3; linepos+= 3, dataframelen++)
			{ 
			sscanf(&strdata[linepos], "%3x", &blkout[dataframelen]);
			/* (Max 16 bytes per line!) */
			}
		
		if (dataframelen > maxData-16)	
			/* if frame is getting full => send frame */
			{ 
			error= programBlk(currentAddr, dataframelen, action);
			byteCtr+= dataframelen; /* Byte Counter */
			currentAddr+= dataframelen; 
			dataframelen=0;
			
			/* bargraph: indicates succession, actualize only when changed. FRGR */
			KBytes = (byteCtr+512)/1024;
			if (KBytesbefore != KBytes)
				{
				KBytesbefore = KBytes;		
				printf("\r%02d KByte ", KBytes);
				printf("\xDE");
				for (i=0;i<KBytes;i+=1) printf("\xB2");
				printf("\xDD");
				}
			}
		
		if (error != ERR_NONE) 
			{
			break;	/* FRGR */
			}
		}
	/* clear bargraph, go to left margin */
	printf("\r \r");
	
	fclose(infile);
	
	return(error);
	} /* programTIText */

int txPasswd(char* passwdFile)
	{
	int i;
	
	if (passwdFile == NULL)
		{
		/* Send "standard" password to get access to protected functions. */
		printf("Transmit standard password...\n");
		/* Fill blkout with 0xff 
		* (Flash is completely erased, the contents of all Flash cells is 0xff)
		*/
		for (i= 0; i < 0x20; i++)
			{ 
			blkout[i]= 0xff; 
			}
		return(bslTxRx(BSL_TXPWORD, /* Command: Transmit Password  */
			0xffe0, 	            /* Address of interupt vectors */
			0x0020, 	            /* Number of bytes             */
			blkout, blkin));
		}
	else
		{
		/* Send TI TXT file holding interrupt vector data as password: */
		printf("Transmit PSW file \"%s\"...\n", passwdFile);
		return(programTIText(passwdFile, ACTION_PASSWD));
		}
	} /* txPasswd */

void WaitForKey() /* FRGR */
	{
	printf("----------------------------------------------------------- ");
	printf("Press any key ... "); getch(); printf("\n");
	}

int signOff(int error, BOOL passwd)
	{
	if (toDo.MSP430X) error= bslTxRx(BSL_MEMOFFSET, 0, (WORD)(0), blkout, blkin);

	if (toDo.Reset)
		{
		bslReset(0); /* Reset MSP430 and start user program. */
		}
	
	switch (error)
		{
		case ERR_NONE:
			if (toDo.Program) printf("Programming completed.");
				else if (toDo.Verify)printf("Verification successful.");
			printf("Prog/Verify: %.1f sec",(float)(Time_BSL_stops-Time_PRG_starts)/1000.0);
			printf(" - Over all: %.1f sec\n",(float)(Time_BSL_stops-Time_BSL_starts)/1000.0);
			break;
		case ERR_BSL_SYNC: 
			printf("ERROR: Synchronization failed!\n");
			printf("Device with boot loader connected?\n");
			break;
		case ERR_VERIFY_FAILED: 
			printf("ERROR: Verification failed!\n");
			break;
		case ERR_ERASE_CHECK_FAILED: 
			printf("ERROR: Erase check failed!\n");
			break;
		case ERR_FILE_OPEN: 
			printf("ERROR: Unable to open input file \"%s\"!\n", (char*)errData); 
			break;
		default: 
			if ((passwd) && (error == ERR_RX_NAK))
				/* If last command == transmit password && Error: */
				printf("ERROR: Password not accepted!\n");
			else
				printf("ERROR: Communication Error!\n");
		} /* switch */
	
	if (toDo.Wait)
		{
		WaitForKey();
		}
	
	comDone();	/* Release serial communication port.	*/
				/* After having released the serial port,
				 * the target is no longer supplied via this port!
	             */
	if (error == ERR_NONE)
		return(0);
	else
		return(1);
	} /* signOff */


void showHelp()
	{
	char *help[]=
		{
		"BSLDEMO [-h][-c{port}][-s{num}][-p{file}][-w][-1][-m{num}][+ecpvruw] {file}",
			"",
			/*
			"The last parameter is required: file name of TI-TXT file to be programmed.",
			"",
			*/
			"Options:",
			"-h       Shows this help screen.",
			"-c{port} Specifies the communication port to be used (e.g. -cCOM2).",
#ifdef WORKAROUND
			"-a{file} Filename of workaround patch (e.g. -aWAROUND.TXT).",
#endif
			"-b{file} Filename of complete loader to be loaded into RAM (e.g. -bBSL.TXT).",
			"-e{startnum}",
			"         Erase Segment where address does point to.",
			/*
			"-f{num}  Max. number of data bytes within one transmitted frame (e.g. -f240).",
			*/
#ifdef ADD_MERASE_CYCLES
			"-m{num}  Number of mass erase cycles (e.g. -m20).",
#endif /* ADD_MERASE_CYCLES */
			"-p{file} Specifies a TI-TXT file with the interrupt vectors that are",
			"         used as password (e.g. -pINT_VECT.TXT).",
			"-r{startnum} {lennum} {file}",
			"         Read memory from startnum till lennum and write to file as TI.TXT.",
			"         (Values in hex format.) ",
			"-s{num}  Changes the baudrate; num=0:9600, 1:19200, 2:38400 (e.g. -s2).",
			"-w       Waits for <ENTER> before closing serial port.",
			"-x       Enable MSP430X Extended Memory support.",
			"-1       Programming and verification is done in one pass through the file.",
			"",
			"Program Flow Specifiers [+aecpvruw]",
			" a       Restore InfoA after mass erase (only with Mass Erase)",
			" e       Mass Erase",
			" c       Erase Check by file {file}",
			" p       Program file {file}",
			" v       Verify by file {file}",
			" r       Reset connected MSP430. Starts application.",
			" u       User Called - Skip entry Sequence.",
			" w       Wait for <ENTER> before closing serial port.",
			" Only the specified actions are executed!",
			"",
			"Default Program Flow Specifiers (if not explicitly given): +ecpvr",
			"",
			"\x1B " /* Marks end of help! <ESC>*/
		};
	
	int i= 0;
	
	while (*help[i] != 0x1B) 
		printf("%s\n", help[i++]);
	printf("Press any key to exit... ");
	getch();
	}


/*-------------------------------------------------------------
* Parse Command Line Parameters ...
*-------------------------------------------------------------
*/
int parseCMDLine(int argc, char *argv[])
{
   /* Default: all actions turned on: */
   toDo.MassErase = 1;
   toDo.EraseCheck= 1;
   toDo.FastCheck = 0;
   toDo.Program = 1;
   toDo.Verify= 1;
   toDo.Reset   = 1;
   toDo.SpeedUp = 0; /* Change Baudrate */
   toDo.UserCalled= 0;
   toDo.Wait   = 0;  /* Do not wait for <Enter> at the end! */
   toDo.OnePass = 0; /* Do erase check, program and verify*/
   toDo.BSLStart= 1;
   toDo.Dump2file = 0;
   toDo.EraseSegment = 0;
   toDo.MSP430X = 0;
   toDo.RestoreInfoA = 0;

   filename   = NULL;
   passwdFile = NULL;
   patchFile = patchFilename;


   if (argc > 1) 
      {
      for (i= 1; i < (argc); i++)
         {
         switch (argv[i][0])
            { 
            case '-':
               switch (argv[i][1])
                  {
                  case 'h': case 'H':
                     showHelp(); /* Show help screen and */
                     return(1);   /* exit program.      */
                     break;
                     
                  case 'c': case 'C':
                     strcpy(comPortName, "\\\\.\\");  /* Required by Windows */
                     memcpy(&comPortName[4], &argv[i][2], strlen(argv[i])-2);
                     break;
                     
                  case 'p': case 'P':
                     strcpy(passwdFilename, &argv[i][2]);
                     passwdFile = passwdFilename;
                     break;
                     
                  case 'w': case 'W':
                     toDo.Wait= 1; /* Do wait for <Enter> at the end! */
                     break;
                     
                  case '1':
                     toDo.OnePass= 1;
                     break;
                     
                  case 's': case 'S':
                     if ((argv[i][2] >= '0') && (argv[i][2] <= '9'))
                        {
                        speed = argv[i][2] - 0x30;   /* convert ASCII to number */
                        toDo.SpeedUp= 1;
                        }
                     break;
                     
                  case 'f': case 'F':
                     if (argv[i][2] != 0)
                        {
                        sscanf(&argv[i][2], "%i", &maxData);
                        /* Make sure that conditions for maxData are met: 
                        * ( >= 16 and == n*16 and <= MAX_DATA_BYTES!)
                        */
                        maxData= (maxData > MAX_DATA_BYTES) ? MAX_DATA_BYTES : maxData;
                        maxData= (maxData <          16) ?         16 : maxData;
                        maxData= maxData - (maxData % 16);
                        printf("Max. number of data bytes within one frame set to %i.\n", 
                           maxData);
                        }
                     break;
                     
#ifdef ADD_MERASE_CYCLES
                  case 'm': case 'M':
                     if (argv[i][2] != 0)
                        {
                        sscanf(&argv[i][2], "%i", &meraseCycles);
                        meraseCycles= (meraseCycles < 1) ? 1 : meraseCycles;
                        printf("Number of mass erase cycles set to %i.\n", meraseCycles);
                        }
                     break;
#endif /* ADD_MERASE_CYCLES */
                     
#ifdef WORKAROUND
                  case 'a': case 'A':
                     strcpy (patchFilename ,&argv[i][2]);
                            patchFile=patchFilename;
                     break;
#endif /* WORKAROUND */
                     
#ifdef NEW_BSL
                  case 'b': case 'B':
                     strcpy (newBSLFilename ,&argv[i][2]);
                            newBSLFile=newBSLFilename;
                     break;
#endif /* NEW_BSL */
                  case 'r': case 'R':
                     toDo.MassErase = 0;
                     toDo.EraseCheck= 0;
                     toDo.FastCheck = 0;
                     toDo.Program = 0;
                     toDo.Verify= 0;

                     toDo.Dump2file = 1;
                     sscanf(&argv[i][2], "%X", &readStart);
                     i++;
                     sscanf(&argv[i][0], "%X", &readLen);
                     i++;
                     readfilename = &argv[i][0];
                     break;
                  case 'e': case 'E':
                     toDo.MassErase = 0;
                     toDo.EraseCheck= 0;
                     toDo.FastCheck = 0;
                     toDo.Program = 0;
                     toDo.Verify= 0;
                     toDo.Reset   = 0;
                     toDo.UserCalled= 0;
                     toDo.BSLStart= 1;
                     toDo.Dump2file = 0;

                     toDo.EraseSegment = 1;
                     sscanf(&argv[i][2], "%X", &readStart);
                     i++;
                     break;
                  case 'x': case 'X':
                     toDo.MSP430X = 1;
                     break;

                  default:
                     printf("ERROR: Illegal command line parameter!\n");
                  } /* switch argv[i][1] */
               break; /* '-' */
               
            case '+':
                     /* Turn all actions off: */
                     toDo.MassErase = 0;
                     toDo.EraseCheck= 0;
                     toDo.FastCheck = 0;
                     toDo.Program = 0;
                     toDo.Verify= 0;
                     toDo.Reset   = 0;
                     toDo.UserCalled= 0;
                     toDo.BSLStart= 1;
					 toDo.RestoreInfoA = 0;

                     /* Turn only specified actions back on:             */
                     for (j= 1; j < (int)(strlen(argv[i])); j++)
                        {
                        switch (argv[i][j])
                           {
                           case 'a': case 'A': 
                              /* Restore InfoA Segment              */
							  toDo.RestoreInfoA = 1;
                           case 'e': case 'E': 
                              /* Erase Flash                        */
                              toDo.MassErase = 1;
                              break;
                           case 'c': case 'C': 
                              /* Erase Check (by file)               */
                              toDo.EraseCheck= 1;
                              break;
                           case 'f': case 'F': 
                              /* Fast Erase Check (by file)            */
                              toDo.FastCheck= 1;
                              break;
                           case 'p': case 'P': 
                              /* Program file                      */
                              toDo.Program = 1;
                              break;
                           case 'r': case 'R': 
                              /* Reset MSP430 before waiting for <Enter>   */
                              toDo.Reset   = 1;
                              break;
                           case 'u': case 'U': 
                              /* Second run without entry sequence      */
                              toDo.UserCalled= 1;
                              break;         
                           case 'v': case 'V': 
                              /* Verify file                        */
                              toDo.Verify= 1;
                              break;
                           case 'w': case 'W': 
                              /* Wait for <Enter> before closing serial port */
                              toDo.Wait   = 1;
                              break;
                           case 'x': case 'X': 
                              /* Start BSL ??? */
                              toDo.BSLStart   = 0;
                              break;
                           default:
                              printf("ERROR: Illegal action specified!\n");
                           } /* switch */
                        } /* for (j) */
                     break; /* '+' */
                     
                  default:
                       filename= argv[i];
         } /* switch argv[i][0] */
      } /* for (i) */
   }
   else
   {
      showHelp();   
      return(1);
   }

   return(0);
}

/*---------------------------------------------------------------
* Main:
*---------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
	const WORD SMALL_RAM_model = 0;
	const WORD LARGE_RAM_model = 1;
	const WORD ROM_model = 0x4567;
	WORD loadedModel = ROM_model;
	int stat = 0;
	unsigned char infoA[0x40];
	
#ifdef NEW_BSL
	newBSLFile= NULL;
#endif // NEW_BSL 
	
	
#ifdef WORKAROUND
	/* Show memory access warning, if working with bootstrap 
	* loader version(s) requiring the workaround patch.
	* Turn warning on by default until we can determine the 
	* actual version of the bootstrap loader.
	*/
	BSLMemAccessWarning= 1; 
#endif /* WORKAROUND */
	
	printf("%s\n(%s)\n", programName, programVersion);
	
    stat = parseCMDLine(argc, argv);
    if (stat != 0) return(stat);


/*-------------------------------------------------------
* Communication with Bootstrap Loader ...
*-------------------------------------------------------
*/


	/* Open COMx port (Change COM-port name to your needs!): */
	if (comInit(comPortName, DEFAULT_TIMEOUT, 4) != 0)
		{ 
		printf("ERROR: Opening COM-Port failed!\n");
		if (toDo.Wait)
			{
			WaitForKey(); /* FRGR */
			}
		return(0);
		}

	if (toDo.UserCalled)
	{ }
    else
	{
		
		bslReset(toDo.BSLStart); /* Invoke the boot loader. */
		//delay(2000); 
		Time_BSL_starts = GetTickCount();
	}


	Repeat:
	
#ifdef NEW_BSL
	


if ((newBSLFile == NULL) || (passwdFile == NULL))
	{ 
	/* If a password file is specified the "new" bootstrap loader can be loaded
	* (if also specified) before the mass erase is performed. Then the mass 
	* erase can be done using the "new" BSL. Otherwise the mass erase is done 
	* now!
	*/
#endif /* NEW_BSL */

	
	if (toDo.RestoreInfoA && toDo.MassErase)
		{

⌨️ 快捷键说明

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