Embed - How to Add a C function to an Embed Model for Simulation or HIL


Adding C functions to your Altair Embed model is simple using the "Extern Definition" block and "Extern Function" block. The Extern Definition block lets you supply any legal file scope C syntax, such as structures with initializers, #includes, #pragmas, typedefs, arrays, and function definitions. The Extern Function block lets you call an external function. In this example we will create a factorial C function and include it in an Embed simulation as well as codegen it and include it as part of the firmware running on a Texas Instruments F28069M launchpad.
Two Examples are described in this document:
Example 1: Include C code into your Embed diagram and simulate it.
Example 2: Create firmware from the same C code function, execute it on a target MCU, and plot the real time results on the PC running Embed. For this example, the Texas Instruments F28069M MCU is used.
Example 1: Include C code into your Embed diagram and simulate it:
Step 1: Copy/paste calcFactorial.c into the \cg\include subfolder located in the Altair Embed installation folder OR any folder, for example; had you created a “/test/codegen” folder in the Embed install folder (ex. “C:\Altair\Embed2025_64\test\codegen), calcFactorial.cfile would be copied into this subfolder.
Step 2: Create an Embed diagram using the Extern blocks. These blocks are universal and can be obtained from any of the Embedded targets, for this example blocks are obtained from the Arduino Target. Add one Extern Definition and Extern Function block to your diagram.
* The Altair Embed installation folder name will vary based on the version of Embed you are using.
Step 3: Configure the Extern Definition and Extern Function blocks to access your c code.
Step 4: Encapsulate the Extern Definition and Extern Function blocks into a compound block with one input and one output, we’ll name the block factorial. Make sure the Extern Function block in the factorial compound block is connected to the compound block input and output pins.
Step 5: Name the Embed diagram and save it. The codegen will produce a .c file with the same you assign to the Embed diagram. Make sure you do not use the C source file name as this will create a duplicate .c file. We will name the Embed diagram calcFactorial88.vsm
Step 6: Select the factorial compound block and select Tools/ Code Gen… to create a DLL
Step 7: After the Compile… is completed, a User Blocks dropdown is automatically added to the Embed Menu bar containing the DLL name in its dropdown menu.
Step 8: Run the diagram to test the DLL and display the results. The simulation will be terminated at 5-factorial because the calcFactorial function returns an integer which limits to 2^16 = 65536
Example 2: Create firmware from the same C code function, execute it on a target MCU, and plot the real time results on the PC running Embed. For this example, the Texas Instruments F28069M MCU is used.
Reuse Steps 1, 2, and 3 from Example 1
Step 4: Add a config block to the diagram for the MCU being used, in this example the F28x config block is used. The diagram becomes:
Step 5: Name the Embed diagram and save it. The codegen will produce a .c file with the same you assign to the Embed diagram. Make sure you do not use the C source file name as this will create a duplicate .c file. We will name the Embed diagram calcFactorial88.vsm
Step 6: Select the factorial compound block and select Tools/ Code Gen… This creates the .out file (firmware file) from the c source file.
Step 7: Insert a Target Interface block from the Embedded/ F280x menu and connect its inputs and outputs
Step 8: Connect the F28069M launchpad to your PC and run the diagram. The Target Interface module loads and executes the firmware to run on the F28069M target. Use the plot block to view the results.