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

📄 stmflashloader.cpp

📁 ST Flash loader的2次开发接口源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:

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

			   BYTE Version ;
			   Commands pCmds;
			   Res = STBL_GET(&Version, &pCmds);
		       if (Res != SUCCESS)
			   {
					//printf("cannot Get BL available commands");
					COM_Close(); getchar(); return 0; 
			   }

			   SetTimeOut(timeout);
			}
			else if (strcmp(argv[arg_index],"-e")==0)
			{
			   //printf("erasing \n");

			   while(arg_index < argc)
			   {

				 if (!WaitForMoreSubOpt) break;
				 
			     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],"--all")==0)   
					 {
						 WaitForMoreSubOpt = false;

						 //printf("erasing all pages OK \n");
						 Res = STBL_ERASE(0xFF, NULL);
						 if (Res != SUCCESS) write_debug_info("erasing all pages", 0 ,0, 0, KO);
						 else write_debug_info("erasing all pages", 0 ,0, 0, OK);
					 }
					 else if (strcmp(argv[arg_index-1],"--sec")==0) 
					 {
						 WaitForMoreSubOpt = true;

						 nsec = atoi(argv[arg_index]);
						 LPBYTE sectors = (LPBYTE)malloc(nsec + 1);


					 	 sectors[0] = 0;
				 		 for(int i = 1; i<= nsec; i++)
						 {
							sectors[0]++;
							arg_index++;
							sectors[sectors[0]] = atoi(argv[arg_index]);
						 }

						 WaitForMoreSubOpt = false;

						 //printf("erasing pages OK \n");
						 Res = STBL_ERASE(nsec, sectors+1);
						 if (Res != SUCCESS)  
						 {
							 write_debug_info("erasing", 0 ,0, 0, KO);
							 COM_Close(); getchar(); return 0;
						 }
						 else write_debug_info("erasing", 0 ,0, 0, OK);

						 arg_index++;
					 }
				 }
				 else 
				 {
                     if (arg_index < argc - 1) printf("bad parameter [%s] \n", argv[arg_index]);
					 break;
				 }
			   }
			}
			else if (strcmp(argv[arg_index],"-u")==0)
			{
				//printf("uploading \n");

				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],"--s")==0) 
					 {
						 size = _tcstoul(argv[arg_index], 0, 16) ;
					 }
					 else */if (strcmp(argv[arg_index-1],"--fn")==0) 
					 {
						 filename = argv[arg_index];
					 }
				 }
				 else 
				 {
                     if (arg_index< argc) if (arg_index < argc - 1) printf("bad parameter [%s] \n", argv[arg_index]);
					 break;
				 }
			   }

			   HANDLE Handle;
			   FILES_CreateImage(&Handle, 0);

			   FILES_CreateImageFromMapping(&Handle,pMapping);

			   DWORD NbElements = 0;
               if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
			   {
				   if (NbElements > 0)
				   {
							for(int i = 0; i< (int)NbElements; i++)
							{
								IMAGEELEMENT Element={0};
								// Get element data size
								if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
								{
									//Upload element data
									Element.Data = (LPBYTE)malloc(Element.dwDataLength);
									if (STBL_UPLOAD(Element.dwAddress, Element.Data, Element.dwDataLength) == SUCCESS)
									{
									   //Insert elment in the Image
									   write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
									   FILES_SetImageElement(Handle,i,FALSE,Element);
									}
									else 
									{
										write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
										COM_Close(); getchar(); return 0;
									}
								}
							}
				   }
			   }

			   //printf("Upload [OK] \n");

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

			   if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Handle) == SUCCESS)
			   {
                      printf( "cannot open file %s \n", filename);
					  COM_Close(); getchar(); return 0;
			   }

			   //printf( "Uploaded data saved to %s \n", filename);

			}
			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);
					
			   char MapFile[256];
			   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]);
				   COM_Close();
				   getchar();
				   return 0;
			   }

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

			   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);
					  COM_Close(); getchar(); return 0;
			   }

			   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);
				   COM_Close(); getchar(); return 0;
			   }

			   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("Th flash may be read protected; use -p --drp to disable write protection." , 0, 0, 0, KO);
								  COM_Close(); getchar(); return 0;
							  }

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

			   bool VerifySuccess = true;
			   if (Verify)
			   {
					for (int el=0; el< (int)NbElements;el++)

⌨️ 快捷键说明

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