Using the C Function Block to Integrate Custom Code in Simulink
Learn about the C Function block, which helps integrate external C code directly into a Simulink® block. See an example demonstrating how to use it.
Hello, I'm Roy Mathew, and I'm a software developer at the MathWorks. In this video, I will introduce a C Function block and how you can use it to integrate external code in the Simulink. C Function block allows you to bring external code into a Simulink model and add any additional C interface code directly inside the block. This block will seamlessly translate signals from the Simulink context into the C code that's on your right.
Now, I'll show you a demo that explains the C Function block. In this demo, you will see how to set up external code to call from a C Function block, import struct types to Simulink to use in a model, how to set up C Function block ports, and where and how to write the C code in the block. Here I have some external code that I want to call from Simulink. Specifically I want to call the function counterbus function and as you can see the function takes in some structures as inputs, and the structures are defined in this header file. This is the model that I want to call this function from.
The first step is to register the external code with Simulink. Simulink knows about it. So for that we open the configuration parameters and simulation target pin. In the header files, I include my header files that I want to call, and in the source file section, I add any corresponding source files. Once it's added, I can now import the custom types that are structures into MATLAB as Simulink buses. For this purpose, I can use a command, Simulink.importEternalCTypes. And this will import the structures as bus types into MATLAB.
The next step is to add to C Function Block into my model. Now I would like to type the C code inside the block. Here I've pasted the prototype with the function that I code, so that I can easily call it. Now I can just start typing the function that I want to call. The first input is pass by pointer, second input due to its pass by value, the third input is in fact an output, which is passed by pointer. And then the fourth input is also passed by pointer.
And that's it. Now I can map these symbols that I've used inside the block code into what's on the block. For that I add the symbols in the symbol table here. You wanted my first input. You two as my second input, which is in major why one is an output, Which is also a bus type. Y2 is also an output, it's just an integer, now I can hit apply another port show up I can connect the block to a similar.
Now I can just assimilate the model, and that's all. As you can see the simulation right now I'll show you how to generate code, generate code you just need to build, here we have the genetic code and in the step function. We have the external function that we plan to call. And it's in line into the genetic code without additional wrappers and you don't need to write any TLC code to create the genetic code.
Now, to recap some cool facts about the C function block, you can write the C code inside the block to call external functions you can write multiple lines of code or you can call multiple functions. You can integrate structure and enumerations directly in the C Function Block without calculating any offsets. Start and terminate methods are also present in the block. The block also has compile and runtime checking and you can generate a code without writing any TLC code. This block was shipped in 20A, and you can try it out. Thank you.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.