Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Trouble inlining C-Mex S-Functions

Asked by Martin Dendaluce on 17 Feb 2013

Hello everyone!

After spending quite a long time fighting with this issue I think I’m at the point of having to ask for some help to clear up my confussion. I consulted the documentation, examples and discussions, and thought I understood, but my failure to get it working proves the contrary.

So: I want to inline a quite heavy iterative algorithm that was given to me in form of a C-Mex S-Function. I’m working with code generation for embedded microcontroller targets, compiling the code in the corresponding IDE (TI CCSv3.3 and CCSv5). Everything was working fine until I decided to implement this S-Function and to inline it, mainly to avoid the S-Function API and improve the efficiency on target.

Writing a wohle new TLC is not a reasonable option for me. So I thought I could use the Legacy Code Tool. Am I wrong here and is this just thought for “pure” C code? Maybe I still could use it later on for registry operations, but first I need to implement the algorithm.

All the legacy_code commands seemed to work (sfcn_cmex_generate, slblock_generate, sfcn_tlc_generate…) except the ‘compile’ which failed with docens of messages like: “sfun_orig.obj : error LNK2005: _ssSetStateAbsTol already defined in ex_sfun_out.obj”

I’m therefore assuming I must use a wrapper. I understand this requires writing a small TLC to embodie the calling function. This shouldn’t be a big deal, as the algorithm has 2 inputs, 1 output, 1 discrite inherited sample time and just needs a couple of values from the previous steps... but I still didn’t manage it. Using it as a common level-2 C-Mex S-Functions works fine.

I’m even starting to think of “extracting” the c routines of the s-function and create a regular .c to work with, but I’m not sure if it’s a good solution... and maybe it’s an unnecessary mess.

Hopefully someone will give me a clue… so that I know in which way to focus my efforts.. .

Regards, and thanks a lot in advance!

Martin

2 Comments

Kaustubha Govind on 19 Feb 2013

Are you using functions like ssSetStateAbsTol in your C code? If yes, that may be the issue. The code that you give to the Legacy Code Tool is supposed to be something that can run independently (including on an embedded target).

Martin Dendaluce on 13 Mar 2013

Thanks for your answer!... and sorry for my huge reaction time, I was unexpectedly overloaded in another project.

I do have the functions ssSetNumSFcnParams, ssSetNumContStates, ssSetNumDiscStates, ssSetNumInputPorts, ssSetInputPortWidth, ssSetInputPortRequiredContiguous, ssSetInputPortDataType, ssSetInputPortDirectFeedThrough, ssSetNumOutputPorts, ssSetOutputPortWidth, ssSetOutputPortDataType, ssSetNumSampleTimes, ssSetNumRWork, ssSetNumIWork, ssSetNumPWork, ssSetNumModes, ssSetNumNonsampledZCs, ssSetOptions, ssSetSampleTime, ssSetOffsetTime inside the functions static void mdlInitializeSizes(SimStruct *S) and static void mdlInitializeSampleTimes(SimStruct *S).

Where you also refferring to those??

I have made a first attempt to adapt it into a s-function builder block to generate a Wrapper TLC, but I'm not sure if this makes sense.

Could I use the legacy code tool for this? Maybe I'm using it wrong. My aim is to make the code execution more efficient, avoiding the s-function API.

Regards and thanks again!

Martin

Martin Dendaluce

1 Answer

Answer by Kaustubha Govind on 13 Mar 2013
Accepted answer

I think you might be using the Legacy Code Tool incorrectly - the code that you supply to the tool is supposed to be standalone C code (probably some legacy algorithms that you inherited which you want to run as part of a Simulink model), and the tool generates the "wrapper" S-function interface like mdlInitializeSizes, mdlOutputs, etc. along with TLC code which basically just asks Simulink Coder to generates calls into your standalone C code. I wonder if it might be easier if you manually create the TLC file, unless there is an easy way to separate out the algorithm part of your S-function into standalone C code, and then generate a new S-function and TLC using Legacy Code Tool or S-function Builder.

4 Comments

Kaustubha Govind on 14 Mar 2013

Yes, if you separate out your algorithm into generic C, then your TLC will just be a skeleton that calls into your algorithm entry points.

Martin Dendaluce on 14 Mar 2013

Is there a significant performance difference between using the Legacy Tool and a S-Function Builder which generates a Wrapper TLC?

It's just that I'm not very confident regarding to manually implementing the TLC with the entry points and the data mapping.

Thanks!

Kaustubha Govind on 15 Mar 2013

Martin: I don't know of a significant difference in the generated TLC for performance - I think you can use whatever you find more convenient.

Kaustubha Govind

Contact us