Highlights from
Embedded Coder Support Package for Green Hills MULTI IDE (R2012b)

image thumbnail

Embedded Coder Support Package for Green Hills MULTI IDE (R2012b)

by

 

Embedded Coder integration for creating models that run directly in Green Hills MULTI IDE

multilinkautointtutorial.m
% MULTILINKAUTOINTTUTORIAL IDE Link tutorial.

% MULTILINKAUTOINTTUTORIAL is an example script intended to get the user started with
% IDE Link component.
%
% A simple target application is loaded and run on the target processor.
% MULTI(R) IDE and debug operations such as setting/deleting breakpoints,
% reading data from the processor and writing data to the processor are demonstrated.
%
% Methods that are demonstrated include
% 
% GHSMULTI object methods:
% run, isrunning, halt, restart, insert, load, build
% read, write, address
%
%
% To run the MULTILINKAUTOINTTUTORIAL from MATLAB, type 'multilinkautointtutorial' at the command
% prompt.
 
% Copyright 2001-2012 The MathWorks, Inc.

echo off
existingVars = who;
dir_before_demo = pwd;

try
disp(sprintf(['=================================================================\n'...
              '  IDE Link component provides a connection between MATLAB\n'...
              '  and MULTI.  This connection enables you to control and manipulate,\n'...
              '  an embedded application using the full power of MATLAB. You can\n'...
              '  use this interface to assist you in debugging and development, \n'...
              '  as well as to create MATLAB scripts for verifying and testing \n'...
              '  algorithms on a processor.\n'...
              '=================================================================\n\n']));
disp('--- Press any key to continue ---');
pause;

disp(sprintf(['=================================================================\n'...
              ' IDE Link component needs the following configuration \n'...
              ' information to establish the connection with MULTI:\n'...
              '  MULTI directory: The directory where multi.exe exists in your \n'...
              '                   MULTI installation\n'...
              '  Configuration:   The default tgt file you use to create a \n'...
              '                   project in MULTI.\n'...
              '  Debug server:    The name (or the command) to connect MULTI \n'...
              '                   debugger to the processor.\n'...
              '  In addition, you can also specify the default host name and \n'...
              '  port number that will be used by ''ghsmulti'' command \n'...
              '  (explained later) to establish connection between MATLAB and \n'...
              '  MULTI. You set the configuration parameters using \n'...
              '  ghsmulticonfig. The ghsmulticonfig command opens a dialog in \n'...
              '  which you enter the information.\n'...
              '=================================================================\n\n']));
disp(sprintf('--- Press any key to continue: ghsmulticonfig ---\n'));
pause;
echo on;
ghsmulticonfig;
echo off;

debugconnection = ghsmultiext.Utilities.multilinkpref('getpref','debugconnection');
cpuoptionc = regexp(debugconnection,'-cpu=\w+','match');
if iscell(cpuoptionc) && ~isempty(cpuoptionc)
    cpuoption = cpuoptionc{1};
    cpuoption = cpuoption(6:end);
else
    cpuoption = [];
end

while isempty(cpuoption)
   disp(sprintf(['=================================================================\n'...
                 'This tutorial depends on a demo application running on a \n'...
                 ' processor controlled by MULTI. Please enter the CPU used in \n'...
                 ' your debug server(%s). For example, the cpu can be one of the \n'...
                 ' following: ppc5554, bf537, ppc7400, v850, arm9\n'], debugconnection));
   cpuoption = input('cpu: ','s');
   disp(sprintf('=================================================================\n\n'));
end

disp(sprintf(['=================================================================\n'...
              'Later in this tutorial we will build an application for cpu: %s\n'...
              '=================================================================\n\n'], cpuoption));    


%========================================================================
% Function to create handle to MULTI: ghsmulti
disp(sprintf(['=================================================================\n'...
              ' You start this tutorial by establishing the connection between \n'...
              '  MATLAB and MULTI.  This connection is represented by a MATLAB \n'...
              '  object that is saved in variable ''id.''  The object is \n'...
              '  created with the ''ghsmulti'' function, which uses the hostname\n'...
              '  of the work station and port number you specified in \n'...
              '  ghsmulticonfig. Optionally, these properties can also be defined \n'...
              '  when you create the object. As part of the object creation, \n'...
              '  ghsmulti starts a service called IDE Link Service \n'...
              '  that MATLAB uses to send commands to MULTI. The object ''id'' \n'...
              '  is used to direct actions to MULTI. Therefore, it appears in \n'...
              '  all methods to come in this tutorial.\n'...
              '=================================================================\n\n']));

disp('--- Press any key to continue:  ghsmulti ---');
pause;

echo on
id = ghsmulti;

echo off

%========================================================================
% Link Status: info, isrunning, disp
disp(sprintf(['=================================================================\n'...
              '  With the connection in place and IDE Link Service, \n'...
              '  it is possible from MATLAB to query MULTI for status. You can \n'...
              '  get status information using ''display''.\n'...
              '=================================================================\n\n']));
disp('--- Press any key to continue:  display ---');
pause;
echo on;

display(id)

echo off

cd(id,pwd);

disp(sprintf(['=================================================================\n'...
              ' The next step is to create executable code for the target \n'...
              '  processor with MULTI. This tutorial creates a MULTI project \n'...
              '  debug_demo in a temporary directory with extension .gpj, and \n'...
              '  then uses ''add'' to add source files to the project. The project \n'...
              '  build options, including the cpu name, are set using \n'...
              '  ''setbuildopt.'' After setting the build options, the project \n'...
              '  is built using ''build'' and then the executable is loaded into\n'...
              '  MULTI debugger using ''open''.\n'...
              '=================================================================\n\n']));
disp('--- Press any key to continue:  cd, new, add, open, setbuildopt---');
pause;
warnstate = warning('off');


cd (tempdir);
copyfile(fullfile(idelinkdemodir('ghsmulti'),'multilinkautointtutorial.c'), pwd, 'f')
echo on;

% Set current working directory of IDE Link Service same
%  as MATLAB's.
cd(id,pwd);                           

% Create a new project.
new(id,'debug_demo.gpj');                                           

% Add the source file to the above project.
add(id,'multilinkautointtutorial.c');                                       


% Set the build options including CPU name.
setbuildopt(id,'Compiler',['-G -cpu=' cpuoption]); 

echo off;
    
warning(warnstate);

%========================================================================
% The MULIT program file: Load
disp(sprintf(['=================================================================\n'...
              ' The main source file used by this tutorial is \n'...  
              '  ''multilinkautointtutorial.c''. Now build the project, and then \n'...
              '  load the program before proceeding. This is done with the ''build'' \n',...
              '  and ''open'' methods. The time it takes to build a project varies \n'...
              '  depending on your configuration. Therefore, a large time-out value \n'...
              '  is supplied with the ''build'' method. IDE Link software \n'...
              '  includes methods for reading the target''s symbol table to \n'...
              '  provide access to data in the processor memory. \n'...
              '  NOTE: The symbol table is available after you build the program \n'...
              '  file and open the file in the debugger. \n'...
              '=================================================================\n\n']));
disp('--- Press any key to continue:  build, open, address, dec2hex ---');
pause;
      
%================================================================================
try
echo on

% Build the project. 
build(id,'all');                  

% Open the program in MULTI debugger. Timeout = 30.
open(id,'debug_demo','program',30);  

echo off
catch boException
echo off    
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    disp(boException.message);
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    disp(sprintf(['\n',...
            '!!!!! Another problem is encountered while building/loading debug_demo \n',...
            'on the target.\n\n',...
            'Please attempt to rebuild the program file with the appropriate files \n',...
            'and the manually load th program file in MULTI debugger.\n']));

    % Ask user to perform some steps and then flag us to continue or not.
    contFlag = 'dummy';
    while ~isempty(contFlag) && ~strcmpi(contFlag,'exit')
        contFlag = input('After loading, hit <enter> to proceed with the demo or type ''exit'' to exit the demo: ','s');
    end
    
    % User does not wish to continue running the demo.
    if ~isempty(contFlag),
        cd (dir_before_demo);
        
        % Clear all variables created by the tutorial.
		multitutorialVars1 = who;
		multitutorialVars1 = setdiff(multitutorialVars1,existingVars);
        for varid1=1:numel(multitutorialVars1)
            clear (multitutorialVars1{varid1});
        end
        clear multitutorialVars1 varid1
        % Parting words
		disp(sprintf('\nExiting MULTITUTORIAL demo...\n'))
        return
    end
            
echo off    
end



% Assume that the target execution file has been loaded by the user.
echo on

% Read the address of global variable: ddat.
ddatA = address(id,'ddat')        

% Convert to hexadecimal representation address.
dec2hex(ddatA)                       

echo off

%=================================================================================

disp(sprintf(['=================================================================\n'...
              ' After the target code is loaded, it is possible to examine and \n'...
              '  modify data values in the target from MATLAB. It is possible to\n'...
              '  read static data values immediately after loading a program on\n'...
              '  the target. The more interesting case is to manipulate data\n'...
              '  values at intermediate points during program execution. To\n'...
              '  facilitate this operation, there are methods to insert and \n'...
              '  remove breakpoints in the program.  The method ''insert'' \n'...
              '  creates a new breakpoint that you can specify by either a \n'...
              '  source file location or a physical memory address. ''remove''  \n'...
              '  can be used to remove a breakpoint that you inserted. For  \n'...
              '  this tutorial, we need to insert breakpoints at lines 24 and \n'...
              '  29 of the ''multilinkautointtutorial.c'' source file.\n'...
              '=================================================================\n\n']));
disp('--- Press any key to continue:  insert ---');
pause;

echo on

% Restore working directory for MULTI.
cd(id,pwd);                          
brkpt1 = 24;
brkpt2 = 29;

% Insert breakpoint at line 24.
insert(id,'multilinkautointtutorial.c',brkpt1);  

% Insert breakpoint at line 29.
insert(id,'multilinkautointtutorial.c',brkpt2);    

echo off

%==========================================================================

symlist=symbol(id);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%====================================================


%========================================================================
% Halt, run, read
disp(sprintf(['=================================================================\n'...
              ' Examine the source file ''multilinkautointtutorial.c'' in the\n'...
              '  MULTI debugger source area.  There should be breakpoints \n'... 
              '  indicated by red stop signs on lines 24 and 29.  Next, locate \n'...
              '  the declaration of two global data arrays: ''ddat'' and ''idat''\n'...
              '  in this file. These arrays can be accessed directly from MATLAB\n'...
              '  command prompt using the ''read'' and ''write'' methods. To \n'...
              '  control target execution, use the ''run'', ''halt'' and ''load''\n'...
              '  methods.  The method ''connect'' establishes the connection\n'...
              '  between MULTI and hardware (or a simulator). The method ''info'' \n'...
              '  returns more information about the target and the status of the\n'...
              '  debugger. ''isrunning'' returns the state of the CPU. The \n'...
              '  following section demonstrates these methods.\n'...
              '=================================================================\n\n']));
disp('--- Press any key to continue:  address, halt ---');
pause;
try
echo on

% Get address of symbols.
ddat_addr = address(id,'ddat');      
idat_addr = address(id,'idat');

% Set type values to use in read. 
ddat_type = 'double';                
idat_type = 'int32';
ddat_value = double([pi 12.3 exp(-1) sin(pi/4)]);
idat_value = int32(1:4);

% Halt the CPU (if necessary).
halt(id)                             

echo off
disp('--- Press any key to continue:  connect, load---');
pause;
echo on

% Connect to the debug server.
connect(id);                         

% Load the program on the target.
load(id,'debug_demo',30);            

echo off
disp('--- Press any key to continue:  info, isrunning---');
pause;
echo on

% Get information from the debugger.
info(id)                             

% Get the cpu state.
cpurunstatus = isrunning(id)

echo off
disp('--- Press any key to continue:  run---');
pause;
echo on

% Wait for program execution to stop at the first breakpoint.
run(id,'runtohalt',30);             
      
echo off
disp('--- Press any key to continue:  read---');
pause;
echo on

% Should equal initialized value from C-Code.
ddatV = read(id,address(id,'ddat'),ddat_type,4)   
idatV = read(id,address(id,'idat'),idat_type,4)

echo off
disp('--- Compare Data: idat, ddat with MULTI, then press return ---');
pause;
echo on

% Write values to memory.
write(id,address(id,'ddat'),ddat_value)          
write(id,address(id,'idat'),idat_value)

% Resume execution from breakpoint and then modify. 
run(id,'runtohalt',30);              

% Read values from memory.
ddatV = read(id,address(id,'ddat'),ddat_type,4)  
idatV = read(id,address(id,'idat'),idat_type,4)
        
% Reset the target.
reset(id);                           

echo off
catch memmodException
echo off
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    disp(memmodException.message);
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    disp(sprintf(['!!!! The program execution failed - Generally this is because the\n'....
            'breakpoint was not included in the source file.  This caused the\n'...
            'CPU to continue execution without halting.  Try re-running the\n'...
            'tutorial and be sure to insert the breakpoint when it is requested.\n'...
            'In some cases, resetting the board before running the tutorial may be\n'...
            'necessary.  It is often helpful to verify the target code in MULTI\n'...
            'before executing the tutorial.\n']));
    
end


%====================================================


disp(sprintf(['\n',...
    '=================================================================\n'...
    ' The object ''id'' you created during this tutorial has a \n'...
    '  connection to MULTI and the IDE Link Service.  \n'...
    '  Closing MATLAB automatically removes these objects, but in some \n'...
    '  cases it might be useful to delete them . Use ''clear'' to remove \n'...
    '  objects from the MATLAB workspace and delete handles they\n'...
    '  contain without closing MATLAB. ''clear all'' deletes all objects\n'...
    '  and variables in your MATLAB workspace. In addition, ''close'' \n'...
    '  is performed on the tutorial project to close the MULTI project \n'...
    '  manager.\n'...
    '=================================================================\n\n']));
disp('--- Press any key to continue:  close, clear ---');
pause;
warnstate = warning('off');
echo on

% Remove breakpoints introduced earlier
remove(id,'multilinkautointtutorial.c',brkpt1);  
remove(id,'multilinkautointtutorial.c',brkpt2);  

 
% Clean-up MULTI
% Close the project file.
close(id,'debug_demo.gpj','project') 

% Clear data objects
clear id

echo off
warning(warnstate);

% Next: print demoLastLine and clear all workspace variables introduced by
% this demo
demoLastLine = '\n**************** Demo complete. ****************\n\n';

catch ex
    echo off
    disp('xxxxxxxxxxxxxxxxxxxxxxxx ERROR xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    disp(ex.message);
    disp('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    demoLastLine='\n--------------Exiting MULTI DEMO---------------\n\n';  
end

echo off
fprintf(demoLastLine);

cd (dir_before_demo);

%clear all variables
multitutorialVars1 = who;
multitutorialVars1 = setdiff(multitutorialVars1,existingVars);
for varid1=1:numel(multitutorialVars1)
    clear (multitutorialVars1{varid1});
end
clear multitutorialVars1 varid1

%EOF multilinkautointotutorial

Contact us