% Copyright (C) 2013-2014 Czech Technical University in Prague
%
% Authors:
%     - Carlos Jenkins <carlos@jenkins.co.cr>
%
% This document contains proprietary information belonging to Czech
% Technical University in Prague. Passing on and copying of this
% document, and communication of its contents is not permitted
% without prior written authorization.
%
% File : rpp_download.m
% Abstract:
%     Code download utility for Simulink RPP Target.
%
%     This function is optionally executed at the end of the build process
%     if it is successful and the user selected "Download compiled binary to
%     RPP" option on the build configuration panel. This function calls
%     loadti.sh script with the generated binary and using configuration for
%     the XDS100v2 JTAG Emulators.
%
% References:
%     loadti utility wiki at http://processors.wiki.ti.com/index.php/Loadti
%     Readme file in <cssroot>/ccs_base/scripting/examples/loadti/readme.txt


function rpp_download(modelName, buildDirectory, use_openocd, use_sdram)
	RppLibRoot = getpref('rpp', 'RppLibRoot');
	CCSRoot    = getpref('rpp', 'CCSRoot');
	
	%TODO: parse the EXE_FILE_EXT from target_tools.mk
	outfile     = fullfile(buildDirectory, [modelName, '.out']);
	downloadLog = fullfile(buildDirectory, 'download.log');

	if use_openocd
		if use_sdram
			command = [...
				RppLibRoot '/../loadopenocd/loadopenocd.sh -d flash -b -s ' ...
				, outfile, ' 2> ' , downloadLog ];
			disp('### Running downloader script:');
			disp(command);
			status = system(command);
			if status ~= 0,
				throw(MException('rpp:targetDlErr', ['Failed to download ' modelName ' to the target']))
			end
			disp(['Done. <a href="matlab:open(''', downloadLog, ''')">Open download log.</a>']);			
		else
			if isunix
				command = [...
					RppLibRoot '/../loadopenocd/loadopenocd.sh -d flash -b -s ' ...
					, outfile, ' 2> ' , downloadLog ];
				disp('### Running downloader script:');
				disp(command);
				status = system(command);
				if status ~= 0,
					throw(MException('rpp:targetDlErr', ['Failed to download ' modelName ' to the target']))
				end
				disp(['Done. <a href="matlab:open(''', downloadLog, ''')">Open download log.</a>']);			
			else
				disp(['### Sorry code download via OpenOCD is available for UNIX systems only. Uncheck the option']);
				disp(['###     -> Uncheck the option "Use OpenOCD to download the compiled binary" to use Ti downloader, which is functional under Windows.']);			end
		end
	else
		if use_sdram
			disp(['### Sorry, Downloading to the SDRAM is not yet implemented.'])
			disp(['###     -> Uncheck the option "Download compiled binary to SDRAM" to download the code to the Flash memory.'])
		else
			if isunix

				disp(['### Downloading ', modelName, ' to RPP board...']);

				% -r,   --reset                 Reset target before run
				% -c,   --cfg-file=CONFIG_FILE  Target setup config file
				command = [...
					'CCS_SCRIPTING="' CCSRoot '/ccs_base/scripting/" ' ...
					'"' RppLibRoot '/../loadti/loadti.sh" -r ' ...
					'-c "', RppLibRoot, '/rpp/TMS570LS3137.ccxml" ', ...
					'"', outfile, ...
					'" 2> "', downloadLog, '"'];
				disp('### Running downloader script:');
				disp(command);
				status = system(command);
				if status ~= 0,
					throw(MException('rpp:targetDlErr', ['Failed to download ' modelName ' to the target']))
				end

				disp(['Done. <a href="matlab:open(''', downloadLog, ''')">Open download log.</a>']);

			else
				disp(['### Sorry code download for Windows is not yet implemented.']);
				disp(['###     -> Add support for non UNIX systems in <targetroot>/rpp/rpp_download.m file.']);
			end	
		end
	end

end
