Hardware specific Functions + Code Replacement + Embedded Coder

Designing C code from an algorithm in Simulink that also calls hardware-specific functions shouldn't be difficult, right?
For example, the algorithm could call a (hardware) function "void switch_Light(uint8)".
Since neither Simulink nor the PC compiler ("Visual Studio" or similar) knows this function, you could define an auxiliary function (for simulation purposes) and get the correct hardware function call (for the external hardware compiler) substituted with a "Code Replacement Library".
Apparently, this option does not exist? The examples included in help demonstrate other scenarios.
Does anyone have experience with this and can sketch a simple, working example of the above function call?

1 Comment

Rather than use the Code Replacement Library, a simpler approach would be to have two versions of function switch_Light(). One version is for simulation and the other is hardware specific. Place these two functions in two different C files. Compile/link to one for simulation and the other for code generation. Use the C Caller block to call your C function. Use the Configuration Parameters "Simulation Target" pane to specify the C file for simulation, and the Code Generation -> Custom Code pane to specify the C file for code generation.
As an alternate to 2 seperate C files, you could use a single C file/function with #ifdef #else #endif macros to seperate the simulation version of your function from one or more hardware-specific implementations.
Mark.

Sign in to comment.

 Accepted Answer

Rather than use the Code Replacement Library, a simpler approach would be to have two versions of function switch_Light(). One version is for simulation and the other is hardware specific. Place these two functions in two different C files. Compile/link to one for simulation and the other for code generation. Use the C Caller block to call your C function. Use the Configuration Parameters "Simulation Target" pane to specify the C file for simulation, and the Code Generation -> Custom Code pane to specify the C file for code generation.
As an alternate to 2 seperate C files, you could use a single C file/function with #ifdef #else #endif macros to seperate the simulation version of your function from one or more hardware-specific implementations.

3 Comments

Hi Mark, thanks for your quick reply!
I had tried your approach initially as well. I'm trying something with that again, but it's complicated.
The point is: I don't expect the content of the "switch_Light()" function (…used here only as an example) to be copied into the code generated by Simulink. One line for the external declaration and one line with the call would be sufficient:
//Declaration
extern void switch_Light(uint8)
<generated code>
<generated code>
switch_Light(u) //Harware - call with calculated u as argument
<generated code>
<generated code>
Besides: Sometimes a simple "I was here!" message or maybe a log entry for loops is enough as a simulation replacement. Maybe you build a fancy interactive interface. All of this is much easier to do with Simulink's on-board tools than to fiddle around with C functions that act as simulation targets.
Insofar, I also try to let the auxiliary functions (used for simulation) be moved out to separate files, so that they become replaced by declaration-equivalent functions in the hardware development environment.
Or do I have a thinking error here??
Hi Tom,
The approach I described will not copy the code from Switch_Light() function into the code from the Simulink model... it will simply make a call to switch_Light() as you described. This example walks you through the steps.
Thanks.
mark.
Thank you! I think, I've got it now.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 13 Apr 2021

Commented:

on 15 Apr 2021

Community Treasure Hunt

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

Start Hunting!