General Architecture
Description of the general architecture of the custom wrapper code generator.
The code generator for a new target is defined as an OML function, which creates the main.c file. The function receives information regarding the model inputs and outputs, the interruption clocks, and all the Wrapper block parameters. It generates the main.c file and places it in the specified folder, along with other support files, if there are any.
A number of APIs are available to facilitate the creation of main.c. But there is no format imposed on the way the code generator creates this C file. Even the information provided by the Wrapper blocks, which mainly consist of code snippets specified by keywords in blocks' params and sysparams, can be specified freely by the developer of the code generator and the associated library blocks. The Custom wrapper code generator simply extracts them from the model and transfers the code snippets and their keywords to the target code generator. It does not process the keywords.
Even if the target library developer can choose any keywords to exchange data between their wrapper blocks and their code generator, it is highly recommended to use a set of standard keywords, as used for existing targets. This facilitates the comprehension of the code by others. It also allows, in some cases, the possibility to use a block with multiple target code generators without having to modify the definition of its params and sysparams parameters.
The Custom Wrapper code generator provides several functionalities to facilitate the process of code generation for final targets, where in this process the code generator uses code snippets provided by the blocks to create main.c. This exchange is made under the control of the Custom Wrapper, which regroups and adds port information so that the code snippet can be properly used by the generator.
A number of generic functions are provided to facilitate the creation of the code, when needed. For example, consider a Wrapper block used to read a digital value on a pin. A code to read from this port needs to be placed in main.c before calling a body generated API. But the block has no information about which variable the read value should be placed; it depends on the way the read block is connected to the rest of the diagram. The block only knows that this value corresponds to its unique output port. In this case, the block cannot provide a C code snippet to be used directly in main.c. This is done by using tokens in the code snippets. For example, for the digital read block for RaspberryPi, the snippet is created as follows:
[' $$1[0] = gpioRead(',num2str(_params.PinNumber),');']
resulting in, for example, the string
$$1[0] = gpioRead(11);
The Custom Wrapper then automatically replaces the $$1 with the
actual variable name before passing the string to the code generator for the target,
resulting in the following string, which the code generator can directly include in
main.c:
inp_4[0] = gpioRead(11);
The Custom wrapper provides functions to perform such operations.