Added evmk2h to program_evm
[keystone-rtos/mcsdk-tools.git] / program_evm / 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();
14 // program_evm environment.
15 testEnv = {};
17 // Get the Debug Server and start a Debug Session
18 var debugServer = script.getServer("DebugServer.1");
20 //***************Functions define***************************
23 function isFile(path)
24 {
25         try
26         {   
27                 file = new java.io.FileReader(path);
28         }
29         catch (ex)
30         {
31                 return false;
32         }
34         return true;
36 }
39 //****************Get New Time Stamp***********************
40 function localTime()
41 {
42         // get time stamp
43         var currentTime = new Date();
44         var year = currentTime.getFullYear();
45         var month = currentTime.getMonth() + 1;
46         month = month + "";
47         if (month.length == 1)
48         {
49                 month = "0" + month;
50         }
51         var day = currentTime.getDate();
52         var hour = currentTime.getHours();
53         var minute = currentTime.getMinutes();
54         minute = minute + "";
55         if (minute.length == 1)
56         {
57                 minute = "0" + minute;
58         }
59         var second = currentTime.getSeconds();
60         second = second + "";
61         if (second.length == 1)
62         {
63                 second = "0" + second;
64         }
65     
66         return (year+"_"+month+"_"+day+"_"+hour+minute+second);
67 }
69 /**
70  * Get error code from the given exception.
71  * @param {exception} The exception from which to get the error code.
72  */
73 function getErrorCode(exception)
74 {
75         var ex2 = exception.javaException;
76         if (ex2 instanceof Packages.com.ti.ccstudio.scripting.environment.ScriptingException) {
77                 return ex2.getErrorID();
78         }
79         return 0;
80 }
81 function fileCopy(source,destination)
82 {
83         inputFile = new File(source);
84     outputFile = new File(destination);
85     infile = new  java.io.FileReader(inputFile);
86     out = new java.io.FileWriter(outputFile);
87     var c;
89     while ((c = infile.read()) != -1)
90       out.write(c);
92     infile.close();
93     out.close();
94 }
96 function pausecomp(millis)
97  {
98   var date = new Date();
99   var curDate = null;
100   do { curDate = new Date(); }
101   while(curDate-date < millis)
102   { 
103   //print("Waiting "+millis+"ms...\r\n") 
104   }
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 writerImages = "";
118 var big_endian = false;
119 var targetFlag = "unknown";
120 var targetConfig = "unknown";
121 var emul560 = false;
122 var xds200 = false;
123 var emulation_spec = "onboard XDS100";
124 testEnv.cioFile = null;
126 if (java.lang.System.getProperty("os.name").match(/Linux/i))
128         host_os = "-linuxhost";
131 // Parse the arguments
132 if (arguments.length > 0 && arguments.length < 3)
134     // parse the board spec
135     var board_spec = arguments[0].toLowerCase();
136     board_spec = board_spec.replace(/^tmd(x|s)/, "");
137     board_spec = board_spec.replace(/^evmc/, "evm");
138     
139     // find endian, user wants
140     if (board_spec.match(/-be$/))
141     {
142         big_endian = true;
143         board_spec = board_spec.replace(/-be$/, "");
144     }
145     else
146         board_spec = board_spec.replace(/-le$/, "");
147         
148     // find onboard emulation option for this board 
149     if (board_spec.match(/lx?e$/))
150     {
151         emul560 = true;
152         emulation_spec = "XDS560 mezzanine";
153         board_spec = board_spec.replace(/e$/, "");
154     }
155       
156     if (board_spec.match(/ls$/))
157     {
158         xds200 = true;
159         emulation_spec = "XDS200 emulator";
160         board_spec = board_spec.replace(/ls$/, "l");
161     }    
162         
163     // for now, use the same software for lx and l variants
164     board_spec = board_spec.replace(/lx$/, "l");
165     
166     // for now, treat evm6618l as an alias for evm6670l
167     board_spec = board_spec.replace(/evm6618/, "evm6670");
168         
169     targetFlag = board_spec;
170     
171     endian_spec = (big_endian ? "-be" : "");
172     
173     board_binaries = script_binaries + targetFlag + endian_spec + "/";
174     targetConfig = java.lang.System.getenv("PROGRAM_EVM_TARGET_CONFIG_FILE");
175     if (!targetConfig)    
176         targetConfig = script_configs + targetFlag + "/" + targetFlag + (emul560 ? "e" : "") + (xds200 ? "s" : "") + host_os + ".ccxml";
178     print("board: " + targetFlag);
179     print("endian: " + (big_endian ? "Big" : "Little"));
180     print("emulation: " + emulation_spec);
181     print("binaries: " + board_binaries);
182     print("ccxml: " + targetConfig);
183     
184     var dir = new File(board_binaries);
185     if (!dir.exists())
186     {
187         print("board binaries directory not found");
188         java.lang.System.exit(2);
189     }
190     
191     if(arguments[1])
192         writerImages = arguments[1];
193     else
194         writeAll = true;   
196 else
198   print("Syntax error in command line");
199         print("Syntax: program_evm.js [tmdx|tmds]evm[c](<device>)l[x][e][-le|-be] [images_to_write]")
200   
201         print("    tmdx: TMDX type EVM")
202         print("    tmds: TMDS type EVM")
203         print("    c: Not used, for backward compatibility")
204         print("    <device> is the board name e.g 6472,6678 etc")
205         print("    l: Low cost EVM")
206         print("    x: EVM supports encryption")
207         print("    e: EVM uses 560 Mezzanine Emulator daughter card")
208         print("    le: Little Endian")
209         print("    be: Big Endian")
211         print("    example: TMDXEVM6678L-le")   
212         print("    [images_to_write] OPTIONAL is a list of the images to be written")
213         print("    example: eeprom50,nor")
214         print("    If not specified all (eeprom50,eeprom51,nand,nor) will be written")
215         java.lang.System.exit(0);
218 var i2cwriterbinary = board_binaries + "eepromwriter_" + targetFlag + ".out";
219 var nandwriterbinary = board_binaries + "nandwriter_" + targetFlag + ".out";
220 var norwriterbinary = board_binaries + "norwriter_" + targetFlag + ".out";
221 var eepromwriter_input51 = board_binaries + "eepromwriter_input51.txt";
222 var eepromwriter_input50 = board_binaries + "eepromwriter_input50.txt";
223 var eepromwriter_input = board_binaries + "eepromwriter_input.txt";
224 var eeprom50 = board_binaries + "eeprom50.bin";
225 var eeprom51 = board_binaries + "eeprom51.bin";
226 var nand = board_binaries + "nand.bin";
227 var nor = board_binaries + "nor.bin";
229 // Note: nAddress is the load address for using eepromwriter.
230 //       nandNorAddress is the address used for nandwriter and norwriter.
231 switch (targetFlag)
233         case "evm6457l":
234                 cpu_id = "C64XP_1";
235                 var nAddress = 0x800000;
236                 var nandNorAddress = nAddress;
237                 var iblByteSwap = false;
238                 break;
239         case "evm6474l":
240                 cpu_id = "C64XP_0";
241                 var nAddress = 0x800000;
242                 var nandNorAddress = nAddress;
243                 var iblByteSwap = false;
244                 break;
245         case "evm6455":
246                 cpu_id = "C64XP_0";
247                 var nAddress = 0x800000;
248                 var nandNorAddress = nAddress;
249                 var iblByteSwap = false;
250                 break;
251         case "evm6474":
252                 cpu_id = "C64XP_1A";
253                 var nAddress = 0x800000;
254                 var nandNorAddress = nAddress;
255                 var iblByteSwap = false;
256                 break;
257         case "evm6472l":
258                 cpu_id = "C64XP_A";
259                 var nAddress = 0x800000;
260                 var nandNorAddress = nAddress;
261                 var iblByteSwap = false;
262                 break;
263         case "evm6670l":
264                 cpu_id = "C66xx_0";
265                 var nAddress = 0x0C000000;
266                 var nandNorAddress = 0x80000000;
267                 var iblByteSwap = false;
268                 break;
269         case "evm6678l":
270                 cpu_id = "C66xx_0";
271                 var nAddress = 0x0C000000;
272                 var nandNorAddress = 0x80000000;
273                 var iblByteSwap = false;
274                 break;
275         case "evm6657l":
276                 cpu_id = "C66xx_0";
277                 var nAddress = 0x0C000000;
278                 var nandNorAddress = 0x80000000;
279                 var iblByteSwap = false;
280                 break;
281         case "evmk2h"
282                 cpu_id = C66xx_0;
283                 var nAddress = 0x0C000000;
284                 var nandNorAddress = 0x80000000;
285                 var iblByteSwap = false;
286         default:
287                 script.traceWrite("Could not file cpu id for target " + targetFlag + "\n");
292 start = localTime();
293 testEnv.cioFile = script_logs+targetFlag+"_"+start+"-cio"+".txt";
294 // Create a log file in the current directory to log script execution
295 script.traceBegin(script_logs+targetFlag+"_"+start+"-trace"+".txt")
297 // Configure target
298 debugServer.setConfig(targetConfig);
299 pausecomp(1000);
300 debugSession = debugServer.openSession("*",cpu_id);
302 if (testEnv.cioFile != null)
303         debugSession.beginCIOLogging(testEnv.cioFile);
304 pausecomp(1000);
305 debugSession.target.connect();
306 pausecomp(1000);
307 debugSession.target.reset();
308 pausecomp(1000);
310 //POST
311 if(writeAll || writerImages.match(/eeprom50/))
313         //Write EEPROM
314         start = localTime();
315         script.traceWrite("Start writing eeprom50");
316         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
317         script.traceWrite("Image:" + eeprom50 + "\r\n");
318         if (isFile(i2cwriterbinary) && isFile(eeprom50)) 
319         {
320                 fileCopy(eepromwriter_input50,eepromwriter_input);
321                 debugSession.memory.loadProgram(i2cwriterbinary);
322                 var nPage = 0x0;
323         //      var nAddress = 0x80000000;
324                 var sFilename = eeprom50 ;
325                 var nTypeSize = 32;
326                 var bByteSwap = false;
327                 try
328                 {
329                         debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
330                 }
331                 catch (ex)
332                 {
333                    errCode = getErrorCode(ex);
334                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
335                                         " to target memory!");
336                 }
337                 debugSession.target.run()
338                 end = localTime();
339         }
340         else
341         {
342                 script.traceWrite("Required EEPROM50 files do not exist in " + board_binaries + "\n");
343          
344         }
347 //IBL 
348 if(writeAll || writerImages.match(/eeprom51/))
350         start = localTime();
351         script.traceWrite("Start writing eeprom51");
352         script.traceWrite("Writer:" + i2cwriterbinary + "\r\n");
353         script.traceWrite("Image:" + eeprom51 + "\r\n");
354         
355         if (isFile(i2cwriterbinary) && isFile(eeprom51)) 
356         {
357                 fileCopy(eepromwriter_input51,eepromwriter_input);
358                 debugSession.memory.loadProgram(i2cwriterbinary);
359                 var nPage = 0x0;
360         //      var nAddress = 0x80000000;
361                 var sFilename = eeprom51;
362                 var nTypeSize = 32;
363                 var bByteSwap = iblByteSwap;
364                 try
365                 {
366                         debugSession.memory.loadRaw(nPage, nAddress, sFilename, nTypeSize, bByteSwap);
367                 }
368                 catch (ex)
369                 {
370                    errCode = getErrorCode(ex);
371                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
372                                         " to target memory!");
373                 }
375                 debugSession.target.run()
376                 end = localTime();
377         }
378         else
379         {
380                 script.traceWrite("Required EEPROM51 files do not exist in " + board_binaries + "\n");
381          
382         }
386 //NAND
387 if(writeAll || writerImages.match(/nand/))
389     if (writerImages.match(/format/))
390     {
391        /* No action is taken for NAND since it is format*/
392     }
393         else
394         {
395         start_nand = localTime();
396         script.traceWrite("Writer:" + nandwriterbinary + "\r\n");
397         script.traceWrite("NAND:" + nand + "\r\n");
398         if (isFile(nand) && isFile(nandwriterbinary)) 
399         {
400                 debugSession.memory.loadProgram(nandwriterbinary);
401                 var nPage = 0x0;
402         //      var nAddress = 0x80000000;
403                 var sFilename = nand;
404                 var nTypeSize = 32;
405                 var bByteSwap = false;
406     
407                 try
408                 {   
409                         script.traceWrite("Start loading nand.bin");
410                         debugSession.memory.loadRaw(nPage, nandNorAddress, sFilename, nTypeSize, bByteSwap);
411     
412                 }
413                 catch (ex)
414                 {
415                    errCode = getErrorCode(ex);
416                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
417                                         " to target memory!");
418                 }
419                 script.traceWrite("Start programming NAND");
420                 debugSession.target.run()
421                 script.traceWrite("End programming NAND");
422                 end_nand = localTime();
423         }
424         else
425         {
426                 script.traceWrite("Required NAND files does not exist in " + board_binaries + "\n");
427          
428         }
429      }
432 //FORMAT the flash
433 if (writerImages.match(/format/))
435 //NAND Erase all
436     if (writerImages.match(/nand/))
437     {
438         start_nand = localTime();
439         script.traceWrite("Writer:" + nandwriterbinary + "\r\n");
440         if (isFile(nandwriterbinary)) 
441         {
442                 debugSession.memory.loadProgram(nandwriterbinary);
443                 var nPage = 0x0;
444                 var nValue = 0x12345678;
445     
446                 try
447                 {   
448                         script.traceWrite("Formatting NAND device ... Initiated");
449                         // Get the address of that symbol
450                         var flag_address = debugSession.symbol.getAddress("nand_erase_flag")                    
451                         debugSession.memory.writeWord(nPage, flag_address, nValue);
452                 }
453                 catch (ex)
454                 {
455                    errCode = getErrorCode(ex);
456                    script.traceWrite("Error code #" + errCode + ", could not set the nandwriter for erasing all nand blocks! ");
457                 }
458                 debugSession.target.run()
459                 script.traceWrite("Formatting NAND device ...Completed");
460                         end_nand = localTime();
461         }
462         else
463         {
464                 script.traceWrite("Required NAND binary does not exist in " + board_binaries + "\n");
465          
466         }
467     }
468         else
469         { 
470           /* No action is taken */
471           script.traceWrite("FLASH FORMAT - No Action is taken, please provide the supported format command string, e.g., format-nand");
472         }
474     if (testEnv.cioFile != null)
475     {
476         // Stop CIO logging.
477         debugSession.endCIOLogging();
478     }
479     
480     debugSession.terminate();
481     debugServer.stop()
482     
483     // Stop logging and exit.
484     script.traceEnd();
485     java.lang.System.exit(0);
486         
489 //NOR
490 if(writeAll || writerImages.match(/nor/))
492         start_nor = localTime();
493         script.traceWrite("Writer:" + norwriterbinary + "\r\n");
494         script.traceWrite("NOR:" + nor + "\r\n");
496         
497         if (isFile(nor) && isFile(norwriterbinary)) 
498         {
499         
500                 debugSession.memory.loadProgram(norwriterbinary);
501                 var nPage = 0x0;
502         //      var nAddress = 0x80000000;
503                 var sFilename = nor ;
504                 var nTypeSize = 32;
505                 var bByteSwap = false;
506                 try
507                 {   
508                         script.traceWrite("Start loading nor.bin");
509                         debugSession.memory.loadRaw(nPage, nandNorAddress, sFilename, nTypeSize, bByteSwap);
511                 }
512                 catch (ex)
513                 {
514                    errCode = getErrorCode(ex);
515                    script.traceWrite("Error code #" + errCode + ", could not load file " + sFilename +
516                                         " to target memory!");
517                 }
518                 script.traceWrite("Start programming NOR");
519                 script.traceWrite(localTime());
520                 debugSession.target.run()
521                 script.traceWrite("End programming NOR");
522                 end_nor = localTime();
523     }
524         else
525         {
526                 script.traceWrite("Required NOR files does not exist in " + board_binaries + "\n");
527          
528         }
532 if (testEnv.cioFile != null)
534         // Stop CIO logging.
535         debugSession.endCIOLogging();
538 debugSession.terminate();
539 debugServer.stop()
541 // Stop logging and exit.
542 script.traceEnd();
543 java.lang.System.exit(0);