Platform-specific code generation

Description of the platform-specific code generation capabilities of Schematic Editor when using C-code export.

Platform-specific code generation allows C code generation for a custom hardware platform. The platform is registered by adding a platform description file into the appropriate directory.

Platform-specific file format

A platform description file is a JSON dictionary with the following keys:
  • id (mandatory) - unique ID for the new platform,
  • name(mandatory) - pretty name for the platform,
  • vendor (optional) - vendor name,
  • description (optional) - short description,
  • type_mappings (mandatory) - dictionary of type mappings, with the following keys:
    • int (mandatory) - type mapping for int Typhoon type. Value must be a list.
    • uint (mandatory) - type mapping for uint Typhoon type. Value must be a list.
    • real (mandatory) - type mapping for real Typhoon type. Value must be a list.

Here's an example of a minimalistic platform description file, that's valid:

{
    "id": "cst",
    "name": "Custom Platformator 9000",
    "type_mappings": {
        "int": ["short", "int", "long"],
        "real": ["float", "double"],
        "uint": ["unsigned int"]
    }
}
Note: Note: platform IDs hil and vhil are reserved and platform definition files with these IDs will not be loaded!

Platform description file location

A platform is registered by adding a platform description file into the appropriate directory. By default, Schematic Editor will collect all files from the following locations:

  • Windows: %appdata%\typhoon\<sw_version>\user-platforms
  • Linux: ~/.local/share/typhoon/<sw_version>/user-platforms

A platform description file can be also placed in any location defined in the User libraries path settings dialog (File > Modify library paths), as shown in the example:

Figure 1. Placement of a platform description file
Note: If new platform description files are added while Schematic Editor is running, Schematic Editor needs to be rebooted in order to load new platform definition files!
To check if platform definition files are loaded correctly, open Model Settings dialog and go to C code export tab. You'll be able to select your platform in the Platform (beta) combobox (Figure 2 ).
Figure 2. Platform (beta) combobox

Platform-specific component

A component can be either platform-independent (generic) or platform-dependent. Generic components work on any platform. However, platform-dependent components can work only on a specific platform(s).

To define a platform-dependent component, you need to define the _supported_platforms property on the component's mask, as shown in Figure 3.

Figure 3. Platform specific component mask

Then, after double-clicking on the component, you need to define which platforms are supported by the component. The property value must be a list of platform IDs, as shown in Figure 4.

Figure 4. Platform specific component dialog

Platform-specific code in handlers

In order to implement platform-specific behavior for a certain component, use the platform_id variable that is provided into handlers.

Note: platform_id is injected in the namespace at the beginning of compilation/export.

For example:

if platform_id == "cst":
  mdl.set_property_value(mdl.prop(container_handle, "value"), 1)
else:
  mdl.set_property_value(mdl.prop(container_handle, "value"), 2)

Platform-specific C function

Functions output_fnc, init_fnc, and update_fnc can be implemented by using the Jinja2 template syntax. Here, platform_id is provided as d.platform_id, while component properties are accessible by d.<property_name> call:
# output_fnc 

{% if d.platform_id == "cst" %}
    {% if d.param1 > 5%}
    out = 5;
    {% else %}
    if (in - {{d.param1}} > 0){
        out = in;
    } else {
        out = 10;
    }
    {% endif %}
{% else %}
out = 0;
{% endif %}

This template-based code will be resolved in the pre-compile handler, and as such will end up in the generated code.

What if a component is not supported by the platform?

In case you generate code for a platform-specific component, not supported by the selected platform, all of the outputs from the component will be zeroed:
// Generated from the component: Subsystem1.Gain1
// Component not supported for this platform. Outputs are zeroed.
_subsystem1_gain1__out = 0;
You will also see a similar warning in the Schematic Editor console: