Code covered by the BSD License  

Highlights from
MPC555 Motor Control Function Blockset

MPC555 Motor Control Function Blockset

by

 

20 Mar 2006 (Updated )

Additional I/O blocks for MPC555 target - specifically targetting TPU functionality

mpc555_tpu_microcode_mask(tpumodel, dptram, str_filename, ...
function str_output_text = mpc555_tpu_microcode_mask(tpumodel, dptram, str_filename, ...
                                                     str_comment, str_macros, ...
                                                     varargin)
% MPC555_TPU_MICROCODE_MASK.M
% 
% str_output_text = mpc555_tpu_microcode_mask(tpumodel, dptram, str_filename, ...
%                                                     str_comment, str_macros, varargin)
% This function generates the text that would be used in a TPU microcode
% mask file to create a ROM maskset to use in DPTRAM with the functions
% specified in varargin in the form: 'function name', function number, (bank
% number) etc...  Bank number is only for the TPU2 variant.
%
% It does NOT check the validity of function names, numbers or banks

newline = [char(13),char(10)]; % Newline character to be used later on

linewidth = 70; % Number of characters per line

% Here's the list of valid TPU models
str_TPU_MODELS={'TPU1','TPU2'};
str_DPTRAM_SIZES={'2k', '512';
                  '4k', '1024';
                  '8k', '2048'};

% Whilst this is unnecessary, define some strings here so we know what we
% are playing with

str_tpumodel = ''; % Current TPU MODEL (TPU1, TPU2)
str_dptramsize = ''; % Current DPTRAM size in long words
str_output_text = ''; % Output text being built up

% Check that a valid TPU model has been selected by the user
% If so, load this into str_tpumodel
if tpumodel <= length(str_TPU_MODELS)
   str_tpumodel = str_TPU_MODELS{tpumodel};
else
    i_bad_tpu_model();
    return;
end

% Check that a valid DPTRAM size has been selected by the user and map it
% to the correct value for the mask file
for iterate1 = 1:size(str_DPTRAM_SIZES,1)
   if strcmp(dptram,str_DPTRAM_SIZES{iterate1,1})
       str_dptramsize = str_DPTRAM_SIZES{iterate1,2};
   end
end
if strcmp(str_dptramsize,'')
    i_bad_dptram_size();
end

% Start constructing the file

% Comment for heading
row_of_stars = ['(',char(ones(1,linewidth-2)*'*'),')'];
open_stars = ['(*',char(ones(1,linewidth-4)*' '),'*)'];
str_output_text = [row_of_stars,newline];
str_output_text = [str_output_text,'(* ', upper(str_filename), char(ones(1,linewidth-6-size(str_filename,2)) * ' '),' *)',newline];
str_output_text = [str_output_text, open_stars, newline];
str_output_text = [str_output_text, i_comment_string(str_comment, linewidth)];
str_output_text = [str_output_text, row_of_stars, newline];

% Define TPU type and DPTRAM size
str_output_text = [str_output_text, '%type ', str_tpumodel, ', ', str_dptramsize, '.', newline, newline];

% Include any macros
for current_macro = 1:length(str_macros)
   str_output_text = [str_output_text, '%include ''',  str_macros{current_macro}, '.ASI''.', newline];
end

str_output_text = [str_output_text, newline, '%org 0,0.', newline, newline];

% Define standard exits to save space in individual functions
str_comment_1 = 'Standard exits to save space in individual functions';
str_output_text = [str_output_text,i_comment_string(str_comment_1,linewidth)];
str_output_text = [str_output_text, 'End_of_phase:     end.', newline];
str_output_text = [str_output_text, 'End_of_link: chan neg_lsl;', newline];
str_output_text = [str_output_text, '                  end.', newline];
str_output_text = [str_output_text, newline, newline];

% Now set up the includes
switch tpumodel
    case 1
        if mod(length(varargin),2) == 0
            for entrynum = 1:2:length(varargin)
                str_output_text = [str_output_text, ...
                    i_include_line(varargin{entrynum},varargin{entrynum+1}), ...
                    newline];
            end;
        else
            error('wrong number of params');
        end
    case 2
        if mod(length(varargin),3) == 0
            for entrynum = 1:3:length(varargin)
                str_output_text = [str_output_text, ...
                    i_include_line(varargin{entrynum},varargin{entrynum+1}, varargin{entrynum+2}), ...
                    newline];
            end;
        else
            error('wrong number of params');
        end
end

% And set up a footer
str_comment_2 = ['Automatically generated by ', mfilename];
str_output_text = [str_output_text, newline];
str_output_text = [str_output_text, row_of_stars,newline];
str_output_text = [str_output_text, i_comment_string(str_comment_2, linewidth)];
str_output_text = [str_output_text, row_of_stars,newline];

end

function i_bad_tpu_model()
    error('Bad TPU model');
end

function i_bad_dptram_size()
    error('Bad DPTRAM size');
end



function str_include = i_include_line(str_fn_name, fn_num, fn_bank)

% Check if the correct number of parameters has been passed
if ~(nargin == 2 || nargin == 3)
    error('Wrong number of parameters');
end

tabchar = sprintf('\t'); % Tab character to be used later on
fullstop = '.';

% Convert numbers to strings before we get into trouble
str_fn_num = dec2hex(fn_num);

if nargin == 3
    str_fn_bank = dec2hex(fn_bank);
end

% Now create the include line
str_include = ['%include ''', str_fn_name, '.uc''',';'];
str_include = [str_include, tabchar, tabchar];
str_include = [str_include, 'function = $', str_fn_num];

% If a bank number is specified then use it
if nargin == 3
    str_include = [str_include, ';', tabchar];
    str_include = [str_include, 'bank = ', str_fn_bank];
end

str_include = [str_include, fullstop];

end

% Function to pick out paragraph marks and identify as a new line for
% processing - takes in a string and returns a cell array with each paragraph as
% a cell entry
function str_wrapped = i_wrap_paragraph(str_unwrapped, linewidth)

reallinewidth = linewidth - 6; % Calculate the number of actual characters usable
stringlength = length(str_unwrapped); % The length of the input string

newline = [char(13),char(10)]; % Newline character - used later on

% Find where the end of each paragraph occurs
paragraph_end_marks = strfind(str_unwrapped, sprintf('\n')); 

% Add 0 to the beginning of the list of paragraph end marks.
paragraph_end_marks=[0,paragraph_end_marks];

% Initialise the cell array to contain the separated paragraphs
str_wrapped = {};

% For each newline, take until the next into the cell array
for current_paragraph = 1:length(paragraph_end_marks)
    current_par_begin = paragraph_end_marks(current_paragraph) + 1;
    if current_paragraph ~= length(paragraph_end_marks)
        current_par_end = paragraph_end_marks(current_paragraph+1) - 1;
    else
        current_par_end = length(str_unwrapped);
    end
    if current_par_end > current_par_begin
        str_current_unwrapped = str_unwrapped(current_par_begin:current_par_end);
    else
        str_current_unwrapped = ' ';
    end
    % Wrap lines to the line width as well.
    str_wrapped=[str_wrapped,i_wrap_string(str_current_unwrapped, linewidth)];
end

end

% Function to wrap a string to a given number of characters width, using as
% many lines as necessary.  Each line is a separate cell in a cell array
function str_wrapped = i_wrap_string(str_unwrapped, linewidth)
reallinewidth = linewidth - 6;
stringlength = length(str_unwrapped);
numlines = ceil(stringlength/reallinewidth);

for n = 1:numlines
    line_begin = (n-1) * reallinewidth + 1;
    if line_begin > stringlength
        break;
    end

    line_end = n * reallinewidth;
    if line_end > stringlength
        line_end = stringlength;
    end
    
    str_wrapped{n}=str_unwrapped( line_begin : line_end );    
end
end

% Function to wrap a comment
function str_wrap_comment = i_comment_string(str_uncommented, linewidth)

newline = [char(13),char(10)]; % Newline character to be used later on

% Convert newline characters and perform wrapping
str_wrapped = i_wrap_paragraph(str_uncommented, linewidth);

if iscell(str_wrapped)
    num_lines = length(str_wrapped);
else
    num_lines = 1;
end

str_wrap_comment = '';

for current_line = 1:num_lines    
    str_wrap_comment = [str_wrap_comment,'(* ', str_wrapped{current_line}, char(ones(1,linewidth-6-size(str_wrapped{current_line},2)) * ' '),' *)',newline];
end
end

Contact us