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

📄 stmflashloader.cpp

📁 stm32烧写软件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
* File Name          : STMFlashLoader.cpp
* Author             : MCD Application Team
* Version            : V1.3
* Date               : 03/05/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"

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

typedef enum STATE {OK,KO};

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


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;
}


void man()
{
	printf("STMicroelectronics Flash Loader command line v1.3 \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");
}

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;
}

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;
}

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;
}

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);
}

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) 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 0;
			}

			if (strcmp(argv[arg_index],"-?")==0) 
			{
				man(); 
				return 0;
			}
			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])) break;
				 else if(Is_SubOption(argv[arg_index]))
				 {
					 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]);
					 break;
				 }
			   }

			   write_debug_info("Connecting", 0 ,0, 0, OK);
			   // Apply serial connection settings
			   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 0;
			   }
		   
               //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;
			   Res = STBL_GET(&Version, &pCmds);
		       if (Res != SUCCESS)
			   {
					if(COM_is_Open()) COM_Close(); 
					printf("\n Press any key to continue ..."); 
					getchar(); 
					return 0; 
			   }

			   SetTimeOut(timeout);
			}

			else if (strcmp(argv[arg_index],"-Rts")==0)
			{
			   while(arg_index < argc)
			   {
			     if (arg_index< argc-1) arg_index++;
			     else break;

				 if(Is_Option(argv[arg_index])) break;
				 else if(Is_SubOption(argv[arg_index]))
				 {
					 if (strcmp(argv[arg_index],"--Hi")==0) 
					 {
						 write_debug_info("[Set Rts line]", 0 ,0, 0,OK);
						 STBL_SetRts(TRUE);
					 }
					 else if (strcmp(argv[arg_index],"--Lo")==0)  
					 {
						 write_debug_info("[Set Rts line]", 0 ,0, 0,OK);

⌨️ 快捷键说明

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