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

    function c2000hostsci_tx(block)
% S-function for Host side serial transmit block
%
%  See also C2000HOSTSCI_RX

% 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  = 1;
  block.NumOutputPorts = 0;
  
  % Setup port properties to be inherited or dynamic
  block.SetPreCompInpPortInfoToDynamic;

  % Override input port properties
  block.InputPort(1).Complexity  = 'Real';
  block.InputPort(1).SamplingMode = 'Sample';
  
  % Register parameters
  block.NumDialogPrms     = 3;
  block.DialogPrmsTunable = {'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 = [-1 0];
  
  %% -----------------------------------------------------------------
  %% Options
  %% -----------------------------------------------------------------
  % Specify if Accelerator should use TLC or call back into 
  % MATLAB file
  block.SetAccelRunOnTLC(false);
  
  %% -----------------------------------------------------------------
  %% Register methods called during update diagram/compilation
  %% -----------------------------------------------------------------
  
  %% 
  %% 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);

  %%
  %% SetInputPortSamplingMode:
  %%   Functionality    : Check and set input and output port 
  %%                      attributes specifying if port is operating 
  %%                      in sample-based or frame-based mode
  %%   C-Mex counterpart: mdlSetInputPortFrameData
  %%   (DSP System Toolbox is required in order to set a port
  %%    to be frame-based)
  %%
  block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
  
  %%
  %% SetInputPortDimensions:
  %%   Functionality    : Check and set input and optionally output
  %%                      port dimensions
  %%   C-Mex counterpart: mdlSetInputPortDimensionInfo
  %%
  block.RegBlockMethod('SetInputPortDimensions', @SetInpPortDims);

  %%
  %% SetInputPortDatatype:
  %%   Functionality    : Check and set input and optionally output
  %%                      port datatypes
  %%   C-Mex counterpart: mdlSetInputPortDataType
  %%
  block.RegBlockMethod('SetInputPortDataType', @SetInpPortDataType);
  
  %%
  %% PostPropagationSetup:
  %%   Functionality    : Setup work areas and state variables. Can
  %%                      also register run-time methods here
  %%   C-Mex counterpart: mdlSetWorkWidths
  %%
  block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);

  %% -----------------------------------------------------------------
  %% 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);

%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 SetInpPortFrameData(block, idx, fd)
  
%  block.InputPort(idx).SamplingMode = fd;
  
%endfunction

function SetInpPortDims(block, idx, di)
  
  block.InputPort(idx).Dimensions = di;

%endfunction

function SetInpPortDataType(block, idx, dt)
  if (dt < 1 || dt > 7)
      DAStudio.error('TARGETSHARED:blocks:SCIHostTxInputDataType');
  else
      block.InputPort(idx).DataTypeID = dt;
  end

%endfunction
  
function SetInpPortComplexSig(block, idx, c)
  
  block.InputPort(idx).Complexity = c;

%endfunction 
    
function DoPostPropSetup(block)
  
  setupBlk = find_system(bdroot, 'FollowLinks', 'on', 'MaskType','c2000 Host SCI Setup');

  switch block.DialogPrm(1).Data
  case 1
      commMode =get_param(setupBlk, 'commModeA');
  case 2
      commMode =get_param(setupBlk, 'commModeB');
  case 3
      commMode =get_param(setupBlk, 'commModeC');
  case 4
      commMode =get_param(setupBlk, 'commModeD');
  end

  switch commMode{1}
  case 'protocol'
    block.RegBlockMethod('Outputs', @ProOutputs);
  case 'raw data'
    block.RegBlockMethod('Outputs', @RawOutputs);
  end

  block.NumDworks = 1;
  block.Dwork(1).Name            = 'InstrID';
  block.Dwork(1).Dimensions      = 1;
  block.Dwork(1).DatatypeID      = 0;      %double
  block.Dwork(1).Complexity      = 'Real'; % real

%endfunction


function Start(block)
    %== Get Serial port ID and store it in to Dwork(1) ==============
    
    setupBlk = find_system(bdroot, 'FollowLinks', 'on', ...
                            'MaskType','c2000 Host SCI Setup');
    
    userdata = get_param(setupBlk{1}, 'userdata');
    
    moduleID =  block.DialogPrm(1).Data;
    
    block.Dwork(1).Data = userdata.serialID(moduleID);
    
    dt = block.InputPort(1).DataTypeID;
    if (dt < 1 || dt > 7)
        DAStudio.error('TARGETSHARED:blocks:SCIHostTxInputDataType');
    end

%endfunction

function WriteRTW(block)
  
   block.WriteRTWParam('matrix', 'M',    [1 2; 3 4]);
   block.WriteRTWParam('string', 'Mode', 'Auto');
   
%endfunction

function RawOutputs(block)

    try
        com = instrfind('id', block.Dwork(1).Data);
        head = block.DialogPrm(2).Data;
        tail = block.DialogPrm(3).Data;

        inputData  = block.InputPort(1).Data;
        dataTypeID = block.InputPort(1).DatatypeID;
        dataDim    = block.InputPort(1).Dimensions;
        charArray  = c2num2char(inputData, dataTypeID, dataDim);
        outputData = [head, charArray, tail];
        
        try
            fwrite(com, outputData);
        catch ME
            % we don't want fwrite timeout error to halt the system
                if ~isempty(strfind(ME.message, 'A timeout occurred during the write operation'))
                    fwrite(com, outputData);
                end
        end

    catch ME
        % we don't want fwrite timeout error to halt the system
        if isempty(strfind(ME.message, 'A timeout occurred during the write operation'))
            fclose(com);
            ME.rethrow;
        end
    end
    
%endfunction

function ProOutputs(block)

    try
        com = instrfind('id', block.Dwork(1).Data);
        inputData  = block.InputPort(1).Data;
        dataTypeID = block.InputPort(1).DatatypeID;
        dataDim    = block.InputPort(1).Dimensions;

        charArray  = c2num2char(inputData, dataTypeID, dataDim);
        charArray(end+1) = tchecksum(charArray);
        charArray  = ['d' charArray];
        
        t0 = clock;
        tmax = com.Timeout;
        rcvFlg = 0;
        
        while(1)

            %== send $SND ===========================================
            try
                fwrite(com, 's');
            catch ME
                if isempty(strfind(ME.message, 'A timeout occurred during the write operation'))
                    ME.rethrow;
                else
                    fwrite(com, 's');
                end
            end

            %== receive $RCV ========================================
            if(com.BytesAvailable)
                rcvFlg = fread(com, com.BytesAvailable, 'uchar');
                rcvFlg = char(rcvFlg(end));
            else
                if(etime(clock, t0) > tmax)
                    break;
                end
            end
            
            %== send DATA ===========================================
            if(strcmp(rcvFlg, 'r'))
                try
                    fwrite(com, charArray);
                catch ME
                    if isempty(strfind(ME.message, 'A timeout occurred during the write operation'))
                        ME.rethrow;
                    else
                        fwrite(com, charArray);
                    end
                end
                break;
            %elseif(strcmp(rcvFlg, 's'))
                %break;
            end
            
            if(etime(clock, t0) > tmax)
                break;
            end
        end
    catch ME
        % we don't want fwrite timeout error to halt the system
        if isempty(strfind(ME.message, 'A timeout occurred during the write operation'))
            fclose(com);
            ME.rethrow;
        end
    end
%endfunction