image thumbnail

Tips for Fixed-Point Modeling and Code Generation for Simulink 7 (R2008a)

by

 

06 May 2008 (Updated )

Tips on fixed-point modeling and code generation for R2008a. Includes referenced model files.

lct_fixpt_signals_script.m
%% Calling Legacy Functions Using Fixed Point Signals
% This demo shows you how to use the Legacy Code Tool to integrate legacy C 
% functions that pass their inputs and outputs using signals of fixed point
% data type.
% 
% The Legacy Code Tool allows you to:
%
% * Provide the legacy function specification,
% * Generate a C-MEX S-function that is used during simulation to call the legacy code, and
% * Compile and build the generated S-function for simulation.
%

%   Copyright 1990-2007 The MathWorks, Inc.
%   $Revision: 1.1.6.5 $  $Date: 2007/12/10 22:41:39 $

%% Providing the Legacy Function Specification
% All functions provided with the Legacy Code Tool take a specific data 
% structure or array of structures as the argument. The data structure is 
% initialized by calling the function legacy_code() using 'initialize' as the
% first input. After initializing the structure, you have to assign its
% properties to values corresponding to the legacy code being integrated.  
% For detailed help on the properties, call 
% <matlab:legacy_code('help') legacy_code('help')>. The 
% prototype of the legacy functions being called in this demo is:
%
% myFixpt timesS16(const myFixpt in1, const myFixpt in2, const uint8_T fracLength)
%
% where myFixpt is logically a fixed point data type which is physically
% a typedef to a 16-bit integer:

myFixpt = Simulink.NumericType;
myFixpt.DataTypeMode = 'Fixed-point: binary point scaling';
myFixpt.Signed = true;
myFixpt.WordLength = 16;
myFixpt.FractionLength = 10;
myFixpt.IsAlias = true;
myFixpt.HeaderFile = 'times_S16S16.h';

%%
% The legacy source code is found in the files
% <matlab:sldemo_lct_util('edit','sldemo_lct_src/timesFixpt.h') timesFixpt.h>, and
% <matlab:sldemo_lct_util('edit','sldemo_lct_src/timesS16.c') timesS16.c>.

% sldemo_sfun_times_s16
def = legacy_code('initialize');
def.SFunctionName = 'lct_fixpt_signals_sfun_times_S16S16';
def.OutputFcnSpec = 'myFixpt y1 = times_S16S16(myFixpt u1, myFixpt u2, uint8 p1)';
def.HeaderFiles   = {'times_S16S16.h'};
def.SourceFiles   = {'times_S16S16.c'};
% def.IncPaths      = {'sldemo_lct_src'}; 
% def.SrcPaths      = {'sldemo_lct_src'}; 

%% Generating the registration and TLC-files used for code generation

legacy_code('sfcn_tlc_generate', def);
legacy_code('rtwmakecfg_generate', def);

%% Demoing the Generated Integration with Legacy Code
% The model <matlab:lct_fixpt_signals sldemo_lct_fixpt_signals> 
% shows integration with the legacy code.

open_system('lct_fixpt_signals');

Contact us