Added C6670 SRIO boot projects, updated tools projects to use the latest PDK version
[keystone-rtos/mcsdk-tools.git] / factory_images / program_evm.js
1 // factory_defaults dss script
2 // Import the DSS packages into our namespace to save on typing
3 importPackage(Packages.com.ti.debug.engine.scripting);
4 importPackage(Packages.com.ti.ccstudio.scripting.environment);
5 importPackage(Packages.java.lang);
6 importPackage(Packages.java.io);
7 importPackage(Packages.java.util);
9 // Create our scripting environment object - which is the main entry point into
10 // any script and the factory for creating other Scriptable ervers and Sessions
11 var script = ScriptingEnvironment.instance()
13 var debugScriptEnv = ScriptingEnvironment.instance();
15 // Get the Debug Server and start a Debug Session
16 var debugServer = script.getServer("DebugServer.1");
18 // Create a log file in the current directory to log script execution
19 script.traceBegin("BFTRunLog.xml", "DefaultStylesheet.xsl")
21 //***************Functions define***************************
22 //****************Open file to write***********************
23 // if attr is true, bytes will be written to the end of the file rather than the beginning
24 function fileWriterOpen(path, attr)
25 {
26         if (attr)
27         {
28                 file = new java.io.FileWriter(path, true);
29                 return file;
30         }
31         else
32         {
33                 file = new java.io.FileWriter(path, false);
34                 return file;
35         }
36 }
38 //****************Write to file***********************
39 function fileWrite(file, string)
40 {            
41         file.write(string);
42 }
44 //**************** Close file*************************
45 function fileClose(file)
46 {            
47         file.close();
48 }
50 //****************Get New Time Stamp***********************
51 function localTime()
52 {
53         // get time stamp
54         var currentTime = new Date();
55         var year = currentTime.getFullYear();
56         var month = currentTime.getMonth() + 1;
57         month = month + "";
58         if (month.length == 1)
59         {
60                 month = "0" + month;
61         }
62         var day = currentTime.getDate();
63         var hour = currentTime.getHours();
64         var minute = currentTime.getMinutes();
65         minute = minute + "";
66         if (minute.length == 1)
67         {
68                 minute = "0" + minute;
69         }
70         var second = currentTime.getSeconds();
71         second = second + "";
72         if (second.length == 1)
73         {
74                 second = "0" + second;
75         }
76     
77         return (year+"_"+month+"_"+day+"_"+hour+minute+second);
78 }
80 /**
81  * Get error code from the given exception.
82  * @param {exception} The exception from which to get the error code.
83  */
84 function getErrorCode(exception)
85 {
86         var ex2 = exception.javaException;
87         if (ex2 instanceof Packages.com.ti.ccstudio.scripting.environment.ScriptingException) {
88                 return ex2.getErrorID();
89         }
90         return 0;
91 }
92 function fileCopy(source,destination)
93 {
94         inputFile = new File(source);
95     outputFile = new File(destination);
96     infile = new  java.io.FileReader(inputFile);
97     out = new java.io.FileWriter(outputFile);
98     var c;
100     while ((c = infile.read()) != -1)
101       out.write(c);
103     infile.close();
104     out.close();
107 //*******************************************
108 // Declarations and Inititalizations
109 var nandwriter_dir    = java.lang.System.getProperty("user.dir");
110 var dss_script_dir    = java.lang.System.getenv("DSS_SCRIPT_DIR");
111 var host_os           = "";
112 var script_logs    = nandwriter_dir+"/logs/";
113 var script_configs = nandwriter_dir+"/configs/";
114 var script_binaries = nandwriter_dir+"/binaries/";
115 var targetConfig = "";
116 var writeAll = false;
117 var big_endian = false;
118 var targetFlag = "unknown";
119 var targetConfig = "unknown";
120 var emul560 = false;
122 if (java.lang.System.getProperty("os.name").match(/Linux/i))
124         host_os = "-linuxhost";
127 // Parse the arguments
128 if (arguments.length > 0 && arguments.length < 3)
130     // parse the board spec
131     var board_spec = arguments[0].toLowerCase();
132     board_spec = board_spec.replace(/^tmd(x|s)/, "");
133     board_spec = board_spec.replace(/^evmc/, "evm");
134     
135     // find endian, user wants
136     if (board_spec.match(/-be$/))
137     {
138         big_endian = true;
139         board_spec = board_spec.replace(/-be$/, "");
140     }
141     else
142         board_spec = board_spec.replace(/-le$/, "");
143         
144     // find onboard emulation option for this board 
145     if (board_spec.match(/lx?e$/))
146     {
147         emul560 = true;
148         board_spec = board_spec.replace(/e$/, "");
149     }
150         
151     // for now, use the same software for lx and l variants
152     board_spec = board_spec.replace(/lx$/, "l");
153     
154     // for now, treat evm6618l as an alias for evm6670l
155     board_spec = board_spec.replace(/evm6618/, "evm6670");
156         
157     targetFlag = board_spec;
158     
159     endian_spec = (big_endian ? "-be" : "");
160     
161     board_binaries = script_binaries + targetFlag + endian_spec + "/";
162     targetConfig = java.lang.System.getenv("PROGRAM_EVM_TARGET_CONFIG_FILE");
163     if (!targetConfig)    
164         targetConfig = script_configs + targetFlag + "/" + targetFlag + (emul560 ? "e" : "") + host_os + ".ccxml";
166     print("board: " + targetFlag);
167     print("endian: " + (big_endian ? "Big" : "Little"));
168     print("emulation: " + (emul560 ? "XDS560 mezzanine" : "onboard XDS100"));
169     print("binaries: " + board_binaries);
170     print("ccxml: " + targetConfig);
171     
172     var dir = new File(board_binaries);
173     if (!dir.exists())
174     {
175         print("board binaries directory not found");
176         java.lang.System.exit(2);
177     }
178     
179     if(arguments[1])
180         var writerImages = arguments[1];
181     else
182         writeAll = true;   
184 else
186         script.traceWrite("syntax error in command line");
187         print("Syntax: program_evm.js <device> [images_to_write]")
188         print("  <device> is the board name with optional -le (little endian, default) or -be (big endian)")
189         print("    example: TMDXEVM6678L-le")   
190         print("  [images_to_write] OPTIONAL is a list of the images to be written")
191         print("    example: eeprom50,nor")
192         print("    If not specified all (eeprom50,eeprom51,nand,nor) will be written")
193         script.traceEnd()
194         java.lang.System.exit(1);
197 var i2cwriterbinary = board_binaries + "eepromwriter_" + targetFlag + ".out";
198 var nandwriterbinary = board_binaries + "nandwriter_" + targetFlag + ".out";
199 var norwriterbinary = board_binaries + "norwriter_" + targetFlag + ".out";
200 var eepromwriter_input51 = board_binaries + "eepromwriter_input51.txt";
201 var eepromwriter_input50 = board_binaries + "eepromwriter_input50.txt";
202 var eepromwriter_input = board_binaries + "eepromwriter_input.txt";
203 var eeprom50 = board_binaries + "eeprom50.bin";
204 var eeprom51 = board_binaries + "eeprom51.bin";
205 var nand = board_binaries + "nand.bin";
206 var nor = board_binaries + "nor.bin";
208 start = localTime();
209 var logFile = fileWriterOpen(script_logs+targetFlag+"_"+start+".txt", 1);
211 // Configure target
212 debugServer.setConfig(targetConfig);
213 debugSession = debugServer.openSession("*","C66xx_0");
214 debugSession.target.connect();
215 debugSession.target.reset();
217 //POST
218 if(writeAll || writerImages.match(/eeprom50/))
220         //Write EEPROM
221         start = localTime();
222         fileWrite(logFile, "Start POST write @ "+start+"\r\n");
223         script.traceWrite("Start writing POST");
224         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
225         script.traceWrite("POST:" + eeprom50 + "\r\n");
227         fileCopy(eepromwriter_input50,eepromwriter_input);
228         debugSession.memory.loadProgram(i2cwriterbinary);
229         var nPage = 0x0;
230         var nAddress = 0x80000000;
231         var sFilename = eeprom50 ;
232         var nTypeSize = 32;
233         var bByteSwap = false;
234         try
235         {
236                 debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
237         }
238         catch (ex)
239         {
240            errCode = getErrorCode(ex);
241            script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
242                                 " to target memory!");
243         }
244         debugSession.target.run()
245         end = localTime();
246         fileWrite(logFile, "\nEnd POST write @ " + end + "\n\n");
249 //IBL 
250 if(writeAll || writerImages.match(/eeprom51/))
252         start = localTime();
253         fileWrite(logFile, "Start IBL write @ "+start+"\r\n");
254         script.traceWrite("Start writing IBL");
255         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
256         script.traceWrite("IBL:" + eeprom51 + "\r\n");
257         
258         fileCopy(eepromwriter_input51,eepromwriter_input);
259         debugSession.memory.loadProgram(i2cwriterbinary);
260         var nPage = 0x0;
261         var nAddress = 0x80000000;
262         var sFilename = eeprom51;
263         var nTypeSize = 32;
264         var bByteSwap = false;
265         try
266         {
267                 debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
268         }
269         catch (ex)
270         {
271            errCode = getErrorCode(ex);
272            script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
273                                 " to target memory!");
274         }
276         debugSession.target.run()
277         end = localTime();
278         fileWrite(logFile, "\nEnd IBL write @ " + end + "\n\n");
281 //NAND
282 if(writeAll || writerImages.match(/nand/))
284         start_nand = localTime();
285         fileWrite(logFile, "\nStart NAND write @ " + start_nand + "\n\n");
286         script.traceWrite("Writer:" + nandwriterbinary + "\r\n");
287         script.traceWrite("NAND:" + nand + "\r\n");
288         debugSession.memory.loadProgram(nandwriterbinary);
289         var nPage = 0x0;
290         var nAddress = 0x80000000;
291         var sFilename = nand;
292         var nTypeSize = 32;
293         var bByteSwap = true;
295         try
296         {   
297                 script.traceWrite("Start loading nand.bin");
298                 debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
300         }
301         catch (ex)
302         {
303            errCode = getErrorCode(ex);
304            script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
305                                 " to target memory!");
306         }
307         script.traceWrite("Start programming NAND");
308         debugSession.target.run()
309         script.traceWrite("End programming NAND");
310         end_nand = localTime();
311         fileWrite(logFile, "\nEnd NAND write @ " + end_nand + "\n\n");
314 //NOR
315 if(writeAll || writerImages.match(/nor/))
317         start_nor = localTime();
318         fileWrite(logFile, "\nStart NOR write @ " + start_nor + "\n\n");
319         script.traceWrite("Writer:" + norwriterbinary + "\r\n");
320         script.traceWrite("NOR:" + nor + "\r\n");
321         debugSession.memory.loadProgram(norwriterbinary);
322         var nPage = 0x0;
323         var nAddress = 0x80000000;
324         var sFilename = nor ;
325         var nTypeSize = 32;
326         var bByteSwap = true;
327         try
328         {   
329                 script.traceWrite("Start loading nor.bin");
330                 debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
332         }
333         catch (ex)
334         {
335            errCode = getErrorCode(ex);
336            script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
337                                 " to target memory!");
338         }
339         script.traceWrite("Start programming NOR");
340         script.traceWrite(localTime());
341         debugSession.target.run()
342         script.traceWrite("End programming NOR");
343         end_nor = localTime();
344         fileWrite(logFile, "\nEnd NOR write @ " + end_nor + "\n\n");
347 // Close log file
348 fileWrite(logFile, "\n\n\n");
349 fileClose(logFile);
351 debugSession.terminate();
352 debugServer.stop()
354 // Stop logging and exit.
355 script.traceEnd();
356 java.lang.System.exit(0);