⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtmc9s12_resource_check.m

📁 simulink real-time workshop for dragon12 development board from
💻 M
📖 第 1 页 / 共 2 页
字号:
% resource check, mc9S12
% fw-08-06

% ensure that the external communication parameters are set to their
% respective values...
rtwOpt = get_param(gcs, 'RTWoptions');

% is this model operating in 'external mode'?
if(~isempty(findstr(rtwOpt, 'ExtMode=1')))
    
    % yes... determine communication parameters
    baud_search_str = '-abaudrate=';
    baudrate_i = findstr(rtwOpt, baud_search_str) + length(baud_search_str);
    baudrate_str = rtwOpt(baudrate_i:baudrate_i+min(find(rtwOpt(baudrate_i:end) == ' ')) - 2);
    
    port_search_str = '-aport="COM';
    port_i = findstr(rtwOpt, port_search_str) + length(port_search_str);
    port_str = rtwOpt(port_i:port_i+min(find(rtwOpt(port_i:end) == '"')) - 2);
    
    % set parameter 'ExtModeMexArgs'
    ext_comm_str = get_param(gcs, 'ExtModeMexArgs');
    verbosity = ext_comm_str(min(find(ext_comm_str == ','))+1:max(find(ext_comm_str == ','))-1);
    verbosity = verbosity(find(verbosity ~= ' '));       % remove blanks
    if(isempty(verbosity))
        verbosity = '0';     % default 'verbosity' : 'off'
    end
    ext_comm_str = [port_str ', ' verbosity ', ' baudrate_str];
    set_param(gcs, 'ExtModeMexArgs', ext_comm_str);
    
    % set parameter 'ExtModeMexFile'
    set_param(gcs, 'ExtModeMexFile', 'ext_comm_mc9S12');
    
    % save model...
    save_system;
    
end

% clear all temporary variables...
clear rtwOpt baud_search_str baudrate_i baudrate_str port_search_str port_i port_str ext_comm_str;


% fix FreePort communication -- avoid inconsistent port settings
kk = get_param(gcs, 'RTWOptions');
m1 = findstr(kk, '-aSCI0=');
if(~isempty(m1))
    kk(m1+7:m1+7+length('"idle"')-1) = '"idle"';        % initialize TLC variable SCI0
else
    kk = [kk ' -aSCI0="idle"'];                         % add missing specifier
    m1 = findstr(kk, '-aSCI0=');
end
m2 = findstr(kk, '-aSCI1=');
if(~isempty(m2))
    kk(m2+7:m2+7+length('"idle"')-1) = '"idle"';        % initialize TLC variable SCI1
else
    kk = [kk ' -aSCI1="idle"'];                         % add missing specifier
    m2 = findstr(kk, '-aSCI1=');
end
n1 = findstr(kk, '-aNumFreePortSCI0=');
if(~isempty(n1))
    kk(n1+18) = '0';                                    % initialize TLC variable NumFreePortSCI0
else
    kk = [kk ' -aNumFreePortSCI0=0'];                   % add missing specifier
    n1 = findstr(kk, '-aNumFreePortSCI0=0');
end
n2 = findstr(kk, '-aNumFreePortSCI1=');
if(~isempty(n2))
    kk(n2+18) = '0';                                    % initialize TLC variable NumFreePortSCI1
else
    kk = [kk ' -aNumFreePortSCI1=0'];                   % add missing specifier
    n2 = findstr(kk, '-aNumFreePortSCI1=0');
end


% set MATLAB comms port (if applicable)
if(~isempty(findstr(kk, 'ExtMode=1')))
    % Running in External Mode -> avoid port clashes...
    ll = findstr(kk, '-aMATLAB_comms_port=');
    RTWmatlabPort = str2num(kk(ll+24)) + 1;
    m1 = findstr(kk, '-aSCI0=');
    m2 = findstr(kk, '-aSCI1=');
    if(RTWmatlabPort == 1)
        kk(m1+7:m1+7+length('"MCOM"')-1) = '"MCOM"';    % declare SCI0 as MATLAB comms port
    else
        kk(m2+7:m2+7+length('"MCOM"')-1) = '"MCOM"';    % declare SCI1 as MATLAB comms port
    end
end

% find blocks with tag 'MC9S12Target_FreePortCom' to ensure consistent settings
% blocks have been tagged using:
% >> set_param(gcb, 'Tag', 'MC9S12Target_FreePortCom')
% >> save_system
% to validate type:
% >> find_system(gcs, 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Tag', 'MC9S12Target_FreePortCom')
% fw-05-05
ll = find_system(gcs, 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Tag', 'MC9S12Target_FreePortCom');

% find all possible 'port' parameters (using first block -- all blocks
% should use the same 'FreePort' setting
% nn = get_param(ll{1}, 'DialogParameters');
% modified_port_text = nn.port.Enum;

% find current port setting
numFreePortSCI0 = 0;
numFreePortSCI1 = 0;
if(~isempty(ll))
    
    % analyse all used ports...
    for(i = 1:length(ll))
        
        nn = get_param(ll{i}, 'port');
        
        % update FreePort specifier
        if(nn(1:3) == 'SCI')
            
            % target block
            if(nn(4) == '0')
                kk(m1+7:m1+7+length('"frPT"')-1) = '"frPT"';    % declare SCI0 as freeport
                numFreePortSCI0 = numFreePortSCI0 + 1;
            else
                kk(m2+7:m2+7+length('"frPT"')-1) = '"frPT"';    % declare SCI1 as freeport
                numFreePortSCI1 = numFreePortSCI1 + 1;
            end
            
        end
        
    end
    
    kk(n1+18) = num2str(numFreePortSCI0);                           % set TLC variable NumFreePortSCI0 (should be a single digit!!!!)
    kk(n2+18) = num2str(numFreePortSCI1);                           % set TLC variable NumFreePortSCI1 (should be a single digit!!!!)
    
    set_param(gcs, 'RTWOptions', kk);
    
    % store changes permanently
    save_system;
    
else
    % ensure consistency with the corresponding RTW option
    mm = findstr(kk, '-aSCI0=');
    if(isempty(mm))
        
        % set a default value
        kk = [kk ' -aSCI0="idle"'];
        
    end
    
    mm = findstr(kk, '-aSCI1=');
    if(isempty(mm))
        
        % set a default value
        kk = [kk ' -aSCI1="idle"'];
        
    end
    
    set_param(gcs, 'RTWOptions', kk);
    
    % store changes permanently
    save_system;
    
end

% check if there are any 'Fuzzy' blocks
MC9S12DriverDataBlock = find_system(gcs, 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Tag', 'MC9S12target_fuzzy');
if(~isempty(MC9S12DriverDataBlock))

    % model has fuzzy blocks -> set TLC variable 'HasFuzzyBlocks'
    kk = get_param(gcs, 'RTWOptions');
    m1 = findstr(kk, '-aHasFuzzyBlocks=');
    if(~isempty(m1))
        % 'HasFuzzyBlocks' already exists
        m2 = m1 + min(findstr(kk(m1:end), '-a'));
        if(~isempty(m2))
            % entry not at the end -> set to new value and append remainder
            kk2 = kk(m2:end);
            kk = [kk(1:m1+16) '1 ' kk2];
        else
            % entry at the end -> set to new value 
            kk = [kk(1:m1+16) '1'];
        end
    else
        % 'HasFuzzyBlocks' does not exist yet
        kk = [kk ' -aHasFuzzyBlocks=1'];             % set macro 'HasFuzzyBlocks'
    end
    
else
    
    % model does not include Fuzzy blocks
    % set macro HasFuzzyBlocks to '0'
    m1 = findstr(kk, '-aHasFuzzyBlocks=');
    if(~isempty(m1))
        % 'HasFuzzyBlocks' already exists
        m2 = m1 + min(findstr(kk(m1:end), '-a'));
        if(~isempty(m2))
            % entry not at the end -> set to new value and append remainder
            kk2 = kk(m2:end);
            kk = [kk(1:m1+16) '0 ' kk2];
        else
            % entry at the end -> set to new value 
            kk = [kk(1:m1+16) '0'];
        end
    else
        % 'HasFuzzyBlocks' does not exist yet
        kk = [kk ' -aHasFuzzyBlocks=0'];             % set macro 'HAS_RFCOMMS'
    end
    
end

set_param(gcs, 'RTWOptions', kk);

% save system
save_system(gcs);



% check timer settings
% all timer dependent blocks have been tagged with label 'MC9S12target_timer'
% to set the block 'Tag' use: 
% >> kk = find_system(gcb, 'FollowLinks', 'on', 'LookUnderMasks', 'all',
% 'Name', 'Timer Block')
% >> set_param(kk{:}, 'Tag', 'MC9S12target_timer')
% ... then save the block / model (fw-08-06)
MC9S12TimerBlocks = find_system(gcs, 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Tag', 'MC9S12target_timer');
MC9S12RFCommsBlocks = find_system(gcs, 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Tag', 'MC9S12target_RFCOMMS');

if(~isempty(MC9S12TimerBlocks))
    
    % model has timers -> check settings
    myTimerPeriods = [];
    myTimerModes = [];
    myTimerChannels = [];
    for(i = 1:length(MC9S12TimerBlocks))
        
        % get current settings
        kk = get_param(MC9S12TimerBlocks{i}, 'RTWData');
        myTimerPeriods = [myTimerPeriods str2num(kk.timerPeriod)];
        myTimerModes = [myTimerModes str2num(kk.timerMode)];
        myTimerChannels = [myTimerChannels str2num(kk.timerChannel)];
        
    end
    
    % check if TC7 is used as core timer
    kk = get_param(gcs, 'RTWOptions');
    m1 = findstr(kk, '-aCore_time="T7I"');
    baseSamplePeriod = str2num(get_param(gcs, 'FixedStep'));
    if(~isempty(m1))
        error(['Model time base currently uses timer TC7. Change to RTI to keep using timer blocks (see the rt9S12Target option pages).'])
        %         % using TC7 as core timer -> add to list
        %         myTimerChannels = [myTimerChannels 7];
        %         myTimerModes = [myTimerModes 1];        % OC
        %         myTimerPeriods = [myTimerPeriods baseSamplePeriod];
    end
    
    % check timer channel allocation
    [kk, ll] = sort(myTimerChannels);
    mm = min(find(diff(kk) == 0));
    if(any(diff(kk) == 0))
        % two blocks use the same timer channel
        error(['Invalid timer channel settings: Timer channel ' num2str(kk(mm)) ' has been allocated more than once.'])
    end
    
    % check if there are RFComms blocks
    if(~isempty(MC9S12RFCommsBlocks))
        
        % force TIMER_PRESCALER to 128... (period: 3.4 s)
        myTimerPeriods = [myTimerPeriods 65535/24e6*128];
        
        % find RF block type (client / server / both)
        HasServer = 0;
        HasClient = 0;
        for(i = 1:length(MC9S12RFCommsBlocks))
            if(~isempty(findstr(MC9S12RFCommsBlocks{i}, 'rfComms_Server')))
                HasServer = 1;
            end
            if(~isempty(findstr(MC9S12RFCommsBlocks{i}, 'rfComms_Client')))
                HasClient = 1;
            end
        end

        
        % 0: none  1: Server  2: Client  3: both
        HasRFCommsBlocks = HasServer + 2*HasClient;
        
        % set macro HAS_RFCOMMS to '1' (server)
        m1 = findstr(kk, '-aHasRFComms=');
        if(~isempty(m1))
            % 'HasRFComms' already exists
            m2 = m1 + min(findstr(kk(m1:end), '-a'));
            if(~isempty(m2))
                % entry not at the end -> set to new value and append remainder
                kk2 = kk(m2:end);
                kk = [kk(1:m1+12) num2str(HasRFCommsBlocks) ' ' kk2];
            else
                % entry at the end -> set to new value 
                kk = [kk(1:m1+12) num2str(HasRFCommsBlocks)];
            end
        else
            % 'HasRFComms' does not exist yet
            kk = [kk ' -aHasRFComms=' num2str(HasRFCommsBlocks)];             % set macro 'HAS_RFCOMMS'
        end
        

        % set macro CLIENT_COUNT (server) to the specified number of clients...
        myClients = [];
        for(i = 1:length(MC9S12RFCommsBlocks))

            % check 'server' and 'client' blocks
            if(~isempty(findstr(MC9S12RFCommsBlocks{i}, 'rfComms_Server')))
        
                % model is 'server' -> get current settings
                ll = get_param(MC9S12RFCommsBlocks{i}, 'RTWData');
                myClients = [myClients str2num(ll.numClient)];
                
            elseif(~isempty(findstr(MC9S12RFCommsBlocks{i}, 'rfComms_Client')))
        
                % model is 'client' -> get current settings
                ll = get_param(MC9S12RFCommsBlocks{i}, 'RTWData');
                myClients = [myClients str2num(ll.ClientNum)];
                
            end
        
        end
        myClients = unique(myClients);
        myNumClients = length(myClients);
        
        m1 = findstr(kk, '-aRFCommsServerChannels=');
        if(~isempty(m1))
            % 'HasRFComms' already exists
            m2 = m1 + min(findstr(kk(m1:end), '-a'));
            if(~isempty(m2))
                % entry not at the end -> set to new value and append remainder
                kk2 = kk(m2:end);
                kk = [kk(1:m1+23) num2str(myNumClients) ' ' kk2];
            else
                % entry at the end -> set to new value 
                kk = [kk(1:m1+23) num2str(myNumClients)];
            end
        else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -