How can I call a function in simulink that is defined in one of the source file in the Custom Code of the Code generation pane ?

I am trying to include custom code into a simulink model and trying to call one of the function that is in the custom code in the model. However, I can not do this using the custom code section under Code generation. How can I call a function that is defined in the custom code of the code generation in the simulink model ? Do I need to define this function in the simulation target code generation pane ? if so what compiler does it use ? I am trying to generate code for F28335 C2000 family of the Texas instrument.

Answers (1)

There are a couple options for calling a custom function from your model.
The first is to use a MATLAB function block. In a MATLAB Function block you can use a function called coder.ceval in order to call your custom code. The examples at the bottom of that page are for using the MATLAB Coder product, but the MATLAB code is specifies is exactly what you would put in the MATLAB Function block. As long as the files containg the function declaration and definition are in the custom code section of the configuration parameters, you should be all set.
The more popular way to incorporate custom code is to use something called S-functions. Lots of information on how to use S-functions can be found starting from this page:
My suggestion would be to look at the Legacy Code Tool, as I have found that to be the easiest to use and extend in the future. If you go that route, you do not even need to include the custom files in the custom code section. Whenever someone uses the block for your custom function, the source and header files will be included automatically in the code generation process (assuming you followed all the steps).
Please take a look at the above documentation pages and let me know in the comments if you have any questions.

3 Comments

Hi Jon,
I have looked at all of these options. I feel that even after including all the files in the custom code section of the simulation target and code generation I am getting compilation errors as shown below.
To list a few:
c:\ti\controlsuite\device_support\f2833x\v133\dsp2833x_headers\include\DSP2833x_Device.h(34) : error C2143: syntax error : missing '{' before 'volatile'
c:\ti\controlsuite\device_support\f2833x\v133\dsp2833x_headers\include\DSP2833x_Device.h(35) : error C2143: syntax error : missing '{' before 'volatile'
c:\ti\controlsuite\device_support\f2833x\v133\dsp2833x_headers\include\DSP2833x_PieVect.h(26) : error C2054: expected '(' to follow 'interrupt'
c:\ti\controlsuite\device_support\f2833x\v133\dsp2833x_headers\include\DSP2833x_PieVect.h(26) : error C2085: 'PINT' : not in formal parameter list
c:\ti\controlsuite\device_support\f2833x\v133\dsp2833x_headers\include\DSP2833x_PieVect.h(35) : error C2016: C requires that a struct or union has at least one member
c:\ti\controlsuite\device_support\f2833x\v133\dsp2833x_headers\include\DSP2833x_PieVect.h(35) : error C2061: syntax error : identifier 'PINT'
I know that this has something to do with the specific target files of the C2000 MCU (F28335), but I have complied the same code in CCSv6 and it has worked with the same directories and libraries included. It's just a matter of compiling this code with the simulink and using code generation tool to generate code. Upon putting in mex -setup into MATLAB command window, the following is the message I get:
>> mex -setup
MEX configured to use 'Microsoft Windows SDK 7.1 (C)' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
Hi Claude,
My guess is that the CCSv6 compiler automatically includes some particular headers, or that there are other keywords allowed by that compiler not allowed by Windows SDK 7.0?
There is actually a support package for Simulink that lets you use the CCS family of compilers and will take care of a lot issues specific to compiling for that target. More information is here:
My suggestion would be to run through using that support package and see if it makes things any easier/straightforward.
Hi Jon,
I have already used and installed this package. However, I have tested and wrote the code in CCSv6. It works the way it should in CCS. When I inlcude the same files in Matlab custom code and call the same function through coder.ceval the compiler has difficulty calling this function due to the compiler errors mentioned above.

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Asked:

on 16 Oct 2014

Commented:

on 17 Oct 2014

Community Treasure Hunt

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

Start Hunting!