###############################################################################################################
#
# Test Script : mem_load.py
# Configuration Script: mem_load.xml
# Analysis Script: mem_load_analysis.py
#
# Script Author : Shantha Condamoor
# Main FSW Package: MEM
# FSW Package Author : Don May
# Test Type: 002b
# Test Procedure: File Management and Memory Upload and Dump Verification
# Test Name: Memory Dump Cancel
# Requirement: 5.3.7.12.5
#              In order to perform memory writes, the SIU FSW shall process commands, from the SC via the CTDB, 
#              that include unit identifier, memory address, memory size, and memory data.
#
# system 0 - testbed host
# system 1 - Spacecraft/1553 bus controller
#
# Test Preconditions: 1) This test is run in both the Boot as well as Application Mode. The test procedure defines
#                        how this test should be run in the Boot Mode and Application modes. During the first part of
#                        of the test, FSW on RAD750 SIU crate must have undergone Primary Boot only.
#                        The test script is run in this mode first and results are recorded and analyzed.
#
#                        Then it is advanced to the Secondary Boot mode by issuing proper commands from the spacecraft
#                        simulator as illustrated in the Test Procedure. In this mode, all Application Modules 
#                        (including LCM - the task to which the MEM task attaches) must have been loaded, 
#                        and all tasks, including (LCM, MEM) must have been initialized and be running. 
#                        The same script is run again in the Application Mode.
#
# Test Postconditions: 1) Test mem_dump must be run after this script is run to verify that the memory upload occurred correctly.
#
# Tests the SIU.
# Currently unsupported in EPU
###############################################################################################################

# system 0 - testbed host
# system 1 - Spacecraft/1553 bus controller

from ltx_scriptinterface import *
import time

def main():
    
    #define interface - REQUIRED
    interface = None
    
    #always a good idea to enclose body in try/except block as LTX will throw exceptions
    try:
        #initialize the script interface -- REQUIRED
        interface = LTX_ScriptInterface()
        
        #start the host shell
        interface.start_sys(0)
        
        #start the spacecraft (SC) shell
        interface.start_sys(1)
        
        #initialize the systems
        interface.exec_ltx_cmds(0,'init')
        interface.exec_ltx_cmds(1,'init')
        
        #start the SC initialization
        interface.write_sys(1,'SCP_init 3',0)
        time.sleep(1)   	             
                                
                                
        #enable Diagnostic dumps
        interface.write_sys(1,'SCP_setDiagnostic 500',0)
		
        time.sleep(1)
	
	# enable ITC Cmd Responses from nid=SIU, for the LAT LCM Master task (1)
	# for normal (not broadcast) commands from SC, when the task is functioning
	# purely as an executing task (0) 
	# Parameters: node id = SIU, Task ID = LCM, class = normal, action = forward,  new
	
	interface.write_sys(1,'ITC_sendCmdResponse 0,1,0,1,0',0)
	time.sleep(1)
	
	# Set to highest level of verbosity level of MEM command display
	interface.write_sys(1,'MEM_setDisplayLevelCmd 5',0)
	time.sleep(1)			
	
	# send the MEM load command
	# Parameters: unit,start, count
	#           unit: LAT unit to receive the command (0=SIU, 1 = EPU0 ...)
	#           start: Starting address
	#           value: 32-bit value to load
	
	# Addresses 0x00000000 - 0x00300000 is used by PBC when in Boot Mode and may be by RTOS when in App mode
	# Addresses 0x00010000 - 0x00300000 is not used by RTOS when in App mode	
	# Addresses 0x00300000 - 0x00C00000 is initialized by PBC when in boot mode
	# Addresses 0x00C00000 - 0x08000000 is not initialized by PBC when in boot mode
	# Addresses 0x00300000 - 0x08000000 is used by RTOS in App mode
	# Addresses 0xC0800000 - 0xC0F00000 is used by SIB EEPROM when in boot mode
	# Addresses 0xC1800000 - 0xC1F00000 is used by SIB EEPROM when in app mode
	#	
	
	# more values will be written during Flight Unit Qualification Testing after code freeze.
	# for now just try a safe address that can be written during boot and app modes.
	#
	interface.write_sys(1,'MEM_sendLoad 0,0x00300000,65535',0)
	time.sleep(1)				

        #disable diagnostic packet dumps
        interface.write_sys(1,'SCP_setDiagnostic 0',0)
	time.sleep(1)	

	#
	# Analyze the results for Pass / Fail criteria
	#
	# The analysis script verifies the following for each of the MEM_sendLoad command that was sent:
	# 1. MEM_sendLoad Telecommand is sent (APID 1604 FC 4) 
	# 2. Reception of the CmdConfirm Telemetry (APID 720) for the sent  MEM_sendLoad Telecommand (APID 1604/4)	
	#    Verify that the command status field in this telemetry indicates success
	# 3. Verify that the values were uploaded correctly by running the mem_dump script.
	#	
	# analyze the results for Pass or Fail criteria
	# interface.exec_analysis(0,'mem_load_analysis.py');		
	
        interface.exec_ltx_cmds(1,'exit')
        interface.exec_ltx_cmds(0,'exit')
        
        interface.stop_sys('all')
        
        #exit the test
        interface.close(0,0)
            
    except Exception,e:
        #if a thrown exception is severe, you might want to handle it
        #if interface is active, try to stop systems and close interface -- OPTIONAL but recommended
        print 'Unhandled exception in main script',e
        if interface != None:
            #forcibly kill all active systems
            interface.kill_all()        
        
if __name__ == "__main__":
    main()
