stm32+i

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

TXT
1,575
字号
										printf("\n Press any key to continue ..."); 
										getchar(); 
										return 1;
									}
								}
							}
				   }
			   }

			   if(!FileExist((LPCTSTR)filename))
			   {
                      printf( "file %s does not exist .. Creating file\n", filename);
					  FILE* fp = fopen((LPCTSTR)filename, "a+");
					  fclose(fp);
			   }

			   printf( "Writing data ...\n");

			   if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Handle) != FILES_NOERROR)
			   {
                     printf( "cannot write to file %s \n", filename);
					 
					 if(COM_is_Open())  
						COM_Close(); 

					 printf("\n Press any key to continue ..."); 
				     getchar(); 
				     return 1;
			   }
			   else
                   printf("\n Uploaded data is dumped on %s", filename); 
			}
			//============================ Get Device map file name ==============================
			else if (strcmp(argv[arg_index],"-i")==0)
			{
			   if (arg_index< argc) 
				   arg_index++;
			   else 
				   break;

			   sprintf(devname,"%s.STmap", argv[arg_index]);

			   char Drive[3], Dir[256], Fname[256], Ext[256];
			   _splitpath(argv[0],Drive,Dir,Fname,Ext);
					
			   sprintf(MapFile, "%s%s%s%s", Drive, Dir , "Map\\", devname);

			   pMapping = NULL;
			   WORD size = 0;

			   WORD PacketSize = 0;
			   pMapping = NULL;
			   WORD Size = 0;
			   char MapName[256]; 
			   // Get the number of sectors in the flash target: pMapping should be NULL
			   // number of sectors is returned in the Size value
			   BYTE PagePerSector = 0;

			   if (!FileExist((LPCTSTR)MapFile))
			   {
					printf("This version is not intended to support the <%s> target\n", argv[arg_index]);
				   
				    if(COM_is_Open())  
						COM_Close();

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

			   FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pMapping, &PagePerSector);
			   // Allocate the mapping structure memory
			   pMapping = (PMAPPING)malloc(sizeof(MAPPING));
			   pMapping->NbSectors = 0;
			   pMapping->pSectors = (PMAPPINGSECTOR) malloc((Size) * sizeof(MAPPINGSECTOR));

			   // Get the mapping info
			   FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pMapping, &PagePerSector);
				   
			   SetPaketSize(PacketSize);

				if (arg_index< argc) 
					arg_index++;
			    else 
					break;
			}
			//============================ DOWNLOAD ==============================================
			else if (strcmp(argv[arg_index],"-d")==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) 
						 arg_index++;
			         else 
						 break;

					 if (strcmp(argv[arg_index-1],"--a")==0)   
					 {
						 address = _tcstoul(argv[arg_index], 0, 16) ;
					 }
					 else if (strcmp(argv[arg_index-1],"--v")==0) 
					 {
						 Verify = true;
						 arg_index--;
					 }
					 else if (strcmp(argv[arg_index-1],"--o")==0) 
					 {
						 optimize = TRUE;
						 arg_index--;
					 }
					 else if (strcmp(argv[arg_index-1],"--fn")==0) 
					 {
						 filename = argv[arg_index];
						 _splitpath(filename,Drive,Dir,Fname,Ext);
	                     ptr=strupr(Ext);
	                     strcpy(Ext, ptr);
					 }
				 }
				 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;
				 }
			   }

			   PMAPPINGSECTOR pSector = pMapping->pSectors;
			   for(int i = 1; i<= (int)pMapping->NbSectors; i++)
			   {
				   if ((strcmp(Ext, ".BIN")!=0) && (i==0))
			          address = pSector->dwStartAddress;

			       pSector->UseForOperation = TRUE;
			       pSector++;
			   }

			   if(!FileExist((LPCTSTR)filename))
			   {
                     printf( "file does not exist %s \n", filename);

					 if(COM_is_Open())  
						COM_Close(); 

					 printf("\n Press any key to continue ..."); 
				     getchar(); 
				     return 1;
			   }
   
			   //*****************************************************
			   BYTE Version;
			   Commands pCmds;
			   CString m_Version;
			   if (STBL_GET(&Version, &pCmds) == SUCCESS)
			   {
					m_Version.Format("%x.%x",Version/16, Version & 0x0F)  ;
			   }	
			   CIni Ini((LPCSTR)MapFile);

			   if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version))
			   {
				   CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, "");
				   CString Path(*__argv);
				   int j=Path.ReverseFind('\\')+1;
				   if(j) Path=Path.Left(j);

				   CString ToFind;

				   ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs);

				   if(!E_W_ROUTINEs.IsEmpty())
				   {
					   if(!FileExist((LPCTSTR)ToFind))
					   {
						   printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind); 
					   }
					   else
					   {
							HANDLE Image;
							if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR)
							{
								FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind);

								DWORD NbElements; 
								if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR)
								{
									for (int el=0; el< (int)NbElements;el++)
									{
										IMAGEELEMENT Element={0};
										if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
										{	   
											Element.Data=new BYTE[Element.dwDataLength];
											if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
											{
												if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
												{
													  
												}
											}
										}
									}

									// Verify writen data
									BOOL VerifySuccess = TRUE;
									_sleep(100);;

#ifndef _VS6_USED
									int el;
#endif

									for (el=0; el< (int)NbElements;el++)
									{
										IMAGEELEMENT Element={0};
										if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
										{
											Element.Data=new BYTE[Element.dwDataLength];
											if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
											{
												if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
												{
													VerifySuccess = FALSE;
													char str[255];
													sprintf(str, "%s at address :0x%X. \n%s \nPlease disable the write protection then try agin.", "Data not matching ", Element.dwAddress, "The page may be write protected.");   
													AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION);
													return 1;
												}
											}
										}
									}
								}
							}
        					else
							{
								 AfxMessageBox("Unable to load data from this file " + ToFind + " ...");
								 return -1;
							}
					   }
				   }
			   }
			   else
			   {
				   int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0);
				   if(family == 3)
				   {
						printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n"); 
				   }
			   }
			   //******************************************************

			   printf("\n DOWNLOADING ... \n\n");

			   HANDLE Handle;
			   if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename,&Handle, 1) == FILES_NOERROR)
			   {
			      FILES_SetImageName(Handle,(LPSTR)(LPCSTR)filename);

				  DWORD NbElements = 0;
				  if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
				  {
						if ( NbElements > 0 )
						{   // if binary file -> change the elemnts address
							if (strcmp(Ext, ".BIN")==0)
							{
								for (int i=0;i< (int)NbElements;i++)
								{
									IMAGEELEMENT Element={0};
									if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
									{
									   Element.Data= (LPBYTE)malloc(Element.dwDataLength);
									   if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
									   {
										   Element.dwAddress = Element.dwAddress + address; 
										   FILES_SetImageElement(Handle, i, FALSE, Element);
									   }
									}
								}
							}
						}
				  }

				  FILES_FilterImageForOperation(Handle, pMapping, OPERATION_UPLOAD, optimize);
			   }
			   else 
			   {  
					printf("cannot open file %s \n", filename);

					if(COM_is_Open())  
						COM_Close(); 

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

			   DWORD NbElements = 0;
               if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
			   {
				   for (int el=0; el< (int)NbElements;el++)
				   {
						IMAGEELEMENT Element={0};
						if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
						{	   
						   Element.Data= (LPBYTE)malloc(Element.dwDataLength);
						   if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
						   {
							  if ((strcmp(Ext, ".BIN")==0) && (el==0)) 
							      Element.dwAddress = address;
							  
							  if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, false /*Optimize*/) != SUCCESS)
							  {
								  write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
								  write_debug_info("The flash may be read protected; use -p --drp to disable write protection." , 0, 0, 0, KO);
								  
								  if(COM_is_Open())
									  COM_Close(); 
								  
								  printf("\n Press any key to continue ..."); 
								  getchar(); 
								  return 1;
							  }

							  write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
						   }
						}
				   }
			   }

			   bool VerifySuccess = true;
			   if (Verify)
			   {
				   printf("\n VERIFYING ... \n\n");


					for (int el=0; el< (int)NbElements;el++)
					{
						IMAGEELEMENT Element={0};
					    if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
						{
							Element.Data=(LPBYTE)malloc(Element.dwDataLength);
							if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
							{
								if ((strcmp(Ext, ".BIN")==0) && (el==0)) 
							        Element.dwAddress = address;

								if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, false /*Optimize*/) != SUCCESS)
								{
									VerifySuccess = false;
									write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
                                    write_debug_info("some pages may be write protected; use -p --dwp to disable write protection." , 0, 0, 0, KO);
									
									if(COM_is_Open())  
										COM_Close(); 
									
									printf("\n Press any key to continue ..."); 
									getchar(); 
									return 1;
								}

								write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
							}
						}
					}
			   }

			}
			//============================ VERIFY ================================================
			else if (strcmp(argv[arg_index],"-v")==0)
			{
				if (arg_index< argc) 
					arg_index++;
			    else 
					break;
			}
			//============================ Program option bytes ==================================
			else if (strcmp(argv[arg_index],"-o")==0)
			{
				while(arg_index < argc)
				{
			     if (arg_index< argc-1) 

⌨️ 快捷键说明

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