[D
 
  /Init [B
  
   	/ProgFlag exch def
   	ProgFlag [B
   		/FilePath 'FirmwareFiles\FirmwareE1100\OS\BootStrapOS_RS\' def
   	]
   	[B
   		/FilePath '' def	
   	] ifelse 

   	'PackInst' nknown not [B 'PackInst' import ] if
   	'RelInfo3R8' nknown not [B 'RelInfo3R8' import ] if

 		/ReleaseVersion RelInfo3R8:RelVer def
		/ReleaseSubVersion RelInfo3R8:RelSubVer def
		/ReleaseBuildInfo RelInfo3R8:RelInfoStr def
    


   	
		/LogMemo Create:LogMemo def
	  /Log [B LogMemo.Log ] def
	  10 LogMemo.SetFontSize
	  LogMemo.Show
    
    % Define constants
    /BootModeRequest '@00' def
    /BootModeResponse '@C5' def
    /FlashErase '@00' def
    /BaudRate 115200 def
    /BufferSize 100 def  
    /TimeOut 10000 def
    /EraseTimeOut 60000 def
    /PreloaderName FilePath 'PL_E1100.dat' add def
    /LoaderName FilePath 'LD_E1100.dat' add def

    /Controllers [A
			%E1100MP
			[D
			   /Name 'E1100-MP'
			   /TypeID $411
			   /TypeSupported 0	
			]
			%E1100MPHC
			[D
			   /Name 'E1100-MP-HC'
			   /TypeID $421
			   /TypeSupported 0	
			]
			%E1100MT
			[D
			   /Name 'E1100-MT'
			   /TypeID $431
			   /TypeSupported 0	
			]
			%E1100MTHC
			[D
			   /Name 'E1100-MT-HC'
			   /TypeID $441
			   /TypeSupported 0	
			]
			%E1100GP
			[D
			   /Name 'E1100-GP'
			   /TypeID $451
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   		$0104		   		   		
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $2 /SWFileName 'BootE1100_S2_GPLC.H86']
			   ]		   		   
			   /PowerExtension 'LC'
			]
			%E1100GPHC
			[D
			   /Name 'E1100-GP-HC'
			   /TypeID $461
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103		   		   		
			   		$0104
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $2 /SWFileName 'BootE1100_S2_GPHC.H86']
			   ]		   		   
			   /PowerExtension 'HC'
			]
			%E1100RS
			[D
			   /Name 'E1100-RS'
			   /TypeID $471
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103		   		   		
					$0104
					$0106 
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_RSLC.H86']
			   ]		   		   
			   /PowerExtension 'LC'
			]
			%E1100RSHC
			[D
			   /Name 'E1100-RS-HC'
			   /TypeID $481
			   /TypeSupported 1	
			   /SupportedHWVersions [A
			   		$0102
			   		$0103		   		   		
					$0104
					$0105
					$0106
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_RSHC.H86']
			   ]		   		   
			   /PowerExtension 'HC'
			]
			%E1100RSXC
			[D
			   /Name 'E1100-RS-XC'
			   /TypeID $511
			   /TypeSupported 1	
			   /SupportedHWVersions [A
			   		$0104   		
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_RSXC.H86']
			   ]		   		   
   		   /PowerExtension 'XC'  
			]
			%E1100DN
			[D
			   /Name 'E1100-DN'
			   /TypeID $491
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0104
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_DNLC.H86']
			   ]		   		   
			   /PowerExtension 'LC'  
			]
			%E1100DNHC
			[D
			   /Name 'E1100-DN-HC'
			   /TypeID $4A1
			   /TypeSupported 1	
			   /SupportedHWVersions [A
			   		$0104
			   		$0105
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_DNHC.H86']
			   ]		   		   
   		   /PowerExtension 'HC'  
			]
			%E1100DNXC
			[D
			   /Name 'E1100-DN-XC'
			   /TypeID $521
			   /TypeSupported 1	
			   /SupportedHWVersions [A
			   		$0104   		
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_DNXC.H86']
			   ]		   		   
   		   /PowerExtension 'XC'  
			]
			%E1100CO
			[D
			   /Name 'E1100-CO'
			   /TypeID $4B1
			   /TypeSupported 1
	   		   /SupportedHWVersions [A
			   		$0104
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_COLC.H86']
			   ]		   		   
  		   /PowerExtension 'LC'  
			]
			%E1100COHC
			[D
			   /Name 'E1100-CO-HC'
			   /TypeID $4C1
			   /TypeSupported 1	
			   /SupportedHWVersions [A
			   		$0104 
			   		$0105
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_COHC.H86']
			   ]		   		   
	   		 /PowerExtension 'HC'
			]
			%E1100COXC
			[D
			   /Name 'E1100-CO-XC'
			   /TypeID $4F1
			   /TypeSupported 1	
			   /SupportedHWVersions [A
			   		$0104   		
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_COXC.H86']
			   ]		   		   
	   		   /PowerExtension 'XC'  
			]
			%E1130DP
			[D
			   /Name 'E1130-DP'
			   /TypeID $4D1
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0104 
			   		$0106			   		  		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_DPLC.H86']
			   ]		   		   
   		   /PowerExtension 'LC'  
			]
			%E1130DPHC
			[D
			   /Name 'E1130-DP-HC'
			   /TypeID $4E1
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0104   		
			   		$0105
			   		$0106  		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_DPHC.H86']
			   ]		   		   
   		   /PowerExtension 'HC'
			]	
			%E1130DPXC
			[D
			   /Name 'E1130-DP-XC'
			   /TypeID $531
			   /TypeSupported 1	
			   /SupportedHWVersions [A
			   		$0104   		
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_DPXC.H86']
			   ]		   		   
   		   /PowerExtension 'XC'  
			]
			%E1130DPHV
			[D
			   /Name 'E1130-DP-HV'
			   /TypeID $501
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0104   		
			   		$0106   		
					$0107 
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $4 /SWFileName 'BootE1100_S4_DPHV.H86']
			   ]		   		   
	   		 /PowerExtension 'HCHV'
			]	 
		] def

   	/GetEloType [B
    	$200680 1 BlockRd.ReadBlock
    	BlockRd.GetResult 0 ne [B /Msg 'BlockRd.ReadBlock: Error while reading' raisese] if
    	BlockRd.GetData 0 get
   	] def
	
  	/GetEloVersion [B
	  	$200682 1 BlockRd.ReadBlock
    	BlockRd.GetResult 0 ne [B /Msg 'BlockRd.ReadBlock: Error while reading' raisese] if
    	BlockRd.GetData 0 get
	  ] def
	
	  /GetEloSWVersion [B
		  $20102A 1 BlockRd.ReadBlock
    	BlockRd.GetResult 0 ne [B /Msg 'BlockRd.ReadBlock: Error while reading' raisese] if
    	BlockRd.GetData 0 get
	  ] def

		/GetEloDictIndex [P
			[A /Type ]
				/Index -1 def
				/CurrentIndex -1 def
				Controllers [B
				  /CurrentIndex CurrentIndex 1 add def
					/TempEloDict exch def
					TempEloDict.TypeID Type eq [B
						/Index CurrentIndex def
						Exitloop
					] if 
				] forall		
			[A /Index]
		] def

	  /LoadBinFile [P 
	    [A /FileName ]
	      FileName fileexist [B
	        /LogFile Create:LogFile def
	        % Open the file with the mode = 0 (read only)
	        FileName 0 LogFile.Open
	        % Create the data container        
	        /Data 0 carr def  
	        [B
	          LogFile.Eof [B ExitLoop ] if	
	          % Read 100 characters from LogFile (or less if end of file occurs).   
	          Data 100 LogFile.ReadChars add                    
	        ] loop  
	        LogFile.Close                         
	      ]
	      [B
	        FileName ' does not exist.' add raisese	        
	      ]	 ifelse             
	    [A /Data ]
	  ] def
	  
	  /LoadTextFile [P 
	    [A /FileName ]
	      FileName fileexist [B
	        /LogFile Create:LogFile def
	        % Open the file with the mode = 0 (read only)
	        FileName 0 LogFile.Open
	        % Create the data container        
	        /Data 0 carr def  
	        [B
	          LogFile.Eof [B ExitLoop ] if	
	          % Read line   
	          Data LogFile.ReadLine add         
	        ] loop  
	        LogFile.Close                         
	      ]             
	      [B
	        FileName ' does not exist.' add raisese	        
	      ]	 ifelse             
	    [A /Data ]
	  ] def

  ] %%%%% End Init
  
  	  	  	  	 	              
  /BootSW [P
    [A  ]
      /Result #0 def
		  % Chose the serial port for the communication with the servo controller
		  /ComSelected #0 def
		  ProgFlag [B
		    /ComPort 'COM' HWConfig:EloComPort cvs add def
  			/ComSelected #1 def   		    
		  ][B
		    PackInst:_SelectCommPort [B
		      /PortType exch def
		      /ComPort exch def 
			/ComSelected #1 def   		    
		    ] if
		  ] ifelse  
		  
		  ComSelected [B     
		    [B      		
		      %/PortType exch def
		      %/ComPort exch def    
		      % Configure and open the serial port
		            
		      /Dialog 'Controller Bootstrap!' 250 100 Create:Form def		      
          'Ok' '' 'Ok' 1 20 30 Dialog.AddButton
          'BootLabel' '' 'Press the boot strap button on the controller!' 5 5 Dialog.AddLabel
          'Cancel' '' 'Cancel' 2 120 30 Dialog.AddButton
          Dialog.Execute 2 eq [B 'Firmware download aborted!' raisese ] if	
          	      
		      /Comm Create:Comm def
		      ComPort BaudRate Comm.Open
		      % Create a buffer for receive the data
		      /RxBuf BufferSize Comm.CreateBuffer def
		      RxBuf Comm.ClearBuffer
		      100 wait
		      'Sending null character...' log      
		      BootModeRequest Comm.SendString
		      'Waiting response to null character...' log
		      RxBuf TimeOut BootModeResponse Comm.WaitForChar         
		      Comm.GetExpired 
		      [B
		        RxBuf Comm.GetBuffer log	 
		        'Error: processor is not in bootstrap mode. Installation aborted.' log 
		        Comm.Close
		      ]  
		      [B 
		        PreloaderName LoadBinFile
		        /Data exch def
		        'Sending preloader...' log
		        Data [B Comm.SendString ] forall	
		        100 wait 
		        LoaderName LoadBinFile 
		        /Data exch def
		        'Sending loader...' log
		        Data [B Comm.SendString ] forall	
		        RxBuf Comm.ClearBuffer
		        'Waiting response from loader...' log      
		        RxBuf EraseTimeOut FlashErase Comm.WaitForChar 
		        Comm.GetExpired [B       	
		      	  'Wrong response from loader (flash problem?). Installation aborted.' log 
		        ] 
		        [B      	 
		          Comm.Close
					    /Device Create:Device def
					    /DeviceName 'LinMotInstall' def      	
					    'PortName' ComPort 
					    'BaudRate' BaudRate cvs 2 DeviceName 'LinMotInstall' Device.CreateDevice
					    /BlockRd DeviceName Create:BlockReader def
					    /BlockWr DeviceName Create:BlockWriter def
					    
							DeviceName /PackInst:OnMessageCallback PackInst:Init
					    [B
					      % Check if controller type is supported
					      /EloType GetEloType def
					      /EloVersion GetEloVersion def
					      /EloSWVersion GetEloSWVersion def
					      /Index EloType GetEloDictIndex def
					      Index -1 eq [B 'Controller type not supported!' raisese ] if
					      /EloDict Controllers Index get def
	              
	              % Check if the version is supported
	              /SupHWVer EloDict /SupportedHWVersions get def
	              /Supported #0 def
	              SupHWVer [A
	                /Version exch def
	                Version EloVersion eq [B 
	                  /Supported #1 def 
	                  Exitloop
	                ] if
	              ] forall				      
					      Supported not [B 'Controller version not supported!' raisese ] if
					      
					      % Get the SW file name
	              /SupDSWVer EloDict /SupportedDSWVersions get def
	              /Supported #0 def
	              SupDSWVer [A
	                /VerDict exch def
	                /DSWVer VerDict /DSWVersion get def
	                 DSWVer EloSWVersion eq [B 
	                  /Supported #1 def
	                  /SWFileName VerDict /SWFileName get def 
	                  Exitloop
	                ] if
	              ] forall				      
					      Supported not [B 'Software version not supported!' raisese ] if
	              
	              /SWFileName FilePath SWFileName add def
	              						      						


					      % load monitor file          					      					      					      
					      /Data SWFileName LoadTextFile def        	
					      'Sending firmware...' log 
					      Data SWFileName PackInst:DownloadH86File        					      					      
									
					      % write release info
								/ReleaseInfoAddr	$200748 def
  							/ReleaseVersionAddr $2007A8 def
								/ReleaseSubVersionAddr $2007AA def
								
								/ReleaseString  ReleaseVersion cvs '.' add ReleaseSubVersion cvs add ' ' add ReleaseBuildInfo add def
								[B
  								ReleaseString length 32 lt [B 
	  							  /ReleaseString ReleaseString ' ' add def
		  						]
			  					[B
                    exitloop  								
				  				] ifelse
				  			] loop	  
									
								/ReleaseStr '' def	
								0 1 ReleaseString length 1 sub	[B
								  /Index exch def 
								  /ReleaseStr ReleaseStr ReleaseString Index 1 getsubstr ord 16 cvrs add def
								] for

								ReleaseInfoAddr ReleaseStr #0 BlockWr.WriteBlock
								
								ReleaseVersionAddr ReleaseVersion PackInst:WriteMemory
								ReleaseSubVersionAddr ReleaseSubVersion PackInst:WriteMemory
																
					      PackInst:ResetDevice            
					      'Firmware loaded successfully.' log		
					      /Result #1 def						      					      					      	
					  %    LogMemo.Hide
					    ] if
					    					    					    
					    Device.DestroyDevice					    
					    
		        ] ifelse     
		      ] ifelse                                        
		    ] 
		    [B
		      /Dict exch def
		      Dict.Msg ' Installation aborted!' add log
		    ] tryexcept	  
		  ] if           		      
		  LogMemo.Remain  		  		               		  
    [A /Result ]  
  ]
  
]