[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
   	'RelInfo3R9' nknown not [B 'RelInfo3R9' import ] if

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


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

    /Controllers [A
			%B1100PP
			[D
			   /Name 'B1100-PP'
			   /TypeID $D01
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_PPLC.H86']
			   ]		   		   
			   /PowerExtension 'LC'
			]
			%B1100PPHC
			[D
			   /Name 'B1100-PP-HC'
			   /TypeID $D11
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_PPHC.H86']
			   ]		   		   
			   /PowerExtension 'HC'
			]
			%B1100PPXC
			[D
			   /Name 'B1100-PP-XC'
			   /TypeID $D21
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_PPXC.H86']
			   ]		   		   
			   /PowerExtension 'XC'
			]

			%B1100VF
			[D
			   /Name 'B1100-VF'
			   /TypeID $D31
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_VFLC.H86']
			   ]		   		   
			   /PowerExtension 'LC'
			]
			%B1100VF-HC
			[D
			   /Name 'B1100-VF-HC'
			   /TypeID $D41
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_VFHC.H86']
			   ]		   		   
			   /PowerExtension 'HC'
			]
			%B1100VF-XC
			[D
			   /Name 'B1100-VF-XC'
			   /TypeID $D51
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_VFXC.H86']
			   ]		   		   
			   /PowerExtension 'XC'
			]


			%B1100GP
			[D
			   /Name 'B1100-GP'
			   /TypeID $D61
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_GPLC.H86']
			   ]		   		   
			   /PowerExtension 'LC'
			]
			%B1100GPHC
			[D
			   /Name 'B1100-GP-HC'
			   /TypeID $D71
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_GPHC.H86']
			   ]		   		   
			   /PowerExtension 'HC'
			]
			%B1100GPXC
			[D
			   /Name 'B1100-GP-XC'
			   /TypeID $D81
			   /TypeSupported 1
			   /SupportedHWVersions [A
			   		$0102
			   		$0103
			   ]
			   /SupportedDSWVersions [A
			   		[D /DSWVersion $10 /SWFileName 'BootB1100_S10_GPXC.H86']
			   ]		   		   
			   /PowerExtension 'XC'
			]
		] def

   	/GetEloType [B
    	$300680 1 BlockRd.ReadBlock
    	BlockRd.GetResult 0 ne [B /Msg 'BlockRd.ReadBlock: Error while reading' raisese] if
    	BlockRd.GetData 0 get
   	] def
	
  	/GetEloVersion [B
	$300682 1 BlockRd.ReadBlock
    	BlockRd.GetResult 0 ne [B /Msg 'BlockRd.ReadBlock: Error while reading' raisese] if
    	BlockRd.GetData 0 get
	  ] def
	
	/GetEloSWVersion [B
	$30102A 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!' 350 100 Create:Form def		      
          'Ok' '' 'Ok' 1 70 30 Dialog.AddButton
          'BootLabel' '' 'On the controller switch S4.4 to "on" and power up the controller!' 5 5 Dialog.AddLabel
          'Cancel' '' 'Cancel' 2 170 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	$300830 def
  							/ReleaseVersionAddr $300850 def
								/ReleaseSubVersionAddr $300852 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
								
								% !!!!!!! HACK CHANGE IN THE NEXT VERSION
								ReleaseVersionAddr '0300' #0 BlockWr.WriteBlock
								ReleaseSubVersionAddr '0900' #0 BlockWr.WriteBlock
																
					      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 ]  
  ]
  
]