gusucode.com > target工具箱matlab源码程序 > target/extensions/processor/shared/ti/blks/masks/c2000hostsci_setup.m

    function c2000hostsci_setup(block)

% Copyright 2006-2014 The MathWorks, Inc.

setup(block);
  
%endfunction

%% Function: setup ===================================================
%% Abstract:
%%   Set up the S-function block's basic characteristics such as:
%%   - Input ports
%%   - Output ports
%%   - Dialog parameters
%%   - Options
%% 
%%   Required         : Yes
%%   C-Mex counterpart: mdlInitializeSizes
%%
function setup(block)

  % Register number of ports
  block.NumInputPorts  = 0;
  block.NumOutputPorts = 0;
  
  % Register parameters
  block.NumDialogPrms     = 24;
  block.DialogPrmsTunable = {'Nontunable','Nontunable','Nontunable','Nontunable','Nontunable','Nontunable', 'Nontunable','Nontunable','Nontunable','Nontunable','Nontunable','Nontunable','Nontunable', 'Nontunable','Nontunable','Nontunable','Nontunable','Nontunable','Nontunable','Nontunable', 'Nontunable','Nontunable','Nontunable','Nontunable'};

  % Register sample times
  %  [0 offset]            : Continuous sample time
  %  [positive_num offset] : Discrete sample time
  %
  %  [-1, 0]               : Port-based sample time
  %  [-2, 0]               : Variable sample time
  block.SampleTimes = [0 0];
  
  %% -----------------------------------------------------------------
  %% Options
  %% -----------------------------------------------------------------
  % Specify if Accelerator should use TLC or call back into 
  % MATLAB file
  block.SetAccelRunOnTLC(false);
  
  %% 
  %% CheckParameters:
  %%   Functionality    : Called in order to allow validation of
  %%                      block's dialog parameters. User is 
  %%                      responsible for calling this method
  %%                      explicitly at the start of the setup method
  %%   C-Mex counterpart: mdlCheckParameters
  %%
  block.RegBlockMethod('CheckParameters', @CheckPrms);

  %% -----------------------------------------------------------------
  %% Register methods called at run-time
  %% -----------------------------------------------------------------
  
  %% 
  %% ProcessParameters:
  %%   Functionality    : Called in order to allow update of run-time
  %%                      parameters
  %%   C-Mex counterpart: mdlProcessParameters
  %%  
  block.RegBlockMethod('ProcessParameters', @ProcessPrms);

  %% 
  %% Start:
  %%   Functionality    : Called in order to initialize state and work
  %%                      area values
  %%   C-Mex counterpart: mdlStart
  %%
  block.RegBlockMethod('Start', @Start);

  %% 
  %% Terminate:
  %%   Functionality    : Called at the end of simulation for cleanup
  %%   C-Mex counterpart: mdlTerminate
  %%
  block.RegBlockMethod('Terminate', @Terminate);

%endfunction

%% -------------------------------------------------------------------
%% The local functions below are provided for illustrative purposes
%% to show how you may implement the various block methods listed
%% above.
%% -------------------------------------------------------------------

function CheckPrms(block)

%endfunction

function ProcessPrms(block)

  block.AutoUpdateRuntimePrms;
 
%endfunction

function Start(block)
    %== Check there is only one setup block in the model ============
    setupBlk = find_system(bdroot, 'FollowLinks', 'on', 'MaskType','c2000 Host SCI Setup');
    if (size(setupBlk, 1) > 1)
        DAStudio.error('TARGETSHARED:blocks:SCIHostSetupOverUse');
    end

    %== Setup serial ports ==========================================
    numComs = 4;
    comParamCell = createSerialParamCell(block);
    
    for i = 1:numComs
        com(i) = setupSerialPort(comParamCell{i});
        if i == 1
            serialID = com(1).id;
        else
            serialID(end+1) = com(i).id;
        end
    end
    
    userdata = get_param(gcbh, 'userdata');
    userdata.serialID = serialID;
    set_param(gcbh, 'userdata', userdata);

    %== Open serial ports ===========================================
    sciBlks = find_system(gcs, 'FollowLinks', 'on', 'RegExp', 'on', ...
            'MaskType', 'c2000 Host SCI [Transmit, Receive]');
    if(isempty(sciBlks))
        DAStudio.error('TARGETSHARED:blocks:SCIHostSetupTxRxNotFound');
    else
        %== Check blocks settings ===================================
        comArray = zeros(numComs, 2); %[com1Tx, com1Rx; com2Tx, com2Rx;...]
        for i = 1: length(sciBlks)
            blkModule   = get_param(sciBlks(i), 'module');
            blkMaskType = get_param(sciBlks(i), 'MaskType');
            
            blkModuleID = str2double(blkModule{1}(end));
            
            switch blkMaskType{1}
            case 'c2000 Host SCI Transmit'
                blkMaskTypeID = 1;
            case 'c2000 Host SCI Receive'
                blkMaskTypeID = 2;
            end

            
            if(comArray(blkModuleID, blkMaskTypeID) == 0)
                comArray(blkModuleID, blkMaskTypeID) = 1;
            else
                DAStudio.error('TARGETSHARED:blocks:SCIHostSetupModuleConflict', blkMaskType{1});
            end
        end
        
        %== Open serial ports =======================================
        for i = 1: numComs
            if(comArray(i,1) || comArray(i,2))
                fopen(com(i));
            end
        end

    end

%endfunction
    
function Terminate(block)
  userdata = get_param(gcbh, 'userdata');
  serialID = userdata.serialID;
  for i = 1: length(serialID)
      com(i) = instrfind('id', serialID(i));
  end
  if isfield(userdata, 'debug') %for QE and debug 
      if userdata.debug == 1
          fclose(com);
      else
          delete(com);
      end
  else
      delete(com);
  end
%endfunction

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                          HELP FUNCTIONS                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function comParamCell = createSerialParamCell(block)
    for i = 1:4
        for j = 1:7
            if j == 1
                comParamCell{i}{j} = sprintf('com%d', i);
            else
                comParamCell{i}{j} = block.DialogPrm((j-1)+(i-1)*6).Data;
            end
        end
    end
%endfunction

function com = setupSerialPort(paramCell)
    portName = paramCell{1};
    baudRate = paramCell{2};
    charLen  = paramCell{3} + 4; %(5,6,7,8)
    commMode = paramCell{4};
    stopBit  = paramCell{5};
    parity   = paramCell{6};
    timeout  = paramCell{7};

    baudArray  = [115200 57600 38400 19200 9600 4800 2400 1200 300 110];
    parityCell = {'none', 'odd', 'even'};
    
    com = serial(portName);
    com.BaudRate = baudArray(baudRate);
    com.DataBits = charLen;
    com.StopBits = stopBit;
    com.parity   = parityCell{parity};
    com.Timeout  = timeout;
    com.Terminator = [];
    com.InputBufferSize = 1024;
    com.OutputBufferSize = 1024;
   
%endfunction