MATLAB Answers

Is there a way to include a precompiled C library in a Stateflow/Simulink chart for embedded systems?

3 views (last 30 days)
Chris Hack
Chris Hack on 4 Aug 2016
Commented: Mark McBroom on 24 Aug 2016
Hi,
I am attempting to create a distributible library of functions for a product that my company is designing. It is intended to be used by one of my colleagues in Stateflow and Simulink to generate the product's operating system, which is essentially a really big state chart. I am trying to make things simpler on both the final compiler and my colleague by putting all of the custom C code that's related to hardware interaction on the microcontroller into a precompiled C library. I have been able to successfully compile the library (generated with the GCC C compiler) into a .a file, which essentially contains all of the object files related to the custom code and optionally a ton of debug information. The library has been tested in a project within my embedded IDE (Atollic TrueSTUDIO) successfully.
Currently, my colleague is calling functions directly from within Stateflow charts, and I would like to be able to move away from giving him all of the source code and into just giving him the compiled library. The problem with this is that even though I've designed my Simulink library blocks to be used without actual access to the functions (every custom C code function call within a generated S-Function is guarded by an #ifdef MATLAB_MEX_FILE /#endif sequence) I cannot get Simulink to use the precompiled library correctly. If I try to call a function directly from within a Stateflow chart, I get a number of errors which look like syntax issues in the MATLAB generated header file which is just bizarre to me. For example, here are some of the errors for the model entitled HighResScanTest:
"HighResScanTest_sfun.c"
HighResScanTest_sfun.c
c:\users\chris hack\desktop\transporter\t2 projects\projects for atollic 5.5.2\hires_scan_test\slprj\_sfprj\highresscantest\_self\sfun\src\HighResScanTest_sfun.h(31) : error C2061: syntax error : identifier 'Sensus_Data'
c:\users\chris hack\desktop\transporter\t2 projects\projects for atollic 5.5.2\hires_scan_test\slprj\_sfprj\highresscantest\_self\sfun\src\HighResScanTest_sfun.h(31) : error C2059: syntax error : ';'
c:\users\chris hack\desktop\transporter\t2 projects\projects for atollic 5.5.2\hires_scan_test\slprj\_sfprj\highresscantest\_self\sfun\src\HighResScanTest_sfun.h(31) : error C3409: empty attribute block is not allowed
c:\users\chris hack\desktop\transporter\t2 projects\projects for atollic 5.5.2\hires_scan_test\slprj\_sfprj\highresscantest\_self\sfun\src\HighResScanTest_sfun.h(31) : error C2059: syntax error : ';'
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\amd64\cl.exe"' : return code '0x2'
Stop.
I attempted to import the library into the model in the Configuration Parameters -> Simulation Target ->Additional Build Information ->Libraries pane (and the associated pane for code generation), but I don't think Simulink was able to correctly identify and import the library for use with the C compiler it uses when generating code (Windows SDK V7.1).
Is there in fact any possible way to get Simulink to use a precompiled embedded C library without giving the model access to the source code? Is there some other way that I can package the hand written C code such that I don't have to send a folder filled with source code files and include every single file that contains a function which is called from within a Stateflow chart in the model configuration parameters?
As always, thank you in advance for your help.
-Chris Hack
  1 Comment
Mark McBroom
Mark McBroom on 24 Aug 2016
In addition to adding the .a file to the configuration you also need to include a .h file that defines the function prototype for the functions in you library. Add #include "myLibrary.h" to the custom code - header file section of the configuration parameters

Sign in to comment.

Answers (1)

Satishkumar Aruljothi
Satishkumar Aruljothi on 10 Aug 2016
Hello Chris, If you want to share your C-files in S-function form with your colleague,then you need compile your c-files using 'mex' in matlab. 1.select the compiler using mex -setup. generate 'C' obj files using mex command.
include_list= your include files , mexoptsstring = -c -g (-v for debug info i.e, verbose mode), prepro- preprocessor definition if you have any
eval(sprintf('mex %s %s\\C\\root\\src_file.c %s %s', mexoptsstring, path_name, include_list, prepro));
combine all the object files
eval(sprintf('mex -g -output %s\\S_function_name *.obj',path_name)
path_name is where your object files are present
Now comes the fun part Specify the input and output ports using s function template
matlabroot/simulink/src/sfuntmpl_doc.c compile this file along with your source file. now you have a mex file ready
Open simulink library and search sfunction and add it to new model and link the sfunction which you have created.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!