]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/rpp/rpp_download.m
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / rpp / rpp_download.m
1 % Copyright (C) 2013-2015 Czech Technical University in Prague
2 %
3 % Authors:
4 %     - Carlos Jenkins <carlos@jenkins.co.cr>
5 %
6 % Permission is hereby granted, free of charge, to any person
7 % obtaining a copy of this software and associated documentation
8 % files (the "Software"), to deal in the Software without
9 % restriction, including without limitation the rights to use,
10 % copy, modify, merge, publish, distribute, sublicense, and/or sell
11 % copies of the Software, and to permit persons to whom the
12 % Software is furnished to do so, subject to the following
13 % conditions:
14
15 % The above copyright notice and this permission notice shall be
16 % included in all copies or substantial portions of the Software.
17
18 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 % EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 % OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 % NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 % HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 % WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 % FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 % OTHER DEALINGS IN THE SOFTWARE.
26 %
27 % File : rpp_download.m
28 % Abstract:
29 %     Code download utility for Simulink RPP Target.
30 %
31 %     This function is executed by rpp_make_rtw_hook.m only if the build process
32 %     finished successfully and RPP_DOWNLOAD is set to TRUE. The RPP_DOWNLOAD option
33 %     is controlled by "Download to RPP board" option in RPP code generation options.
34 %
35 %     Avoid executing this function with Code Composer Studio running on background.
36 %
37 % Parameters:
38 %     use_openocd - When set to TRUE, an OpenOCD will be used for the code
39 %                 download. This tool is available only for Unix based systems.
40 %                 When set to FALSE, a Texas Instruments Loader will be used.
41 %                 for the code download. The TI tool is available for Unix based
42 %                 systems and Windows.
43 %                 This parameter is controled by "Use OpenOCD to download the compiled binary"
44 %                 option in RPP code generation options.
45 %     use_sdram - When set to TRUE, the code will be downloaded into SDRAM. This option
46 %                 requires a base code for the MPU, EMIF, POM and SDRAM configuration to
47 %                 be present in the device Flash memory. The presence of the code is not
48 %                 verified.
49 %                 When set to FALSE, the code will be downloaded into the Flash memory.
50 %                 This option is usefull for debugging as the Flash memory has very limited
51 %                 number of rewrites.
52 %                 NOTE: The code execution may be slower from the SDRAM than from the Flash.
53 %                 NOTE: This function is not yet implemented.
54 %                 This parameter is controlled by "Download compiled binary to SDRAM"
55 %                 option in RPP code generation options.
56 %
57 % References:
58 %     loadti utility wiki at http://processors.wiki.ti.com/index.php/Loadti
59 %     Readme file in <cssroot>/ccs_base/scripting/examples/loadti/readme.txt
60 %     OpenOCD wiki at https://rtime.felk.cvut.cz/hw/index.php/TMS570LS3137#OpenOCD_setup_and_Flashing
61
62 function rpp_download(modelName, buildDirectory, use_openocd, use_sdram)
63         RppLibRoot = getpref('rpp', 'RppLibRoot');
64         CCSRoot    = getpref('rpp', 'CCSRoot');
65         
66         %TODO: parse the EXE_FILE_EXT from target_tools.mk
67         outfile     = fullfile(buildDirectory, [modelName, '.out']);
68         downloadLog = fullfile(buildDirectory, 'download.log');
69
70         if use_openocd
71                 if use_sdram
72                         disp(['### Sorry, Downloading to the SDRAM is not yet implemented.'])
73                         disp(['###     -> Uncheck the option "Download compiled binary to SDRAM" (set use_openocd to false) to download the code to the Flash memory.'])
74                 else
75                         if isunix
76                                 command = [...
77                                         RppLibRoot '/../loadopenocd/loadopenocd.sh -d flash -b -s ' ...
78                                         , outfile, ' 2> ' , downloadLog ];
79                                 disp('### Running downloader script:');
80                                 disp(command);
81                                 status = system(command);
82                                 if status ~= 0,
83                                         throw(MException('rpp:targetDlErr', ['Failed to download ' modelName ' to the target']))
84                                 end
85                                 disp(['Done. <a href="matlab:open(''', downloadLog, ''')">Open download log.</a>']);                    
86                         else
87                                 disp(['### Sorry code download via OpenOCD is available for UNIX systems only. Uncheck the option']);
88                                 disp(['###     -> Uncheck the option "Use OpenOCD to download the compiled binary" to use Ti downloader, which is functional under Windows.']);
89                         end
90                 end
91         else
92                 if use_sdram
93                         disp(['### Sorry, Downloading to the SDRAM is not yet implemented.'])
94                         disp(['###     -> Uncheck the option "Download compiled binary to SDRAM" (set use_sdram to false) to download the code to the Flash memory.'])
95         else
96             disp(['### Downloading ', modelName, ' to RPP board...']);
97                         ccxml_filename = ccxml_read(RppLibRoot);
98                         if isunix
99                                 % -r,   --reset                 Reset target before run
100                                 % -c,   --cfg-file=CONFIG_FILE  Target setup config file
101                                 command = [...
102                                         'CCS_SCRIPTING="' CCSRoot '/ccs_base/scripting/" ' ...
103                                         '"' RppLibRoot '/../loadti/loadti.sh" -r ' ...
104                                         '-c "', ccxml_filename, '" ',  ...
105                                         '"', outfile, ...
106                                         '" 2> "', downloadLog, '"'];
107                         else
108                                 % -r,   --reset                 Reset target before run
109                                 % -c,   --cfg-file=CONFIG_FILE  Target setup config file
110                                 command = [...
111                                         'set CCS_SCRIPTING="' CCSRoot '\ccs_base\DebugServer"&&' ...
112                                         '"' RppLibRoot '\..\loadti\loadti.bat" -r ' ...
113                                         '-c "', ccxml_filename, '" ',  ...
114                                         '"', outfile];
115             end
116             disp('### Running downloader script:');
117             disp(command);
118             status = system(command, '-echo');
119             if status ~= 0,
120                 throw(MException('rpp:targetDlErr', ['Failed to download ' modelName ' to the target']))
121             end
122             disp(['Done. <a href="matlab:open(''', downloadLog, ''')">Open download log.</a>']);    
123         end
124         end
125
126 end
127
128 function ccxml_path = ccxml_read(RppLibRoot)
129     f = fopen(fullfile(RppLibRoot, 'Makefile.config'), 'r');
130     while ~feof(f),
131         l = fgetl(f);
132         [val, count] = sscanf(l, ' TARGET_CCXML = %s');
133         if count, ccxml=val; end
134         [val, count] = sscanf(l, ' TARGET = %s');
135         if count, target=val; end
136     end
137     ccxml_path = fullfile(RppLibRoot, 'build', target, ccxml);
138     fclose(f);
139 end