This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28335: Exit DSS script with an error code

Part Number: TMS320F28335

Hi,

I am implementing a DSS script and would like to return a non-zero code when the script fails for whatever reason.

Please advise how to do this.

I am using the dss.bat, thus:

C:\ti\ccs1040\ccs\ccs_base\scripting\bin\dss.bat Z:\DSS-Scripts\dss-flash.js --ccxml '"C:\TMP_CAP_DIR\SP3_0_5_Target.ccxml"' --mask 80 --cpu 1 --key - --app '"C:\TMP_CAP_DIR\SP3_DSP.out"' --chk -

My script is attached but renamed as a text file.

Thanks,

Jason

dss-flash_RENAMED.txt
importPackage(Packages.com.ti.debug.engine.scripting);
importPackage(Packages.com.ti.ccstudio.scripting.environment);
importPackage(Packages.java.lang);


function printExit(exit_reason, exit_code)
{
    for(i = 0; i < cpuSessions.length; terminateSession(cpuSessions[i++]));
    debugServer.stop();

    print( "\n" + ( (exit_code > 0) ? "ERROR: " : "" ) + exit_reason + "\n");

    java.lang.System.exit(exit_code);
//    return process.exit(exit_code);
}


function deMangleString(argStr)
{
    tempStr = [];
    for(j = 0; j < argStr.length; j++)
    {
        var ch = argStr[j];
        if(ch == "'") tempStr[j] = "";
        else if(ch == "|") tempStr[j] = " ";
        else tempStr[j] = ch;
    }
    return tempStr.join("");
}


function initSession(sessionName)
{
    var session;
    print("Opening session \"" + sessionName + "\"");
    var attempt = 0;
    while(attempt < 3)
    {
        try
        {
            session = debugServer.openSession(sessionName);
            print("Session [" + sessionName + "] opened: " + session);
            session.target.connect();
            print( "Connected to [" + sessionName + "]: " + session.target.isConnected() + "\n" );
            return session;
        }
        catch( ex )
        {
            attempt++;
            print( "Exception " + attempt + " caught: " + ex);
        }
    }
    printExit("Failed to connect to CPU [" + sessionName +"]", 6);
}


function terminateSession(session)
{
    print( "Closing session [" + session.getCPUName() + "]");
    try
    {
        session.target.disconnect();
        print( "Tgt [" + session + "] disconnected: " + !session.target.isConnected() );
        session.terminate();
    }
    catch( ex )
    {
        print( "Exception caught: " + ex );
    }
    print("\n");
}


//
// main program
//
var FlashKeyNames =
[
    "FlashKey0",
    "FlashKey1",
    "FlashKey2",
    "FlashKey3",
    "FlashKey4",
    "FlashKey5",
    "FlashKey6",
    "FlashKey7"
];

var FlashSectors = 
[
    "FlashSectorA",
    "FlashSectorB",
    "FlashSectorC",
    "FlashSectorD",
    "FlashSectorE",
    "FlashSectorF",
    "FlashSectorG",
    "FlashSectorH"
];

var script = ScriptingEnvironment.instance();
var debugServer = script.getServer( "DebugServer.1" );
var cpuSessions = [];

var deviceCCXMLFile = null;
var applicationFile = null;
var eraseSector = [false, false, false, false, false, false, false, false];
var FlashKeyValues = [ "FFFF", "FFFF", "FFFF", "FFFF", "FFFF", "FFFF", "FFFF", "FFFF" ];
var selectedCPU = -1;
var softwareChecksum = 0;

var useKey = false;
var useMask = false;
var useDownload = false;
var useChecksum = false;

try
{

    for(i = 0; i < arguments.length; i++)
    {
        inputArg = arguments[i];
        switch (inputArg)
        {
            case '--ccxml':
                i++;
                var deviceCCXMLFile = deMangleString(arguments[i]);
                print("CCXML set to " + deviceCCXMLFile);
                break;

            case '--mask':
                i++;
                if(arguments[i] != "-")
                {
                    hexMask = parseInt(arguments[i], 16);
                    if(hexMask == NaN)
                    {
                        printExit("Invalid erase mask (" + arguments[i] + ")", 1);
                    }
                    for(j = 0; j < 8; j++)
                    {
                        eraseSector[j] = (hexMask & (1 << (7 - j))) > 0 ? false : true;
                        print("\t" + FlashSectors[j] + " = " + eraseSector[j]);
                    }
                    useMask = true;
                }
                break;
        
            case "--cpu":
                i++;
                selectedCPU = parseInt(arguments[i]);
                break;

            case "--key":
                i++;
                if(arguments[i] != "-")
                {
                    if(arguments[i].length != 16)
                    {
                        printExit("Security key should be 16 characters", 2);
                    }
                    print("Setting security key:");
                    for(j = 0; j < 8; j++)
                    {
                        FlashKeyValues[j] = arguments[i].substring(j*4, 4);
                        print("\t" + FlashKeyNames[j] + " = " + FlashKeyValues[j]);
                    }
                    useKey = true;
                }
                break;

            case '--app':
                i++;
                var appFName = deMangleString(arguments[i]);
                print("App filename = " + appFName);
                if(appFName.substring(appFName.lastIndexOf('\\')+1) != "-")
                {
                    applicationFile = deMangleString(arguments[i]);
                    print("Download file set to " + applicationFile);
                    useDownload = true;
                }
                break;

            case '--chk':
                i++;
                if(arguments[i] != "-")
                {
                    softwareChecksum = parseInt(arguments[i], 16);
                    if(softwareChecksum == NaN)
                    {
                        printExit("Invalid erase mask (" + arguments[i] + ")", 3);
                    }
                    useChecksum = true;
                }
                break;
            
            case '--log':
                i++;
                var logLevel = arguments[i];
                switch(logLevel)
                {
                    case 'OFF':
                    case 'SEVERE':
                    case 'WARNING':
                    case 'INFO':
                    case 'CONFIG':
                    case 'FINE':
                    case 'FINER':
                    case 'FINEST':
                    case 'ALL':
                        print("LogLevel set to " + logLevel);
                        script.traceSetConsoleLevel(logLevel);
                        break;

                    default:
                        print("LogLevel [" + logLevel + "] unsupported");
                        break;
                }
                
            default:
                break;
        }
    }

    if(deviceCCXMLFile == null || deviceCCXMLFile == "")
    {
        printExit("No CCXML specified", 4);
    }
    if(selectedCPU < 0)
    {
        printExit("No CPU target specified", 5);
    }

    debugServer.setConfig( deviceCCXMLFile );
    cpuArr = debugServer.getListOfCPUs();
}
catch(ex)
{
    printExit("Unspecified error:" + ex, 20);
}


for(i = 0; i < cpuArr.length; cpuSessions[i] = initSession(cpuArr[i++]));


try
{
    var session = cpuSessions[selectedCPU];
    var cpu = cpuArr[selectedCPU];

    if(useKey == true)
    {
        print("Unlocking");
        for(i = 0; i < 8; session.flash.options.setString(FlashKeyNames[i], FlashKeyValues[i++]));
        session.flash.unlock();
        print("CPU " + session.getCPUName() + " unlocked");
    }

    if(useMask == true)
    {
        print("Erasing flash on " + cpu);
        for(i = 0; i < 8; session.flash.options.setBoolean(FlashSectors[i], eraseSector[i++]));
        session.flash.erase();
        print("CPU " + session.getCPUName() + " flash eraseed");
    }

    if(useDownload == true)
    {
        print("Flashing " + cpu);
        session.memory.loadProgram(applicationFile);
        print(applicationFile + " flashed to " + session.getCPUName());
    }

    if(useKey == true)
    {
        for(i = 0; i < 8; session.flash.options.setString(FlashKeyNames[i], FlashKeyValues[i++]));
        session.flash.lock();
        print("CPU " + session.getCPUName() + " locked");
    }

    if(useChecksum == true)
    {
        print("Calculating checksum");
        var calc = session.flash.getFlashChecksum();
        if(calc != softwareChecksum)
        {
            printExit("Checksum failure: calculated checksum = " + calc + ",  NOT  " + softwareChecksum.toString(16), 7);
        }
        print("Checksum " + softwareChecksum.toString(16) + " verified");
        print("\n");
    }
}
catch(ex)
{
    printExit("Operation error " + ex, 5);
}

printExit("DSS Script finished", 0);

/*
unlockCPU(s);
print("\n");
eraseFlash(s, eraseMask);
print("\n");
lockCPU(s);
print("\n");
flashApplication(s, applicationFile);
print("\n");
*/

  • Hello,

    java.lang.System.exit(exit_code) should work. When i use that to exit my javascript and return an error code, it seems to work as expected. Echoing %ERRORLEVEL% will display the error code. As long as your script can handle all the exceptions thrown and can call printExit, the desired error code should be set when the script fails.

    Thanks

    ki

  • Hi,

    I came to the same conclusion, but the issue is that the dss.bat script ignores the return value from java.

    I have consequently edited it from

    ===

    :THEEND
    endlocal

    ===

    to

    ===

    :THEEND
    exit /b %errorlevel%

    ===

    Should this be a general change in the released script?

    Thanks,

    Jason