stm32+i

来自「STM32烧写」· 代码 · 共 1,575 行 · 第 1/4 页

TXT
1,575
字号
/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
* File Name          : STMFlashLoader.cpp
* Author             : MCD Application Team
* Version            : v2.0.0
* Date               : 03/07/2009
* Description        : STM Flash Loader command line version
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

#include "stdafx.h"
#include "string.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <dos.h>

#include "../STBLLIB/STBLLIB.h"
#include "../Files/Files.h"
#include "ini.h"


#define NONE = 0;
#define ODD  = 1;
#define EVEN = 2;

typedef enum STATE {OK,KO};

char MapFile[256];
PMAPPING pMapping;

int TimeBO = 100;

BOOL SHOW_OK = TRUE;  // Set to TRUE/FALSE to show/hide OK status  messages
BOOL SHOW_KO = TRUE;  // Set to TRUE/FALSE to show/hide KO status  messages

/*******************************************************************************************/
/* Function    : FileExist                                                                 */
/* IN          : file name                                                                 */
/* OUT         : boolean                                                                   */
/* Description : verify if the given file exists                                           */
/*******************************************************************************************/
bool FileExist(LPCTSTR filename)
{
	// Data structure for FindFirstFile
	WIN32_FIND_DATA findData;

	// Clear find structure
	ZeroMemory(&findData, sizeof(findData));

	// Search the file
	HANDLE hFind = FindFirstFile( filename, &findData );
	if ( hFind == INVALID_HANDLE_VALUE )
	{
	// File not found
	return false;
	}

	// File found

	// Release find handle
	FindClose( hFind );
	hFind = NULL;

	// The file exists
	return true;
}

/*******************************************************************************************/
/* Function    : man                                                                       */
/* IN          :                                                                           */
/* OUT         :                                                                           */
/* Description : print the manual on the standard output                                   */
/*******************************************************************************************/
void man()
{
	printf("STMicroelectronics Flash Loader command line v2.0.0 \n\n");
	printf(" Usage : \n");
	printf("    STMFlashLoader.exe [options] [Agrument][[options] [Agrument]...] \n\n");
    
	printf("     -?                     (Show this help) \n");
	printf("     -c                     (establish connection to the COM port) \n");
	printf("       --pn  port_number    (e.g 1, 2 ..., default 1) \n");
	printf("       --br  baud_reate     (e.g 115200, 57600 ..., default 57600) \n");
	printf("       --db  data_bits      (value in {5,6,7,8} ..., default 8) \n");
	printf("       --pr  parity         (value in {NONE,ODD,EVEN} ..., default EVEN) \n");
	printf("       --sb  stop_bits      (value in {1,1.5,2} ..., default 1) \n");
	printf("       --to  time_out       ((ms) e.g 1000, 2000, 3000 ..., default 5000) \n");

	printf("     -i  device_name        (e.g STM32F10xxBxx, STM32F10xx8xx, STM32F10xx6xx [map file names in the Map directory]) \n");
	printf("     -e                     (erase flash pages\n");
	printf("       --all all pages      (erase all pages\n");
	printf("       --sec number_of pages_group pages_group_codes (erase specified group pages e.g 1,2,3,...) \n");

	printf("     -u                     (upload flash contents to the specified file (bin, hex or s19 file; the file type is recognized by its extension))\n");
	printf("       --fn  file_name      (full path name) \n");

	printf("     -d                     (download the conent of the specified file into MCU flash) \n");
	printf("       --a   address(hex)   (start addres; ignored if the target file is not a binary file) \n");
	printf("       --fn  file_name      (full path name (bin, hex or s19 file; the file type is recognized by its extension)) \n");
	printf("       --v                  (verify after download) \n");
	printf("       --o                  (optimize; removes FFs data)\n");

    printf("     -o                     (get or set option bytes \n");
	printf("       --get --fn file_name (get option bytes from the device and write it in the specified file) \n");
	printf("       --set --fn file_name (load option bytes from the specified file and write it to the device) \n");
	printf("       --set --vals --OPB hex_val (set the specified option byte; OPB in (User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) \n");

	printf("     -p                     (activate or disactivate protection) \n");
	printf("       --erp                (enable read protection, all options following this one will fail) \n");
	printf("       --drp                (disable read protection) \n");
	printf("       --ewp                (enable write protection) for sector codes (e.g 1,2,3,...) \n");
	printf("       --dwp                (disable write protection) \n");

	printf("     -r                     (run the flash code at the specified address \n");
	printf("       --a address(hex)     (address) \n");

	printf("     -Rts                   (set Rts line to Hi, Lo)\n");
	printf("       --State              (State in {Hi, Lo})\n");
	printf("     -Dtr                   (set Rts line to Hi, Lo)\n");
	printf("       --State              (State in {Hi, Lo})\n");
}

/*******************************************************************************************/
/* Function    : ParityToInt                                                               */
/* IN          : parity as string (NONE, ODD, EVEN)                                        */
/* OUT         : integer                                                                   */
/* Description : Get the integer representation of the given parity                        */
/*******************************************************************************************/
int ParityToInt(char* parity)
{
    if (strcmp(parity,"NONE")==0) return 0;
    else if(strcmp(parity,"ODD")==0) return 1;
    else if(strcmp(parity,"EVEN")==0) return 2;
    else return 2;
}

/*******************************************************************************************/
/* Function    : Is_Option                                                                 */
/* IN          : option as string                                                          */
/* OUT         : boolean                                                                   */
/* Description : Verify if the given string present an option                              */
/*******************************************************************************************/
bool Is_Option(char* option)
{
	if (strcmp(option,"-?")==0) return true;
	else if (strcmp(option,"-c")==0) return true;
	else if (strcmp(option,"-i")==0) return true;
	else if (strcmp(option,"-e")==0) return true;
	else if (strcmp(option,"-u")==0) return true;
	else if (strcmp(option,"-d")==0) return true;
	else if (strcmp(option,"-v")==0) return true;
	else if (strcmp(option,"-p")==0) return true;
	else if (strcmp(option,"-r")==0) return true;
	else if (strcmp(option,"-o")==0) return true;
	else if (strcmp(option,"-Rts")==0) return true;
	else if (strcmp(option,"-Dtr")==0) return true;
    else return false;
}

/*******************************************************************************************/
/* Function    : Is_SubOption                                                              */
/* IN          : sub-option as string                                                      */
/* OUT         : boolean                                                                   */
/* Description : Verify if the given string present a sub-option                           */
/*******************************************************************************************/
bool Is_SubOption(char* suboption)
{
	if (strcmp(suboption,"--pn")==0) return true;  
	else if (strcmp(suboption,"--br")==0) return true;  
	else if (strcmp(suboption,"--db")==0) return true;  
	else if (strcmp(suboption,"--pr")==0) return true;  
	else if (strcmp(suboption,"--sb")==0) return true;  
	else if (strcmp(suboption,"--to")==0) return true;  
	else if (strcmp(suboption,"--lcs")==0) return true; 
	else if (strcmp(suboption,"--all")==0) return true; 
	else if (strcmp(suboption,"--sec")==0) return true; 
	else if (strcmp(suboption,"--a")==0) return true;
	else if (strcmp(suboption,"--s")==0) return true;
	else if (strcmp(suboption,"--fn")==0) return true;
	else if (strcmp(suboption,"--v")==0) return true;
	else if (strcmp(suboption,"--o")==0) return true;
	else if (strcmp(suboption,"--erp")==0) return true;
	else if (strcmp(suboption,"--drp")==0) return true;
	else if (strcmp(suboption,"--ewp")==0) return true;
	else if (strcmp(suboption,"--dwp")==0) return true;
	else if (strcmp(suboption,"--get")==0) return true;
	else if (strcmp(suboption,"--set")==0) return true;
	else if (strcmp(suboption,"--vals")==0) return true;
	else if (strcmp(suboption,"--RDP")==0) return true;
	else if (strcmp(suboption,"--User")==0) return true;
	else if (strcmp(suboption,"--Data0")==0) return true;
	else if (strcmp(suboption,"--Data1")==0) return true;
	else if (strcmp(suboption,"--WRP0")==0) return true;
	else if (strcmp(suboption,"--WRP1")==0) return true;
	else if (strcmp(suboption,"--WRP2")==0) return true;
	else if (strcmp(suboption,"--WRP3")==0) return true;
	else if (strcmp(suboption,"--Hi")==0) return true;
	else if (strcmp(suboption,"--Lo")==0) return true;
	else return false;
}

/*******************************************************************************************/
/* Function    : write_debug_info                                                          */
/* IN          :                                                                           */
/* OUT         :                                                                           */
/* Description : print the output messages on the standart output                          */
/*******************************************************************************************/
void write_debug_info(char *msg, int page, DWORD addr, float size, STATE status)
{
	char d_info[256];

	if((page==0) && (addr==0) && (size==0))
	{
		if(status == OK) 
		   sprintf(d_info, "%s \t\t\t\t [OK] \n", msg);  
	    else 
		   sprintf(d_info, "%s \t\t\t\t [KO] \n", msg); 
	}
	else if(status == OK) 
		sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(Kb) \t [OK] \n", msg, page, addr, (float)size);  
	else 
		sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(Kb) \t [KO] \n", msg, page, addr, (float)size); 

	if((SHOW_OK && (status == OK)) || (SHOW_KO && (status == KO))) printf(d_info);
}

/*******************************************************************************************/
/* Function    : main                                                                      */
/* IN          :                                                                           */
/* OUT         :                                                                           */
/* Description :                                                                           */
/*******************************************************************************************/
int main(int argc, char* argv[])
{
    START:

	BYTE Res = SUCCESS;
	BYTE User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3;
	bool WaitForMoreSubOpt = TRUE;

	//Initializing default serial connection parameters
	int    portname   = 1;
	long   BaudRate   = 57600 ;
	int    DataBits   = 8;
	int    parity     = ParityToInt("EVEN"); 
	double nbStopBit  = 1;
	int    timeout    = 5000;

	int    nsec     = 0;
	DWORD  address  = 0x00000000;
	DWORD  size     = 0x00000000;
	char*  filename;
	char   devname[256] = "STM32F10xx.STmap";
	bool   Verify = false;
	BOOL   optimize = FALSE;

	char Drive[3], Dir[256], Fname[256], Ext[256];
	char *ptr;

	if (argc == 1)  // wrong parameters
		man();
	else
	{
		int arg_index = 1;

		while(arg_index < argc)
		{
			if(!Is_Option(argv[arg_index])) 
			{
			  if (arg_index < argc - 1) 
				  printf("bad parameter [%s] \n", argv[arg_index]);

			  if(COM_is_Open())  
				  COM_Close(); 

			  printf("\n Press any key to continue ..."); 
			  getchar(); 
			  return 1;
			}

			//============================ Show the man =========================================
			if (strcmp(argv[arg_index],"-?")==0) 
			{
				man(); 
				return 1;
			}
			//=============================== connect ============================================
			else if (strcmp(argv[arg_index],"-c")==0)
			{
			   while(arg_index < argc)
			   {
			     if (arg_index< argc-1) 
					 arg_index++;
			     else 
					 break;

				 if(Is_Option(argv[arg_index])) // Set default connection settings and continue with the next option
					 break;
				 
				 else if(Is_SubOption(argv[arg_index])) // Get connection settings
				 {
					 if (arg_index< argc-1) 
						 arg_index++;
			         else 
						 break;

					 if (strcmp(argv[arg_index-1],"--pn")==0)       portname = atoi(argv[arg_index]);//port name  (e.g COM1, COM2 ..., default COM1) \n");
					 else if (strcmp(argv[arg_index-1],"--br")==0)  BaudRate = atoi(argv[arg_index]);//baud reate (e.g 115200, 128000 ..., default 115200) \n");
					 else if (strcmp(argv[arg_index-1],"--db")==0)  DataBits = atoi(argv[arg_index]);//data bits  (in {5,6,7,8} ..., default 8) \n");
					 else if (strcmp(argv[arg_index-1],"--pr")==0)  parity   = ParityToInt(argv[arg_index]); //parity     (in {NONE,ODD,EVEN} ..., default EVEN) \n");
					 else if (strcmp(argv[arg_index-1],"--sb")==0)  nbStopBit= atof(argv[arg_index]);//stop bits  (in {1,1.5,2} ..., default 1) \n");
					 else if (strcmp(argv[arg_index-1],"--to")==0)  timeout  = atoi(argv[arg_index]);//time out   (e.g 1000, 2000, 3000 ..., default 5) \n");
				 }
				 else 
				 {
					 if (arg_index < argc - 1) 
						printf("bad parameter [%s] \n", argv[arg_index]);

					 if(COM_is_Open())  
						COM_Close(); 

					 printf("\n Press any key to continue ..."); 
				     getchar(); 
				     return 1;
				 }
			   }

			   write_debug_info("Connecting", 0 ,0, 0, OK);
			   // Apply serial connection settings
			   TARGET_SetComIntType(0);
			   SetCOMSettings(portname, BaudRate, DataBits, parity, nbStopBit);
			   // Opening seria connection
			   Res = COM_Open();
			   SetTimeOut(1000);

			   if ((Res != SUCCESS) && (Res != COM_ALREADY_OPENED)) 
			   {
				    write_debug_info("Connecting", 0 ,0, 0, KO);
					printf("Cannot open the com port, the port may \n be used by another application \n");

					if(COM_is_Open())  
						COM_Close(); 

					printf("\n Press any key to continue ..."); 
					getchar(); 
					return 1;
			   }
		   
               //sending BL config byte (0x7F) & identifing target
			   Res = STBL_Init_BL();
			   if (Res == UNREOGNIZED_DEVICE)
			   {
				    write_debug_info("Configuring", 0 ,0, 0, KO);

			 	    if(COM_is_Open()) 
						COM_Close();

					printf("Unrecognized device... Please, reset your device then try again \n");

					if(COM_is_Open()) 
						COM_Close(); 
					
					printf("Please, reset your device then then perss any key to continue \n");
					printf("\n Press any key to continue ..."); 
					getchar();
					goto START;
			   }
			   else if (Res != SUCCESS) 		
			   {
				    write_debug_info("Configuring", 0 ,0, 0, KO);
					printf("No response from the target, the Boot loader can not be started. \nPlease, verify the boot mode configuration, reset your device then try again. \n");
					
					if(COM_is_Open()) 
						COM_Close(); 
					
					printf("Please, reset your device then then perss any key to continue \n");
					printf("\n Press any key to continue ..."); 
					getchar();
					goto START;
			   }

			   _sleep(TimeBO);
			   write_debug_info("Configuring", 0 ,0, 0, OK);

			   //Getting Target informations (version, available commands)
			   BYTE Version ;
			   Commands pCmds;

⌨️ 快捷键说明

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